user3667089 user3667089 - 2 months ago 13
C++ Question

How to change file permission when copying by add_custom_command in cmake

I am able to copy files by cmake using something like

file(GLOB IMAGES ${PROJECT_SOURCE_DIR}/Image/*)
file(COPY ${IMAGES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Image
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)


but the downside is this command will only be invoked when cmake is reconfigured. I figured out a way to copy them whenever a build is triggered like below

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/Image"
"${CMAKE_CURRENT_BINARY_DIR}/Image")


this suits my needs by I am not able to figure out a way to alter the file permission like the first method. I need to alternate the file permission to make sure it is right otherwise some images might be wrongly classified as executable when running the below command

find . -executable -type f


Is it possible to alter the file permission when copying from add_custom_command?

Answer

CMake developers actually responds back with an answer that works. The key is creating a temporary cmake script with file(COPY) in it and invoke it.

file(GLOB IMAGES ${PROJECT_SOURCE_DIR}/Image/*)

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/foo.cmake" "file(COPY ${IMAGES} 
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Image 
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)")

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND 
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/foo.cmake)
Comments