CMake:基于变量内容的add_custom_command参数 [英] Cmake: add_custom_command argument based on variable content

查看:114
本文介绍了CMake:基于变量内容的add_custom_command参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to have a Cmake function to copy some binaries to a specific location. Fo this I have the following function definition :

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配置输出:

I have target_1, target_2 and target_3 defined inside my project tree. With this in mind I got the following Cmake configure output :


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

CMake Warning (dev) at binary_copy.cmake:15 (add_custom_command):

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

Policy CMP0040 is not set: The target in the TARGET signature of add_custom_command() must exist. Run "cmake --help-policy CMP0040" for policy details. Use the cmake_policy command to set the policy and suppress this warning.

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

It seems that target in unknown in this context...but it does exist and there is no typo. What is the issue here?

推荐答案

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

You are setting up the copy_cmd variable in the collect_binaries function as a CMake string. The add_custom_command however requires a CMake list to parse the arguments correctly, i.e.:

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 )

这篇关于CMake:基于变量内容的add_custom_command参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆