Mike Finch
Mike Finch

Reputation: 887

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.

Upvotes: 2

Views: 1117

Answers (2)

Tsyvarev
Tsyvarev

Reputation: 66298

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}
)

Upvotes: 6

Kevin
Kevin

Reputation: 18396

You could use foreach to add custom targets for each header file, but also pull the add_dependencies() call into the loop block:

add_executable( myapp main.cxx )

foreach(cur_header ${other_headers})
  # Get the filename from the full path.
  get_filename_component(my_header_name ${cur_header} NAME)
  # Add a new custom target for the current header.
  add_custom_target( prebuild_${my_header_name}
    COMMENT "Prebuild step: copy other headers"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cur_header} ${CMAKE_SOURCE_DIR}/include/other
  )
  # My application depends on the each pre-build target.
  add_dependencies( myapp prebuild_${my_header_name} )
endforeach()

Upvotes: 1

Related Questions