Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active March 31, 2025 21:50
Show Gist options
  • Save scivision/6cd88d4b7b084169a7aa182f70741614 to your computer and use it in GitHub Desktop.
Save scivision/6cd88d4b7b084169a7aa182f70741614 to your computer and use it in GitHub Desktop.
CMake file permissions for find_library, find_file, find_path

CMake file permissions for find_libary, find_file, find_path

CMake find_program() has particular file permissions behavior described in CMP0109

This CMake script explores / verifies such behavior for find_library, find_file, and find_path. Note that behavior on Windows is distinct from other operating systems due to distinct file permissions on Windows in general.

On Unix-like file systems, read permission is required for find_library, find_file, etc.

cmake -B build

Example output: Linux

-- noread_shared_lib: noread_shared_lib-NOTFOUND
-- noread_static_lib: noread_static_lib-NOTFOUND
-- noread_shared_file: noread_shared_file-NOTFOUND
-- nowrite_shared_p: /tmp/build/libnowrite_shared.so
-- nowrite_static_p: /tmp/build/libnowrite_static.a
cmake_minimum_required(VERSION 3.29)
project(find_perm LANGUAGES NONE)
set(noread_shared ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}noread_shared${CMAKE_SHARED_LIBRARY_SUFFIX})
set(noread_static ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}noread_static${CMAKE_STATIC_LIBRARY_SUFFIX})
# EXISTS requires read permissions
# https://cmake.org/cmake/help/latest/command/if.html#exists
# no read permissions means can't touch the file
if(NOT (EXISTS ${noread_static} OR IS_WRITABLE ${noread_static}))
file(TOUCH ${noread_shared} ${noread_static})
# make the files write-only (no read permissions)
file(CHMOD ${noread_shared} ${noread_static} PERMISSIONS OWNER_WRITE)
endif()
find_library(noread_shared_lib
NAMES noread_shared
PATHS ${CMAKE_CURRENT_BINARY_DIR}
NO_DEFAULT_PATH)
find_file(noread_shared_file
NAMES ${CMAKE_SHARED_LIBRARY_PREFIX}noread_shared${CMAKE_SHARED_LIBRARY_SUFFIX}
PATHS ${CMAKE_CURRENT_BINARY_DIR}
NO_DEFAULT_PATH)
find_library(noread_static_lib
NAMES noread_static
PATHS ${CMAKE_CURRENT_BINARY_DIR}
NO_DEFAULT_PATH)
message(STATUS "noread_shared_lib: ${noread_shared_lib}")
message(STATUS "noread_static_lib: ${noread_static_lib}")
message(STATUS "noread_shared_file: ${noread_shared_file}")
set(nowrite_shared ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}nowrite_shared${CMAKE_SHARED_LIBRARY_SUFFIX})
set(nowrite_static ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}nowrite_static${CMAKE_STATIC_LIBRARY_SUFFIX})
if(NOT EXISTS ${nowrite_static})
file(TOUCH ${nowrite_shared} ${nowrite_static})
file(CHMOD ${nowrite_shared} ${nowrite_static} PERMISSIONS OWNER_READ)
endif()
find_library(nowrite_shared_p
NAMES nowrite_shared
PATHS ${CMAKE_CURRENT_BINARY_DIR}
NO_DEFAULT_PATH)
find_library(nowrite_static_p
NAMES nowrite_static
PATHS ${CMAKE_CURRENT_BINARY_DIR}
NO_DEFAULT_PATH)
message(STATUS "nowrite_shared_p: ${nowrite_shared_p}")
message(STATUS "nowrite_static_p: ${nowrite_static_p}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment