Mental model
Make builds a dependency graph of targets and runs a recipe only when its target is missing or older than any prerequisite (file mtime comparison). Targets that are not files are declared .PHONY and always run.
target: prereq1 prereq2 | order-only-prereq
recipe line 1 # TAB-indented; each line runs in its own shell
@recipe line 2 # @ = don't echo the command
-recipe line 3 # - = ignore a non-zero exit
- Order-only prerequisites (after
|) must exist but don’t trigger rebuilds by being newer. Use for output directories. - The first target in the file is the default for a bare
make.
Variables and environment
| Syntax | Meaning | Use for |
|---|---|---|
X = val |
Recursive: expanded every time it’s used | Values that reference later-defined vars |
X := val |
Simple: expanded once, at definition | Default choice; $(shell ...) results |
X ?= val |
Set only if not already set (env or CLI) | Overridable defaults: CSV ?= data/raw/x.csv |
X += val |
Append | Building flag lists |
$(X) / $$X |
Make variable / shell variable inside a recipe | $$ escapes $ for the shell |
Precedence, highest first: command line (make train EPOCHS=5) → Makefile → environment.