8

我想要一个 Cmake 函数来将一些二进制文件复制到特定位置。为此,我有以下函数定义:

function ( collect_binaries TARGET_NAME DEST_DIR )
   set ( targetsToCopy ${ARGN} )

   set ( copy_cmd "COMMAND ${CMAKE_COMMAND} -E make_directory ${DEST_DIR}\n" )

   foreach ( target ${targetsToCopy} )
      LIST( APPEND copy_cmd "COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> ${DEST_DIR}$<TARGET_FILE_NAME:${target}>\n")
   endforeach( target ${targetsToCopy} )

   #message( FATAL_ERROR ${copy_cmd} )
   add_custom_target( ${TARGET_NAME} )
   add_custom_command( TARGET ${TARGET_NAME} PRE_BUILD ${copy_cmd} )

endfunction( collect_binaries )

以及以下用法:

collect_binaries( bin_copy ${PROJECT_BINARY_DIR}/out/ target_1 target_2 target3 )

我在项目树中定义了 target_1、target_2 和 target_3。考虑到这一点,我得到了以下 Cmake 配置输出:

binary_copy.cmake:15 (add_custom_command) 处的 CMake 警告 (dev):

未设置策略 CMP0040:add_custom_command() 的 TARGET 签名中的目标必须存在。运行“cmake --help-policy CMP0040”以获取策略详细信息。使用 cmake_policy 命令设置策略并禁止显示此警告。

在这种情况下,目标似乎是未知的......但它确实存在并且没有错字。这里有什么问题?

4

2 回答 2

4

copy_cmd您正在将函数中的变量设置collect_binaries为 CMake 字符串。然而,add_custom_command需要一个 CMake 列表来正确解析参数,即:

function ( collect_binaries TARGET_NAME DEST_DIR )
   set ( targetsToCopy ${ARGN} )

   set ( copy_cmd COMMAND ${CMAKE_COMMAND} -E make_directory ${DEST_DIR} )

   foreach ( target ${targetsToCopy} )
      LIST( APPEND copy_cmd COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> "${DEST_DIR}$<TARGET_FILE_NAME:${target}>")
   endforeach( target ${targetsToCopy} )

   #message( FATAL_ERROR ${copy_cmd} )
   add_custom_target( ${TARGET_NAME} )
   add_custom_command( TARGET ${TARGET_NAME} PRE_BUILD ${copy_cmd} )

endfunction( collect_binaries )
于 2015-11-13T17:09:03.937 回答
0

命令流add_custom_command(TARGET ...)仅适用于库和可执行目标。它不适用于使用add_cusom_target.

您需要使您的自定义目标依赖于复制的二进制文件,并创建用于复制该二进制文件的命令。(注意,任何 cmake 命令的参数列表都不应该用引号括起来)。

function ( collect_binaries TARGET_NAME DEST_DIR )
    set ( targetsToCopy ${ARGN} )

    set (create_directory_cmd COMMAND ${CMAKE_COMMAND} -E make_directory ${DEST_DIR})

    # Create one custom command per every file to be copied.
    # Also collect list of output files.
    set(output_files)
    foreach ( target ${targetsToCopy} )
        set(output_file "${DEST_DIR}/$<TARGET_FILE_NAME:${target}>")
        add_custom_command(OUTPUT ${output_file}
            ${create_directory_cmd}
            COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> ${output_file})
        list(APPEND output_files ${output_file})
        # It is sufficient to create directory only once. Just clear corresponded variable.
        set(create_directory_cmd)
    endforeach( target ${targetsToCopy} )

    add_custom_target( ${TARGET_NAME} DEPENDS ${output_files})
endfunction( collect_binaries )

如果您只想复制二进制文件,但您对原始文件不感兴趣,那么告诉 CMake 在您需要的地方创建二进制文件会更简单。任何一个

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/)

创建目标之前或

set_target_properties(target_1 target_2 target3
    PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/)

创建目标后就足够了。

于 2015-11-13T17:22:42.107 回答