CMake: How to copy only specific file extensions f

2019-04-08 10:23发布

问题:

I am using the following custom command to copy all the files within the config directory into the build directory. The problem is that I don't want the .svn directory to be copied as well. I am looking for a way to either exclude the .svn directory or to copy files with specific extension. e.g. I want only files with xml or conf extensions to be copied. What should I do?

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

related question

回答1:

To copy just the .xml and .conf files, you can use the file(GLOB ...) command:

# 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()

It's a similar process to get all files not in the .svn subdirectory:

# 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