## wildcard
The `wildcard` function is used to match a specified pattern and return a list of file names that match the pattern. It is particularly useful for including all files with a specific extension in a directory or when the exact file names are not known in advance.
Here's the syntax for the `wildcard` function:
```cmake
$(wildcard PATTERN)
```
For example, to get all C source files in a directory, you can use:
```cmake
SRC = $(wildcard *.c)
```
In the Makefile I provided earlier, the `wildcard` function is used to find all Dhall files in the `SRC_DIR`:
```cmake
DHALL_FILES = $(wildcard $(SRC_DIR)/*.dhall)
```
This will return a list of all Dhall files in the `./charters/` directory.
## patsubst
The `patsubst` function is used to replace text in a list of words that match a specified pattern. It is useful for transforming a list of file names from one form to another, such as converting a list of source files to a list of corresponding object files or output files.
Here's the syntax for the `patsubst` function:
```cmake
$(patsubst PATTERN,REPLACEMENT,TEXT)
```
For example, to convert a list of C source files to a list of object files, you can use:
```cmake
OBJ = $(patsubst %.c,%.o,$(SRC))
```
In the Makefile I provided earlier, the `patsubst` function is used to generate the corresponding JSON file paths in the `OUT_DIR` from the Dhall files in the `SRC_DIR`:
```cmake
JSON_FILES = $(patsubst $(SRC_DIR)/%.dhall,$(OUT_DIR)/%.json,$(DHALL_FILES))
```
This will take the list of Dhall files found with the `wildcard` function, and for each Dhall file, replace the `$(SRC_DIR)/%.dhall` pattern with `$(OUT_DIR)/%.json`. As a result, you get a list of JSON files in the `./out/dashboards/` directory corresponding to the Dhall files in the `./charters/` directory.
Certainly! Here are succinct examples for each built-in function, with the function name as an H2 heading:
## subst
Replace all occurrences of 'old' with 'new' in the text 'old-text-old'.
```make
TEXT = old-text-old
NEW_TEXT = $(subst old,new,$(TEXT))
```
The value of `NEW_TEXT` will be 'new-text-new'.
## addprefix
Add the prefix 'lib' to each word in the list 'a b c'.
```make
WORDS = a b c
PREFIXED_WORDS = $(addprefix lib,$(WORDS))
```
The value of `PREFIXED_WORDS` will be 'liba libb libc'.
## addsuffix
Add the suffix '.o' to each word in the list 'a b c'.
```make
WORDS = a b c
SUFFIXED_WORDS = $(addsuffix .o,$(WORDS))
```
The value of `SUFFIXED_WORDS` will be 'a.o b.o c.o'.
## filter
Select words in the list 'a b c d e' that match the pattern '?b*'.
```make
WORDS = a b c d e
FILTERED_WORDS = $(filter ?b*,$(WORDS))
```
The value of `FILTERED_WORDS` will be 'b'.
## filter-out
Remove words in the list 'a b c d e' that match the pattern '?b*'.
```make
WORDS = a b c d e
FILTERED_WORDS = $(filter-out ?b*,$(WORDS))
```
The value of `FILTERED_WORDS` will be 'a c d e'.
## join
Join two parallel lists 'a b c' and '1 2 3'.
```make
LIST1 = a b c
LIST2 = 1 2 3
JOINED = $(join $(LIST1),$(LIST2))
```
The value of `JOINED` will be 'a1 b2 c3'.
## word
Return the 2nd word in the list 'a b c d e'.
```make
WORDS = a b c d e
SECOND_WORD = $(word 2,$(WORDS))
```
The value of `SECOND_WORD` will be 'b'.
## words
Count the number of words in the list 'a b c d e'.
```make
WORDS = a b c d e
WORD_COUNT = $(words $(WORDS))
```
The value of `WORD_COUNT` will be '5'.
## sort
Sort the list of words 'e d c b a' and remove duplicates.
```make
WORDS = e d c b a
SORTED_WORDS = $(sort $(WORDS))
```
The value of `SORTED_WORDS` will be 'a b c d e'.
## dir
Extract the directory part of each file name in the list 'dir1/file1 dir2/file2 dir3/file3'.
```make
NAMES = dir1/file1 dir2/file2 dir3/file3
DIRS = $(dir $(NAMES))
```
The value of `DIRS` will be 'dir1/ dir2/ dir3/'.
## notdir
Extract the non-directory part of each file name in the list 'dir1/file1 dir2/file2 dir3/file3'.
```make
NAMES = dir1/file1 dir2/file2 dir3/file3
FILES = $(notdir $(NAMES))
```
The value of `FILES` will be 'file1 file2 file3'.
## suffix
Extract the suffix (file extension) of each file name in the list 'file1.txt file2.c file3.h'.
```make
NAMES = file1.txt file2.c file3.h
SUFFIXES = $(suffix $(NAMES))
```
The value of `SUFFIXES` will be '.txt .c .h'.
## basename
Extract the base name (file name without the suffix) of each file name in the list 'file1.txt file2.c file3.h'.
```make
NAMES = file1.txt file2.c file3.h
BASENAMES = $(basename $(NAMES))
```
The value of `BASENAMES` will be 'file1 file2 file3'.
## foreach
Print the square of each number in the list '1 2 3'.
```make
NUMBERS = 1 2 3
SQUARES = $(foreach num,$(NUMBERS),$(shell echo $(($(num) * $(num)))))
```
The value of `SQUARES` will be '1 4 9'.
## call
Define and invoke a user-defined function to multiply two numbers.
```make
multiply = $(shell echo $(($1 * $2)))
RESULT = $(call multiply,3,4)
```
The value of `RESULT` will be '12'.
## shell
Execute a shell command to list all files in the current directory.
```make
FILES = $(shell ls)
```
The value of `FILES` will be the list of files in the current directory.
## if
Evaluate a conditional expression to check if a variable is set.
```make
VAR ?=
RESULT = $(if $(VAR),The variable is set.,The variable is not set.)
```
The value of `RESULT` will be 'The variable is not set.' if `VAR` is not set.
## or
Return the first non-empty argument, or an empty string if all arguments are empty.
```make
A ?=
B = value
RESULT = $(or $(A),$(B))
```
The value of `RESULT` will be 'value'.
## and
Return the first empty argument, or the last argument if all arguments are non-empty.
```make
A = value1
B = value2
RESULT = $(and $(A),$(B))
```
The value of `RESULT` will be 'value2'.