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. Words that do not match the pattern are kept without change in the output. 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 %suffix,%replacement,$(var))
For example, you might have a list of object files:
objects = foo.o bar.o baz.o
To get the list of corresponding source files, you could simply write:
$(objects:.o=.c)
instead of using the general form:
$(patsubst %.o,%.c,$(objects))