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.

Five files with their modification times. raw.csv and tidy.py, both modified at 09:00, point to tidy.csv, modified at 09:05, which is up to date and skipped. tidy.csv and plot.py point to figure.svg, modified at 09:06. plot.py was edited at 10:30, so figure.svg is out of date and rebuilt.
Figure 1. 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 to python3. A value given on the command line replaces the one in the file, so make PYTHON=.venv/bin/python runs 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 clean deletes both outputs, and .PHONY: clean tells make that no file called clean is involved. See phony targets.

What should I watch out for?

  • Recipe lines need a tab. With spaces, make stops with a missing separator error. 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 cd on one line is forgotten on the next. Join commands that depend on each other with &&, as in cd 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 to make. In a recipe, make reads $HOME as the variable H followed by the text OME, so echo $HOME prints OME. Write $$HOME to pass $HOME to the shell.
  • make only knows the prerequisites you list. If plot.py imports a style.py that the rule doesn't list, editing style.py won't redraw figure.svg. List every file a recipe reads.
  • A failed recipe can leave a target that looks up to date. If tidy.py crashes after writing half of tidy.csv, make stops with an error, but the partial tidy.csv is now newer than its prerequisites. The next make skips it, draws the figure from the partial data, and succeeds. Add a line with .DELETE_ON_ERROR: to the Makefile, and make deletes a target whose recipe fails.
  • make compares 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, as cp -p does, may not trigger a rebuild at all. make -B rebuilds 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. make reruns only the steps whose inputs changed, in the right order.
  • Short names for project commands. A Makefile of phony targets, such as make test and make lint, gives everyone on a project the same commands. You don't use the timestamp checks, but make is 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.


Reading Time

~4 min read

Published

Category

Tags

Stay in Touch