如何强制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变量?

回答

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

根据文档,

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

如果CMAKE_POLICY_DEFAULT_CMP0077没有设置,则cmake_minimum_required(VERSION 3.7)在通话CMakeLists.txt的日期库的重置这一政策为原来的行为的价值。如果 CMake 没有这样做,FetchContent如果父项目更新其 CMakeLists.txt 以需要更新的版本,则通过或 git 子模块包含的子项目的构建很容易中断。


以上是如何强制cmake使用新版本的CMP0077(允许从变量设置选项)的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>