Mr. Fegur
Mr. Fegur

Reputation: 797

CMAKE Forcing copy of specified files in post build even if no change in source files

I have a project laid out like so:

CMakeLists.txt
|
|--------subdir1
|          |--CMakeLists.txt
|          |--sourcefiles
|          |--filetocopy1
|
|--------subdir2
           |--CMakeLists.txt
           |--sourcefiles
           |--filetocopy2 

I want to copy filetocopy1 and filetocopy2 to a specified output directory in the build folder. So in both, I have something like

add_custom_command(                                                                                                                                                       
  TARGET nameoftargetinsubdir1                                                                                                                                             
  POST_BUILD                                                                                                                                                              
  COMMAND ${CMAKE_COMMAND} -E copy                                                                                                                                        
  "${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1"                                                                                                    
  "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"                                                                                                                                     
  )

The problem is that if the filetocopy1 or filetocopy2 change, but the source files don't, then invoking make in the build folder doesn't copy the files. Is there some way to force it to copy those files? I'm getting the feeling I might have to put the copy commands in the top level CMakeLists.txt file.

Upvotes: 1

Views: 4943

Answers (2)

Soeren
Soeren

Reputation: 1829

If you simply want to copy these files to another place, I recommend to use the following command:

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1 ${CMAKE_BINARY_DIR}/ COPYONLY) 

https://cmake.org/cmake/help/v3.10/command/configure_file.html

This command is simple and effective. And it will copy your file to the destination, whenever cmake is invoked.

Upvotes: -1

Tsyvarev
Tsyvarev

Reputation: 66298

The main purpose of POST_BUILD custom commands is being run whenever target executable/library is rebuilt. If you don't need such behavior, use common custom commands with OUTPUT and DEPENDS option, combined with add_custom_target:

# If someone needs file "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/filetocopy1",
# this command will create it if the file doesn't exist or is older than
# "${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1".
add_custom_command(
  OUTPUT "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/filetocopy1"
  COMMAND ${CMAKE_COMMAND} -E copy
  "${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1"
  "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
  DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1"
  )

# Custom target for activate the custom command above
add_custom_target(copy_file1 DEPENDS "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/filetocopy1")

# Whenever target 'nameoftargetinsubdir1' is requested, the custom target will be evaluated.
add_dependencies(nameoftargetinsubdir1 copy_file1)

Upvotes: 5

Related Questions