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

时间:2016-09-01 23:22:43

标签: c++ cmake

我可以使用类似

之类的东西通过cmake复制文件
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时才会调用此命令。我想出了一种方法来复制它们,无论何时触发构建如下

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

这符合我的需求我无法找到像第一种方法那样改变文件权限的方法。我需要替换文件权限以确保它是正确的,否则在运行以下命令时,某些图像可能被错误地归类为可执行文件

find . -executable -type f

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

1 个答案:

答案 0 :(得分:2)

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

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)