Files
oldlinux-files/study/Ref-docs/manual make/make_8.html
2024-02-19 00:25:23 -05:00

1167 lines
35 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 - Functions for Transforming Text</TITLE>
<link href="make_9.html" rel=Next>
<link href="make_7.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_7.html">previous</A>, <A HREF="make_9.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="SEC75" HREF="make_toc.html#TOC75">Functions for Transforming Text</A></H1>
<P>
<A NAME="IDX488"></A>
</P>
<P>
<STRONG>Functions</STRONG> allow you to do text processing in the makefile to compute
the files to operate on or the commands to use. You use a function in a
<STRONG>function call</STRONG>, where you give the name of the function and some text
(the <STRONG>arguments</STRONG>) for the function to operate on. The result of the
function's processing is substituted into the makefile at the point of the
call, just as a variable might be substituted.
</P>
<H2><A NAME="SEC76" HREF="make_toc.html#TOC76">Function Call Syntax</A></H2>
<P>
<A NAME="IDX489"></A>
<A NAME="IDX490"></A>
<A NAME="IDX491"></A>
<A NAME="IDX492"></A>
</P>
<P>
A function call resembles a variable reference. It looks like this:
</P>
<PRE>
$(<VAR>function</VAR> <VAR>arguments</VAR>)
</PRE>
<P>
or like this:
</P>
<PRE>
${<VAR>function</VAR> <VAR>arguments</VAR>}
</PRE>
<P>
Here <VAR>function</VAR> is a function name; one of a short list of names
that are part of <CODE>make</CODE>. You can also essentially create your own
functions by using the <CODE>call</CODE> builtin function.
</P>
<P>
The <VAR>arguments</VAR> are the arguments of the function. They are
separated from the function name by one or more spaces or tabs, and if
there is more than one argument, then they are separated by commas.
Such whitespace and commas are not part of an argument's value. The
delimiters which you use to surround the function call, whether
parentheses or braces, can appear in an argument only in matching pairs;
the other kind of delimiters may appear singly. If the arguments
themselves contain other function calls or variable references, it is
wisest to use the same kind of delimiters for all the references; write
<SAMP>`$(subst a,b,$(x))'</SAMP>, not <SAMP>`$(subst a,b,${x})'</SAMP>. This
is because it is clearer, and because only one type of delimiter is
matched to find the end of the reference.
</P>
<P>
The text written for each argument is processed by substitution of
variables and function calls to produce the argument value, which
is the text on which the function acts. The substitution is done in the
order in which the arguments appear.
</P>
<P>
Commas and unmatched parentheses or braces cannot appear in the text of an
argument as written; leading spaces cannot appear in the text of the first
argument as written. These characters can be put into the argument value
by variable substitution. First define variables <CODE>comma</CODE> and
<CODE>space</CODE> whose values are isolated comma and space characters, then
substitute these variables where such characters are wanted, like this:
</P>
<PRE>
comma:= ,
empty:=
space:= $(empty) $(empty)
foo:= a b c
bar:= $(subst $(space),$(comma),$(foo))
# bar is now `a,b,c'.
</PRE>
<P>
Here the <CODE>subst</CODE> function replaces each space with a comma, through
the value of <CODE>foo</CODE>, and substitutes the result.
</P>
<H2><A NAME="SEC77" HREF="make_toc.html#TOC77">Functions for String Substitution and Analysis</A></H2>
<P>
<A NAME="IDX493"></A>
</P>
<P>
Here are some functions that operate on strings:
</P>
<DL COMPACT>
<DT><CODE>$(subst <VAR>from</VAR>,<VAR>to</VAR>,<VAR>text</VAR>)</CODE>
<DD>
<A NAME="IDX494"></A>
Performs a textual replacement on the text <VAR>text</VAR>: each occurrence
of <VAR>from</VAR> is replaced by <VAR>to</VAR>. The result is substituted for
the function call. For example,
<PRE>
$(subst ee,EE,feet on the street)
</PRE>
substitutes the string <SAMP>`fEEt on the strEEt'</SAMP>.
<DT><CODE>$(patsubst <VAR>pattern</VAR>,<VAR>replacement</VAR>,<VAR>text</VAR>)</CODE>
<DD>
<A NAME="IDX495"></A>
Finds whitespace-separated words in <VAR>text</VAR> that match
<VAR>pattern</VAR> and replaces them with <VAR>replacement</VAR>. Here
<VAR>pattern</VAR> may contain a <SAMP>`%'</SAMP> which acts as a wildcard,
matching any number of any characters within a word. If
<VAR>replacement</VAR> also contains a <SAMP>`%'</SAMP>, the <SAMP>`%'</SAMP> is replaced
by the text that matched the <SAMP>`%'</SAMP> in <VAR>pattern</VAR>.
<A NAME="IDX496"></A>
<A NAME="IDX497"></A>
<A NAME="IDX498"></A>
<A NAME="IDX499"></A>
<A NAME="IDX500"></A>
<SAMP>`%'</SAMP> characters in <CODE>patsubst</CODE> function invocations can be
quoted with preceding backslashes (<SAMP>`\'</SAMP>). Backslashes that would
otherwise quote <SAMP>`%'</SAMP> characters can be quoted with more backslashes.
Backslashes that quote <SAMP>`%'</SAMP> characters or other backslashes are
removed from the pattern before it is compared file names or has a stem
substituted into it. Backslashes that are not in danger of quoting
<SAMP>`%'</SAMP> characters go unmolested. For example, the pattern
<TT>`the\%weird\\%pattern\\'</TT> has <SAMP>`the%weird\'</SAMP> preceding the
operative <SAMP>`%'</SAMP> character, and <SAMP>`pattern\\'</SAMP> following it. The
final two backslashes are left alone because they cannot affect any
<SAMP>`%'</SAMP> character.
Whitespace between words is folded into single space characters;
leading and trailing whitespace is discarded.
For example,
<PRE>
$(patsubst %.c,%.o,x.c.c bar.c)
</PRE>
produces the value <SAMP>`x.c.o bar.o'</SAMP>.
Substitution references (see section <A HREF="make_6.html#SEC61">Substitution References</A>) are a simpler way to get the effect of the <CODE>patsubst</CODE>
function:
<PRE>
$(<VAR>var</VAR>:<VAR>pattern</VAR>=<VAR>replacement</VAR>)
</PRE>
is equivalent to
<PRE>
$(patsubst <VAR>pattern</VAR>,<VAR>replacement</VAR>,$(<VAR>var</VAR>))
</PRE>
The second shorthand simplifies one of the most common uses of
<CODE>patsubst</CODE>: replacing the suffix at the end of file names.
<PRE>
$(<VAR>var</VAR>:<VAR>suffix</VAR>=<VAR>replacement</VAR>)
</PRE>
is equivalent to
<PRE>
$(patsubst %<VAR>suffix</VAR>,%<VAR>replacement</VAR>,$(<VAR>var</VAR>))
</PRE>
For example, you might have a list of object files:
<PRE>
objects = foo.o bar.o baz.o
</PRE>
To get the list of corresponding source files, you could simply write:
<PRE>
$(objects:.o=.c)
</PRE>
instead of using the general form:
<PRE>
$(patsubst %.o,%.c,$(objects))
</PRE>
<DT><CODE>$(strip <VAR>string</VAR>)</CODE>
<DD>
<A NAME="IDX501"></A>
<A NAME="IDX502"></A>
<A NAME="IDX503"></A>
<A NAME="IDX504"></A>
Removes leading and trailing whitespace from <VAR>string</VAR> and replaces
each internal sequence of one or more whitespace characters with a
single space. Thus, <SAMP>`$(strip a b c )'</SAMP> results in <SAMP>`a b c'</SAMP>.
The function <CODE>strip</CODE> can be very useful when used in conjunction
with conditionals. When comparing something with the empty string
<SAMP>`'</SAMP> using <CODE>ifeq</CODE> or <CODE>ifneq</CODE>, you usually want a string of
just whitespace to match the empty string (see section <A HREF="make_7.html#SEC71">Conditional Parts of Makefiles</A>).
Thus, the following may fail to have the desired results:
<PRE>
.PHONY: all
ifneq "$(needs_made)" ""
all: $(needs_made)
else
all:;@echo 'Nothing to make!'
endif
</PRE>
Replacing the variable reference <SAMP>`$(needs_made)'</SAMP> with the
function call <SAMP>`$(strip $(needs_made))'</SAMP> in the <CODE>ifneq</CODE>
directive would make it more robust.
<DT><CODE>$(findstring <VAR>find</VAR>,<VAR>in</VAR>)</CODE>
<DD>
<A NAME="IDX505"></A>
<A NAME="IDX506"></A>
<A NAME="IDX507"></A>
<A NAME="IDX508"></A>
Searches <VAR>in</VAR> for an occurrence of <VAR>find</VAR>. If it occurs, the
value is <VAR>find</VAR>; otherwise, the value is empty. You can use this
function in a conditional to test for the presence of a specific
substring in a given string. Thus, the two examples,
<PRE>
$(findstring a,a b c)
$(findstring a,b c)
</PRE>
produce the values <SAMP>`a'</SAMP> and <SAMP>`'</SAMP> (the empty string),
respectively. See section <A HREF="make_7.html#SEC74">Conditionals that Test Flags</A>, for a practical application of
<CODE>findstring</CODE>.
<A NAME="IDX509"></A>
<A NAME="IDX510"></A>
<A NAME="IDX511"></A>
<DT><CODE>$(filter <VAR>pattern</VAR>...,<VAR>text</VAR>)</CODE>
<DD>
Returns all whitespace-separated words in <VAR>text</VAR> that <EM>do</EM> match
any of the <VAR>pattern</VAR> words, removing any words that <EM>do not</EM>
match. The patterns are written using <SAMP>`%'</SAMP>, just like the patterns
used in the <CODE>patsubst</CODE> function above.
The <CODE>filter</CODE> function can be used to separate out different types
of strings (such as file names) in a variable. For example:
<PRE>
sources := foo.c bar.c baz.s ugh.h
foo: $(sources)
cc $(filter %.c %.s,$(sources)) -o foo
</PRE>
says that <TT>`foo'</TT> depends of <TT>`foo.c'</TT>, <TT>`bar.c'</TT>,
<TT>`baz.s'</TT> and <TT>`ugh.h'</TT> but only <TT>`foo.c'</TT>, <TT>`bar.c'</TT> and
<TT>`baz.s'</TT> should be specified in the command to the
compiler.
<DT><CODE>$(filter-out <VAR>pattern</VAR>...,<VAR>text</VAR>)</CODE>
<DD>
<A NAME="IDX512"></A>
<A NAME="IDX513"></A>
<A NAME="IDX514"></A>
Returns all whitespace-separated words in <VAR>text</VAR> that <EM>do not</EM>
match any of the <VAR>pattern</VAR> words, removing the words that <EM>do</EM>
match one or more. This is the exact opposite of the <CODE>filter</CODE>
function.
Removes all whitespace-separated words in <VAR>text</VAR> that <EM>do</EM>
match the <VAR>pattern</VAR> words, returning only the words that <EM>do
not</EM> match. This is the exact opposite of the <CODE>filter</CODE>
function.
For example, given:
<PRE>
objects=main1.o foo.o main2.o bar.o
mains=main1.o main2.o
</PRE>
the following generates a list which contains all the object files not
in <SAMP>`mains'</SAMP>:
<PRE>
$(filter-out $(mains),$(objects))
</PRE>
<A NAME="IDX515"></A>
<A NAME="IDX516"></A>
<DT><CODE>$(sort <VAR>list</VAR>)</CODE>
<DD>
Sorts the words of <VAR>list</VAR> in lexical order, removing duplicate
words. The output is a list of words separated by single spaces.
Thus,
<PRE>
$(sort foo bar lose)
</PRE>
returns the value <SAMP>`bar foo lose'</SAMP>.
<A NAME="IDX517"></A>
<A NAME="IDX518"></A>
<A NAME="IDX519"></A>
Incidentally, since <CODE>sort</CODE> removes duplicate words, you can use
it for this purpose even if you don't care about the sort order.
</DL>
<P>
Here is a realistic example of the use of <CODE>subst</CODE> and
<CODE>patsubst</CODE>. Suppose that a makefile uses the <CODE>VPATH</CODE> variable
to specify a list of directories that <CODE>make</CODE> should search for
prerequisite files
(see section <A HREF="make_4.html#SEC27"><CODE>VPATH</CODE>: Search Path for All Prerequisites</A>).
This example shows how to
tell the C compiler to search for header files in the same list of
directories.
</P>
<P>
The value of <CODE>VPATH</CODE> is a list of directories separated by colons,
such as <SAMP>`src:../headers'</SAMP>. First, the <CODE>subst</CODE> function is used to
change the colons to spaces:
</P>
<PRE>
$(subst :, ,$(VPATH))
</PRE>
<P>
This produces <SAMP>`src ../headers'</SAMP>. Then <CODE>patsubst</CODE> is used to turn
each directory name into a <SAMP>`-I'</SAMP> flag. These can be added to the
value of the variable <CODE>CFLAGS</CODE>, which is passed automatically to the C
compiler, like this:
</P>
<PRE>
override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
</PRE>
<P>
The effect is to append the text <SAMP>`-Isrc -I../headers'</SAMP> to the
previously given value of <CODE>CFLAGS</CODE>. The <CODE>override</CODE> directive is
used so that the new value is assigned even if the previous value of
<CODE>CFLAGS</CODE> was specified with a command argument (see section <A HREF="make_6.html#SEC66">The <CODE>override</CODE> Directive</A>).
</P>
<H2><A NAME="SEC78" HREF="make_toc.html#TOC78">Functions for File Names</A></H2>
<P>
<A NAME="IDX520"></A>
<A NAME="IDX521"></A>
</P>
<P>
Several of the built-in expansion functions relate specifically to
taking apart file names or lists of file names.
</P>
<P>
Each of the following functions performs a specific transformation on a
file name. The argument of the function is regarded as a series of file
names, separated by whitespace. (Leading and trailing whitespace is
ignored.) Each file name in the series is transformed in the same way and
the results are concatenated with single spaces between them.
</P>
<DL COMPACT>
<DT><CODE>$(dir <VAR>names</VAR>...)</CODE>
<DD>
<A NAME="IDX522"></A>
<A NAME="IDX523"></A>
<A NAME="IDX524"></A>
Extracts the directory-part of each file name in <VAR>names</VAR>. The
directory-part of the file name is everything up through (and
including) the last slash in it. If the file name contains no slash,
the directory part is the string <SAMP>`./'</SAMP>. For example,
<PRE>
$(dir src/foo.c hacks)
</PRE>
produces the result <SAMP>`src/ ./'</SAMP>.
<DT><CODE>$(notdir <VAR>names</VAR>...)</CODE>
<DD>
<A NAME="IDX525"></A>
<A NAME="IDX526"></A>
<A NAME="IDX527"></A>
Extracts all but the directory-part of each file name in <VAR>names</VAR>.
If the file name contains no slash, it is left unchanged. Otherwise,
everything through the last slash is removed from it.
A file name that ends with a slash becomes an empty string. This is
unfortunate, because it means that the result does not always have the
same number of whitespace-separated file names as the argument had;
but we do not see any other valid alternative.
For example,
<PRE>
$(notdir src/foo.c hacks)
</PRE>
produces the result <SAMP>`foo.c hacks'</SAMP>.
<DT><CODE>$(suffix <VAR>names</VAR>...)</CODE>
<DD>
<A NAME="IDX528"></A>
<A NAME="IDX529"></A>
<A NAME="IDX530"></A>
Extracts the suffix of each file name in <VAR>names</VAR>. If the file name
contains a period, the suffix is everything starting with the last
period. Otherwise, the suffix is the empty string. This frequently
means that the result will be empty when <VAR>names</VAR> is not, and if
<VAR>names</VAR> contains multiple file names, the result may contain fewer
file names.
For example,
<PRE>
$(suffix src/foo.c src-1.0/bar.c hacks)
</PRE>
produces the result <SAMP>`.c .c'</SAMP>.
<DT><CODE>$(basename <VAR>names</VAR>...)</CODE>
<DD>
<A NAME="IDX531"></A>
<A NAME="IDX532"></A>
<A NAME="IDX533"></A>
Extracts all but the suffix of each file name in <VAR>names</VAR>. If the
file name contains a period, the basename is everything starting up to
(and not including) the last period. Periods in the directory part are
ignored. If there is no period, the basename is the entire file name.
For example,
<PRE>
$(basename src/foo.c src-1.0/bar hacks)
</PRE>
produces the result <SAMP>`src/foo src-1.0/bar hacks'</SAMP>.
<DT><CODE>$(addsuffix <VAR>suffix</VAR>,<VAR>names</VAR>...)</CODE>
<DD>
<A NAME="IDX534"></A>
<A NAME="IDX535"></A>
<A NAME="IDX536"></A>
The argument <VAR>names</VAR> is regarded as a series of names, separated
by whitespace; <VAR>suffix</VAR> is used as a unit. The value of
<VAR>suffix</VAR> is appended to the end of each individual name and the
resulting larger names are concatenated with single spaces between
them. For example,
<PRE>
$(addsuffix .c,foo bar)
</PRE>
produces the result <SAMP>`foo.c bar.c'</SAMP>.
<DT><CODE>$(addprefix <VAR>prefix</VAR>,<VAR>names</VAR>...)</CODE>
<DD>
<A NAME="IDX537"></A>
<A NAME="IDX538"></A>
<A NAME="IDX539"></A>
The argument <VAR>names</VAR> is regarded as a series of names, separated
by whitespace; <VAR>prefix</VAR> is used as a unit. The value of
<VAR>prefix</VAR> is prepended to the front of each individual name and the
resulting larger names are concatenated with single spaces between
them. For example,
<PRE>
$(addprefix src/,foo bar)
</PRE>
produces the result <SAMP>`src/foo src/bar'</SAMP>.
<DT><CODE>$(join <VAR>list1</VAR>,<VAR>list2</VAR>)</CODE>
<DD>
<A NAME="IDX540"></A>
<A NAME="IDX541"></A>
<A NAME="IDX542"></A>
Concatenates the two arguments word by word: the two first words (one
from each argument) concatenated form the first word of the result, the
two second words form the second word of the result, and so on. So the
<VAR>n</VAR>th word of the result comes from the <VAR>n</VAR>th word of each
argument. If one argument has more words that the other, the extra
words are copied unchanged into the result.
For example, <SAMP>`$(join a b,.c .o)'</SAMP> produces <SAMP>`a.c b.o'</SAMP>.
Whitespace between the words in the lists is not preserved; it is
replaced with a single space.
This function can merge the results of the <CODE>dir</CODE> and
<CODE>notdir</CODE> functions, to produce the original list of files which
was given to those two functions.
<DT><CODE>$(word <VAR>n</VAR>,<VAR>text</VAR>)</CODE>
<DD>
<A NAME="IDX543"></A>
<A NAME="IDX544"></A>
<A NAME="IDX545"></A>
Returns the <VAR>n</VAR>th word of <VAR>text</VAR>. The legitimate values of
<VAR>n</VAR> start from 1. If <VAR>n</VAR> is bigger than the number of words
in <VAR>text</VAR>, the value is empty. For example,
<PRE>
$(word 2, foo bar baz)
</PRE>
returns <SAMP>`bar'</SAMP>.
<DT><CODE>$(wordlist <VAR>s</VAR>,<VAR>e</VAR>,<VAR>text</VAR>)</CODE>
<DD>
<A NAME="IDX546"></A>
<A NAME="IDX547"></A>
<A NAME="IDX548"></A>
Returns the list of words in <VAR>text</VAR> starting with word <VAR>s</VAR> and
ending with word <VAR>e</VAR> (inclusive). The legitimate values of <VAR>s</VAR>
and <VAR>e</VAR> start from 1. If <VAR>s</VAR> is bigger than the number of words
in <VAR>text</VAR>, the value is empty. If <VAR>e</VAR> is bigger than the number
of words in <VAR>text</VAR>, words up to the end of <VAR>text</VAR> are returned.
If <VAR>s</VAR> is greater than <VAR>e</VAR>, nothing is returned. For example,
<PRE>
$(wordlist 2, 3, foo bar baz)
</PRE>
returns <SAMP>`bar baz'</SAMP>.
<DT><CODE>$(words <VAR>text</VAR>)</CODE>
<DD>
<A NAME="IDX549"></A>
<A NAME="IDX550"></A>
Returns the number of words in <VAR>text</VAR>.
Thus, the last word of <VAR>text</VAR> is
<CODE>$(word $(words <VAR>text</VAR>),<VAR>text</VAR>)</CODE>.
<DT><CODE>$(firstword <VAR>names</VAR>...)</CODE>
<DD>
<A NAME="IDX551"></A>
<A NAME="IDX552"></A>
The argument <VAR>names</VAR> is regarded as a series of names, separated
by whitespace. The value is the first name in the series. The rest
of the names are ignored.
For example,
<PRE>
$(firstword foo bar)
</PRE>
produces the result <SAMP>`foo'</SAMP>. Although <CODE>$(firstword
<VAR>text</VAR>)</CODE> is the same as <CODE>$(word 1,<VAR>text</VAR>)</CODE>, the
<CODE>firstword</CODE> function is retained for its simplicity.
<DT><CODE>$(wildcard <VAR>pattern</VAR>)</CODE>
<DD>
<A NAME="IDX553"></A>
<A NAME="IDX554"></A>
The argument <VAR>pattern</VAR> is a file name pattern, typically containing
wildcard characters (as in shell file name patterns). The result of
<CODE>wildcard</CODE> is a space-separated list of the names of existing files
that match the pattern.
See section <A HREF="make_4.html#SEC22">Using Wildcard Characters in File Names</A>.
</DL>
<H2><A NAME="SEC79" HREF="make_toc.html#TOC79">The <CODE>foreach</CODE> Function</A></H2>
<P>
<A NAME="IDX555"></A>
<A NAME="IDX556"></A>
</P>
<P>
The <CODE>foreach</CODE> function is very different from other functions. It
causes one piece of text to be used repeatedly, each time with a different
substitution performed on it. It resembles the <CODE>for</CODE> command in the
shell <CODE>sh</CODE> and the <CODE>foreach</CODE> command in the C-shell <CODE>csh</CODE>.
</P>
<P>
The syntax of the <CODE>foreach</CODE> function is:
</P>
<PRE>
$(foreach <VAR>var</VAR>,<VAR>list</VAR>,<VAR>text</VAR>)
</PRE>
<P>
The first two arguments, <VAR>var</VAR> and <VAR>list</VAR>, are expanded before
anything else is done; note that the last argument, <VAR>text</VAR>, is
<STRONG>not</STRONG> expanded at the same time. Then for each word of the expanded
value of <VAR>list</VAR>, the variable named by the expanded value of <VAR>var</VAR>
is set to that word, and <VAR>text</VAR> is expanded. Presumably <VAR>text</VAR>
contains references to that variable, so its expansion will be different
each time.
</P>
<P>
The result is that <VAR>text</VAR> is expanded as many times as there are
whitespace-separated words in <VAR>list</VAR>. The multiple expansions of
<VAR>text</VAR> are concatenated, with spaces between them, to make the result
of <CODE>foreach</CODE>.
</P>
<P>
This simple example sets the variable <SAMP>`files'</SAMP> to the list of all files
in the directories in the list <SAMP>`dirs'</SAMP>:
</P>
<PRE>
dirs := a b c d
files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
</PRE>
<P>
Here <VAR>text</VAR> is <SAMP>`$(wildcard $(dir)/*)'</SAMP>. The first repetition
finds the value <SAMP>`a'</SAMP> for <CODE>dir</CODE>, so it produces the same result
as <SAMP>`$(wildcard a/*)'</SAMP>; the second repetition produces the result
of <SAMP>`$(wildcard b/*)'</SAMP>; and the third, that of <SAMP>`$(wildcard c/*)'</SAMP>.
</P>
<P>
This example has the same result (except for setting <SAMP>`dirs'</SAMP>) as
the following example:
</P>
<PRE>
files := $(wildcard a/* b/* c/* d/*)
</PRE>
<P>
When <VAR>text</VAR> is complicated, you can improve readability by giving it
a name, with an additional variable:
</P>
<PRE>
find_files = $(wildcard $(dir)/*)
dirs := a b c d
files := $(foreach dir,$(dirs),$(find_files))
</PRE>
<P>
Here we use the variable <CODE>find_files</CODE> this way. We use plain <SAMP>`='</SAMP>
to define a recursively-expanding variable, so that its value contains an
actual function call to be reexpanded under the control of <CODE>foreach</CODE>;
a simply-expanded variable would not do, since <CODE>wildcard</CODE> would be
called only once at the time of defining <CODE>find_files</CODE>.
</P>
<P>
The <CODE>foreach</CODE> function has no permanent effect on the variable
<VAR>var</VAR>; its value and flavor after the <CODE>foreach</CODE> function call are
the same as they were beforehand. The other values which are taken from
<VAR>list</VAR> are in effect only temporarily, during the execution of
<CODE>foreach</CODE>. The variable <VAR>var</VAR> is a simply-expanded variable
during the execution of <CODE>foreach</CODE>. If <VAR>var</VAR> was undefined
before the <CODE>foreach</CODE> function call, it is undefined after the call.
See section <A HREF="make_6.html#SEC59">The Two Flavors of Variables</A>.
</P>
<P>
You must take care when using complex variable expressions that result in
variable names because many strange things are valid variable names, but
are probably not what you intended. For example,
</P>
<PRE>
files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
</PRE>
<P>
might be useful if the value of <CODE>find_files</CODE> references the variable
whose name is <SAMP>`Esta escrito en espanol!'</SAMP> (es un nombre bastante largo,
no?), but it is more likely to be a mistake.
</P>
<H2><A NAME="SEC80" HREF="make_toc.html#TOC80">The <CODE>if</CODE> Function</A></H2>
<P>
<A NAME="IDX557"></A>
<A NAME="IDX558"></A>
</P>
<P>
The <CODE>if</CODE> function provides support for conditional expansion in a
functional context (as opposed to the GNU <CODE>make</CODE> makefile
conditionals such as <CODE>ifeq</CODE> (see section <A HREF="make_7.html#SEC73">Syntax of Conditionals</A>).
</P>
<P>
An <CODE>if</CODE> function call can contain either two or three arguments:
</P>
<PRE>
$(if <VAR>condition</VAR>,<VAR>then-part</VAR>[,<VAR>else-part</VAR>])
</PRE>
<P>
The first argument, <VAR>condition</VAR>, first has all preceding and
trailing whitespace stripped, then is expanded. If it expands to any
non-empty string, then the condition is considered to be true. If it
expands to an empty string, the condition is considered to be false.
</P>
<P>
If the condition is true then the second argument, <VAR>then-part</VAR>, is
evaluated and this is used as the result of the evaluation of the entire
<CODE>if</CODE> function.
</P>
<P>
If the condition is false then the third argument, <VAR>else-part</VAR>, is
evaluated and this is the result of the <CODE>if</CODE> function. If there is
no third argument, the <CODE>if</CODE> function evaluates to nothing (the
empty string).
</P>
<P>
Note that only one of the <VAR>then-part</VAR> or the <VAR>else-part</VAR> will be
evaluated, never both. Thus, either can contain side-effects (such as
<CODE>shell</CODE> function calls, etc.)
</P>
<H2><A NAME="SEC81" HREF="make_toc.html#TOC81">The <CODE>call</CODE> Function</A></H2>
<P>
<A NAME="IDX559"></A>
<A NAME="IDX560"></A>
<A NAME="IDX561"></A>
</P>
<P>
The <CODE>call</CODE> function is unique in that it can be used to create new
parameterized functions. You can write a complex expression as the
value of a variable, then use <CODE>call</CODE> to expand it with different
values.
</P>
<P>
The syntax of the <CODE>call</CODE> function is:
</P>
<PRE>
$(call <VAR>variable</VAR>,<VAR>param</VAR>,<VAR>param</VAR>,...)
</PRE>
<P>
When <CODE>make</CODE> expands this function, it assigns each <VAR>param</VAR> to
temporary variables <CODE>$(1)</CODE>, <CODE>$(2)</CODE>, etc. The variable
<CODE>$(0)</CODE> will contain <VAR>variable</VAR>. There is no maximum number of
parameter arguments. There is no minimum, either, but it doesn't make
sense to use <CODE>call</CODE> with no parameters.
</P>
<P>
Then <VAR>variable</VAR> is expanded as a <CODE>make</CODE> variable in the context
of these temporary assignments. Thus, any reference to <CODE>$(1)</CODE> in
the value of <VAR>variable</VAR> will resolve to the first <VAR>param</VAR> in the
invocation of <CODE>call</CODE>.
</P>
<P>
Note that <VAR>variable</VAR> is the <EM>name</EM> of a variable, not a
<EM>reference</EM> to that variable. Therefore you would not normally use
a <SAMP>`$'</SAMP> or parentheses when writing it. (You can, however, use a
variable reference in the name if you want the name not to be a
constant.)
</P>
<P>
If <VAR>variable</VAR> is the name of a builtin function, the builtin function
is always invoked (even if a <CODE>make</CODE> variable by that name also
exists).
</P>
<P>
The <CODE>call</CODE> function expands the <VAR>param</VAR> arguments before
assigning them to temporary variables. This means that <VAR>variable</VAR>
values containing references to builtin functions that have special
expansion rules, like <CODE>foreach</CODE> or <CODE>if</CODE>, may not work as you
expect.
</P>
<P>
Some examples may make this clearer.
</P>
<P>
This macro simply reverses its arguments:
</P>
<PRE>
reverse = $(2) $(1)
foo = $(call reverse,a,b)
</PRE>
<P>
Here <VAR>foo</VAR> will contain <SAMP>`b a'</SAMP>.
</P>
<P>
This one is slightly more interesting: it defines a macro to search for
the first instance of a program in <CODE>PATH</CODE>:
</P>
<PRE>
pathsearch = $(firstword $(wildcard $(addsufix /$(1),$(subst :, ,$(PATH)))))
LS := $(call pathsearch,ls)
</PRE>
<P>
Now the variable LS contains <CODE>/bin/ls</CODE> or similar.
</P>
<P>
The <CODE>call</CODE> function can be nested. Each recursive invocation gets
its own local values for <CODE>$(1)</CODE>, etc. that mask the values of
higher-level <CODE>call</CODE>. For example, here is an implementation of a
<STRONG>map</STRONG> function:
</P>
<PRE>
map = $(foreach a,$(2),$(call $(1),$(a)))
</PRE>
<P>
Now you can <VAR>map</VAR> a function that normally takes only one argument,
such as <CODE>origin</CODE>, to multiple values in one step:
</P>
<PRE>
o = $(call map,origin,o map MAKE)
</PRE>
<P>
and end up with <VAR>o</VAR> containing something like <SAMP>`file file default'</SAMP>.
</P>
<P>
A final caution: be careful when adding whitespace to the arguments to
<CODE>call</CODE>. As with other functions, any whitespace contained in the
second and subsequent arguments is kept; this can cause strange
effects. It's generally safest to remove all extraneous whitespace when
providing parameters to <CODE>call</CODE>.
</P>
<H2><A NAME="SEC82" HREF="make_toc.html#TOC82">The <CODE>origin</CODE> Function</A></H2>
<P>
<A NAME="IDX562"></A>
<A NAME="IDX563"></A>
<A NAME="IDX564"></A>
</P>
<P>
The <CODE>origin</CODE> function is unlike most other functions in that it does
not operate on the values of variables; it tells you something <EM>about</EM>
a variable. Specifically, it tells you where it came from.
</P>
<P>
The syntax of the <CODE>origin</CODE> function is:
</P>
<PRE>
$(origin <VAR>variable</VAR>)
</PRE>
<P>
Note that <VAR>variable</VAR> is the <EM>name</EM> of a variable to inquire about;
not a <EM>reference</EM> to that variable. Therefore you would not normally
use a <SAMP>`$'</SAMP> or parentheses when writing it. (You can, however, use a
variable reference in the name if you want the name not to be a constant.)
</P>
<P>
The result of this function is a string telling you how the variable
<VAR>variable</VAR> was defined:
</P>
<DL COMPACT>
<DT><SAMP>`undefined'</SAMP>
<DD>
if <VAR>variable</VAR> was never defined.
<DT><SAMP>`default'</SAMP>
<DD>
if <VAR>variable</VAR> has a default definition, as is usual with <CODE>CC</CODE>
and so on. See section <A HREF="make_10.html#SEC96">Variables Used by Implicit Rules</A>.
Note that if you have redefined a default variable, the <CODE>origin</CODE>
function will return the origin of the later definition.
<DT><SAMP>`environment'</SAMP>
<DD>
if <VAR>variable</VAR> was defined as an environment variable and the
<SAMP>`-e'</SAMP> option is <EM>not</EM> turned on (see section <A HREF="make_9.html#SEC92">Summary of Options</A>).
<DT><SAMP>`environment override'</SAMP>
<DD>
if <VAR>variable</VAR> was defined as an environment variable and the
<SAMP>`-e'</SAMP> option <EM>is</EM> turned on (see section <A HREF="make_9.html#SEC92">Summary of Options</A>).
<DT><SAMP>`file'</SAMP>
<DD>
if <VAR>variable</VAR> was defined in a makefile.
<DT><SAMP>`command line'</SAMP>
<DD>
if <VAR>variable</VAR> was defined on the command line.
<DT><SAMP>`override'</SAMP>
<DD>
if <VAR>variable</VAR> was defined with an <CODE>override</CODE> directive in a
makefile (see section <A HREF="make_6.html#SEC66">The <CODE>override</CODE> Directive</A>).
<DT><SAMP>`automatic'</SAMP>
<DD>
if <VAR>variable</VAR> is an automatic variable defined for the
execution of the commands for each rule
(see section <A HREF="make_10.html#SEC101">Automatic Variables</A>).
</DL>
<P>
This information is primarily useful (other than for your curiosity) to
determine if you want to believe the value of a variable. For example,
suppose you have a makefile <TT>`foo'</TT> that includes another makefile
<TT>`bar'</TT>. You want a variable <CODE>bletch</CODE> to be defined in <TT>`bar'</TT>
if you run the command <SAMP>`make -f bar'</SAMP>, even if the environment contains
a definition of <CODE>bletch</CODE>. However, if <TT>`foo'</TT> defined
<CODE>bletch</CODE> before including <TT>`bar'</TT>, you do not want to override that
definition. This could be done by using an <CODE>override</CODE> directive in
<TT>`foo'</TT>, giving that definition precedence over the later definition in
<TT>`bar'</TT>; unfortunately, the <CODE>override</CODE> directive would also
override any command line definitions. So, <TT>`bar'</TT> could
include:
</P>
<PRE>
ifdef bletch
ifeq "$(origin bletch)" "environment"
bletch = barf, gag, etc.
endif
endif
</PRE>
<P>
If <CODE>bletch</CODE> has been defined from the environment, this will redefine
it.
</P>
<P>
If you want to override a previous definition of <CODE>bletch</CODE> if it came
from the environment, even under <SAMP>`-e'</SAMP>, you could instead write:
</P>
<PRE>
ifneq "$(findstring environment,$(origin bletch))" ""
bletch = barf, gag, etc.
endif
</PRE>
<P>
Here the redefinition takes place if <SAMP>`$(origin bletch)'</SAMP> returns either
<SAMP>`environment'</SAMP> or <SAMP>`environment override'</SAMP>.
See section <A HREF="make_8.html#SEC77">Functions for String Substitution and Analysis</A>.
</P>
<H2><A NAME="SEC83" HREF="make_toc.html#TOC83">The <CODE>shell</CODE> Function</A></H2>
<P>
<A NAME="IDX565"></A>
<A NAME="IDX566"></A>
<A NAME="IDX567"></A>
<A NAME="IDX568"></A>
</P>
<P>
The <CODE>shell</CODE> function is unlike any other function except the
<CODE>wildcard</CODE> function
(see section <A HREF="make_4.html#SEC25">The Function <CODE>wildcard</CODE></A>) in that it
communicates with the world outside of <CODE>make</CODE>.
</P>
<P>
The <CODE>shell</CODE> function performs the same function that backquotes
(<SAMP>``'</SAMP>) perform in most shells: it does <STRONG>command expansion</STRONG>. This
means that it takes an argument that is a shell command and returns the
output of the command. The only processing <CODE>make</CODE> does on the result,
before substituting it into the surrounding text, is to convert each
newline or carriage-return / newline pair to a single space. It also
removes the trailing (carriage-return and) newline, if it's the last
thing in the result.
</P>
<P>
The commands run by calls to the <CODE>shell</CODE> function are run when the
function calls are expanded. In most cases, this is when the makefile is
read in. The exception is that function calls in the commands of the rules
are expanded when the commands are run, and this applies to <CODE>shell</CODE>
function calls like all others.
</P>
<P>
Here are some examples of the use of the <CODE>shell</CODE> function:
</P>
<PRE>
contents := $(shell cat foo)
</PRE>
<P>
sets <CODE>contents</CODE> to the contents of the file <TT>`foo'</TT>, with a space
(rather than a newline) separating each line.
</P>
<PRE>
files := $(shell echo *.c)
</PRE>
<P>
sets <CODE>files</CODE> to the expansion of <SAMP>`*.c'</SAMP>. Unless <CODE>make</CODE> is
using a very strange shell, this has the same result as
<SAMP>`$(wildcard *.c)'</SAMP>.
</P>
<H2><A NAME="SEC84" HREF="make_toc.html#TOC84">Functions That Control Make</A></H2>
<P>
<A NAME="IDX569"></A>
<A NAME="IDX570"></A>
</P>
<P>
These functions control the way make runs. Generally, they are used to
provide information to the user of the makefile or to cause make to stop
if some sort of environmental error is detected.
</P>
<DL COMPACT>
<DT><CODE>$(error <VAR>text</VAR>...)</CODE>
<DD>
<A NAME="IDX571"></A>
<A NAME="IDX572"></A>
<A NAME="IDX573"></A>
Generates a fatal error where the message is <VAR>text</VAR>. Note that the
error is generated whenever this function is evaluated. So, if you put
it inside a command script or on the right side of a recursive variable
assignment, it won't be evaluated until later. The <VAR>text</VAR> will be
expanded before the error is generated.
For example,
<PRE>
ifdef ERROR1
$(error error is $(ERROR1))
endif
</PRE>
will generate a fatal error during the read of the makefile if the
<CODE>make</CODE> variable <CODE>ERROR1</CODE> is defined. Or,
<PRE>
ERR = $(error found an error!)
.PHONY: err
err: ; $(ERR)
</PRE>
will generate a fatal error while <CODE>make</CODE> is running, if the
<CODE>err</CODE> target is invoked.
<DT><CODE>$(warning <VAR>text</VAR>...)</CODE>
<DD>
<A NAME="IDX574"></A>
<A NAME="IDX575"></A>
<A NAME="IDX576"></A>
This function works similarly to the <CODE>error</CODE> function, above,
except that <CODE>make</CODE> doesn't exit. Instead, <VAR>text</VAR> is expanded
and the resulting message is displayed, but processing of the makefile
continues.
The result of the expansion of this function is the empty string.
</DL>
<P><HR><P>
<p>Go to the <A HREF="make_1.html">first</A>, <A HREF="make_7.html">previous</A>, <A HREF="make_9.html">next</A>, <A HREF="make_19.html">last</A> section, <A HREF="make_toc.html">table of contents</A>.
</BODY>
</HTML>