add_if_exists = $(foreach d,$(1),$(if $(wildcard $(srctree)/$(d)),$(d) ,)) the usage of wildcard and foreach
时间: 2023-10-09 13:10:10 浏览: 95
`wildcard` is a function in GNU Make that allows you to perform wildcard pattern matching on file names. It takes a pattern argument and returns a space-separated list of file names that match the pattern. For example, `$(wildcard *.c)` would return a list of all the `.c` files in the current directory.
`foreach` is another function in GNU Make that allows you to iterate over a list of values and perform some action for each value. It takes two arguments: a variable name and a list of values. For each value in the list, the variable is set to that value and the specified action is performed.
In the `add_if_exists` example you provided, `wildcard` is used to check if certain directories exist in the source tree. `foreach` is then used to iterate over a list of directories and call `wildcard` on each directory. If the directory exists, it is added to the return list. If it does not exist, it is skipped.
Here is a breakdown of the `add_if_exists` code:
```
add_if_exists = $(foreach d,$(1),$(if $(wildcard $(srctree)/$(d)),$(d),))
```
- `add_if_exists` is a variable that contains the function definition.
- `$(1)` is a reference to the first argument passed to the function.
- `foreach d,$(1),` sets up a loop that iterates over the values in the first argument and sets the variable `d` to each value in turn.
- `$(wildcard $(srctree)/$(d))` performs wildcard pattern matching on the directory `$(srctree)/$(d)` to see if it exists.
- `$(if $(wildcard $(srctree)/$(d)),$(d),)` is a conditional statement that returns `$(d)` if the directory exists, or an empty string if it doesn't.
- The overall effect is to return a space-separated list of directories that exist in the source tree.
阅读全文