Monday, July 09, 2007

Gnu-make: define virables

There are two ways that a variable in GNU make can have a value; we call
them the two flavors of variables. The two flavors are distinguished in
how they are defined and in what they do when expanded.

The first flavor of variable is a recursively expanded variable.
Variables of this sort are defined by lines using `=' (see section
Setting Variables) or by the define directive (see section Defining
Variables Verbatim). The value you specify is installed verbatim; if it
contains references to other variables, these references are expanded
whenever this variable is substituted (in the course of expanding some
other string). When this happens, it is called recursive expansion.

For example,

foo = $(bar)
bar = $(ugh)
ugh = Huh?

all:;echo $(foo)

will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to `$(ugh)'
which finally expands to `Huh?'.

This flavor of variable is the only sort supported by other versions of
make. It has its advantages and its disadvantages. An advantage (most
would say) is that:

CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar

will do what was intended: when `CFLAGS' is expanded in a command, it
will expand to `-Ifoo -Ibar -O'. A major disadvantage is that you cannot
append something on the end of a variable, as in

CFLAGS = $(CFLAGS) -O

because it will cause an infinite loop in the variable expansion.
(Actually make detects the infinite loop and reports an error.)

Another disadvantage is that any functions (see section Functions for
Transforming Text) referenced in the definition will be executed every
time the variable is expanded. This makes make run slower; worse, it
causes the wildcard and shell functions to give unpredictable results
because you cannot easily control when they are called, or even how many
times.

To avoid all the problems and inconveniences of recursively expanded
variables, there is another flavor: simply expanded variables.

Simply expanded variables are defined by lines using `:=' (see
section Setting Variables). 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.

GNU-Make: Variables from the Environment

Variables from the Environment
Variables in make can come from the environment in which make is run.
Every environment variable that make sees when it starts up is
transformed into a make variable with the same name and value. But an
explicit assignment in the makefile, or with a command argument,
overrides the environment. (If the `-e' flag is specified, then values
from the environment override assignments in the makefile. See section
Summary of Options. But this is not recommended practice.)

Thus, by setting the variable CFLAGS in your environment, you can cause
all C compilations in most makefiles to use the compiler switches you
prefer. This is safe for variables with standard or conventional
meanings because you know that no makefile will use them for other
things. (But this is not totally reliable; some makefiles set CFLAGS
explicitly and therefore are not affected by the value in the
environment.)

When make is invoked recursively, variables defined in the outer
invocation can be passed to inner invocations through the environment
(see section Recursive Use of make). By default, only variables that
came from the environment or the command line are passed to recursive
invocations. You can use the export directive to pass other variables.
See section Communicating Variables to a Sub-make, for full details.

Blog Archive