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 items
Simple assign
NAME = myapp
Conditional (only if unset)
NAME ?= default
Append
FLAGS += -O2
Auto vars in recipe
$@ (target) $< (first prereq) $^ (all prereqs)
Shell command
VERSION := $(shell git rev-parse --short HEAD)

CLI args & patterns

5 items
Pattern rule
%.o: %.c\n\t$(CC) -c $< -o $@
Multi-target
make build test
Override var
make build NAME=other
Dry-run
make -n build
Verbose
make -d build

Common 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.

Further reading