setting CMP0091 policy on command line broken by cmake_minimum_required
When building different open source projects using the VS MSVC toolchain I use the following technique to set the target runtime (the following example is for a release build with a static CRT runtime linkage):
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>" ..
This usually works fairly well even for projects (CMakeLists.txt files) which are not at all aware about CMP0091
.
Recently I have been trying to use this on libzmq
(https://github.com/zeromq/libzmq), where the top-level CMakeLists.txt
starts with this:
# CMake build script for ZeroMQ
project(ZeroMQ)
if(${CMAKE_SYSTEM_NAME} STREQUAL Darwin)
cmake_minimum_required(VERSION 3.0.2)
else()
cmake_minimum_required(VERSION 2.8.12)
endif()
After tracing and debugging I figured out that CMP0091
is not set to NEW
, when checked in the internal script:
Windows-MSVC.cmake(323): cmake_policy(GET CMP0091 __WINDOWS_MSVC_CMP0091 )
Windows-MSVC.cmake(324): if(__WINDOWS_MSVC_CMP0091 STREQUAL NEW )
and it seems to be directly related to cmake_minimum_required
command. When I comment this out, it works as expected. So it is easily hot-fixed, but I am reluctant to submit a PR to libzmq
because I am not sure that cmake behavior is correct.
By setting the CMP0091
explicitly at the command line it should be clear what the user intention is. cmake
alone should know its own version (cmake version 3.20.21032501-MSVC_2
in this particular case), yet it seems to clear the policy set by user regardless. I guess it should be OK if it does it for policies which are not explicitly set, to ensure the consistent policy state for the environment, but it should not set defaults for policies set explicitly.
The point is that the use cmake_minimum_required
in libzmq
is not wrong - it specifies the minimum version the build needs to succeed, but it should not prevent using more advanced features if the present cmake
version supports them.