CMake:如何获取安装规则的目标位置 [英] CMake: how to get target location for install rule

查看:213
本文介绍了CMake:如何获取安装规则的目标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行此操作的CMakeLists.txt:

I have a CMakeLists.txt that does this:

get_target_property(myloc mytarget LOCATION)

它曾经可以正常工作,但是使用 LOCATION 弃用了CMake 3.0(请参见< a href = https://cmake.org/cmake/help/v3.0/policy/CMP0026.html rel = noreferrer> https://cmake.org/cmake/help/v3.0/policy/ CMP0026.html )。因此,我尝试使用生成器表达式:

It used to work fine, but CMake 3.0 deprecated using LOCATION (see https://cmake.org/cmake/help/v3.0/policy/CMP0026.html). So I tried using a generator expression:

set(myloc $<TARGET_FILE:mytarget>)

这似乎可行,除了生成器表达式并非到处都被求值,它们似乎仅在设置其他目标的属性时起作用,并在生成步骤而不是之前的配置步骤中解决。问题是,我需要知道 install()规则中的目标位置,像这样(实际用途不是 strip 但这没关系):

This seemed like it would work, except that generator expressions are not evaluated everywhere, they only seem to work when setting properties of other targets, and are resolved during the "generation" step, not the earlier "configuration" step. The problem is, I need to know the target location in an install() rule, something like this (the real use is not strip but that doesn't matter):

install(CODE "execute_process(COMMAND strip ${myloc})")

在使用 LOCATION 时,此方法很好用,但是现在已经过时了,我想不通正确的方法。问题的根源似乎是在目标路径未知的情况下,在配置步骤中调用了 install()

This worked fine when using LOCATION but now that's deprecated and I can't figure out the right way to do this. The root of the problem seems to be that install() is invoked during the "configuration" step, when the target path is not known.

在调用 install()之前,我如何弥合这一差距,并像以前一样发现目标输出路径?

How can I bridge this gap, and discover the target output path as I used to do, before calling install()?

推荐答案

理想情况下,可以通过发电机表达式

Ideally, it would be done via generator expressions:

install(CODE "execute_process(COMMAND strip $<TARGET_FILE:mytarget>)")

不幸的是,对生成器的支持仅在CMake 3.14中添加了 install 命令的 CODE SCRIPT 模式下的表达式(请参见该版本中安装命令的文档)。

Unfortunately, support for generator expressions in CODE and SCRIPT modes of install command has been added only in CMake 3.14 (see documentation of install command in that version).

在CMake 3.14之前,您只能使用单一配置生成器(例如

Before CMake 3.14 you may work only with single-configuration generators (e.g. with Makefile but not with Visual Studio).

在这种情况下,您可以禁用CMP0026警告,如蒂勒曼的答案并读取LOCATION属性。

In such conditions, you may either disable CMP0026 warning, as suggested in the Th.Thielemann's answer and read LOCATION property.

也可以使用 install(SCRIPT )命令流,其中使用文件(生成)命令,类似于 configure_file ,但可用于生成器表达式:

Or you may use install(SCRIPT) flow of the command, where the script is prepared with file(GENERATE) command which is like configure_file but works with generator expressions:

file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake
     CONTENT "execute_process(COMMAND strip $<TARGET_FILE:mytarget>)")

install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake)






请注意,使用 file(GENERATE)的方法仍然不适用于 multi-configuration 生成器: CMake要求 OUTPUT 子句的文件名在配置之间是唯一的。可以使用


Note, that the approach with file(GENERATE) still doesn't work with multi-configuration generators: CMake requires filename for OUTPUT clause to be unique between the configurations. One may "fix" that with

file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake_$<CONFIG>
     CONTENT "execute_process(COMMAND strip $<TARGET_FILE:mytarget>)")

install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake_${CMAKE_BUILD_TYPE})

但这仍然行不通:在多配置生成器中, CMAKE_BUILD_TYPE 被评估

but this still won't work: in multi-configuration generators CMAKE_BUILD_TYPE is evaluated to empty string.

(将 $ {CMAKE_BUILD_TYPE} 替换为 $< CONFIG>)。 install(SCRIPT)命令中的仅在CMake 3.14及更高版本中可用,但在这些版本中,整个 file(GENERATE )并不需要,也可以只使用第一个摘要。)

(Replacing ${CMAKE_BUILD_TYPE} with $<CONFIG> in install(SCRIPT) command would work only in CMake 3.14 and after, but in these versions the whole file(GENERATE) is not needed and one may just use the very first snippet.)

这篇关于CMake:如何获取安装规则的目标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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