我使用以下自定义命令将config
目录中的所有文件复制到build
目录中。问题是我不希望复制.svn
目录。我正在寻找一种方法来排除.svn
目录或复制具有特定扩展名的文件。例如我只希望复制包含xml
或conf
扩展名的文件。我该怎么办?
add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:MyTarget>)
答案 0 :(得分:12)
要仅复制.xml
和.conf
文件,您可以使用file(GLOB ...)
命令:
# Gather list of all .xml and .conf files in "/config"
file(GLOB ConfigFiles ${CMAKE_SOURCE_DIR}/config/*.xml
${CMAKE_SOURCE_DIR}/config/*.conf)
foreach(ConfigFile ${ConfigFiles})
add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy ${ConfigFile} $<TARGET_FILE_DIR:MyTarget>)
endforeach()
这是一个类似的过程,可以让所有文件都不在.svn
子目录中:
# Gather list of all files in "/config"
file(GLOB ConfigFiles RELATIVE ${CMAKE_SOURCE_DIR}/config
${CMAKE_SOURCE_DIR}/config/*)
# Gather list of all files in "/config/.svn"
file(GLOB SvnConfigFiles RELATIVE ${CMAKE_SOURCE_DIR}/config
${CMAKE_SOURCE_DIR}/config/.svn/*)
# Remove ".svn" and its contents from the list
list(REMOVE_ITEM ConfigFiles .svn ${SvnConfigFiles})
foreach(ConfigFile ${ConfigFiles})
add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy ${CMAKE_SOURCE_DIR}/config/${ConfigFile}
$<TARGET_FILE_DIR:MyTarget>)
endforeach()