Search code examples
cmakecross-platform

cmake generator expression for runtime output directory


Is there a generator expression for CMAKE_RUNTIME_OUTPUT_DIRECTORY?

I want to copy a directory to the runtime output directory, which I currently do like this:

 add_custom_target(copy_target ALL)
 add_custom_command(TARGET copy_target POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_directory
      ${CMAKE_CURRENT_SOURCE_DIR}/foo
      $<TARGET_FILE_DIR:other_target>/foo)

Just copying to CMAKE_RUNTIME_OUTPUT_DIRECTORY will not work for multi-config generators (Visual Studio, XCode) that create subdirectories for each configuration.

The example above works but it creates an unnecesary dependency of copy_target on other_target. I would prefer something like ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_$<CONFIG>} but this was not accepted (unexpected <). Also, the solution should work with single-config generators as well, which ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_$<CONFIG>} probably wouldn't.

Also $<TARGET_FILE_DIR:copy_target> does not work, because copy_target is a dummy, not a binary.


Solution

  • The variable CMAKE_CFG_INTDIR may be helpful, e.g.:

    add_custom_command(TARGET copy_target POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_directory
          ${CMAKE_CURRENT_SOURCE_DIR}/foo
          ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/foo)