CMake can query numerous system and filesystem parameters via commands file()
and
cmake_host_system_information.
These functions fill some gaps in built-in CMake functionality.
Last active
August 8, 2025 20:23
-
-
Save scivision/c492ffef574e447253dd5594c414d978 to your computer and use it in GitHub Desktop.
CMake file system inquiry functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get disk space available to the user | |
cmake_minimum_required(VERSION 3.20) | |
function(disk_available file outvar) | |
if(WIN32) | |
cmake_path(ABSOLUTE_PATH file) | |
string(SUBSTRING "${file}" 0 1 letter) | |
set(cmd1 pwsh -c "(Get-Volume -DriveLetter '${letter}').SizeRemaining") | |
set(cmd2 more) # pass-thru | |
elseif(APPLE) | |
set(cmd1 df -k "${file}") | |
set(cmd2 awk "NR==2 {print \$4*1024}") | |
else() | |
set(cmd1 df -B1 "${file}") | |
set(cmd2 awk "NR==2 {print \$4}") | |
endif() | |
execute_process(COMMAND ${cmd1} | |
COMMAND ${cmd2} | |
OUTPUT_VARIABLE out | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
RESULT_VARIABLE ret | |
) | |
if(ret EQUAL 0) | |
set(${outvar} ${out} PARENT_SCOPE) | |
else() | |
set(${outvar} -1 PARENT_SCOPE) | |
endif() | |
endfunction() | |
if(CMAKE_SCRIPT_MODE_FILE) | |
set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT) | |
disk_available("." out) | |
message(STATUS "Disk space available: ${out}") | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get disk space capacity to the user | |
cmake_minimum_required(VERSION 3.20) | |
function(disk_capacity file outvar) | |
if(WIN32) | |
cmake_path(ABSOLUTE_PATH file) | |
string(SUBSTRING "${file}" 0 1 letter) | |
set(cmd1 pwsh -c "(Get-Volume -DriveLetter '${letter}').Size") | |
set(cmd2 more) # pass-thru | |
elseif(APPLE) | |
set(cmd1 df -k "${file}") | |
set(cmd2 awk "NR==2 {print \$2*1024}") | |
else() | |
set(cmd1 df -B1 "${file}") | |
set(cmd2 awk "NR==2 {print \$2}") | |
endif() | |
execute_process(COMMAND ${cmd1} | |
COMMAND ${cmd2} | |
OUTPUT_VARIABLE out | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
RESULT_VARIABLE ret | |
) | |
if(ret EQUAL 0) | |
set(${outvar} ${out} PARENT_SCOPE) | |
else() | |
set(${outvar} -1 PARENT_SCOPE) | |
endif() | |
endfunction() | |
if(CMAKE_SCRIPT_MODE_FILE) | |
set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT) | |
disk_capacity("." out) | |
message(STATUS "Disk space capacity: ${out}") | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get file owner name | |
cmake_minimum_required(VERSION 3.10) | |
function(file_owner file outvar) | |
if(WIN32) | |
set(cmd1 pwsh -c "(Get-Acl -Path '${file}').Owner") | |
elseif(APPLE) | |
set(cmd1 stat -f %Su "${file}") | |
else() | |
set(cmd1 stat -c %U "${file}") | |
endif() | |
execute_process(COMMAND ${cmd1} | |
OUTPUT_VARIABLE out | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
COMMAND_ECHO STDOUT | |
RESULT_VARIABLE ret | |
) | |
if(ret EQUAL 0) | |
set(${outvar} ${out} PARENT_SCOPE) | |
else() | |
set(${outvar} "" PARENT_SCOPE) | |
endif() | |
endfunction() | |
if(CMAKE_SCRIPT_MODE_FILE) | |
set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT) | |
file_owner("." out) | |
message(STATUS "Path owner: ${out}") | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get filesystem type like "NTFS" or "ext4" | |
cmake_minimum_required(VERSION 3.20) | |
function(filesystem_type file outvar) | |
if(WIN32) | |
cmake_path(ABSOLUTE_PATH file) | |
string(SUBSTRING "${file}" 0 1 letter) | |
set(cmd1 pwsh -c "(Get-Volume -DriveLetter '${letter}').FileSystem") | |
set(cmd2 more) # pass-thru | |
elseif(APPLE) | |
set(cmd1 df -aHY "${file}") | |
set(cmd2 awk "NR==2 {print \$2}") | |
else() | |
set(cmd1 df --output=fstype "${file}") | |
set(cmd2 tail -n 1) | |
endif() | |
execute_process(COMMAND ${cmd1} | |
COMMAND ${cmd2} | |
OUTPUT_VARIABLE out | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
COMMAND_ECHO STDOUT | |
RESULT_VARIABLE ret | |
) | |
if(ret EQUAL 0) | |
set(${outvar} ${out} PARENT_SCOPE) | |
else() | |
set(${outvar} "" PARENT_SCOPE) | |
endif() | |
endfunction() | |
if(CMAKE_SCRIPT_MODE_FILE) | |
set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT) | |
filesystem_type("." out) | |
message(STATUS "Filesystem type: ${out}") | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://cmake.org/cmake/help/latest/command/if.html#is-executable | |
# | |
# On Windows FAT32, NTFS across programs, IS_READABLE generally implies IS_WRITABLE | |
cmake_minimum_required(VERSION 3.29) | |
if(NOT DEFINED file) | |
set(file ${CMAKE_CURRENT_LIST_FILE}) | |
endif() | |
set(x 0) | |
set(r 0) | |
set(w 0) | |
if(IS_EXECUTABLE ${file}) | |
set(x 1) | |
endif() | |
if(IS_READABLE ${file}) | |
set(r 1) | |
endif() | |
if(IS_WRITABLE ${file}) | |
set(w 1) | |
endif() | |
message(STATUS "File ${file}: exe:${x} read:${r} write:${w}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.20) | |
function(is_removable_drive file outvar) | |
set(r false) | |
if(WIN32) | |
cmake_path(ABSOLUTE_PATH file) | |
cmake_path(GET file ROOT_NAME drive) | |
set(cmd1 wmic logicaldisk where DeviceID="${drive}" get DriveType) | |
execute_process(COMMAND ${cmd1} | |
OUTPUT_VARIABLE out | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
COMMAND_ECHO STDOUT | |
COMMAND_ERROR_IS_FATAL ANY | |
) | |
else() | |
set(cmd1 df "${file}") | |
set(cmd2 tail -n 1) | |
set(cmd3 awk "{print \$1}") | |
execute_process(COMMAND ${cmd1} | |
COMMAND ${cmd2} | |
COMMAND ${cmd3} | |
OUTPUT_VARIABLE out | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
COMMAND_ECHO STDOUT | |
COMMAND_ERROR_IS_FATAL ANY | |
) | |
endif() | |
if(WIN32) | |
if(out MATCHES "(2|5)$") | |
set(r true) | |
endif() | |
# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk | |
elseif(APPLE) | |
set(cmd diskutil info "${out}") | |
execute_process(COMMAND ${cmd} | |
OUTPUT_VARIABLE m2 | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
COMMAND_ECHO STDOUT | |
COMMAND_ERROR_IS_FATAL ANY | |
) | |
if(m2 MATCHES "Removable Media: *Removable") | |
set(r true) | |
endif() | |
else() | |
if(out MATCHES "/dev/([A-Za-z])+") | |
set(f1 /sys/class/block/${CMAKE_MATCH_1}/removable) | |
if(EXISTS ${f1}) | |
file(READ ${f1} y) | |
string(STRIP ${y} y) | |
if(y STREQUAL "1") | |
set(r true) | |
endif() | |
endif() | |
endif() | |
endif() | |
set(${outvar} ${r} PARENT_SCOPE) | |
endfunction() | |
if(CMAKE_SCRIPT_MODE_FILE) | |
set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT) | |
is_removable_drive("." out) | |
message(STATUS "Is removable drive: ${out}") | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright © 2025 SciVision | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment