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:
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
lsrm -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):
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 testrm -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 lintruns 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:
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 -j1mkdir -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:
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 -j1mkdir -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.hbefore 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?
- Runs the recipe as usual
- Reports
'test' is up to dateand runs nothing (if it has no newer prerequisites) - Deletes the file
- Fails with an error
Why does build/%.o: %.c build cause needless rebuilds?
- Directories can't be prerequisites
- The directory's time stamp changes whenever a file is added to it, making objects look out of date
%doesn't match directoriesmkdir -pis slow
In app: main.o util.o | bin, which automatic variable contains bin?
$^lt;$|$?