Debugging Makefiles
"Why did that rebuild?", "why didn't that rebuild?" and "what value does this variable actually have?" are the three questions of Makefile debugging. GNU Make answers all three itself if you ask with the right option.
| Option | Answers |
|---|---|
-n (--dry-run) |
what would run, without running it |
--trace |
why each recipe runs, with file and line |
--debug=b (or -d for everything) |
Make's reasoning about each target |
-p (--print-data-base) |
every variable and rule after reading the makefiles, including built-ins |
-q (--question) |
exit status 0 if everything is up to date, 1 if not; runs nothing |
--warn-undefined-variables |
warn whenever an undefined variable is expanded (typos!) |
-B (--always-make) |
treat every target as out of date |
All sessions in this lesson use the same small project:
mkdir -p dbg && cd dbg
printf 'int util(void) { return 1; }\n' > util.c
printf 'int util(void);\nint main(void) { return util() - 1; }\n' > main.c
cat > Makefile <<'MK'
CFLAGS := -O2
OBJS := main.o util.o
app: $(OBJS)
$(CC) $^ -o $@
%.o: %.c
$(CC) $(CFLAGS) -c lt; -o $@
MK
make -j1 -n # print, don't run
ls
make -j1 >/dev/null
make -j1 -q; echo "up to date? exit $?"
sleep 1 && touch util.c
make -j1 -q; echo "up to date? exit $?"cc -O2 -c main.c -o main.o
cc -O2 -c util.c -o util.o
cc main.o util.o -o app
Makefile
main.c
util.c
up to date? exit 0
up to date? exit 1
-n printed three commands and created nothing. Remember that -n still runs lines containing $(MAKE) (and lines with the + prefix), and it still remakes included makefiles: "dry" is not always completely dry.
--trace: why did this run?
--trace prints, before each recipe, the target and the reason: the target doesn't exist, or which prerequisites are newer:
mkdir -p dbg && cd dbg
printf 'int util(void) { return 1; }\n' > util.c
printf 'int util(void);\nint main(void) { return util() - 1; }\n' > main.c
printf 'CFLAGS := -O2\nOBJS := main.o util.o\napp: $(OBJS)\n\t$(CC) $^ -o $@\n%%.o: %%.c\n\t$(CC) $(CFLAGS) -c lt; -o $@\n' > Makefile
make -j1 >/dev/null
sleep 1 && touch util.c
make -j1 --traceMakefile:6: update target 'util.o' due to: util.c
cc -O2 -c util.c -o util.o
Makefile:4: update target 'app' due to: util.o
cc main.o util.o -o app
The line numbers point at the rule's recipe, so in a Makefile assembled from many included fragments --trace also tells you which file the rule came from. When a target rebuilds on every run, --trace usually names the culprit at once: a prerequisite that is never created (a misspelled file, a phony target used as a file prerequisite) or a directory whose time stamp keeps changing.
--debug=b: Make's reasoning
--debug=b ("basic") narrates every target Make considers, including the ones it leaves alone. The output is long, so filter it:
mkdir -p dbg && cd dbg
printf 'int util(void) { return 1; }\n' > util.c
printf 'int util(void);\nint main(void) { return util() - 1; }\n' > main.c
printf 'CFLAGS := -O2\nOBJS := main.o util.o\napp: $(OBJS)\n\t$(CC) $^ -o $@\n%%.o: %%.c\n\t$(CC) $(CFLAGS) -c lt; -o $@\n' > Makefile
make -j1 >/dev/null
sleep 1 && touch util.c
make -j1 --debug=b | grep -v -e '^GNU Make' -e '^Built for' -e '^Copyright' -e '^License' -e '^This is free' -e '^There is NO'Reading makefiles...
Updating makefiles....
Updating goal targets....
Prerequisite 'util.c' is newer than target 'util.o'.
Must remake target 'util.o'.
cc -O2 -c util.c -o util.o
Successfully remade target file 'util.o'.
Prerequisite 'util.o' is newer than target 'app'.
Must remake target 'app'.
cc main.o util.o -o app
Successfully remade target file 'app'.
Other levels: --debug=v (verbose: also the targets that were fine), --debug=i (implicit-rule search: which pattern rules Make tried for each file), --debug=m (remaking makefiles), --debug=j (job start and exit, with PIDs), -d (all of them; thousands of lines). --debug=i is the one to reach for when Make uses the "wrong" rule or claims there's no rule.
-p: the database
make -p prints Make's whole internal state after reading the makefiles: every variable with its origin, every rule, implicit or explicit. It's a few thousand lines, mostly built-ins, so always filter it. Combine with -q (or -f /dev/null) so nothing gets built:
mkdir -p dbg && cd dbg
printf 'CFLAGS := -O2\nOBJS := main.o util.o\napp: $(OBJS)\n\t$(CC) $^ -o $@\n' > Makefile
touch main.c util.c
make -p -q | wc -l | awk '{print ($1 > 1000 ? "more than 1000 lines" : $1)}'
echo "--- our variables, with their origin comments:"
make -p -q | grep -B1 -E '^(CFLAGS|OBJS|CC) '
echo "--- the built-in definition behind \$(COMPILE.c):"
make -p -f /dev/null 2>/dev/null | grep -E '^COMPILE\.c '
echo "--- the rule for app:"
make -p -q | grep -A3 '^app:'more than 1000 lines
--- our variables, with their origin comments:
# makefile (from 'Makefile', line 2)
OBJS := main.o util.o
--
# makefile (from 'Makefile', line 1)
CFLAGS := -O2
--
# default
CC = cc
--- the built-in definition behind $(COMPILE.c):
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
--- the rule for app:
app: main.o util.o
# Implicit rule search has not been done.
# File does not exist.
# File has been updated.
The database shows the values after all assignments and includes, which is what you need when several files and the command line each set a variable.
Two quick tricks
A "print any variable" rule is worth adding to every Makefile you maintain:
mkdir -p dbg && cd dbg
cat > Makefile <<'MK'
SRCS := $(sort $(wildcard *.c))
OBJS := $(SRCS:.c=.o)
CFLAGS := -O2 $(EXTRA_FLAGS)
print-%:
@echo '$* = $($*) [$(origin $*), $(flavor $*)]'
app: $(OBJS)
$(CC) $(CFLAGS) $(LDFLAG) $^ -o $@
MK
touch b.c a.c
make -j1 print-OBJS
make -j1 print-CFLAGS EXTRA_FLAGS=-g
echo "--- typo hunting:"
make -j1 -n --warn-undefined-variables app 2>&1 | grep warningOBJS = a.o b.o [file, simple]
CFLAGS = -O2 -g [file, simple]
--- typo hunting:
Makefile:3: warning: undefined variable 'EXTRA_FLAGS'
make: warning: undefined variable 'CPPFLAGS'
make: warning: undefined variable 'TARGET_ARCH'
make: warning: undefined variable 'CPPFLAGS'
make: warning: undefined variable 'TARGET_ARCH'
Makefile:9: warning: undefined variable 'LDFLAG'
--warn-undefined-variables found the typo LDFLAG (the conventional name is LDFLAGS), along with EXTRA_FLAGS, which is optional by design. The make: lines without a line number come from the built-in %.o: %.c rule, whose $(COMPILE.c) mentions CPPFLAGS and TARGET_ARCH. Expect that kind of noise (defining such variables as empty, or make -r, silences it), but read the list once in a while. Toolkit: Debugging & Inspection adds remake, a Make with a real debugger.
Test yourself
Which option tells you, for each recipe it runs, which prerequisite made the target out of date?
-n--trace-q-B
Why combine make -p with -q or -f /dev/null?
- To make the output shorter
- So that printing the database doesn't also build targets
- Because
-pneeds it - To show colours
Which --debug level shows the implicit-rule search Make did for each file?
--debug=b--debug=i--debug=j--debug=m