c – CMake:如何仅将特定文件扩展名从一个目录复制到另一个目录

我使用以下自定义命令将config目录中的所有文件复制到构建目录中.问题是我不想复制.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>)

related question

最佳答案
要仅复制.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()

转载注明原文:c – CMake:如何仅将特定文件扩展名从一个目录复制到另一个目录 - 代码日志