如何在cmake中通过add_custom_command复制时更改文件权限

如何在cmake中通过add_custom_command复制时更改文件权限

问题描述:

我可以使用像

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)

但是缺点是这个命令只有在重新配置cmake时被调用。

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

从add_custom_command复制时是否可以更改文件权限?

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

CMake开发人员实际上回复了一个有效的答案。关键是创建一个临时的cmake脚本文件(COPY)并调用它。

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)