首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >覆盖超级构建CMakeLists.txt中的缓存变量

覆盖超级构建CMakeLists.txt中的缓存变量
EN

Stack Overflow用户
提问于 2020-07-02 18:28:54
回答 1查看 777关注 0票数 0

上下文

我有一个超级构建CMake项目,它集成了一个foo CMake项目(想想FetchContent()add_subdirectory())。

我希望能够更改foo中缓存变量的值(即不喜欢默认值)

问:我怎么能做到这一点?

这是一个简单的测试。

注意:我还测试了一个“选项()”,看看在这种情况下它是如何平滑的。

协议

布局CMakeLists.txt:

代码语言:javascript
运行
复制
# This is a super build project
cmake_minimum_required(VERSION 3.0)

project(SuperBuild VERSION 1.0 LANGUAGES CXX)

# Trying to override the FOO option of "third party" foo
set(FOO OFF)
# Trying to override the BAR cache variable of "third party" foo
set(BAR "foo")

# Incorporate the Foo third party project.
# Should be done using FetchContent(GIT_REPOSITORY .../foo.git) but, here
# I used add_subdirectory(foo) to keep it simple...
add_subdirectory(foo)

foo/CMakeLists.txt

代码语言:javascript
运行
复制
cmake_minimum_required(VERSION 3.0)

# option() honors normal variables.
# without this policy, cmake will "clear" the super build variable value
# and will use the option defualt value -_-
# see: https://cmake.org/cmake/help/git-stage/policy/CMP0077.html
if(POLICY CMP0077)
  cmake_policy(SET CMP0077 NEW)
endif()

# An option ON by default
option(FOO "My FOO option" ON)
message(WARNING "FOO: ${FOO}")

# A string variable cached with default "bar"
# we can override it on command line using for example -DBAR:STRING="foo"
# but here since we are in a super build we want to override it in the super
# build CMakeLists.txt directly
set(BAR "bar" CACHE STRING "My cache entry with default 'bar'")
message(WARNING "BAR: ${BAR}")

测试1

代码语言:javascript
运行
复制
echo "First run"
cmake -S. -Bbuild
echo "Second run"
cmake -S. -Bbuild

观察到的

代码语言:javascript
运行
复制
First run

-- The CXX compiler identification is GNU 10.1.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at foo/CMakeLists.txt:13 (message):
  FOO: OFF
CMake Warning at foo/CMakeLists.txt:20 (message):
  BAR: bar
-- Configuring done
-- Generating done
-- Build files have been written to: build

Second run

CMake Warning at foo/CMakeLists.txt:13 (message):
  FOO: OFF
CMake Warning at foo/CMakeLists.txt:20 (message):
  BAR: foo
-- Configuring done
-- Generating done
-- Build files have been written to: build

因此,第一次运行时,FOO选项将被超级构建变量正确覆盖,这要归功于CMP0077,而BAR缓存变量则设置为默认值。

在第二次运行期间,奇怪的是(任何欢迎的解释,IMHO是BAR已经在缓存中,所以超级构建集将更新它) BAR采用超级构建的值.

侧测

从头开始,只需测试我们可以覆盖cmdline中的值(不是我的情况,但仍然很有趣)。

代码语言:javascript
运行
复制
rm -rf build
cmake -S. -Bbuild -DBAR:STRING=plop
CMake Warning at foo/CMakeLists.txt:13 (message):
  FOO: OFF
CMake Warning at foo/CMakeLists.txt:20 (message):
  BAR: plop
-- Configuring done
-- Generating done
-- Build files have been written to: build

cmake -S. -Bbuild -DBAR:STRING=plop
CMake Warning at foo/CMakeLists.txt:13 (message):
  FOO: OFF
CMake Warning at foo/CMakeLists.txt:20 (message):
  BAR: plop
-- Configuring done
-- Generating done
-- Build files have been written to: build

对不起,与github相反的非语法突出显示,所以不承认cmake.

EN

回答 1

Stack Overflow用户

发布于 2020-07-02 18:31:37

一种可能的解决办法

在超级构建中,我可以使用:

代码语言:javascript
运行
复制
set(BAR "foo" CACHE STRING "Override foo default value")

缺点:

需要一个"docstring"

  • need才能知道底层变量类型

  • 将其公开给超级构建用户?

  • 丑陋的IMHO,请参阅option()和变量interaction:

如果

已将<variable>设置为普通变量或缓存变量,则该命令不执行任何操作(请参见策略CMP0077)。

参考文献:https://cmake.org/cmake/help/latest/command/option.html

编辑:正如Tsyvarev指出的,我们可以使用:

代码语言:javascript
运行
复制
set(BAR "foo" CACHE INTERNAL "")

它应该修复123,大部分是4

注意:要备份和重新应用以前缓存的变量值,请查看重复的答案.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62702858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档