For example, $(subst ee,EE,feet on the street) substitutes the string fEEt on the strEEt.
$(patsubst pattern, replacement, text)
% 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 compares 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 are a simpler way to get the effect of the patsubst function (see Substitution references).
To get the list of corresponding
source files, you could simply write:
instead of using the general form:
$(strip string)
The function, strip, can be very useful when used in conjunction with conditionals.
When comparing something with an empty string using ifeq or ifneq, you usually want a string of just whitespace to match the empty string (see Conditional parts of makefiles).
Thus, the following may fail
to have the desired results.
Replacing the variable reference, $(needs_made), with the function call $(strip $(needs_made)) in the ifneq directive would make it more robust.
$(findstring find,in)
$(findstring a,a b c)
$(findstring a,b c)
$(filter pattern ...,text)
The filter
function can be used to separate out different types of strings (such as
file names) in a variable. Consider the following, for example.
$(filter-out pattern ...,text)
Given the previous lines, the following then generates a list which contains all the object files not in mains.
$(filter-out $(mains),$(objects))
$(sort list)
Thus, $(sort foo bar lose) returns the value, bar foo lose.
Incidentally, since sort removes duplicate words, you can use it for this purpose even if you dont care about the sort order.
The following is a realistic example of the use of subst and patsubst.
Suppose that a makefile uses
the VPATH
variable to specify a list of directories that make should search for dependency
files (see VPATH:
search path for all dependencies).
The following example shows how to tell the C compiler to search for header
files in the same list of directories. The value of VPATH
is a list of directories separated by colons, such as src:../headers.
First, the subst
function is used to change the colons to spaces:
This produces src
../headers. Then,
patsubst
is used to turn each directory name into a -I
flag. These can be added to the value of the variable CFLAGS
which is passed automatically to the C compiler, as in the following.
The effect is to append the text, -Isrc -I../headers, to the previously given value of CFLAGS. The override directive is used so that the new value is assigned even if the previous value of CFLAGS was specified with a command argument (see The override directive).