linuxcmakeprebuild

cmake 2.8 custom target to copy multiple files


I am compelled to use an older cmake version 2.8.12, in a Linux environment.

As a pre-build step, I must copy multiple header files from a source directory to a destination directory. I decided to use the add_custom_target clause. If that in itself is the Bad Idea, please let me know. For example:

add_custom_target( prebuild
  COMMENT "Prebuild step: copy other headers"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different  ${CMAKE_SOURCE_DIR}/../other/include/alpha.h  ${CMAKE_SOURCE_DIR}/include/other
  COMMAND ${CMAKE_COMMAND} -E copy_if_different  ${CMAKE_SOURCE_DIR}/../other/include/bravo.h  ${CMAKE_SOURCE_DIR}/include/other
  COMMAND ${CMAKE_COMMAND} -E copy_if_different  ${CMAKE_SOURCE_DIR}/../other/include/charlie.h  ${CMAKE_SOURCE_DIR}/include/other
)

add_executable( myapp main.cxx )

# My application depends on the pre-build step.
add_dependencies( myapp prebuild )

set_target_properties( myapp PROPERTIES COMPILE_FLAGS "-g" )
install( TARGETS myapp DESTINATION ${BIN_INSTALL_DIR} )

It would be tedious to list each header file. I know how to search for all of the header files and put them in a list variable. For example.

file( GLOB other_headers "${CMAKE_SOURCE_DIR}/../other/include/*.h" )

But, how do I put that list variable to use within the add_custom_target clause?

Is there a way to copy multiple files within the add_custom_target clause?

Is there a better way to copy multiple files as a pre-build step that can be a dependency for the build of my application?

Being restricted to an older version of cmake limits my options. The following are things I have tried without success.


Solution

  • Using a foreach loop within the add_custom_target clause does not work.

    By using foreach you may create a variable with all required commands. Then use that variable in add_custom_target:

    set(commands)
    
    # Assume 'other_headers' contain list of files
    foreach(header ${other_headers})
      list(APPEND commands
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header}  ${CMAKE_SOURCE_DIR}/include/other)
    endforeach()
    
    add_custom_target( prebuild
      COMMENT "Prebuild step: copy other headers"
      ${commands}
    )