Section 12.14

Make

Definition

Make is a build tool that brings files up to date by running commands from rules in a file called a makefile. Each rule names a target file, the prerequisite files it is made from, and a recipe: commands for the shell, the program that runs typed commands, that make the target. Make rebuilds a target when the target does not exist or when a prerequisite has a later modification time than the target, after first bringing the prerequisites up to date. It does not look at file contents.

The idea

After a change to one file of a program with 500 source files, a rebuild should compile one file and link, not compile 500. A tool must therefore know which outputs depend on which inputs, and which inputs changed. Make answers the first question with rules that people write, and the second with a cheap substitute: the time at which the file system last recorded a change to each file. Both answers can be wrong without warning. A rule can omit an input, and a modification time can fail to reflect a change of content; Make then reports a target as up to date although it was built from old inputs.

Stuart Feldman wrote Make at Bell Labs in 1976. POSIX (Portable Operating System Interface), the family of standards for Unix-like operating systems, defines its core. GNU Make, from the GNU project (GNU is a recursive name that stands for GNU’s Not Unix), is the most widely used implementation and adds many extensions.

How it works

A rule looks like this:

app.o: main.c config.h
cc -c main.c -o app.o

The first line names the target, app.o, and after the colon its prerequisites. Each recipe line must begin with a tab character, and each runs in its own shell. A variable such as CFLAGS is used as $(CFLAGS); inside a recipe, $@ stands for the target and $< for the first prerequisite.

Make works in these steps.

  1. Read the makefile and every makefile it includes. GNU Make first brings each included makefile up to date and, if it remade any, starts again; this is how generated dependency files take effect.

  2. Choose the goals: the targets named on the command line, or else the default goal. Unless the makefile sets it explicitly, this is the first eligible target.

  3. For each target, first bring each prerequisite that is itself a target up to date, recursively.

  4. Run the recipe if the target does not exist or if any normal prerequisite has a later modification time than the target, compared with the precision that the file system stores.

  5. After the recipe, treat the target as up to date; Make does not check that the recipe wrote it.

  6. If a command fails, meaning it ends with a nonzero exit status §13.24, stop, unless -k asks Make to continue with other targets. With -j, several recipes run at once.

Further features control these decisions.

Worked example

The makefile has one rule:

app.o: main.c
cc -c main.c -o app.o

The file main.c includes config.h, which the rule does not list. On the build machine’s clock, the recorded modification times are config.h 09:00:00, main.c 10:00:10 and app.o 10:00:20.

Case 1, a missing prerequisite. At 10:05:00 the developer changes a constant in config.h and runs make app.o. Make compares only main.c (10:00:10) with app.o (10:00:20); 10:00:10 is earlier, so Make reports that app.o is up to date, and app.o keeps the old constant. With config.h listed, by hand or through -MMD, Make compares 10:05:00 with 10:00:20, finds the header later, and recompiles.

Case 2, a misleading time. The developer restores an older version of main.c from a backup with a tool that keeps the original modification time, 09:30:00. The content changed, but 09:30:00 is earlier than 10:00:20, so Make does nothing.

Case 3, a coarse clock. On a file system that stores modification times in whole seconds, app.o is written at 10:00:20.3 and main.c saved again at 10:00:20.8. Both are recorded as 10:00:20. Equal is not later, so Make does nothing.

Comparing two recorded times, such as 10:00:20 and 10:00:10, shows only which was recorded later, not whether any content changed. Case 1 is an incomplete dependency graph; cases 2 and 3 are unreliable evidence of change.

Stated precisely

Let tt be a target with ordinary prerequisites p1,…,pnp_1, \dots, p_n, and write mtime(f)\mathrm{mtime}(f) for the modification time of file ff as the file system records it, read after the prerequisites have been brought up to date. Unless an option forces or suppresses rebuilding (for example -B, which rebuilds the targets considered), GNU Make runs the recipe of tt when at least one of these holds: (1) tt is declared phony; (2) no file named tt exists; (3) mtime(pi)>mtime(t)\mathrm{mtime}(p_i) > \mathrm{mtime}(t) for some ii with 1≤i≤n1 \le i \le n; (4) some pip_i is phony, which is why the GNU Make manual says a phony target should not be a prerequisite of a real file. Nothing else enters the decision: not file contents, the text of the recipe, variable values, the environment, or any undeclared file. Declaring the makefile as a normal prerequisite catches a flag change made by editing that file. Changing only CFLAGS on the command line leaves its timestamp untouched. One solution is to record the effective flags in a prerequisite file and update it when those flags change.

An incremental build, which runs only the recipes this rule selects, gives the same targets as a build from scratch under three assumptions.

  1. Complete inputs: every relevant change to source, settings or tools is reflected in a declared prerequisite. The recipe produces the same result from those inputs.

  2. Honest times: whenever a file’s content changes, its modification time becomes later than that of every target built from the old content, which needs one clock, no restored old times and enough time resolution.

  3. All or nothing: every recipe either writes its complete target or leaves no target with a later time behind. The GNU Make directive .DELETE_ON_ERROR helps by deleting a changed target when its recipe reports failure. It does not undo changes to other files.

Limits and common mistakes

A target name means only what its recipe does: a target called test or release proves nothing about what was tested or released.

Touching every file, or make -B, forces a full rebuild but does not repair a missing prerequisite, and the next incremental build is stale again. Incremental validation §10.42 has the same weakness: reusing an earlier check result is sound only if every input it depended on is tracked.

A file on a network file system written by a machine with a different clock, or a file dated in the future, breaks the comparison; GNU Make warns about clock skew, a difference between clocks. With -j, a missing prerequisite becomes a race condition §9.2: a recipe may start before an undeclared generated file exists, and whether it fails depends on timing.

A makefile that runs Make again in each subdirectory, called recursive make, splits the dependency graph so that no single run sees every dependency; Peter Miller’s paper “Recursive Make Considered Harmful” describes the failures [503]. Make also does not record recipes, so it cannot notice that a command changed, whereas Ninja §12.13 records each command and rebuilds when it changes.

Questions answered

How do timestamps and missing prerequisites affect rebuild decisions?

Make, the rule-based build tool, decides from recorded times alone: apart from phony targets and options that force a rebuild, it rebuilds a target only when the target file is missing or one of its declared prerequisites has a later modification time than the target, so a change that does not appear as a later time on a declared prerequisite causes no rebuild, and the old target survives.

The opposite error also happens: saving a file without changing it makes Make rebuild everything that depends on it. The remedies are to track inputs, to let the compiler generate header dependencies (-MMD -MP), to record the effective build flags when callers can override them, to use .DELETE_ON_ERROR, and to keep one clock. A forced full rebuild is not a repair.

Related entries: CMake (§12.12), Ninja (§12.13), Transitive dependency (§10.43), Incremental validation (§10.42), Directed acyclic graph (DAG) (§14.17), Race condition (§9.2). Further reading: [503].