如果未在命令行上明确指定,则将安装前缀自动设置为自定义路径 [英] Set installation prefix automatically to custom path if not explicitly specified on the command line

查看:95
本文介绍了如果未在命令行上明确指定,则将安装前缀自动设置为自定义路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些内部测试,除非用户明确覆盖,否则我希望将安装前缀默认为构建目录的子目录。我知道用户可以通过以下方式指定安装前缀:

For some internal tests, I would like the install prefix to default to a subdirectory of the build directory, unless explicitly overridden by the user. I know the user can specify a install prefix by:

$ cmake -DCMAKE_INSTALL_PREFIX=/foo/bar ..

但是如果用户未指定 ,则默认为 $ {PWD} /已安装

But if the user does not specify this, it should default to, e.g. ${PWD}/installed.

变量 CMAKE_INSTALL_PREFIX 已设置为 / usr / local ,所以我不能在设置它之前先检查它是否未设置/为空。

The variable CMAKE_INSTALL_PREFIX is already set to /usr/local, so I cannot just check to see if it unset/empty before setting it.

我当前的解决方案是添加一个用户必须调用的自定义开关,以指定要尊重 CMAKE_INSTALL_PREFIX 变量:

My current solution is to add a custom switch that the user has to invoke to specify that the CMAKE_INSTALL_PREFIX variable gets respected:

option(ENABLE_INSTALL_PREFIX "Install build targets to system (path given by '-DCMAKE_INSTALL_PREFIX' or '${CMAKE_INSTALL_PREFIX}' if not specified)." OFF)
if ( ENABLE_INSTALL_PREFIX )
    set (CMAKE_INSTALL_PREFIX installed CACHE PATH "Installation root")
else()
    set (CMAKE_INSTALL_PREFIX installed CACHE PATH "Installation root" FORCE)
endif()

我的问题是:

(a )除了需要传递给CMake以获得 CMAKE_INSTALL_PREFIX 的额外标志的麻烦之外,上述内容是否还有其他问题?有效果吗?

(a) Are there any issues with the above, beyond the annoyance of the extra flag needing to be passed to CMake to get CMAKE_INSTALL_PREFIX to have an effect?

(b)是否有更好,更清洁,更强大,更惯用和/或更讨厌的方式来实现上述目标?

(b) Is there a better, cleaner, more robust, more idiomatic and/or less annoying way to achieve the above?

谢谢。

推荐答案

CMake设置布尔变量 CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT 如果未明确指定 CMAKE_INSTALL_PREFIX 且已初始化为默认设置。您可以通过以下方式覆盖它:

CMake sets the boolean variable CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT if CMAKE_INSTALL_PREFIX has not been explicitly specified and is initialized to its default setting. You can override it in the following way:

if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/installed" CACHE PATH "default install path" FORCE )
endif()

这篇关于如果未在命令行上明确指定,则将安装前缀自动设置为自定义路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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