TExcelTAD
Home › Build Fundamentals: From Source to Binary › Field Notes: Build Hygiene

Field Notes: Build Hygiene

Habits that keep builds fast, correct and boring, whatever tool you end up using. They apply to a 50-line Makefile and to a 5,000-target Bazel monorepo alike.

If make clean fixes it, the graph is wrong

"Try a clean build" is a workaround, not a fix. When a clean build succeeds where an incremental one failed (or produced different output), some dependency is missing from the build graph: a header not tracked (Header Dependencies), a generated file not declared (Parallel Builds), a flag or environment variable not part of the key (Hermetic Builds). Find the missing edge. Every clean build you do to "fix" things costs your whole team that build time again next week.

Build out of the source tree

Keep outputs in a separate directory (build/, out/, target/). Cleaning becomes rm -rf build, several configurations live side by side (build-debug/, build-release/, build-arm64/), the source tree stays read-only-friendly, and git status stays quiet. Make doesn't do this on its own, but it is only a few lines:

bash · try it yourself
mkdir -p proj/src && cd proj && git init -q
printf 'int twice(int x) { return 2 * x; }\n' > src/util.c
printf '#include <stdio.h>\nint twice(int);\nint main(void) { printf("%%d\\n", twice(21)); return 0; }\n' > src/main.c
cat > Makefile <<'MK'
BUILD ?= build
SRCS := src/main.c src/util.c
OBJS := $(SRCS:src/%.c=$(BUILD)/%.o)

$(BUILD)/app: $(OBJS)
	$(CC) $^ -o $@
$(BUILD)/%.o: src/%.c | $(BUILD)
	$(CC) $(CFLAGS) -c 
lt; -o $@ $(BUILD): mkdir -p $@ MK printf 'build*/\n' > .gitignore make -j1 && build/app make -j1 BUILD=build-release CFLAGS=-O2 -s && build-release/app git status --short
mkdir -p build
cc  -c src/main.c -o build/main.o
cc  -c src/util.c -o build/util.o
cc build/main.o build/util.o -o build/app
42
42
?? .gitignore
?? Makefile
?? src/

git status shows only the files that belong in the repository; both build directories are ignored by one pattern. The | $(BUILD) is an order-only prerequisite: the directory must exist, but its timestamp (which changes whenever a file inside is added) never triggers a rebuild. CMake, Meson and Cargo build out of tree by default.

Never commit build outputs

Objects, libraries, executables, generated headers and compile_commands.json are products of the build, not sources. Committed outputs go stale, bloat history forever, and cause merge conflicts nobody can resolve. The exceptions are deliberate and documented: a vendored, hashed third-party archive, or a generated file checked in because generating it needs a tool users don't have (then CI should verify that it is up to date).

Make the build show its commands

When something compiles differently in CI than on your machine, the first question is "what exact command ran?". Know how to get it:

Tool Show full commands
Make make V=1 (if the Makefile supports it), make --trace (also says why), make -n
CMake (Makefile generator) cmake --build build -- VERBOSE=1, or CMAKE_VERBOSE_MAKEFILE=ON
Ninja ninja -v, ninja -t commands target
Meson meson compile -C build -v
Cargo cargo build -v
Gradle --info, --debug, or a build scan

make --trace is underused. It prints the reason each target is remade:

bash · try it yourself
mkdir -p tr && cd tr
printf 'int main(void) { return 0; }\n' > main.c
printf 'app: main.o\n\tgcc main.o -o app\nmain.o: main.c\n\tgcc -c main.c\n' > Makefile
make -j1 -s
sleep 1 && touch main.c
make -j1 --trace
Makefile:4: update target 'main.o' due to: main.c
gcc -c main.c
Makefile:2: update target 'app' due to: main.o
gcc main.o -o app

Stamp builds with their version, not the time

Every binary you ship should be able to say exactly which source it came from. git describe gives a readable version from the most recent tag plus the number of commits since and the commit hash; pass it in as a define. (In this session's repository, Git's dates are fixed, so the commit hashes are the same on every run.)

bash · try it yourself
mkdir -p stamp && cd stamp && git init -q
printf '#include <stdio.h>\nint main(void) { printf("tool %%s\\n", APP_VERSION); return 0; }\n' > main.c
git add main.c && git commit -q -m "first release"
git tag -a v1.2.0 -m "1.2.0"
gcc -DAPP_VERSION="\"$(git describe --tags --always --dirty)\"" main.c -o tool && ./tool
echo '/* fix */' >> main.c && git commit -qam "fix a bug"
gcc -DAPP_VERSION="\"$(git describe --tags --always --dirty)\"" main.c -o tool && ./tool
echo '/* experiment */' >> main.c
gcc -DAPP_VERSION="\"$(git describe --tags --always --dirty)\"" main.c -o tool && ./tool
tool v1.2.0
tool v1.2.0-1-ge6ef9b6
tool v1.2.0-1-ge6ef9b6-dirty

v1.2.0-1-g… means "one commit after v1.2.0, at commit …", and -dirty warns that the working tree had uncommitted changes, which is a build that should never be released. Unlike __DATE__, this keeps the build reproducible (Reproducible Builds). Remember that the version define is an input: a build system must rebuild the file that uses it when the version changes (CMake projects usually generate a small version.c for that reason, so only one file recompiles).

More rules worth adopting

  • -Werror in CI, not in release tarballs. New compiler versions add warnings; a tarball that fails to build with next year's GCC because of -Werror frustrates every packager. Turn it on in your CI configuration, not in the default flags.
  • Pin and record the toolchain. Note the compiler version in build logs and release metadata (gcc --version | head -1). For CI, use a pinned container image or toolchain package, not "whatever ubuntu-latest has today".
  • List sources explicitly. Globbing ($(wildcard src/*.c), CMake's file(GLOB)) silently picks up stray files and misses new ones until reconfiguration. Explicit lists make additions visible in code review.
  • One command from clean checkout to tested artifact. If building needs a wiki page of manual steps, CI and new colleagues will get it wrong. Script it.
  • Measure before optimising. Know your clean and incremental build times, and which steps dominate (ninja -t tools, .ninja_log, Clang's -ftime-trace). Build Performance & Artifacts goes deep.

Test yourself

An incremental build fails, but a clean build succeeds. What does that usually mean?

  1. The compiler is broken
  2. A dependency is missing from the build graph
  3. The disk is full
  4. Nothing, clean builds are normal

What does -dirty in git describe output mean?

  1. The tag is invalid
  2. The working tree had uncommitted changes when the build was made
  3. The commit is unsigned
  4. The build failed

Why should -Werror be enabled in CI rather than in a project's default flags?

  1. It is slow
  2. Future compiler versions add warnings, which would break builds for users and packagers
  3. CI doesn't show warnings
  4. It disables optimisation