Chapter 6: How to Use Variables 61
Simply expanded variables are defined by lines using ‘:=’ or ‘::=’ (see Section 6.5 [Setting
Variables], page 65). Both forms are equivalent in GNU make; however only the ‘::=’ form
is described by the POSIX standard (support for ‘::=’ was added to the POSIX standard
in 2012, so older versions of make won’t accept this form either).
The value of a simply expanded variable is scanned once and for all, expanding any
references to other variables and functions, when the variable is defined. The actual value
of the simply expanded variable is the result of expanding the text that you write. It does
not contain any references to other variables; it contains their values as of the time this
variable was defined. Therefore,
x := foo
y := $(x) bar
x := later
is equivalent to
y := foo bar
x := later
When a simply expanded variable is referenced, its value is substituted verbatim.
Here is a somewhat more complicated example, illustrating the use of ‘:=’ in conjunction
with the shell function. (See Section 8.13 [The shell Function], page 97.) This example
also shows use of the variable MAKELEVEL, which is changed when it is passed down from
level to level. (See Section 5.7.2 [Communicating Variables to a Sub-make], page 52, for
information about MAKELEVEL.)
ifeq (0,${MAKELEVEL})
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif
An advantage of this use of ‘:=’ is that a typical ‘descend into a directory’ recipe then looks
like this:
${subdirs}:
${MAKE} -C $@ all
Simply expanded variables generally make complicated makefile programming more pre-
dictable because they work like variables in most programming languages. They allow you
to redefine a variable using its own value (or its value processed in some way by one of
the expansion functions) and to use the expansion functions much more efficiently (see
Chapter 8 [Functions for Transforming Text], page 83).
You can also use them to introduce controlled leading whitespace into variable values.
Leading whitespace characters are discarded from your input before substitution of variable
references and function calls; this means you can include leading spaces in a variable value
by protecting them with variable references, like this:
nullstring :=
space := $(nullstring) # end of the line
Here the value of the variable space is precisely one space. The comment
‘# end of the line’ is included here just for clarity. Since trailing space characters are not
stripped from variable values, just a space at the end of the line would have the same
Kommentare zu diesen Handbüchern