Cheatsheets/Makefile
Tools

Makefile

Build automation — targets, variables, and pattern rules.

Basic structure

target: dep1 dep2
	command

# Phony targets don't produce output files
.PHONY: clean test build

clean:
	rm -rf dist/

test:
	npm test

Variables

foo = barLazy (recursive) assignment
foo := barImmediate assignment
foo ?= barSet only if unset
foo += barAppend to variable
foo != echo hiShell command result
$(foo)Expand variable

Magic variables

$@Current target name
$<First prerequisite
$^All prerequisites
$*Stem of pattern rule match
$(@D)Directory part of target

Command prefixes

-commandIgnore errors (continue on failure)
@commandSuppress command echo
+commandRun even in dry-run mode (make -n)

Functions

$(wildcard *.c)Expand glob
$(patsubst %.c,%.o,$(src))Pattern substitution
$(shell find . -name "*.js")Shell command
$(filter %.js,$(files))Filter by pattern
$(strip $(var))Remove whitespace

Options

make -eEnv vars override file vars
make -BRebuild all unconditionally
make -sSilent mode
make -j4Parallel jobs
make -nDry run (print, no execute)