CMake: How to have a target for copying files

I use the following command to copy configuration files to the assembly directory after each compilation.

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

This action is triggered every time a project is compiled. Is it possible to create a target in CMakeLists.txt to copy files without having to compile something? Something like "make a copy."

+9
cmake
Jan 23 '13 at 7:42 on
source share
1 answer

You should be able to add a new custom target named copy and make it target for your custom commands:

 file(GLOB ConfigFiles ${CMAKE_SOURCE_DIR}/config/*.xml ${CMAKE_SOURCE_DIR}/config/*.conf) add_custom_target(copy) foreach(ConfigFile ${ConfigFiles}) add_custom_command(TARGET copy PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${ConfigFile} $<TARGET_FILE_DIR:MyTarget>) endforeach() 

Now user commands will only be executed if you build copy .

If you want to keep this copy target as a dependency of MyTarget so that you can either copy the files or copy them if you build MyTarget , you will need to break the circular dependency. ( MyTarget depends on copy , but copy depends on MyTarget to get the location of the copy-to directory).

To do this, you can resort to the old way of getting the target output directory:

 add_custom_target(copy) get_target_property(MyTargetLocation MyTarget LOCATION) get_filename_component(MyTargetDir ${MyTargetLocation} PATH) foreach(ConfigFile ${ConfigFiles}) add_custom_command(TARGET copy PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${ConfigFile} ${MyTargetDir}) endforeach() add_dependencies(MyTarget copy) 
+24
Jan 23 '13 at 8:07
source share



All Articles