make: Wildcard Function

1 
1 4.4.3 The Function 'wildcard'
1 -----------------------------
1 
1 Wildcard expansion happens automatically in rules.  But wildcard
1 expansion does not normally take place when a variable is set, or inside
1 the arguments of a function.  If you want to do wildcard expansion in
1 such places, you need to use the 'wildcard' function, like this:
1 
1      $(wildcard PATTERN...)
1 
1 This string, used anywhere in a makefile, is replaced by a
1 space-separated list of names of existing files that match one of the
1 given file name patterns.  If no existing file name matches a pattern,
1 then that pattern is omitted from the output of the 'wildcard' function.
1 Note that this is different from how unmatched wildcards behave in
11 rules, where they are used verbatim rather than ignored (⇒Wildcard
 Pitfall).
1 
1    One use of the 'wildcard' function is to get a list of all the C
1 source files in a directory, like this:
1 
1      $(wildcard *.c)
1 
1    We can change the list of C source files into a list of object files
1 by replacing the '.c' suffix with '.o' in the result, like this:
1 
1      $(patsubst %.c,%.o,$(wildcard *.c))
1 
11 (Here we have used another function, 'patsubst'.  ⇒Functions for
 String Substitution and Analysis Text Functions.)
1 
1    Thus, a makefile to compile all C source files in the directory and
1 then link them together could be written as follows:
1 
1      objects := $(patsubst %.c,%.o,$(wildcard *.c))
1 
1      foo : $(objects)
1              cc -o foo $(objects)
1 
1 (This takes advantage of the implicit rule for compiling C programs, so
11 there is no need to write explicit rules for compiling the files.  ⇒
 The Two Flavors of Variables Flavors, for an explanation of ':=', which
1 is a variant of '='.)
1