Chapter 8: Functions for Transforming Text 83
8 Functions for Transforming Text
Functions allow you to do text processing in the makefile to compute the files to operate
on or the commands to use in recipes. You use a function in a function call, where you
give the name of the function and some text (the arguments) 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.
8.1 Function Call Syntax
A function call resembles a variable reference. It can appear anywhere a variable reference
can appear, and it is expanded using the same rules as variable references. A function call
looks like this:
$(function arguments)
or like this:
${function arguments}
Here function is a function name; one of a short list of names that are part of make. You
can also essentially create your own functions by using the call built-in function.
The arguments 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 ‘$(subst a,b,$(x))’,
not ‘$(subst a,b,${x})’. This is because it is clearer, and because only one type of delim-
iter is matched to find the end of the reference.
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.
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
comma and space whose values are isolated comma and space characters, then substitute
these variables where such characters are wanted, like this:
comma:= ,
empty:=
space:= $(empty) $(empty)
foo:= a b c
bar:= $(subst $(space),$(comma),$(foo))
# bar is now ‘a,b,c’.
Here the subst function replaces each space with a comma, through the value of foo, and
substitutes the result.
Comentários a estes Manuais