skip to content

After a CMake project raises its policy version, add_executable(app main) fails with Cannot find source file; why, and how do you migrate policies safely?

level: seniorimportance: should knowfreq 35%

answer

  1. a policy flipped to NEW
  2. the warning you ignored
  3. cmake --help-policy
  4. SET NEW, rarely OLD
  5. if(POLICY) for older CMake

basics

~20 s

Raising the policy version set CMP0115 to NEW, so CMake stopped guessing the .cpp extension. Fix the code to name main.cpp; migrate by reading each 'Policy CMPxxxx is not set' warning first and only using cmake_policy(SET ... OLD) temporarily.

solid answer

~50 s

With `cmake_minimum_required(VERSION 3.10)`, policy `CMP0115` (introduced in 3.20) is unset, so CMake keeps its `OLD` behaviour, appends known extensions to `main`, finds `main.cpp`, and prints `Policy CMP0115 is not set: Source file extensions must be explicit.` Changing the call to `3.10...4.4` sets every policy through 4.4 to `NEW`; with `CMP0115` `NEW`, `main` is taken literally and configure fails with `Cannot find source file: main`. The cure is the code: write `main.cpp`. A safe migration reads the policy warnings under the current version, fixes each one, then raises `<policy_max>`. `cmake --help-policy CMP0115` explains any policy, `cmake_policy(GET ...)` reports its setting, and `cmake_policy(SET CMP0115 OLD)` buys time but should be rare and temporary, because the OLD behaviour is deprecated and eventually removed. Use `if(POLICY CMPxxxx)` before setting a policy that older supported CMake releases do not know.

code

cmake · 8 lines
cmake
cmake_minimum_required(VERSION 3.10...4.4)
project(pol CXX)

# CMP0115 is NEW from the policy version 4.4: no extension guessing
add_executable(app main.cpp)

cmake_policy(GET CMP0115 cmp0115_state)
message(STATUS "CMP0115=${cmp0115_state}")   # prints CMP0115=NEW

go deeper

for a junior

Know that CMake prints 'Policy CMPxxxx is not set' warnings and that they announce behaviour that will change when the project's version is raised.

for a middle

Explain how the policy version flips policies to NEW, and use cmake --help-policy and cmake_policy(GET) to see what one changes.

for a senior

Diagnose a configure failure caused by a policy turning NEW, fix the code rather than pinning OLD, and run CI across the supported CMake range.

for a principal

Own the cadence of policy upgrades: when the team raises policy_max, who fixes warnings, and when temporary OLD settings must be removed.

## What happened The project began with: ```cmake cmake_minimum_required(VERSION 3.10) project(pol CXX) add_executable(app main) ``` The file on disk is `main.cpp`. Under CMake 4.4 this configures, but with a warning: ```plaintext CMake Warning (policy) at CMakeLists.txt:3 (add_executable): Policy CMP0115 is not set: Source file extensions must be explicit. Run "cmake --help-policy CMP0115" for policy details. Use the cmake_policy command to set the policy and suppress this warning. ``` Someone then modernizes the first line to `cmake_minimum_required(VERSION 3.10...4.4)`, and configure fails: ```plaintext CMake Error at CMakeLists.txt:3 (add_executable): Cannot find source file: main ``` ## Why: a policy changed its behaviour **Policy `CMP0115`**, introduced in CMake 3.20, governs whether CMake guesses source file extensions. Its `OLD` behaviour appends known extensions to a name it cannot find, so `main` becomes `main.cpp`; its `NEW` behaviour requires the extension to be written. The **policy version** decides the setting: - with policy version 3.10, `CMP0115` (3.20) is newer, so it is **unset**: `OLD` behaviour plus a warning; - with policy version 4.4, it is older than the policy version, so it is **`NEW`**: the guess is gone. Nothing about the CMake binary changed. The project changed which behaviour it asked for, and code that relied on the old one broke. The warning had announced the break all along. ## Tools for reading policies | Need | Command | |---|---| | What does this policy change? | `cmake --help-policy CMP0115` | | What is its current setting here? | `cmake_policy(GET CMP0115 var)` gives `NEW`, `OLD` or empty | | Use the new behaviour now | `cmake_policy(SET CMP0115 NEW)` | | Keep the old behaviour for a while | `cmake_policy(SET CMP0115 OLD)` | | Guard against older CMake | `if(POLICY CMP0115)` around the `SET` | | Default an unset policy from outside | `-DCMAKE_POLICY_DEFAULT_CMP0115=NEW` | The `cmake-policies` manual is blunt about `OLD`: a policy should almost never be set to `OLD`, except to silence warnings in an otherwise frozen codebase or temporarily during a migration. `OLD` behaviours are deprecated from the release that introduces the policy, may draw warnings after about two years, and may be removed in a later major release at least six years on, as CMake 4.0 did for everything older than 3.5. ## Scope of a policy setting Policy settings live on a **stack**. Each `add_subdirectory` gets its own entry, so a `cmake_policy(SET ...)` in a subdirectory does not leak to its parent or siblings. Scripts loaded with `include()` or `find_package()` also get their own entry unless called with `NO_POLICY_SCOPE`. For a temporary change inside one file, `block(SCOPE_FOR POLICIES)` (since 3.25) or a `cmake_policy(PUSH)` and `POP` pair restores the previous settings automatically. ## A safe migration procedure 1. **Configure under the current version and collect warnings.** Each `Policy CMPxxxx is not set` line names a behaviour that will change. 2. **Read each policy** with `cmake --help-policy` and fix the code so it works under `NEW`; for `CMP0115` that means writing `main.cpp`. 3. **Raise `<policy_max>`** in `cmake_minimum_required` to the newest CMake you test, leaving `<min>` at the oldest you support. 4. **Run CI at both ends** of the range, so code that depends on a policy's `OLD` behaviour in the older release is caught. 5. **Set `OLD` only as a dated workaround**, ideally in a narrow scope, with a tracked task to remove it. 6. **Repeat with each CMake upgrade**, because every release adds policies. ## Pitfalls - Silencing warnings wholesale hides exactly the messages that predict this break; turn them off only for third-party code you do not own. - A `cmake_policy(SET ... NEW)` for a policy the oldest supported CMake does not know is an error there; wrap it in `if(POLICY ...)`. - Setting `CMAKE_POLICY_DEFAULT_CMP<NNNN>` in a preset or CI script changes only **unset** policies; a project that sets the policy itself wins.

  • Your project must still configure with CMake 3.16, but you want CMP0135's NEW behaviour. How do you set it?
    Wrap it: `if(POLICY CMP0135) cmake_policy(SET CMP0135 NEW) endif()`. CMP0135 arrived in 3.24, so CMake 3.16 does not know it, and an unguarded `cmake_policy(SET ...)` would fail there. Newer releases apply the NEW behaviour; older ones skip the call.
  • Does cmake_policy(SET CMP0115 OLD) in a subdirectory affect the parent directory?
    No. Each `add_subdirectory` gets its own entry on the policy stack, so the setting applies to that directory and its children only. Files loaded by `include()` share the caller's entry only when called with `NO_POLICY_SCOPE`.

  • Policy warnings are cosmetic and safe to suppress permanently
  • Setting every new policy to OLD is the safe way to upgrade CMake
  • The CMake upgrade itself broke the build, not the project's policy version
  • cmake_policy(SET ...) in a subdirectory changes the whole project
  • An unknown policy ID in cmake_policy(SET) is silently ignored by older CMake