Automatic Variables in Make Recipes
Modern build systems are great for real-world projects, but make
is my go-to solution for small builds on Linux.
Make defines many automatic variables to be used in recipes.
They are easy to forget. So I wrote myself a makefile. It works as an interactive reminder.1
It defines a few example rules, using a custom macro. When I run with “make”, it builds 8 targets, but the build recipes do nothing but print information about the rule.
Below is the report of test_2/baz
target:
makefile:16: Building test_2/baz (from foo) (foo y.bar z.bar newer)
RULE > test_2/baz : foo y.bar z.bar << RULE
( %/baz : foo y.bar z.bar ) << PATTERN RULE
_
AUTO > @ : test_2/baz << |
AUTO > @D : test_2 << |
AUTO > @F : baz << |
AUTO > * : test_2 << |
AUTO > *D : . << |
AUTO > *F : test_2 << | AUTOMATIC
AUTO > < : foo << | VARIABLES
AUTO > <D : . << |
AUTO > <F : foo << |
AUTO > ^ : foo y.bar z.bar << |
AUTO > ^D : . . . << |
AUTO > ^F : foo y.bar z.bar << _|
\/ ________________
\ \__ VALUE
\__ VARIABLE
Here is the makefile:
The shell replacement trick at the beginning is not mine. I am not sure of its origin, but it exists in The GNU Make Book by John Graham-Cumming. Every makefile I write starts with that snippet. Very useful.
Also, I use make
with --debug
option to find out how it matches patterns.
-
If you find errors, please report in the blog’s Issues page. ↩