Files
2024-02-19 00:25:23 -05:00

1299 lines
38 KiB
HTML

<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54
from ../texi/make.texinfo on 19 July 2000 -->
<TITLE>GNU make - How to Use Variables</TITLE>
<link href="make_7.html" rel=Next>
<link href="make_5.html" rel=Previous>
<link href="make_toc.html" rel=ToC>
</HEAD>
<BODY>
<p>Go to the <A HREF="make_1.html">first</A>, <A HREF="make_5.html">previous</A>, <A HREF="make_7.html">next</A>, <A HREF="make_19.html">last</A> section, <A HREF="make_toc.html">table of contents</A>.
<P><HR><P>
<H1><A NAME="SEC57" HREF="make_toc.html#TOC57">How to Use Variables</A></H1>
<P>
<A NAME="IDX414"></A>
<A NAME="IDX415"></A>
<A NAME="IDX416"></A>
<A NAME="IDX417"></A>
</P>
<P>
A <STRONG>variable</STRONG> is a name defined in a makefile to represent a string
of text, called the variable's <STRONG>value</STRONG>. These values are
substituted by explicit request into targets, prerequisites, commands,
and other parts of the makefile. (In some other versions of <CODE>make</CODE>,
variables are called <STRONG>macros</STRONG>.)
<A NAME="IDX418"></A>
</P>
<P>
Variables and functions in all parts of a makefile are expanded when
read, except for the shell commands in rules, the right-hand sides of
variable definitions using <SAMP>`='</SAMP>, and the bodies of variable
definitions using the <CODE>define</CODE> directive.
</P>
<P>
Variables can represent lists of file names, options to pass to compilers,
programs to run, directories to look in for source files, directories to
write output in, or anything else you can imagine.
</P>
<P>
A variable name may be any sequence of characters not containing <SAMP>`:'</SAMP>,
<SAMP>`#'</SAMP>, <SAMP>`='</SAMP>, or leading or trailing whitespace. However,
variable names containing characters other than letters, numbers, and
underscores should be avoided, as they may be given special meanings in the
future, and with some shells they cannot be passed through the environment to a
sub-<CODE>make</CODE>
(see section <A HREF="make_5.html#SEC52">Communicating Variables to a Sub-<CODE>make</CODE></A>).
</P>
<P>
Variable names are case-sensitive. The names <SAMP>`foo'</SAMP>, <SAMP>`FOO'</SAMP>,
and <SAMP>`Foo'</SAMP> all refer to different variables.
</P>
<P>
It is traditional to use upper case letters in variable names, but we
recommend using lower case letters for variable names that serve internal
purposes in the makefile, and reserving upper case for parameters that
control implicit rules or for parameters that the user should override with
command options (see section <A HREF="make_9.html#SEC90">Overriding Variables</A>).
</P>
<P>
A few variables have names that are a single punctuation character or
just a few characters. These are the <STRONG>automatic variables</STRONG>, and
they have particular specialized uses. See section <A HREF="make_10.html#SEC101">Automatic Variables</A>.
</P>
<H2><A NAME="SEC58" HREF="make_toc.html#TOC58">Basics of Variable References</A></H2>
<P>
<A NAME="IDX419"></A>
<A NAME="IDX420"></A>
<A NAME="IDX421"></A>
<A NAME="IDX422"></A>
</P>
<P>
To substitute a variable's value, write a dollar sign followed by the name
of the variable in parentheses or braces: either <SAMP>`$(foo)'</SAMP> or
<SAMP>`${foo}'</SAMP> is a valid reference to the variable <CODE>foo</CODE>. This
special significance of <SAMP>`$'</SAMP> is why you must write <SAMP>`$$'</SAMP> to have
the effect of a single dollar sign in a file name or command.
</P>
<P>
Variable references can be used in any context: targets, prerequisites,
commands, most directives, and new variable values. Here is an
example of a common case, where a variable holds the names of all the
object files in a program:
</P>
<PRE>
objects = program.o foo.o utils.o
program : $(objects)
cc -o program $(objects)
$(objects) : defs.h
</PRE>
<P>
Variable references work by strict textual substitution. Thus, the rule
</P>
<PRE>
foo = c
prog.o : prog.$(foo)
$(foo)$(foo) -$(foo) prog.$(foo)
</PRE>
<P>
could be used to compile a C program <TT>`prog.c'</TT>. Since spaces before
the variable value are ignored in variable assignments, the value of
<CODE>foo</CODE> is precisely <SAMP>`c'</SAMP>. (Don't actually write your makefiles
this way!)
</P>
<P>
A dollar sign followed by a character other than a dollar sign,
open-parenthesis or open-brace treats that single character as the
variable name. Thus, you could reference the variable <CODE>x</CODE> with
<SAMP>`$x'</SAMP>. However, this practice is strongly discouraged, except in
the case of the automatic variables (see section <A HREF="make_10.html#SEC101">Automatic Variables</A>).
</P>
<H2><A NAME="SEC59" HREF="make_toc.html#TOC59">The Two Flavors of Variables</A></H2>
<P>
<A NAME="IDX423"></A>
<A NAME="IDX424"></A>
<A NAME="IDX425"></A>
<A NAME="IDX426"></A>
<A NAME="IDX427"></A>
</P>
<P>
There are two ways that a variable in GNU <CODE>make</CODE> can have a value;
we call them the two <STRONG>flavors</STRONG> of variables. The two flavors are
distinguished in how they are defined and in what they do when expanded.
</P>
<P>
<A NAME="IDX428"></A>
The first flavor of variable is a <STRONG>recursively expanded</STRONG> variable.
Variables of this sort are defined by lines using <SAMP>`='</SAMP>
(see section <A HREF="make_6.html#SEC64">Setting Variables</A>) or by the <CODE>define</CODE> directive
(see section <A HREF="make_6.html#SEC67">Defining Variables Verbatim</A>). 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 <STRONG>recursive expansion</STRONG>.
</P>
<P>
For example,
</P>
<PRE>
foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:;echo $(foo)
</PRE>
<P>
will echo <SAMP>`Huh?'</SAMP>: <SAMP>`$(foo)'</SAMP> expands to <SAMP>`$(bar)'</SAMP> which
expands to <SAMP>`$(ugh)'</SAMP> which finally expands to <SAMP>`Huh?'</SAMP>.
</P>
<P>
This flavor of variable is the only sort supported by other versions of
<CODE>make</CODE>. It has its advantages and its disadvantages. An advantage
(most would say) is that:
</P>
<PRE>
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
</PRE>
<P>
will do what was intended: when <SAMP>`CFLAGS'</SAMP> is expanded in a command,
it will expand to <SAMP>`-Ifoo -Ibar -O'</SAMP>. A major disadvantage is that you
cannot append something on the end of a variable, as in
</P>
<PRE>
CFLAGS = $(CFLAGS) -O
</PRE>
<P>
because it will cause an infinite loop in the variable expansion.
(Actually <CODE>make</CODE> detects the infinite loop and reports an error.)
<A NAME="IDX429"></A>
<A NAME="IDX430"></A>
</P>
<P>
Another disadvantage is that any functions
(see section <A HREF="make_8.html#SEC75">Functions for Transforming Text</A>)
referenced in the definition will be executed every time the variable is
expanded. This makes <CODE>make</CODE> run slower; worse, it causes the
<CODE>wildcard</CODE> and <CODE>shell</CODE> functions to give unpredictable results
because you cannot easily control when they are called, or even how many
times.
</P>
<P>
To avoid all the problems and inconveniences of recursively expanded
variables, there is another flavor: simply expanded variables.
</P>
<P>
<A NAME="IDX431"></A>
<A NAME="IDX432"></A>
<A NAME="IDX433"></A>
<STRONG>Simply expanded variables</STRONG> are defined by lines using <SAMP>`:='</SAMP>
(see section <A HREF="make_6.html#SEC64">Setting Variables</A>).
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 <EM>as of the time this variable was defined</EM>. Therefore,
</P>
<PRE>
x := foo
y := $(x) bar
x := later
</PRE>
<P>
is equivalent to
</P>
<PRE>
y := foo bar
x := later
</PRE>
<P>
When a simply expanded variable is referenced, its value is substituted
verbatim.
</P>
<P>
Here is a somewhat more complicated example, illustrating the use of
<SAMP>`:='</SAMP> in conjunction with the <CODE>shell</CODE> function.
(See section <A HREF="make_8.html#SEC83">The <CODE>shell</CODE> Function</A>.) This example
also shows use of the variable <CODE>MAKELEVEL</CODE>, which is changed
when it is passed down from level to level.
(See section <A HREF="make_5.html#SEC52">Communicating Variables to a Sub-<CODE>make</CODE></A>, for information about <CODE>MAKELEVEL</CODE>.)
</P>
<P>
<A NAME="IDX434"></A>
<A NAME="IDX435"></A>
<PRE>
ifeq (0,${MAKELEVEL})
cur-dir := $(shell pwd)
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif
</PRE>
<P>
An advantage of this use of <SAMP>`:='</SAMP> is that a typical
`descend into a directory' command then looks like this:
</P>
<PRE>
${subdirs}:
${MAKE} cur-dir=${cur-dir}/$@ -C $@ all
</PRE>
<P>
Simply expanded variables generally make complicated makefile programming
more predictable 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 section <A HREF="make_8.html#SEC75">Functions for Transforming Text</A>).
</P>
<P>
<A NAME="IDX436"></A>
<A NAME="IDX437"></A>
<A NAME="IDX438"></A>
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:
</P>
<PRE>
nullstring :=
space := $(nullstring) # end of the line
</PRE>
<P>
Here the value of the variable <CODE>space</CODE> is precisely one space. The
comment <SAMP>`# end of the line'</SAMP> is included here just for clarity.
Since trailing space characters are <EM>not</EM> stripped from variable
values, just a space at the end of the line would have the same effect
(but be rather hard to read). If you put whitespace at the end of a
variable value, it is a good idea to put a comment like that at the end
of the line to make your intent clear. Conversely, if you do <EM>not</EM>
want any whitespace characters at the end of your variable value, you
must remember not to put a random comment on the end of the line after
some whitespace, such as this:
</P>
<PRE>
dir := /foo/bar # directory to put the frobs in
</PRE>
<P>
Here the value of the variable <CODE>dir</CODE> is <SAMP>`/foo/bar '</SAMP>
(with four trailing spaces), which was probably not the intention.
(Imagine something like <SAMP>`$(dir)/file'</SAMP> with this definition!)
</P>
<P>
<A NAME="IDX439"></A>
<A NAME="IDX440"></A>
<A NAME="IDX441"></A>
There is another assignment operator for variables, <SAMP>`?='</SAMP>. This
is called a conditional variable assignment operator, because it only
has an effect if the variable is not yet defined. This statement:
</P>
<PRE>
FOO ?= bar
</PRE>
<P>
is exactly equivalent to this
(see section <A HREF="make_8.html#SEC82">The <CODE>origin</CODE> Function</A>):
</P>
<PRE>
ifeq ($(origin FOO), undefined)
FOO = bar
endif
</PRE>
<P>
Note that a variable set to an empty value is still defined, so
<SAMP>`?='</SAMP> will not set that variable.
</P>
<H2><A NAME="SEC60" HREF="make_toc.html#TOC60">Advanced Features for Reference to Variables</A></H2>
<P>
<A NAME="IDX442"></A>
</P>
<P>
This section describes some advanced features you can use to reference
variables in more flexible ways.
</P>
<H3><A NAME="SEC61" HREF="make_toc.html#TOC61">Substitution References</A></H3>
<P>
<A NAME="IDX443"></A>
<A NAME="IDX444"></A>
<A NAME="IDX445"></A>
<A NAME="IDX446"></A>
</P>
<P>
<A NAME="IDX447"></A>
<A NAME="IDX448"></A>
A <STRONG>substitution reference</STRONG> substitutes the value of a variable with
alterations that you specify. It has the form
<SAMP>`$(<VAR>var</VAR>:<VAR>a</VAR>=<VAR>b</VAR>)'</SAMP> (or
<SAMP>`${<VAR>var</VAR>:<VAR>a</VAR>=<VAR>b</VAR>}'</SAMP>) and its meaning is to take the value
of the variable <VAR>var</VAR>, replace every <VAR>a</VAR> at the end of a word with
<VAR>b</VAR> in that value, and substitute the resulting string.
</P>
<P>
When we say "at the end of a word", we mean that <VAR>a</VAR> must appear
either followed by whitespace or at the end of the value in order to be
replaced; other occurrences of <VAR>a</VAR> in the value are unaltered. For
example:
</P>
<PRE>
foo := a.o b.o c.o
bar := $(foo:.o=.c)
</PRE>
<P>
sets <SAMP>`bar'</SAMP> to <SAMP>`a.c b.c c.c'</SAMP>. See section <A HREF="make_6.html#SEC64">Setting Variables</A>.
</P>
<P>
A substitution reference is actually an abbreviation for use of the
<CODE>patsubst</CODE> expansion function (see section <A HREF="make_8.html#SEC77">Functions for String Substitution and Analysis</A>). We provide
substitution references as well as <CODE>patsubst</CODE> for compatibility with
other implementations of <CODE>make</CODE>.
</P>
<P>
<A NAME="IDX449"></A>
Another type of substitution reference lets you use the full power of
the <CODE>patsubst</CODE> function. It has the same form
<SAMP>`$(<VAR>var</VAR>:<VAR>a</VAR>=<VAR>b</VAR>)'</SAMP> described above, except that now
<VAR>a</VAR> must contain a single <SAMP>`%'</SAMP> character. This case is
equivalent to <SAMP>`$(patsubst <VAR>a</VAR>,<VAR>b</VAR>,$(<VAR>var</VAR>))'</SAMP>.
See section <A HREF="make_8.html#SEC77">Functions for String Substitution and Analysis</A>,
for a description of the <CODE>patsubst</CODE> function.
</P>
<PRE>
For example:
foo := a.o b.o c.o
bar := $(foo:%.o=%.c)
</PRE>
<P>
sets <SAMP>`bar'</SAMP> to <SAMP>`a.c b.c c.c'</SAMP>.
</P>
<H3><A NAME="SEC62" HREF="make_toc.html#TOC62">Computed Variable Names</A></H3>
<P>
<A NAME="IDX450"></A>
<A NAME="IDX451"></A>
<A NAME="IDX452"></A>
<A NAME="IDX453"></A>
<A NAME="IDX454"></A>
<A NAME="IDX455"></A>
<A NAME="IDX456"></A>
</P>
<P>
Computed variable names are a complicated concept needed only for
sophisticated makefile programming. For most purposes you need not
consider them, except to know that making a variable with a dollar sign
in its name might have strange results. However, if you are the type
that wants to understand everything, or you are actually interested in
what they do, read on.
</P>
<P>
Variables may be referenced inside the name of a variable. This is
called a <STRONG>computed variable name</STRONG> or a <STRONG>nested variable
reference</STRONG>. For example,
</P>
<PRE>
x = y
y = z
a := $($(x))
</PRE>
<P>
defines <CODE>a</CODE> as <SAMP>`z'</SAMP>: the <SAMP>`$(x)'</SAMP> inside <SAMP>`$($(x))'</SAMP> expands
to <SAMP>`y'</SAMP>, so <SAMP>`$($(x))'</SAMP> expands to <SAMP>`$(y)'</SAMP> which in turn expands
to <SAMP>`z'</SAMP>. Here the name of the variable to reference is not stated
explicitly; it is computed by expansion of <SAMP>`$(x)'</SAMP>. The reference
<SAMP>`$(x)'</SAMP> here is nested within the outer variable reference.
</P>
<P>
The previous example shows two levels of nesting, but any number of levels
is possible. For example, here are three levels:
</P>
<PRE>
x = y
y = z
z = u
a := $($($(x)))
</PRE>
<P>
Here the innermost <SAMP>`$(x)'</SAMP> expands to <SAMP>`y'</SAMP>, so <SAMP>`$($(x))'</SAMP>
expands to <SAMP>`$(y)'</SAMP> which in turn expands to <SAMP>`z'</SAMP>; now we have
<SAMP>`$(z)'</SAMP>, which becomes <SAMP>`u'</SAMP>.
</P>
<P>
References to recursively-expanded variables within a variable name are
reexpanded in the usual fashion. For example:
</P>
<PRE>
x = $(y)
y = z
z = Hello
a := $($(x))
</PRE>
<P>
defines <CODE>a</CODE> as <SAMP>`Hello'</SAMP>: <SAMP>`$($(x))'</SAMP> becomes <SAMP>`$($(y))'</SAMP>
which becomes <SAMP>`$(z)'</SAMP> which becomes <SAMP>`Hello'</SAMP>.
</P>
<P>
Nested variable references can also contain modified references and
function invocations (see section <A HREF="make_8.html#SEC75">Functions for Transforming Text</A>),
just like any other reference.
For example, using the <CODE>subst</CODE> function
(see section <A HREF="make_8.html#SEC77">Functions for String Substitution and Analysis</A>):
</P>
<PRE>
x = variable1
variable2 := Hello
y = $(subst 1,2,$(x))
z = y
a := $($($(z)))
</PRE>
<P>
eventually defines <CODE>a</CODE> as <SAMP>`Hello'</SAMP>. It is doubtful that anyone
would ever want to write a nested reference as convoluted as this one, but
it works: <SAMP>`$($($(z)))'</SAMP> expands to <SAMP>`$($(y))'</SAMP> which becomes
<SAMP>`$($(subst 1,2,$(x)))'</SAMP>. This gets the value <SAMP>`variable1'</SAMP> from
<CODE>x</CODE> and changes it by substitution to <SAMP>`variable2'</SAMP>, so that the
entire string becomes <SAMP>`$(variable2)'</SAMP>, a simple variable reference
whose value is <SAMP>`Hello'</SAMP>.
</P>
<P>
A computed variable name need not consist entirely of a single variable
reference. It can contain several variable references, as well as some
invariant text. For example,
</P>
<PRE>
a_dirs := dira dirb
1_dirs := dir1 dir2
a_files := filea fileb
1_files := file1 file2
ifeq "$(use_a)" "yes"
a1 := a
else
a1 := 1
endif
ifeq "$(use_dirs)" "yes"
df := dirs
else
df := files
endif
dirs := $($(a1)_$(df))
</PRE>
<P>
will give <CODE>dirs</CODE> the same value as <CODE>a_dirs</CODE>, <CODE>1_dirs</CODE>,
<CODE>a_files</CODE> or <CODE>1_files</CODE> depending on the settings of <CODE>use_a</CODE>
and <CODE>use_dirs</CODE>.
</P>
<P>
Computed variable names can also be used in substitution references:
</P>
<PRE>
a_objects := a.o b.o c.o
1_objects := 1.o 2.o 3.o
sources := $($(a1)_objects:.o=.c)
</PRE>
<P>
defines <CODE>sources</CODE> as either <SAMP>`a.c b.c c.c'</SAMP> or <SAMP>`1.c 2.c 3.c'</SAMP>,
depending on the value of <CODE>a1</CODE>.
</P>
<P>
The only restriction on this sort of use of nested variable references
is that they cannot specify part of the name of a function to be called.
This is because the test for a recognized function name is done before
the expansion of nested references. For example,
</P>
<PRE>
ifdef do_sort
func := sort
else
func := strip
endif
bar := a d b g q c
foo := $($(func) $(bar))
</PRE>
<P>
attempts to give <SAMP>`foo'</SAMP> the value of the variable <SAMP>`sort a d b g
q c'</SAMP> or <SAMP>`strip a d b g q c'</SAMP>, rather than giving <SAMP>`a d b g q c'</SAMP>
as the argument to either the <CODE>sort</CODE> or the <CODE>strip</CODE> function.
This restriction could be removed in the future if that change is shown
to be a good idea.
</P>
<P>
You can also use computed variable names in the left-hand side of a
variable assignment, or in a <CODE>define</CODE> directive, as in:
</P>
<PRE>
dir = foo
$(dir)_sources := $(wildcard $(dir)/*.c)
define $(dir)_print
lpr $($(dir)_sources)
endef
</PRE>
<P>
This example defines the variables <SAMP>`dir'</SAMP>, <SAMP>`foo_sources'</SAMP>, and
<SAMP>`foo_print'</SAMP>.
</P>
<P>
Note that <STRONG>nested variable references</STRONG> are quite different from
<STRONG>recursively expanded variables</STRONG>
(see section <A HREF="make_6.html#SEC59">The Two Flavors of Variables</A>), though both are
used together in complex ways when doing makefile programming.
</P>
<H2><A NAME="SEC63" HREF="make_toc.html#TOC63">How Variables Get Their Values</A></H2>
<P>
<A NAME="IDX457"></A>
<A NAME="IDX458"></A>
</P>
<P>
Variables can get values in several different ways:
</P>
<UL>
<LI>
You can specify an overriding value when you run <CODE>make</CODE>.
See section <A HREF="make_9.html#SEC90">Overriding Variables</A>.
<LI>
You can specify a value in the makefile, either
with an assignment (see section <A HREF="make_6.html#SEC64">Setting Variables</A>) or with a
verbatim definition (see section <A HREF="make_6.html#SEC67">Defining Variables Verbatim</A>).
<LI>
Variables in the environment become <CODE>make</CODE> variables.
See section <A HREF="make_6.html#SEC68">Variables from the Environment</A>.
<LI>
Several <STRONG>automatic</STRONG> variables are given new values for each rule.
Each of these has a single conventional use.
See section <A HREF="make_10.html#SEC101">Automatic Variables</A>.
<LI>
Several variables have constant initial values.
See section <A HREF="make_10.html#SEC96">Variables Used by Implicit Rules</A>.
</UL>
<H2><A NAME="SEC64" HREF="make_toc.html#TOC64">Setting Variables</A></H2>
<P>
<A NAME="IDX459"></A>
<A NAME="IDX460"></A>
<A NAME="IDX461"></A>
<A NAME="IDX462"></A>
<A NAME="IDX463"></A>
</P>
<P>
To set a variable from the makefile, write a line starting with the
variable name followed by <SAMP>`='</SAMP> or <SAMP>`:='</SAMP>. Whatever follows the
<SAMP>`='</SAMP> or <SAMP>`:='</SAMP> on the line becomes the value. For example,
</P>
<PRE>
objects = main.o foo.o bar.o utils.o
</PRE>
<P>
defines a variable named <CODE>objects</CODE>. Whitespace around the variable
name and immediately after the <SAMP>`='</SAMP> is ignored.
</P>
<P>
Variables defined with <SAMP>`='</SAMP> are <STRONG>recursively expanded</STRONG> variables.
Variables defined with <SAMP>`:='</SAMP> are <STRONG>simply expanded</STRONG> variables; these
definitions can contain variable references which will be expanded before
the definition is made. See section <A HREF="make_6.html#SEC59">The Two Flavors of Variables</A>.
</P>
<P>
The variable name may contain function and variable references, which
are expanded when the line is read to find the actual variable name to use.
</P>
<P>
There is no limit on the length of the value of a variable except the
amount of swapping space on the computer. When a variable definition is
long, it is a good idea to break it into several lines by inserting
backslash-newline at convenient places in the definition. This will not
affect the functioning of <CODE>make</CODE>, but it will make the makefile easier
to read.
</P>
<P>
Most variable names are considered to have the empty string as a value if
you have never set them. Several variables have built-in initial values
that are not empty, but you can set them in the usual ways
(see section <A HREF="make_10.html#SEC96">Variables Used by Implicit Rules</A>).
Several special variables are set
automatically to a new value for each rule; these are called the
<STRONG>automatic</STRONG> variables (see section <A HREF="make_10.html#SEC101">Automatic Variables</A>).
</P>
<P>
If you'd like a variable to be set to a value only if it's not already
set, then you can use the shorthand operator <SAMP>`?='</SAMP> instead of
<SAMP>`='</SAMP>. These two settings of the variable <SAMP>`FOO'</SAMP> are identical
(see section <A HREF="make_8.html#SEC82">The <CODE>origin</CODE> Function</A>):
</P>
<PRE>
FOO ?= bar
</PRE>
<P>
and
</P>
<PRE>
ifeq ($(origin FOO), undefined)
FOO = bar
endif
</PRE>
<H2><A NAME="SEC65" HREF="make_toc.html#TOC65">Appending More Text to Variables</A></H2>
<P>
<A NAME="IDX464"></A>
<A NAME="IDX465"></A>
<A NAME="IDX466"></A>
</P>
<P>
Often it is useful to add more text to the value of a variable already defined.
You do this with a line containing <SAMP>`+='</SAMP>, like this:
</P>
<PRE>
objects += another.o
</PRE>
<P>
This takes the value of the variable <CODE>objects</CODE>, and adds the text
<SAMP>`another.o'</SAMP> to it (preceded by a single space). Thus:
</P>
<PRE>
objects = main.o foo.o bar.o utils.o
objects += another.o
</PRE>
<P>
sets <CODE>objects</CODE> to <SAMP>`main.o foo.o bar.o utils.o another.o'</SAMP>.
</P>
<P>
Using <SAMP>`+='</SAMP> is similar to:
</P>
<PRE>
objects = main.o foo.o bar.o utils.o
objects := $(objects) another.o
</PRE>
<P>
but differs in ways that become important when you use more complex values.
</P>
<P>
When the variable in question has not been defined before, <SAMP>`+='</SAMP>
acts just like normal <SAMP>`='</SAMP>: it defines a recursively-expanded
variable. However, when there <EM>is</EM> a previous definition, exactly
what <SAMP>`+='</SAMP> does depends on what flavor of variable you defined
originally. See section <A HREF="make_6.html#SEC59">The Two Flavors of Variables</A>, for an
explanation of the two flavors of variables.
</P>
<P>
When you add to a variable's value with <SAMP>`+='</SAMP>, <CODE>make</CODE> acts
essentially as if you had included the extra text in the initial
definition of the variable. If you defined it first with <SAMP>`:='</SAMP>,
making it a simply-expanded variable, <SAMP>`+='</SAMP> adds to that
simply-expanded definition, and expands the new text before appending it
to the old value just as <SAMP>`:='</SAMP> does
(see section <A HREF="make_6.html#SEC64">Setting Variables</A>, for a full explanation of <SAMP>`:='</SAMP>).
In fact,
</P>
<PRE>
variable := value
variable += more
</PRE>
<P>
is exactly equivalent to:
</P>
<P>
<PRE>
variable := value
variable := $(variable) more
</PRE>
<P>
On the other hand, when you use <SAMP>`+='</SAMP> with a variable that you defined
first to be recursively-expanded using plain <SAMP>`='</SAMP>, <CODE>make</CODE> does
something a bit different. Recall that when you define a
recursively-expanded variable, <CODE>make</CODE> does not expand the value you set
for variable and function references immediately. Instead it stores the text
verbatim, and saves these variable and function references to be expanded
later, when you refer to the new variable (see section <A HREF="make_6.html#SEC59">The Two Flavors of Variables</A>). When you use <SAMP>`+='</SAMP> on a recursively-expanded variable,
it is this unexpanded text to which <CODE>make</CODE> appends the new text you
specify.
</P>
<PRE>
variable = value
variable += more
</PRE>
<P>
is roughly equivalent to:
</P>
<PRE>
temp = value
variable = $(temp) more
</PRE>
<P>
except that of course it never defines a variable called <CODE>temp</CODE>.
The importance of this comes when the variable's old value contains
variable references. Take this common example:
</P>
<PRE>
CFLAGS = $(includes) -O
...
CFLAGS += -pg # enable profiling
</PRE>
<P>
The first line defines the <CODE>CFLAGS</CODE> variable with a reference to another
variable, <CODE>includes</CODE>. (<CODE>CFLAGS</CODE> is used by the rules for C
compilation; see section <A HREF="make_10.html#SEC95">Catalogue of Implicit Rules</A>.)
Using <SAMP>`='</SAMP> for the definition makes <CODE>CFLAGS</CODE> a recursively-expanded
variable, meaning <SAMP>`$(includes) -O'</SAMP> is <EM>not</EM> expanded when
<CODE>make</CODE> processes the definition of <CODE>CFLAGS</CODE>. Thus, <CODE>includes</CODE>
need not be defined yet for its value to take effect. It only has to be
defined before any reference to <CODE>CFLAGS</CODE>. If we tried to append to the
value of <CODE>CFLAGS</CODE> without using <SAMP>`+='</SAMP>, we might do it like this:
</P>
<PRE>
CFLAGS := $(CFLAGS) -pg # enable profiling
</PRE>
<P>
This is pretty close, but not quite what we want. Using <SAMP>`:='</SAMP>
redefines <CODE>CFLAGS</CODE> as a simply-expanded variable; this means
<CODE>make</CODE> expands the text <SAMP>`$(CFLAGS) -pg'</SAMP> before setting the
variable. If <CODE>includes</CODE> is not yet defined, we get <SAMP>` -O
-pg'</SAMP>, and a later definition of <CODE>includes</CODE> will have no effect.
Conversely, by using <SAMP>`+='</SAMP> we set <CODE>CFLAGS</CODE> to the
<EM>unexpanded</EM> value <SAMP>`$(includes) -O -pg'</SAMP>. Thus we preserve
the reference to <CODE>includes</CODE>, so if that variable gets defined at
any later point, a reference like <SAMP>`$(CFLAGS)'</SAMP> still uses its
value.
</P>
<H2><A NAME="SEC66" HREF="make_toc.html#TOC66">The <CODE>override</CODE> Directive</A></H2>
<P>
<A NAME="IDX467"></A>
<A NAME="IDX468"></A>
<A NAME="IDX469"></A>
</P>
<P>
If a variable has been set with a command argument
(see section <A HREF="make_9.html#SEC90">Overriding Variables</A>),
then ordinary assignments in the makefile are ignored. If you want to set
the variable in the makefile even though it was set with a command
argument, you can use an <CODE>override</CODE> directive, which is a line that
looks like this:
</P>
<PRE>
override <VAR>variable</VAR> = <VAR>value</VAR>
</PRE>
<P>
or
</P>
<PRE>
override <VAR>variable</VAR> := <VAR>value</VAR>
</PRE>
<P>
To append more text to a variable defined on the command line, use:
</P>
<PRE>
override <VAR>variable</VAR> += <VAR>more text</VAR>
</PRE>
<P>
See section <A HREF="make_6.html#SEC65">Appending More Text to Variables</A>.
</P>
<P>
The <CODE>override</CODE> directive was not invented for escalation in the war
between makefiles and command arguments. It was invented so you can alter
and add to values that the user specifies with command arguments.
</P>
<P>
For example, suppose you always want the <SAMP>`-g'</SAMP> switch when you run the
C compiler, but you would like to allow the user to specify the other
switches with a command argument just as usual. You could use this
<CODE>override</CODE> directive:
</P>
<PRE>
override CFLAGS += -g
</PRE>
<P>
You can also use <CODE>override</CODE> directives with <CODE>define</CODE> directives.
This is done as you might expect:
</P>
<PRE>
override define foo
bar
endef
</PRE>
<P>
See the next section for information about <CODE>define</CODE>.
</P>
<H2><A NAME="SEC67" HREF="make_toc.html#TOC67">Defining Variables Verbatim</A></H2>
<P>
<A NAME="IDX470"></A>
<A NAME="IDX471"></A>
<A NAME="IDX472"></A>
<A NAME="IDX473"></A>
<A NAME="IDX474"></A>
</P>
<P>
Another way to set the value of a variable is to use the <CODE>define</CODE>
directive. This directive has an unusual syntax which allows newline
characters to be included in the value, which is convenient for defining
canned sequences of commands
(see section <A HREF="make_5.html#SEC55">Defining Canned Command Sequences</A>).
</P>
<P>
The <CODE>define</CODE> directive is followed on the same line by the name of the
variable and nothing more. The value to give the variable appears on the
following lines. The end of the value is marked by a line containing just
the word <CODE>endef</CODE>. Aside from this difference in syntax, <CODE>define</CODE>
works just like <SAMP>`='</SAMP>: it creates a recursively-expanded variable
(see section <A HREF="make_6.html#SEC59">The Two Flavors of Variables</A>).
The variable name may contain function and variable references, which
are expanded when the directive is read to find the actual variable name
to use.
</P>
<PRE>
define two-lines
echo foo
echo $(bar)
endef
</PRE>
<P>
The value in an ordinary assignment cannot contain a newline; but the
newlines that separate the lines of the value in a <CODE>define</CODE> become
part of the variable's value (except for the final newline which precedes
the <CODE>endef</CODE> and is not considered part of the value).
</P>
<P>
The previous example is functionally equivalent to this:
</P>
<PRE>
two-lines = echo foo; echo $(bar)
</PRE>
<P>
since two commands separated by semicolon behave much like two separate
shell commands. However, note that using two separate lines means
<CODE>make</CODE> will invoke the shell twice, running an independent subshell
for each line. See section <A HREF="make_5.html#SEC46">Command Execution</A>.
</P>
<P>
If you want variable definitions made with <CODE>define</CODE> to take
precedence over command-line variable definitions, you can use the
<CODE>override</CODE> directive together with <CODE>define</CODE>:
</P>
<PRE>
override define two-lines
foo
$(bar)
endef
</PRE>
<P>
See section <A HREF="make_6.html#SEC66">The <CODE>override</CODE> Directive</A>.
</P>
<H2><A NAME="SEC68" HREF="make_toc.html#TOC68">Variables from the Environment</A></H2>
<P>
<A NAME="IDX475"></A>
<A NAME="IDX476"></A>
Variables in <CODE>make</CODE> can come from the environment in which
<CODE>make</CODE> is run. Every environment variable that <CODE>make</CODE> sees when
it starts up is transformed into a <CODE>make</CODE> variable with the same name
and value. But an explicit assignment in the makefile, or with a command
argument, overrides the environment. (If the <SAMP>`-e'</SAMP> flag is specified,
then values from the environment override assignments in the makefile.
See section <A HREF="make_9.html#SEC92">Summary of Options</A>.
But this is not recommended practice.)
</P>
<P>
Thus, by setting the variable <CODE>CFLAGS</CODE> 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 <CODE>CFLAGS</CODE> explicitly
and therefore are not affected by the value in the environment.)
</P>
<P>
When <CODE>make</CODE> is invoked recursively, variables defined in the
outer invocation can be passed to inner invocations through the
environment (see section <A HREF="make_5.html#SEC50">Recursive Use of <CODE>make</CODE></A>). By
default, only variables that came from the environment or the command
line are passed to recursive invocations. You can use the
<CODE>export</CODE> directive to pass other variables.
See section <A HREF="make_5.html#SEC52">Communicating Variables to a Sub-<CODE>make</CODE></A>, for full details.
</P>
<P>
Other use of variables from the environment is not recommended. It is not
wise for makefiles to depend for their functioning on environment variables
set up outside their control, since this would cause different users to get
different results from the same makefile. This is against the whole
purpose of most makefiles.
</P>
<P>
Such problems would be especially likely with the variable <CODE>SHELL</CODE>,
which is normally present in the environment to specify the user's choice
of interactive shell. It would be very undesirable for this choice to
affect <CODE>make</CODE>. So <CODE>make</CODE> ignores the environment value of
<CODE>SHELL</CODE> (except on MS-DOS and MS-Windows, where <CODE>SHELL</CODE> is
usually not set. See section <A HREF="make_5.html#SEC46">Command Execution</A>.)
</P>
<H2><A NAME="SEC69" HREF="make_toc.html#TOC69">Target-specific Variable Values</A></H2>
<P>
<A NAME="IDX477"></A>
<A NAME="IDX478"></A>
</P>
<P>
Variable values in <CODE>make</CODE> are usually global; that is, they are the
same regardless of where they are evaluated (unless they're reset, of
course). One exception to that is automatic variables
(see section <A HREF="make_10.html#SEC101">Automatic Variables</A>).
</P>
<P>
The other exception is <STRONG>target-specific variable values</STRONG>. This
feature allows you to define different values for the same variable,
based on the target that <CODE>make</CODE> is currently building. As with
automatic variables, these values are only available within the context
of a target's command script (and in other target-specific assignments).
</P>
<P>
Set a target-specific variable value like this:
</P>
<PRE>
<VAR>target</VAR> ... : <VAR>variable-assignment</VAR>
</PRE>
<P>
or like this:
</P>
<PRE>
<VAR>target</VAR> ... : override <VAR>variable-assignment</VAR>
</PRE>
<P>
Multiple <VAR>target</VAR> values create a target-specific variable value for
each member of the target list individually.
</P>
<P>
The <VAR>variable-assignment</VAR> can be any valid form of assignment;
recursive (<SAMP>`='</SAMP>), static (<SAMP>`:='</SAMP>), appending (<SAMP>`+='</SAMP>), or
conditional (<SAMP>`?='</SAMP>). All variables that appear within the
<VAR>variable-assignment</VAR> are evaluated within the context of the
target: thus, any previously-defined target-specific variable values
will be in effect. Note that this variable is actually distinct from
any "global" value: the two variables do not have to have the same
flavor (recursive vs. static).
</P>
<P>
Target-specific variables have the same priority as any other makefile
variable. Variables provided on the command-line (and in the
environment if the <SAMP>`-e'</SAMP> option is in force) will take precedence.
Specifying the <CODE>override</CODE> directive will allow the target-specific
variable value to be preferred.
</P>
<P>
There is one more special feature of target-specific variables: when you
define a target-specific variable, that variable value is also in effect
for all prerequisites of this target (unless those prerequisites override
it with their own target-specific variable value). So, for example, a
statement like this:
</P>
<PRE>
prog : CFLAGS = -g
prog : prog.o foo.o bar.o
</PRE>
<P>
will set <CODE>CFLAGS</CODE> to <SAMP>`-g'</SAMP> in the command script for
<TT>`prog'</TT>, but it will also set <CODE>CFLAGS</CODE> to <SAMP>`-g'</SAMP> in the
command scripts that create <TT>`prog.o'</TT>, <TT>`foo.o'</TT>, and
<TT>`bar.o'</TT>, and any command scripts which create their prerequisites.
</P>
<H2><A NAME="SEC70" HREF="make_toc.html#TOC70">Pattern-specific Variable Values</A></H2>
<P>
<A NAME="IDX479"></A>
<A NAME="IDX480"></A>
</P>
<P>
In addition to target-specific variable values (see section <A HREF="make_6.html#SEC69">Target-specific Variable Values</A>), GNU <CODE>make</CODE> supports
pattern-specific variable values. In this form, a variable is defined
for any target that matches the pattern specified. Variables defined in
this way are searched after any target-specific variables defined
explicitly for that target, and before target-specific variables defined
for the parent target.
</P>
<P>
Set a pattern-specific variable value like this:
</P>
<PRE>
<VAR>pattern</VAR> ... : <VAR>variable-assignment</VAR>
</PRE>
<P>
or like this:
</P>
<PRE>
<VAR>pattern</VAR> ... : override <VAR>variable-assignment</VAR>
</PRE>
<P>
where <VAR>pattern</VAR> is a %-pattern. As with target-specific variable
values, multiple <VAR>pattern</VAR> values create a pattern-specific variable
value for each pattern individually. The <VAR>variable-assignment</VAR> can
be any valid form of assignment. Any command-line variable setting will
take precedence, unless <CODE>override</CODE> is specified.
</P>
<P>
For example:
</P>
<PRE>
%.o : CFLAGS = -O
</PRE>
<P>
will assign <CODE>CFLAGS</CODE> the value of <SAMP>`-O'</SAMP> for all targets
matching the pattern <CODE>%.o</CODE>.
</P>
<P><HR><P>
<p>Go to the <A HREF="make_1.html">first</A>, <A HREF="make_5.html">previous</A>, <A HREF="make_7.html">next</A>, <A HREF="make_19.html">last</A> section, <A HREF="make_toc.html">table of contents</A>.
</BODY>
</HTML>