Make / Makefile Cheatsheet
Old, universal, and still the easiest task-runner. Tab-indented rules only.
1 credit
Rule basics
makefile
.PHONY: build test clean build: dist/app.js dist/app.js: src/*.ts npm run build test: npm test clean: rm -rf dist
`.PHONY` marks targets that don't correspond to files — always run. Use tabs (NOT spaces) for recipe lines.
Variables
5 itemsSimple assign
NAME = myappConditional (only if unset)
NAME ?= defaultAppend
FLAGS += -O2Auto vars in recipe
$@ (target) $< (first prereq) $^ (all prereqs)Shell command
VERSION := $(shell git rev-parse --short HEAD)CLI args & patterns
5 itemsPattern rule
%.o: %.c\n\t$(CC) -c $< -o $@Multi-target
make build testOverride var
make build NAME=otherDry-run
make -n buildVerbose
make -d buildCommon patterns
- Put `help:` first — show `@grep -E '^[a-z.-]+:' Makefile` so `make` without args lists targets.
- Never call `rm -rf /` paths derived from variables — assert expected prefix first.
- Use `$(MAKE)` instead of `make` when recursing into sub-Makefiles.