Policies & CMake 4
CMake has to fix mistakes in its own behaviour without breaking the thousands of projects that depend on the old behaviour. Its mechanism for that is the policy: every behaviour change gets a number (CMP0077, CMP0168, …), an OLD behaviour (the way it used to work) and a NEW behaviour. A project chooses, usually through cmake_minimum_required, and CMake keeps both alive for years.
How policies get set
cmake_minimum_required(VERSION 3.25) # every policy up to 3.25 -> NEW
cmake_minimum_required(VERSION 3.25...4.4) # NEW up to 4.4 if CMake is that new, else up to the running version
cmake_policy(SET CMP0168 NEW) # one policy explicitly
cmake_policy(GET CMP0168 result) # read the current settingA policy newer than your policy version is unset. Unset policies use the OLD behaviour, and many warn (Policy CMP0xxx is not set: …) when your project would behave differently under NEW. Those warnings are a to-do list for upgrading.
mkdir -p pol && cd pol
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Pol LANGUAGES NONE)
foreach(p CMP0077 CMP0135 CMP0167 CMP0168 CMP0177)
cmake_policy(GET ${p} state)
if(state STREQUAL "")
set(state "unset (OLD behaviour)")
endif()
message("${p}: ${state}")
endforeach()
CM
cmake -S . -B build --log-level=NOTICE 2>&1 | grep '^CMP'
echo "--- with a version range up to 4.4"
sed -i 's/VERSION 3.25)/VERSION 3.25...4.4)/' CMakeLists.txt
cmake -S . -B build2 --log-level=NOTICE 2>&1 | grep '^CMP'CMP0077: NEW
CMP0135: NEW
CMP0167: unset (OLD behaviour)
CMP0168: unset (OLD behaviour)
CMP0177: unset (OLD behaviour)
--- with a version range up to 4.4
CMP0077: NEW
CMP0135: NEW
CMP0167: NEW
CMP0168: NEW
CMP0177: NEW
| Policy | Introduced | NEW behaviour |
|---|---|---|
| CMP0077 | 3.13 | option() honours an existing normal variable (parents can preset a subproject's options) |
| CMP0135 | 3.24 | archives downloaded with URL get the extraction time as timestamps (so rebuilds happen after an update) |
| CMP0167 | 3.30 | the old FindBoost module is removed; find_package(Boost) uses Boost's own config file |
| CMP0168 | 3.30 | FetchContent populates directly instead of through a sub-build |
| CMP0177 | 3.31 | install() DESTINATION paths are normalized |
The version range is how a project says "I need 3.25, but I've tested with 4.4's behaviour". On CMake 4.4 all those policies became NEW; on CMake 3.25 the project would still configure, with the 3.25 behaviour.
CMake 4: compatibility below 3.5 is gone
Every policy has a lifetime. CMake 4.0 removed the OLD behaviour of all policies introduced before 3.5, which means a project whose cmake_minimum_required asks for an older version can no longer be configured. Many old projects (and old copies of third-party code vendored into new ones) say VERSION 2.8 or 3.1. Here is exactly what CMake 4.4 does with them:
mkdir -p old && cd old
echo 'int main() { return 0; }' > main.cpp
for v in 2.8.12 3.1 3.5 3.9 3.10; do
printf 'cmake_minimum_required(VERSION %s)\nproject(Old CXX)\nadd_executable(app main.cpp)\n' "$v" > CMakeLists.txt
rm -rf build
cmake -S . -B build -G Ninja --log-level=NOTICE > log.txt 2>&1
echo "VERSION $v -> exit $?: $(grep -m1 -oE 'Compatibility with CMake < [0-9.]+ (has been removed|will be removed)' log.txt || echo 'no warning')"
doneVERSION 2.8.12 -> exit 1: Compatibility with CMake < 3.5 has been removed
VERSION 3.1 -> exit 1: Compatibility with CMake < 3.5 has been removed
VERSION 3.5 -> exit 0: Compatibility with CMake < 3.10 will be removed
VERSION 3.9 -> exit 0: Compatibility with CMake < 3.10 will be removed
VERSION 3.10 -> exit 0: no warning
- Below 3.5: a hard error.
- 3.5 to 3.9: works, with a deprecation warning: compatibility with versions below 3.10 is the next to go.
- 3.10 and later: silent.
The full error message tells you the options:
mkdir -p old && cd old
printf 'cmake_minimum_required(VERSION 3.1)\nproject(Old CXX)\n' > CMakeLists.txt
cmake -S . -B build -G Ninja --log-level=NOTICE
echo "=== escape hatch: CMAKE_POLICY_VERSION_MINIMUM"
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_POLICY_VERSION_MINIMUM=3.5 2>&1 | grep -E '^CMake Warning|Compatibility with|^-- 'CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 3.5 has been removed from CMake.
Update the VERSION argument <min> value. Or, use the <min>...<max> syntax
to tell CMake that the project requires at least <min> but has been updated
to work with policies introduced by <max> or earlier.
Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.
-- Configuring incomplete, errors occurred!
=== escape hatch: CMAKE_POLICY_VERSION_MINIMUM
CMake Warning (deprecated) at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 3.10 will be removed from a future version of
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/old/build
CMAKE_POLICY_VERSION_MINIMUM=3.5 (a cache variable or environment variable) makes CMake treat any older cmake_minimum_required as 3.5. It is meant for building other people's old projects (a dependency you can't easily patch), not as a fix for your own: for your own project, raise the minimum. When the old code is a vendored dependency, upgrading it, or replacing add_subdirectory(old_lib) with a package from a package manager, is the long-term fix.
Other CMake 4 changes worth knowing
- Removed long-deprecated modules and behaviours whose policies were already old, for example
exec_programand the oldFindBoost/FindCUDApaths (when the corresponding policies are NEW). - The generator list keeps the deprecated "extra generators" (
CodeBlocks - Ninja,Eclipse CDT4 - …) marked as deprecated (see Generators); use the File API based IDE integrations instead. find_packageunderstands CPS (<Name>.cps) package descriptions in addition to<Name>Config.cmake.- New releases keep adding policies at a steady pace; reading the release notes' "Policies" section before bumping the version range is a five-minute job that prevents surprises.
A practical upgrade routine
- Raise the upper end of the range (
3.25...4.4) and configure with the new CMake. - Read every
Policy CMP… is not setwarning and the release notes for the new policies. - Fix the code, or pin a single policy with
cmake_policy(SET CMP0xxx OLD)with a comment and a ticket to remove it. - Raise the lower end only when you actually need a feature from the newer version, since it forces every user to upgrade.
Test yourself
What does CMake 4.4 do with cmake_minimum_required(VERSION 3.1)?
- Configures normally
- Warns and continues
- Fails: compatibility with CMake < 3.5 has been removed
- Silently upgrades to 4.4
Your project uses cmake_minimum_required(VERSION 3.25) and runs on CMake 4.4. What is the state of CMP0168 (introduced in 3.30)?
- NEW
- OLD, set explicitly
- Unset, which means OLD behaviour
- Removed
What is the purpose of -DCMAKE_POLICY_VERSION_MINIMUM=3.5?
- Make your new project faster
- Configure an old third-party project that requests a version below 3.5, treating it as 3.5
- Downgrade CMake
- Enable CPS