A Makefile is a list of rules that tells make how to create each file from the files it's made from, so that after a change, make reruns only the steps that are out of date. make is a command-line tool that reads the Makefile in the current folder. This note is about GNU Make, the make on most Linux systems and on macOS. See the GNU Make manual.
What problem does it solve?
You keep a small analysis in weather/. tidy.py drops incomplete rows from raw.csv and writes tidy.csv, and plot.py draws figure.svg from tidy.csv:
python3 tidy.py raw.csv tidy.csv
python3 plot.py tidy.csv figure.svg
After you change the colors in plot.py, only the second command needs to run. When new data arrives in raw.csv, both do, in this order. If you always run both, you wait for a cleaning step that had nothing new to do. If you run only the one you think is needed, sooner or later you'll skip one by mistake, and the figure will show old data without any warning.
How does a rule work?
A Makefile describes each step as a rule. A rule names a target, the file to create, and its prerequisites, the files the target is made from. Below them, its recipe gives the commands that create the target. Save this as weather/Makefile:
figure.svg: tidy.csv plot.py
python3 plot.py tidy.csv figure.svg
tidy.csv: raw.csv tidy.py
python3 tidy.py raw.csv tidy.csv
Each recipe line starts with a tab character, not spaces.
Run make in weather/. With no arguments, it builds the first target in the file, figure.svg. Before it runs a target's recipe, it brings the target's prerequisites up to date, so tidy.csv comes first. make prints each command, then runs it:
make
python3 tidy.py raw.csv tidy.csv
python3 plot.py tidy.csv figure.svg
Run make again, and nothing runs:
make: `figure.svg' is up to date.
The output in this note is from GNU Make 3.81, the version that comes with macOS. Other versions may word their messages differently.
make decides by modification time. It runs a target's recipe when the target doesn't exist, or when any prerequisite was modified after the target was. Edit plot.py, and the next make reruns only the plot:
python3 plot.py tidy.csv figure.svg
Figure 1 shows the comparisons behind that decision.
make checks every rule, but only figure.svg has a prerequisite newer than itself, so one of the two recipes runs.Edit raw.csv instead, and both recipes run. The new tidy.csv is then newer than figure.svg, so the plot is redrawn too.
To build a single target, name it, as in make tidy.csv. To see which commands would run without running them, use make -n.
What else goes in a Makefile?
Most Makefiles also use variables and phony targets. Here is the same Makefile with both:
PYTHON = python3
figure.svg: tidy.csv plot.py
$(PYTHON) plot.py $< $@
tidy.csv: raw.csv tidy.py
$(PYTHON) tidy.py $< $@
.PHONY: clean
clean:
rm -f tidy.csv figure.svg
- Variables.
$(PYTHON)expands topython3. A value given on the command line replaces the one in the file, somake PYTHON=.venv/bin/pythonruns both scripts with your virtual environment's Python. - Automatic variables. In a recipe,
$@is the target and$<is the first prerequisite, so each file name is written once. See automatic variables. - Phony targets. A phony target is a name for a command rather than a file.
make cleandeletes both outputs, and.PHONY: cleantellsmakethat no file calledcleanis involved. See phony targets.
What should I watch out for?
- Recipe lines need a tab. With spaces,
makestops with amissing separatorerror. Set your editor to keep tabs in Makefiles, and check the indentation of any Makefile you copy from a web page. - Each recipe line runs in its own shell. A
cdon one line is forgotten on the next. Join commands that depend on each other with&&, as incd output && ls. GNU Make 3.82 added.ONESHELL:to run a whole recipe in one shell, but the 3.81 that comes with macOS ignores it. $belongs tomake. In a recipe,makereads$HOMEas the variableHfollowed by the textOME, soecho $HOMEprintsOME. Write$$HOMEto pass$HOMEto the shell.makeonly knows the prerequisites you list. Ifplot.pyimports astyle.pythat the rule doesn't list, editingstyle.pywon't redrawfigure.svg. List every file a recipe reads.- A failed recipe can leave a target that looks up to date. If
tidy.pycrashes after writing half oftidy.csv,makestops with an error, but the partialtidy.csvis now newer than its prerequisites. The nextmakeskips it, draws the figure from the partial data, and succeeds. Add a line with.DELETE_ON_ERROR:to the Makefile, andmakedeletes a target whose recipe fails. makecompares times, not contents. Switching Git branches gives the files that differ a new modification time, so targets can be rebuilt even when their inputs end up as they were. A file copied with its old time kept, ascp -pdoes, may not trigger a rebuild at all.make -Brebuilds every target.
When is a Makefile worth it?
- Files made from files, in several steps. Data pipelines, generated figures, LaTeX documents, and compiled code all fit.
makereruns only the steps whose inputs changed, in the right order. - Short names for project commands. A Makefile of phony targets, such as
make testandmake lint, gives everyone on a project the same commands. You don't use the timestamp checks, butmakeis usually installed already on Linux and macOS. On Windows, it has to be installed separately.
If a project turns files into other files in more than one step, write a Makefile, list every file each recipe reads, and add .DELETE_ON_ERROR:. If you only need a few named commands, a Makefile is a reasonable choice, but a short script works as well.