Section 12.12

CMake

Full name: Cross-platform Make.

Definition

CMake is a build-system generator: a program that reads a description of what to build and writes the input files of another build tool, such as Ninja §12.13 or Make, which then runs the compiler and linker §12.4. The description is a set of files named CMakeLists.txt. CMake works in two stages: configure runs those files, examines the compiler and records its decisions in a cache file in the build directory, and generate writes the build files. Its developers say the C in the name stands for cross-platform.

The idea

A C project may have to build with several compilers, on several operating systems, with several build tools, and often for a processor other than the one the build runs on. With CMake the project states once what to build, for example this program from these source files with this library, and CMake writes suitable build files for each combination. Kitware, a software company, develops CMake and first released it in 2000.

The cost is a second layer of state. CMake makes many decisions once, on the first configure of a build directory: which compiler, which options, where each library is. It records them in the cache, the file CMakeCache.txt, and starts every later run from those values. A changed default in CMakeLists.txt therefore does not change a decision already in the cache, much as printing a new blank form changes nothing on a copy already filled in. The picture stops at one point: values that the project sets outside the cache are computed again on every run.

How it works

The main terms are these.

A build directory goes through these steps.

  1. First configure, for example:

    cmake -S . -B build -G Ninja
    -DCMAKE_TOOLCHAIN_FILE=arm.cmake

    CMake creates the cache, stores in it every -D value, and runs CMakeLists.txt. At the first project() command it reads the toolchain file, then identifies the compiler by compiling small test programs and stores the compiler’s path in the cache. Each option() or set(... CACHE ...) that finds no entry creates one with its default. The results of library searches and of compiler feature checks are cached too. A configure_file() command writes a file from a template, for example a header config.h from config.h.in.

  2. Generate: CMake writes the build files, for Ninja the file build.ninja.

  3. Build, with cmake --build build or by running the build tool. The build tool compiles, links, and runs the custom commands declared with add_custom_command, such as a code generator.

  4. Later runs: the build files contain a rule that runs CMake again when a CMakeLists.txt file or a script it includes changes. That run starts from the existing cache.

Every configure starts from the cache and so replays the recorded decisions. These rules decide which value wins.

A toolchain file typically sets these variables.

Every target inherits these settings, and so do the test programs of the compiler checks. The toolchain file’s path is itself a cache entry, so later runs read the same file again.

Worked example

A project’s CMakeLists.txt contains option(ENABLE_LOG "Build logging" OFF). A developer configures once with cmake -S . -B build -G Ninja, and the cache now holds ENABLE_LOG:BOOL=OFF. Later the developer changes the default to ON and runs cmake --build build.

  1. Ninja sees that CMakeLists.txt changed after the build files were written, and runs CMake again.

  2. CMake loads the cache, in which ENABLE_LOG is OFF.

  3. The option() command finds an existing entry and does nothing.

  4. CMake writes the build files again with logging still off. The build succeeds, and the program has no logging, although CMakeLists.txt says ON.

Three commands give the intended result: cmake -DENABLE_LOG=ON build, which writes the entry; cmake --fresh -S . -B build, which discards the cache; or a configure into a new, empty directory. To see the effective value without configuring again, run cmake -N -L build, which lists the cache entries.

Stated precisely

The state of a build directory is its cache together with the files generated from it. Each configure computes that state from the -D values, the existing cache, the CMakeLists.txt files and the scripts they include, the toolchain file, the environment (partly on the first run only), the results of examining the compiler, and the CMake version with its policy settings, the switches by which newer versions keep older behavior. For one cache entry, the value after a configure is the -D value if one was given; otherwise the existing entry, if any; otherwise the default of the first option() or set(... CACHE ...) that runs for it. A set(... CACHE ... FORCE) that runs replaces the value in every case.

A build from the directory gives the intended program only under three assumptions.

  1. Effective configuration: the cache and generated files hold the intended values, as read from them, not as inferred from CMakeLists.txt, a requested option or a toolchain file’s name.

  2. Declared inputs: every custom command lists all its input files with DEPENDS, because the build tool tracks only the dependencies written into the build files.

  3. Current file lists: a source list from file(GLOB ...) is computed at configure time, so a new source file is not built until CMake runs again.

Limits and common mistakes

A successful configure is not a successful build, and a successful build does not show that the intended target, options and compiler were used. Check the exact compiler commands in the build tool’s verbose output or in compile_commands.json, which CMake writes for the Ninja and makefile generators when CMAKE_EXPORT_COMPILE_COMMANDS is on.

An environment change is invisible until CMake runs again, and CC and CXX are not read again even then. The CMake documentation advises against file(GLOB) for source lists; its CONFIGURE_DEPENDS flag makes the build check the list again, but not reliably with every generator. A custom command with an incomplete DEPENDS list can leave a stale generated file, as the Ninja entry shows.

Two build directories configured differently give different programs from the same source, so a result is reproducible only together with the cache and toolchain file of its build directory. A presets file, CMakePresets.json (CMake 3.19 and later), records the generator, toolchain file and cache values under version control and makes the first configure repeatable.

Configuring a project runs its scripts, which can run any command with the user’s permissions. Configuring an untrusted project means running untrusted code.

Questions answered

What is cached, generated, or inherited from a toolchain file?

CMake, a build-system generator, caches its decisions in each build directory, generates build files and some source files, and passes the settings of a toolchain file on to every target and compiler check.

To know the effective configuration, read the cache of the actual build directory (cmake -N -L build) and its generated files, not the requested options.

Related entries: Cross-compilation (§12.1), Ninja (§12.13), Make (§12.14), Toolchain (§12.2), Firmware build profile (§6.44). Further reading: [108].