Next: Running, Previous: Conditionals, Up: Top [Contents][Index]
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.
| • Syntax of Functions | How to write a function call. | |
| • Text Functions | General-purpose text manipulation functions. | |
| • File Name Functions | Functions for manipulating file names. | |
| • Conditional Functions | Functions that implement conditions. | |
| • Foreach Function | Repeat some text with controlled variation. | |
| • File Function | Write text to a file. | |
| • Call Function | Expand a user-defined function. | |
| • Value Function | Return the un-expanded value of a variable. | |
| • Eval Function | Evaluate the arguments as makefile syntax. | |
| • Origin Function | Find where a variable got its value. | |
| • Flavor Function | Find out the flavor of a variable. | |
| • Make Control Functions | Functions that control how make runs. | |
| • Shell Function | Substitute the output of a shell command. | |
| • Guile Function | Use GNU Guile embedded scripting language. |
Next: Text Functions, Previous: Functions, Up: Functions [Contents][Index]
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 delimiter 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.
Next: File Name Functions, Previous: Syntax of Functions, Up: Functions [Contents][Index]
Here are some functions that operate on strings:
$(subst from,to,text)Performs a textual replacement on the text text: each occurrence of from is replaced by to. The result is substituted for the function call. For example,
$(subst ee,EE,feet on the street)
produces the value ‘fEEt on the strEEt’.
$(patsubst pattern,replacement,text)Finds whitespace-separated words in text that match pattern and replaces them with replacement. Here pattern may contain a ‘%’ which acts as a wildcard, matching any number of any characters within a word. If replacement also contains a ‘%’, the ‘%’ is replaced by the text that matched the ‘%’ in pattern. Only the first ‘%’ in the pattern and replacement is treated this way; any subsequent ‘%’ is unchanged.
‘%’ characters in patsubst function invocations can be
quoted with preceding backslashes (‘\’). Backslashes that would
otherwise quote ‘%’ characters can be quoted with more backslashes.
Backslashes that quote ‘%’ 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
‘%’ characters go unmolested. For example, the pattern
the\%weird\\%pattern\\ has ‘the%weird\’ preceding the
operative ‘%’ character, and ‘pattern\\’ following it. The
final two backslashes are left alone because they cannot affect any
‘%’ character.
Whitespace between words is folded into single space characters; leading and trailing whitespace is discarded.
For example,
$(patsubst %.c,%.o,x.c.c bar.c)
produces the value ‘x.c.o bar.o’.
Substitution references (see Substitution
References) are a simpler way to get the effect of the patsubst
function:
$(var:pattern=replacement)
is equivalent to
$(patsubst pattern,replacement,$(var))
The second shorthand simplifies one of the most common uses of
patsubst: replacing the suffix at the end of file names.
$(var:suffix=replacement)
is equivalent to
$(patsubst %suff