I have the usecase that I want to include a subproject, and set some variables for options of that subproject. With policy 0077 set to new, I can do that. However I am experiencing some problems, that somehow setting the policy gets overwritten by the cmake_minimum_required
of the subpreject.
I narrowed it to a minimal example:
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(base)
cmake_policy(SET CMP0077 NEW)
cmake_policy(GET CMP0077 _POLICY_0077)
message(STATUS "POLICY is ${_POLICY_0077}")
set(MY_OPTION OFF)
add_subdirectory(project)
project/CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(my_proj)
cmake_policy(GET CMP0077 _POLICY_0077)
message(STATUS "POLICY is ${_POLICY_0077}")
option(MY_OPTION ON)
message(STATUS "MY_OPTION ${MY_OPTION}")
Running CMake prints:
-- POLICY is NEW
-- POLICY is
CMake Warning (dev) at project/CMakeLists.txt:8 (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 'MY_OPTION'.
This warning is for project developers. Use -Wno-dev to suppress it.
-- MY_OPTION OFF
I would expect that setting the policy in the top level CMake file would persist over all files included with add_subdirectory()
.
Thus I would expect that CMake should twice print POLICY is NEW
, and not only once.
Obviously, I cannot change the project/CMakeLists.txt
when including e.g. a thirdparty library or such.
And the weirdest thing is, that apparently, it does actually honor the variable, since the output prints “OFF”.
Any thougths?