Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change file permission when copying by add_custom_command in cmake

Tags:

c++

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?

like image 252
user3667089 Avatar asked Oct 21 '25 13:10

user3667089


1 Answers

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)
like image 135
user3667089 Avatar answered Oct 23 '25 02:10

user3667089



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!