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.
Build directory: a directory, separate from the source, that holds everything CMake and the build tool write. One source tree can have several, with different settings, for example one for the development computer and one for a microcontroller §6.1.
Target: a named product, declared with
add_executableoradd_library, with its own sources, compiler options §12.3 and libraries to link.Cache: the file
CMakeCache.txtin the build directory, holding typed entries such asENABLE_LOG:BOOL=OFF.Generator: the kind of build files to write, chosen with
-G, for exampleNinjaorUnix Makefiles; the second writes makefiles §12.14 for GNU Make (GNU, a name that stands for GNU’s Not Unix, is the free-software project that publishes it).Toolchain §12.2 file: a CMake script, named with
-DCMAKE_TOOLCHAIN_FILE=or, since CMake 3.21,--toolchain, that describes the compiler and the target system for cross-compilation §12.1.
A build directory goes through these steps.
First configure, for example:
cmake -S . -B build -G Ninja
-DCMAKE_TOOLCHAIN_FILE=arm.cmakeCMake creates the cache, stores in it every
-Dvalue, and runsCMakeLists.txt. At the firstproject()command it reads the toolchain file, then identifies the compiler by compiling small test programs and stores the compiler’s path in the cache. Eachoption()orset(... CACHE ...)that finds no entry creates one with its default. The results of library searches and of compiler feature checks are cached too. Aconfigure_file()command writes a file from a template, for example a headerconfig.hfromconfig.h.in.Generate: CMake writes the build files, for Ninja the file
build.ninja.Build, with
cmake --build buildor by running the build tool. The build tool compiles, links, and runs the custom commands declared withadd_custom_command, such as a code generator.Later runs: the build files contain a rule that runs CMake again when a
CMakeLists.txtfile 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
-D NAME=VALUEon the command line writes the cache entry, replacing any value there.option(NAME ...)andset(NAME value CACHE ...)write only when no entry exists, unlesssetis given the keywordFORCE.An ordinary
set(NAME value)creates a normal variable, which hides the cache entry inside its scope without changing the cache.The environment variables
CC,CXXandCFLAGS, which name the C compiler, the C++ compiler and the C compiler options, are read on the first configure only; their values then live in the cache asCMAKE_C_COMPILER,CMAKE_CXX_COMPILERandCMAKE_C_FLAGS.The compiler and the generator are fixed at the first configure. Changing either needs a new build directory or
cmake --fresh(CMake 3.24 and later), which discards the cache.
A toolchain file typically sets these variables.
CMAKE_SYSTEM_NAME: the target’s operating system,Genericfor a device without one. Setting it marks the build as a cross-compilation.CMAKE_SYSTEM_PROCESSOR: the target’s processor family.CMAKE_C_COMPILER: the cross-compiler, such asarm-none-eabi-gcc.CMAKE_SYSROOTandCMAKE_FIND_ROOT_PATH: where the target’s headers and libraries are, with settings that keep library searches out of the build machine’s own directories.CMAKE_C_FLAGS_INIT: initial compiler options, which become the cachedCMAKE_C_FLAGSon the first configure only.CMAKE_TRY_COMPILE_TARGET_TYPEset toSTATIC_LIBRARY: compiler checks then build a library instead of a complete program, which would fail to link on a device without start-up code.
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.
Ninja sees that
CMakeLists.txtchanged after the build files were written, and runs CMake again.CMake loads the cache, in which
ENABLE_LOGisOFF.The
option()command finds an existing entry and does nothing.CMake writes the build files again with logging still off. The build succeeds, and the program has no logging, although
CMakeLists.txtsaysON.
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.
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.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.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.
Cached in
CMakeCache.txt: every value given with-D, everyoption()and cache variable, the toolchain file’s path, the chosen compiler and generator, the build type, the compiler options, and the results of library searches and compiler checks. Later runs start from these values, and a changed default inCMakeLists.txtdoes not replace an existing entry. The environment variablesCC,CXXandCFLAGSare read on the first configure only and then live in the cache.Generated: the build files for the chosen build tool, such as
build.ninja; files written from templates byconfigure_file(), such as a configuration header; and, at build time, the outputs of custom commands.Inherited from a toolchain file: the target system name and processor, the cross-compiler, the system root and the rules for searching libraries, the initial compiler options (which seed the cached options on the first configure only), and the setting that makes compiler checks build a library instead of a program.
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].