CMake policies vs. minimum CMake version

CMake policies typically default to OLD behavior compatible with prior versions of CMake. However, cmake_minimum_required() automatically sets all the CMake policies to “new” through the minimum CMake version. For example, a project with cmake_minimum_required(VERSION 3.14) implicitly sets to NEW all CMake policies CMP0088 and older.

Tradespace

New versions of CMake don’t get the benefits of new behaviors until specifying the new behavior is OK, either implicitly via cmake_minimum_required() or explicitly via cmake_policy(). The downsides of a too-old cmake_minimum_required() include that you have to set if(CMAKE_VERSION VERSION_GREATER_EQUAL ...) statements around extra cmake_policy() statements.

Important policies

Here are a few CMake policies we generally use, typically implicitly by choosing at least CMake 3.13:

cmake_minimum_required(VERSION 3.13)
  • CMP0074 find_package uses PackageName_ROOT variables
  • CMP0076 target_sources() command converts relative paths to absolute. This allows specifying targets in the top-level CMakeLists.txt while having add_subdirectory() containing their sources.
  • CMP0077 option() honors normal variables. Does not manipulate variables when user has defined their value.

Deprecation

CMake gradually deprecates old policies, meaning the deprecated policies default to NEW behavior. For example, CMake 3.18 deprecates CMP0071 and older.