Clean, Incremental & No-op Builds
"The build takes four minutes" means nothing until you say which build. There are three you should always measure separately, because each one is slowed down by different things and fixed by different techniques.
| Build | Starting point | What it should cost | Typical fixes |
|---|---|---|---|
| Clean | empty build directory | every TU compiled and linked once | PCH, unity builds, caches, more cores |
| Incremental | one file edited | only what depends on that file | header hygiene, forward declarations, smaller targets |
| No-op | nothing changed | zero commands | remove always-run steps, fix generators |
The no-op build is the one people forget. It runs after every git pull with no relevant change, every IDE save, every "did it build?" check. If it costs ten seconds, everyone pays ten seconds hundreds of times a day.
Counting work instead of timing it
Ninja's status line, [3/7], says how many commands it ran and how many it planned. That count is deterministic, so it's the right measure for comparing the three builds. This session builds a small project, then runs a no-op build, edits one source file, and then edits a header that two files include:
mkdir -p kinds/src && cd kinds
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Kinds CXX)
add_executable(app src/main.cpp src/parser.cpp src/report.cpp src/store.cpp)
target_include_directories(app PRIVATE src)
CM
printf '#pragma once\nint parse();\nint report();\nint store();\n' > src/api.hpp
printf '#pragma once\nstruct Config { int verbose = 0; };\n' > src/config.hpp
printf '#include "api.hpp"\nint main() { return parse() + report() + store() - 3; }\n' > src/main.cpp
printf '#include "api.hpp"\n#include "config.hpp"\nint parse() { return Config{}.verbose + 1; }\n' > src/parser.cpp
printf '#include "api.hpp"\n#include "config.hpp"\nint report() { return Config{}.verbose + 1; }\n' > src/report.cpp
printf '#include "api.hpp"\nint store() { return 1; }\n' > src/store.cpp
cmake -S . -B build -G Ninja > /dev/null
count() { cmake --build build | grep -c '^\[' ; }
echo "clean build: $(count) commands"
echo "no-op build: $(cmake --build build)"
sleep 1 && touch src/store.cpp
echo "edit store.cpp: $(count) commands"
sleep 1 && touch src/config.hpp
echo "edit config.hpp: $(count) commands"clean build: 5 commands
no-op build: ninja: no work to do.
edit store.cpp: 2 commands
edit config.hpp: 3 commands
- The clean build ran 5 commands: four compiles and one link.
- The no-op build ran nothing, which is exactly what you want.
- Editing a
.cppfile recompiled it and relinked (2). - Editing
config.hpprecompiled the two files that include it, plus the link (3). Ninja knew which ones from the header dependencies the compiler wrote (-MD).
The incremental cost of a header is "how many TUs include it, directly or indirectly". A header included by 400 files turns a one-line edit into a near-clean build. The header cost lesson measures that fan-out.
When the no-op build isn't free
Some CMake patterns make every build do work even when nothing changed. A custom target with no outputs is always out of date:
mkdir -p noop/src && cd noop
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Noop CXX)
add_executable(app src/main.cpp)
# Anti-pattern: a stamp step without OUTPUT runs on every build
add_custom_target(stamp ALL COMMAND ${CMAKE_COMMAND} -E echo "stamping build info")
add_dependencies(app stamp)
CM
echo 'int main() { return 0; }' > src/main.cpp
cmake -S . -B build -G Ninja > /dev/null
cmake --build build > /dev/null
echo "--- second build, nothing changed:"
cmake --build build
echo "--- third build:"
cmake --build build--- second build, nothing changed:
[1/1] cd /home/learner/noop/build && /opt/cmake/bin/cmake -E echo stamping\ build\ info
stamping build info
--- third build:
[1/1] cd /home/learner/noop/build && /opt/cmake/bin/cmake -E echo stamping\ build\ info
stamping build info
Here the step is cheap, but in real projects it's often a version-stamp generator, a code generator or a script that copies resources, and because app depends on it, anything it touches can trigger recompiles and relinks too. The fix is to give the step real OUTPUTs and DEPENDS with add_custom_command, and to write generated files only when their content changes (Ninja's restat, see The Build Log & -d explain).
Other common no-op killers:
file(GLOB … CONFIGURE_DEPENDS)re-checks the directories on every build (cheap for small trees, noticeable for huge ones).- Generated headers rewritten on every configure (a timestamp or
git describeinconfigure_file), which recompile everything that includes them. - Tests or packaging hooked into
ALL.
Measure all three
A useful habit is a tiny script in the repository that reports the three counts for the current tree, run in CI on every change to the build files:
cmake --build build --target clean && cmake --build build | grep -c '^\[' # clean
cmake --build build | grep -c '^\[' # no-op: must print 0
touch src/core/common.hpp && cmake --build build | grep -c '^\[' # a popular headerThe first number tells you about raw compile cost, the second about build-system hygiene, and the third about your header structure.
Test yourself
Editing config.hpp recompiled two files and relinked. Why exactly two?
- Ninja recompiles two files per header
- Two TUs included config.hpp, recorded in the compiler's dependency output
- Because of -j1
- CMake re-ran configure
What should a no-op build cost?
- About 10% of a clean build
- Exactly one link
- Zero commands
- One configure run
Why does add_custom_target(stamp ALL COMMAND …) run on every build?
- Custom targets have no outputs, so they are always considered out of date
- Because of ALL alone
- Because Ninja ignores custom commands
- Because the command uses cmake -E