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

时间:2012-11-18 08:10:17

标签: c++ cmake

我使用以下自定义命令将config目录中的所有文件复制到build目录中。问题是我不希望复制.svn目录。我正在寻找一种方法来排除.svn目录或复制具有特定扩展名的文件。例如我只希望复制包含xmlconf扩展名的文件。我该怎么办?

add_custom_command(TARGET MyTarget PRE_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy_directory
                       ${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:MyTarget>)

related question

1 个答案:

答案 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()