Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to copy only specific file extensions from one directory into another

Tags:

c++

cmake

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

like image 629
B Faley Avatar asked Nov 18 '12 08:11

B Faley


1 Answers

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()
like image 100
Fraser Avatar answered Nov 15 '22 10:11

Fraser