← ClaudeAtlas

makefile-best-practiceslisted

Makefile best practices, patterns, and templates for GNU Make 4.x — dependency graphs, task-runner workflows, parallel-safe recipes, self-documenting help targets, and language-specific patterns (Go, Python, Node, Docker, Helm, POSIX).
air-gapped/skills · ★ 3 · AI & Automation · score 79
Install: claude install-skill air-gapped/skills
# Makefile Best Practices **Target:** GNU Make 4.x. Covers Make as both a build system (dependency-driven compilation) and a task runner (developer workflow automation). ## Golden Rules ### 0. Simplicity First - Start with the minimum viable solution; each target does ONE thing well. - Default to <=10 focused targets; expand only on explicit request. ### 1. Make is a Dependency Graph, Not a Script Targets represent outputs; prerequisites represent inputs; recipes transform inputs → outputs. Think graph-first. ```makefile # WRONG: Script thinking - order-dependent, breaks with -j build: compile src/a.c compile src/b.c link # RIGHT: Graph thinking - declares real dependencies program: a.o b.o $(CC) -o $@ $^ %.o: %.c $(CC) -c $< -o $@ ``` ### 2. Correctness Under `make -j` is the Real Bar If it breaks with parallel builds, it's broken. Always declare real dependencies. ```makefile # WRONG: Hidden dependency, races under -j generated.h: ./generate-header.sh > $@ main.o: main.c # Missing: generated.h $(CC) -c $< -o $@ # RIGHT: Explicit dependency main.o: main.c generated.h $(CC) -c $< -o $@ ``` Validate dependency correctness with `make --shuffle=random -j` (GNU Make 4.4+). Randomizing prerequisite order exposes missing edges that a fixed order hides. ### 3. Phony vs File Targets Drive Behavior Use `.PHONY` for commands, not for artifacts. Understanding timestamps is 80% of Make proficiency. ```makefile .PHONY: clean test lint help # Commands - always r