记录一个cmake局部变量传入子项目问题CMP0077

最近在搞一个依赖了开源库fftw(写此文时用的官网最新发布版3.3.10,https://www.fftw.org/fftw-3.3.10.tar.gz)的代码,项目使用cmake构建,fftw依赖以子项目的方式引入:

add_subdirectory(dep/fftw-3.3.10)

测试后发现调用代码使用的是float版的接口,也就是需要通过设置“ENABLE_FLOAT”这个选项来编译fftw,参见:

set (FFTW_VERSION 3.3.9)

set (PREC_SUFFIX)
if (ENABLE_FLOAT)
  set (FFTW_SINGLE TRUE)
  set (BENCHFFT_SINGLE TRUE)
  set (PREC_SUFFIX f)
endif ()

由于这个是调用代码的刚需,所以不想通过cmake命令行传递此设置,于是想到了再add_subdirectory前临时设置下这个选项,如下:

set(CUR_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS FALSE)
set(ENABLE_FLOAT TRUE)
add_subdirectory(dep/fftw-3.3.10)
set(BUILD_SHARED_LIBS ${CUR_BUILD_SHARED_LIBS})

同时,还设置了编译静态库的选项,当然,也是仅作用于fftw构建上,随后触发cmake时得到如下警告信息:

[cmake] CMake Warning (dev) at dep/fftw-3.3.10/CMakeLists.txt:13 (option):
[cmake] Policy CMP0077 is not set: option() honors normal variables. Run “cmake
[cmake] –help-policy CMP0077″ for policy details. Use the cmake_policy command to
[cmake] set the policy and suppress this warning.
[cmake]
[cmake] For compatibility with older versions of CMake, option is clearing the
[cmake] normal variable ‘BUILD_SHARED_LIBS’.
[cmake] This warning is for project developers. Use -Wno-dev to suppress it.
[cmake]
[cmake] CMake Warning (dev) at dep/fftw-3.3.10/CMakeLists.txt:20 (option):
[cmake] Policy CMP0077 is not set: option() honors normal variables. Run “cmake
[cmake] –help-policy CMP0077″ for policy details. Use the cmake_policy command to
[cmake] set the policy and suppress this warning.
[cmake]
[cmake] For compatibility with older versions of CMake, option is clearing the
[cmake] normal variable ‘ENABLE_FLOAT’.
[cmake] This warning is for project developers. Use -Wno-dev to suppress it.

并且,实际构建时发现设置的选项并没有起作用,在fftw内部cmake加了些message确认两个选项并没有如愿生效!于是乎跟着提示信息查了下这个CMP0077,找到了这个网页:https://stackoverflow.com/questions/66340703/how-to-force-cmake-to-use-the-new-version-of-cmp0077-allow-options-to-be-set-fr,读了一遍后发现原来是cmake版本导致的脚本代码行为差异,我的cmake项目依赖最小版本指定的是:“cmake_minimum_required(VERSION 3.8)”,按照上面网页的提示,需要增加额外设置,开启CMP0077 NEW的策略:

set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

主CMakeLists.txt增加上述配置后,警告消失,并且设置的变量正确传入了fftw的cmake项目构建中,问题解决!

博主友情提示:

如您在评论中需要提及如QQ号、电子邮件地址或其他隐私敏感信息,欢迎使用>>博主专用加密工具v3<<处理后发布,原文只有博主可以看到。