上下文
我有一个超级构建CMake项目,它集成了一个foo CMake项目(想想FetchContent()或add_subdirectory())。
我希望能够更改foo中缓存变量的值(即不喜欢默认值)
问:我怎么能做到这一点?
这是一个简单的测试。
注意:我还测试了一个“选项()”,看看在这种情况下它是如何平滑的。
协议
布局CMakeLists.txt:
# 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
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
echo "First run"
cmake -S. -Bbuild
echo "Second run"
cmake -S. -Bbuild观察到的
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中的值(不是我的情况,但仍然很有趣)。
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.
发布于 2020-07-02 18:31:37
一种可能的解决办法
在超级构建中,我可以使用:
set(BAR "foo" CACHE STRING "Override foo default value")缺点:
需要一个"docstring"
option()和变量interaction:。
如果
已将
<variable>设置为普通变量或缓存变量,则该命令不执行任何操作(请参见策略CMP0077)。
参考文献:https://cmake.org/cmake/help/latest/command/option.html
编辑:正如Tsyvarev指出的,我们可以使用:
set(BAR "foo" CACHE INTERNAL "")它应该修复1,2,3,大部分是4。
注意:要备份和重新应用以前缓存的变量值,请查看重复的答案.
https://stackoverflow.com/questions/62702858
复制相似问题