What CMake & Meson Generate
In day-to-day C and C++ work you rarely write a build.ninja; you meet the ones that CMake and Meson write. This lesson generates both for the same two-file project and reads them with the tools from the previous lessons, so the files stop being a black box. CMake itself is taught properly in Modern CMake; Meson in Meson, Autotools & xmake.
CMake with the Ninja generator
cmake -G Ninja writes build.ninja into the build directory, plus CMakeFiles/rules.ninja, which it includes. Each target gets its own compile and link rules:
mkdir -p calc && cd calc
printf 'int add(int a, int b) { return a + b; }\n' > add.c
printf '#include <stdio.h>\nint add(int, int);\nint main(void) { printf("%%d\\n", add(2, 3)); return 0; }\n' > main.c
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(calc C)
add_library(mathlib STATIC add.c)
add_executable(calc main.c)
target_link_libraries(calc PRIVATE mathlib)
CM
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release > /dev/null
ls build
echo "--- rules:"
grep '^rule' build/CMakeFiles/rules.ninja
echo "--- inside the compile rule and the re-run rule:"
grep -A8 '^rule C_COMPILER__calc' build/CMakeFiles/rules.ninja | grep -E '^ (depfile|deps) '
grep -A5 '^rule RERUN_CMAKE' build/CMakeFiles/rules.ninja | grep -E '^ generator'
echo "--- the compile of main.c:"
grep -A4 '^build CMakeFiles/calc.dir/main.c.o' build/build.ninja
ninja -C build -j1
build/calcCMakeCache.txt
CMakeFiles
build.ninja
cmake_install.cmake
--- rules:
rule C_COMPILER__mathlib_unscanned_Release
rule C_STATIC_LIBRARY_LINKER__mathlib_Release
rule C_COMPILER__calc_unscanned_Release
rule C_EXECUTABLE_LINKER__calc_Release
rule CUSTOM_COMMAND
rule RERUN_CMAKE
rule CLEAN
rule HELP
--- inside the compile rule and the re-run rule:
depfile = $DEP_FILE
deps = gcc
generator = 1
--- the compile of main.c:
build CMakeFiles/calc.dir/main.c.o: C_COMPILER__calc_unscanned_Release /home/learner/calc/main.c || cmake_object_order_depends_target_calc
CONFIG = Release
DEP_FILE = CMakeFiles/calc.dir/main.c.o.d
FLAGS = -O3 -DNDEBUG -fno-diagnostics-color
OBJECT_DIR = CMakeFiles/calc.dir
ninja: Entering directory `build'
[1/4] Building C object CMakeFiles/mathlib.dir/add.c.o
[2/4] Linking C static library libmathlib.a
[3/4] Building C object CMakeFiles/calc.dir/main.c.o
[4/4] Linking C executable calc
5
Everything from the Ninja lessons is here:
- Build-level variables (
FLAGS,DEP_FILE) fill in a shared rule's command, as in Rules, Build Statements & Variables. The flags forReleasecame fromCMAKE_BUILD_TYPE. - Header dependencies: the rule has
depfile = $DEP_FILEanddeps = gcc. - Order-only inputs:
|| cmake_object_order_depends_target_calcis a phony target that makes sure anything the target's sources might need (generated headers, the objects of dependencies' custom commands) exists before compiling, without forcing recompiles. RERUN_CMAKEis agenerator = 1rule whose output isbuild.ninjaitself, with everyCMakeLists.txtas an input. Edit aCMakeLists.txt, runninja, and it first re-runs CMake, then builds with the new file.
ninja -C build (or cmake --build build, which calls Ninja for you) is how you build. The tools from Ninja's Built-in Tools work on this tree too: ninja -C build -t targets rule C_COMPILER__calc_unscanned_Release, ninja -C build -t commands calc, ninja -C build -t compdb.
Meson
Meson always generates Ninja files on Linux. Its build.ninja is a single file with generic rules (c_COMPILER, c_LINKER) and per-edge ARGS:
mkdir -p calc && cd calc
printf 'int add(int a, int b) { return a + b; }\n' > add.c
printf '#include <stdio.h>\nint add(int, int);\nint main(void) { printf("%%d\\n", add(2, 3)); return 0; }\n' > main.c
cat > meson.build <<'MS'
project('calc', 'c')
mathlib = static_library('mathlib', 'add.c')
executable('calc', 'main.c', link_with: mathlib)
MS
meson setup build --buildtype=release > /dev/null
grep '^rule' build/build.ninja
echo "--- the compile of main.c:"
grep -A4 '^build calc.p/main.c.o' build/build.ninja | grep -v DEPFILE_UNQUOTED
ninja -C build -j1
build/calcrule c_COMPILER
rule STATIC_LINKER
rule c_LINKER
rule CUSTOM_COMMAND
rule REGENERATE_BUILD
--- the compile of main.c:
build calc.p/main.c.o: c_COMPILER ../main.c
DEPFILE = calc.p/main.c.o.d
ARGS = -Icalc.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O3
ninja: Entering directory `build'
[1/4] Compiling C object libmathlib.a.p/add.c.o
[2/4] Linking static target libmathlib.a
[3/4] Compiling C object calc.p/main.c.o
[4/4] Linking target calc
5
Meson's build tree is relative to itself (../main.c), CMake's uses absolute source paths. Both use one directory per target for objects (calc.p/, CMakeFiles/calc.dir/), and both regenerate themselves through a generator rule (REGENERATE_BUILD). In this course's image Meson also found ccache on the PATH and wrapped the compiler with it automatically; CMake only does that if you ask (CMAKE_C_COMPILER_LAUNCHER=ccache).
And Makefiles?
CMake can also generate Makefiles (-G "Unix Makefiles", the default on Linux when no generator is chosen and CMAKE_GENERATOR isn't set). Those are recursive Makefiles that call $(MAKE) -f CMakeFiles/….dir/build.make per target, plus CMake's own dependency scanning. They work, but no-op builds are noticeably slower than with Ninja, and -j must be passed by hand (cmake --build build -j 8). Most projects and IDEs default to Ninja for that reason.
| CMake + Ninja | CMake + Makefiles | Meson | |
|---|---|---|---|
| Generated files | build.ninja, CMakeFiles/rules.ninja |
Makefile + one build.make per target |
build.ninja |
| Rules | per target and config | recursive sub-makes | generic, flags per edge |
| Header deps | deps = gcc |
CMake's depend.make / compiler depfiles |
deps = gcc |
| Re-runs itself when build files change | yes (RERUN_CMAKE) |
yes (cmake_check_build_system) |
yes (REGENERATE_BUILD) |
Test yourself
What happens when you edit CMakeLists.txt and run ninja in a CMake build tree?
- Nothing changes until you run cmake by hand
- The
RERUN_CMAKEgenerator edge regeneratesbuild.ninjafirst, then Ninja builds with it - Ninja reports an error
- Everything is rebuilt from scratch
In CMake's generated file, what does || cmake_object_order_depends_target_calc express?
- An implicit output
- Order-only inputs: things that must exist before compiling, without forcing recompiles
- A pool
- A comment
How do CMake and Meson pass per-file flags to a shared compile rule?
- Through environment variables
- With build-statement variables (
FLAGS,ARGS) that the rule's command references - By generating one rule per file
- Through response files only