如何强制cmake使用新版本的CMP0077(允许从变量中设置选项)



我正在尝试添加Howard Hinnant的date库作为我构建的子目录。以下是简单的设置:

$ git clone https://github.com/HowardHinnant/date.git
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(date_test)
set(BUILD_TZ_LIB ON)
set(USE_SYSTEM_TZ_DB ON)
set(ENABLE_DATE_TESTING OFF)
add_subdirectory(date)

这里没什么事。当我尝试使用cmake 3.18.3按原样配置这个构建时,我得到了一堆输出,比如:

CMake Warning (dev) at date/CMakeLists.txt:30 (option):
Policy CMP0077 is not set: option() honors normal variables.  Run "cmake
--help-policy CMP0077" for policy details.  Use the cmake_policy command to
set the policy and suppress this warning.
For compatibility with older versions of CMake, option is clearing the
normal variable 'USE_SYSTEM_TZ_DB'.
This warning is for project developers.  Use -Wno-dev to suppress it.
CMake Warning (dev) at date/CMakeLists.txt:34 (option):
Policy CMP0077 is not set: option() honors normal variables.  Run "cmake
--help-policy CMP0077" for policy details.  Use the cmake_policy command to
set the policy and suppress this warning.
For compatibility with older versions of CMake, option is clearing the
normal variable 'ENABLE_DATE_TESTING'.
This warning is for project developers.  Use -Wno-dev to suppress it.
CMake Warning (dev) at date/CMakeLists.txt:37 (option):
Policy CMP0077 is not set: option() honors normal variables.  Run "cmake
--help-policy CMP0077" for policy details.  Use the cmake_policy command to
set the policy and suppress this warning.
For compatibility with older versions of CMake, option is clearing the
normal variable 'BUILD_TZ_LIB'.
This warning is for project developers.  Use -Wno-dev to suppress it.
# date: USE_SYSTEM_TZ_DB OFF
# date: MANUAL_TZ_DB OFF
# date: USE_TZ_DB_IN_DOT OFF
# date: BUILD_SHARED_LIBS OFF
# date: ENABLE_DATE_TESTING OFF
# date: DISABLE_STRING_VIEW OFF

值得注意的是,我的变量被忽略了(USE_SYSTEM_TZ_DBOFF,而我希望它是ON)。

如果我按照错误所说的去做,并添加cmake_policy(SET CMP0077 NEW),那么。。。我得到了完全相同的东西。关于策略的警告消息相同。同样忽略我设置的变量。

有没有办法设置这些变量并将它们传播到date构建中,或者我是否有将它们声明为CACHE INTERNAL变量?

在调用add_subdirectory将日期库添加到项目之前,通过set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)设置该策略的默认值可以解决问题。

根据文件,

默认情况下,命令cmake_minimum_required(VERSION)和cmake_policy(VERSION)不设置给定版本后引入的策略。将CMAKE_POLICY_DEFAULT_CMP设置为OLD或NEW以指定策略CMP的默认值,其中是策略编号。

未设置CMAKE_POLICY_DEFAULT_CMP0077时,日期库的CMakeLists.txt中的cmake_minimum_required(VERSION 3.7)调用会将该策略的值重置为OLD行为。如果CMake不这样做,如果父项目更新其CMakeLists.txt以需要更新的版本,则通过FetchContent或git子模块包含的子项目的构建可能很容易中断。

最新更新