TExcelTAD
Home › GNU Make & Ninja › Phony Targets & Order-only Prerequisites

Phony Targets & Order-only Prerequisites

Two special kinds of prerequisite relationship fix two classic Makefile bugs: a clean target that mysteriously stops working, and a program that relinks every time you build.

.PHONY: targets that are actions

clean, test, install and all name actions, not files. Make doesn't know that. If a file called clean ever appears in the directory (a script, a log, a folder), Make sees a target with no prerequisites that already exists, decides it is up to date and does nothing:

bash · try it yourself
mkdir -p phony && cd phony
cat > Makefile <<'MK'
clean:
	rm -f *.o app
	@echo "cleaned"
MK
touch app main.o
make -j1 clean
touch clean                 # oops: a file with the target's name
touch app main.o
make -j1 clean
ls
rm -f *.o app
cleaned
make: 'clean' is up to date.
Makefile
app
clean
main.o

Declaring the target as a prerequisite of the special target .PHONY tells Make it is not a file. A phony target is always considered out of date, and Make doesn't even look for a file of that name (no implicit-rule search either):

bash · try it yourself
mkdir -p phony && cd phony
cat > Makefile <<'MK'
.PHONY: all clean test

all: app

app:
	@echo "linking app" && touch app

test: app
	@echo "running tests"

clean:
	rm -f app
	@echo "cleaned"
MK
touch clean test
make -j1 clean
make -j1 test
make -j1 test
rm -f app
cleaned
linking app
running tests
running tests

Rules for phony targets:

  • Declare every action target: all, clean, test, install, uninstall, dist, check, help…
  • Never make a real file depend on a phony target. Because the phony target is always out of date, the file would be rebuilt on every run.
  • A phony target can depend on other phony targets: check: test lint runs both.

Order-only prerequisites: "exists" is enough

Objects usually go into a build directory, which must exist before the compiler writes into it. The obvious fix is to make the directory a prerequisite. But a directory's modification time changes every time a file inside it is created or deleted, so anything that depends on the directory looks out of date after each compile:

bash · try it yourself
mkdir -p ordinary && cd ordinary
printf 'int main(void) { return 0; }\n' > main.c
printf 'int helper(void) { return 1; }\n' > helper.c
cat > Makefile <<'MK'
build/app: build/main.o build/helper.o build
	gcc build/main.o build/helper.o -o $@

build/%.o: %.c build
	gcc -c 
lt; -o $@ build: mkdir -p $@ MK make -j1 echo "--- second run, nothing edited:" make -j1
mkdir -p build
gcc -c main.c -o build/main.o
gcc -c helper.c -o build/helper.o
gcc build/main.o build/helper.o -o build/app
--- second run, nothing edited:
gcc -c main.c -o build/main.o
gcc -c helper.c -o build/helper.o
gcc build/main.o build/helper.o -o build/app

The second run rebuilt everything although nothing changed: creating helper.o and then app inside build/ bumped the directory's time stamp past the objects, so every object looked older than one of its prerequisites. A third run would do the same again. Prerequisites listed after a | are order-only: Make builds them first if they are missing, but never rebuilds the target because they are newer:

bash · try it yourself
mkdir -p orderonly && cd orderonly
printf 'int main(void) { return 0; }\n' > main.c
printf 'int helper(void) { return 1; }\n' > helper.c
cat > Makefile <<'MK'
build/app: build/main.o build/helper.o
	gcc $^ -o $@

build/%.o: %.c | build
	gcc -c 
lt; -o $@ build: mkdir -p $@ MK make -j1 echo "--- second run, nothing edited:" make -j1
mkdir -p build
gcc -c main.c -o build/main.o
gcc -c helper.c -o build/helper.o
gcc build/main.o build/helper.o -o build/app
--- second run, nothing edited:
make: 'build/app' is up to date.

Also note $^: order-only prerequisites are not part of it (they're in $|), so the link line stays clean.

Other uses of order-only

Order-only prerequisites mean "this must happen before, but its changes don't concern me". Besides directories:

  • generated headers: "generate version.h before compiling anything", combined with real dependency tracking (Automatic Header Dependencies) for the files that actually include it;
  • tool bootstrapping: build a code generator before the sources that use its output.

An alternative for directories is to create them inside the recipe: @mkdir -p $(@D) as the first recipe line. It's simple and robust; it just runs mkdir once per object.

Test yourself

A file named test exists and test is not declared .PHONY. What does make test do?

  1. Runs the recipe as usual
  2. Reports 'test' is up to date and runs nothing (if it has no newer prerequisites)
  3. Deletes the file
  4. Fails with an error

Why does build/%.o: %.c build cause needless rebuilds?

  1. Directories can't be prerequisites
  2. The directory's time stamp changes whenever a file is added to it, making objects look out of date
  3. % doesn't match directories
  4. mkdir -p is slow

In app: main.o util.o | bin, which automatic variable contains bin?

  1. $^
  2. lt;
  3. $|
  4. $?