运行时输出目录的cmake生成器表达式

时间:2015-08-04 19:08:12

标签: cmake cross-platform

CMAKE_RUNTIME_OUTPUT_DIRECTORY是否有生成器表达式?

我想将目录复制到运行时输出目录,我目前这样做:

 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)

只是复制到CMAKE_RUNTIME_OUTPUT_DIRECTORY不适用于为每个配置创建子目录的多配置生成器(Visual Studio,XCode)。

上面的示例有效,但它会在copy_target上创建other_target的不必要的依赖关系。我更喜欢像${CMAKE_RUNTIME_OUTPUT_DIRECTORY_$<CONFIG>}这样的东西,但这不被接受(意外<)。此外,该解决方案也应该与单配置生成器一起使用,${CMAKE_RUNTIME_OUTPUT_DIRECTORY_$<CONFIG>}可能不会。

同样$<TARGET_FILE_DIR:copy_target>不起作用,因为copy_target是虚拟的,而不是二进制文件。

2 个答案:

答案 0 :(得分:2)

变量CMAKE_CFG_INTDIR可能会有所帮助,例如:

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)

答案 1 :(得分:1)

我看到sakra回答了你的问题,而且我对你的具体问题没有更好的答案。但我确实建议简化多配置构建(适用于Visual Studio)和单配置构建(适用于Linux makefiles)。我喜欢将输出目录折叠为扁平结构,所有配置工件都具有唯一的后缀,因此它们不会发生冲突。我使用以下init_output_directories宏执行此操作:

# initialize the variables defining output directories
#
# Sets the following variables:
#
# - :cmake:data:`CMAKE_ARCHIVE_OUTPUT_DIRECTORY`
# - :cmake:data:`CMAKE_LIBRARY_OUTPUT_DIRECTORY`
# - :cmake:data:`CMAKE_RUNTIME_OUTPUT_DIRECTORY`
#
# plus the per-config variants, ``*_$<CONFIG>``
#
# @public
#
macro(init_output_directories)

  # Directory for output files
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib 
    CACHE PATH "Output directory for static libraries.")

  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    CACHE PATH "Output directory for shared libraries.")

  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
    CACHE PATH "Output directory for executables and DLL's.")

  foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
    string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
    set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${CMAKE_BINARY_DIR}/bin" CACHE PATH "" FORCE)
    set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${CMAKE_BINARY_DIR}/lib" CACHE PATH "" FORCE)
    set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${CMAKE_BINARY_DIR}/lib" CACHE PATH "" FORCE)
  endforeach()

endmacro()