diff --git a/docs/index.html b/docs/index.html index af5c1d1..a185b23 100644 --- a/docs/index.html +++ b/docs/index.html @@ -397,6 +397,12 @@ +
    +
  1. The filter function +
  2. + +
+ @@ -729,7 +735,7 @@

Implicit Rules

rm -f blah*

Static Pattern Rules

-

Static pattern rules are another way to write less in a Makefile, but I'd say are more useful and a bit less "magic". Here's their syntax:

+

Static pattern rules are another way to write less in a Makefile. Here's their syntax:

targets...: target-pattern: prereq-patterns ...
    commands

The essence is that the given target is matched by the target-pattern (via a % wildcard). Whatever was matched is called the stem. The stem is then substituted into the prereq-pattern, to generate the target's prereqs.

@@ -771,7 +777,7 @@

Static Pattern Rules

rm -f *.c *.o all

Static Pattern Rules and Filter

-

While I introduce functions later on, I'll foreshadow what you can do with them. The filter function can be used in Static pattern rules to match the correct files. In this example, I made up the .raw and .result extensions.

+

While I introduce the filter function later on, it's common to use in static pattern rules, so I'll mention that here. The filter function can be used in Static pattern rules to match the correct files. In this example, I made up the .raw and .result extensions.

obj_files = foo.result bar.o lose.o
 src_files = foo.raw bar.c lose.c
 
@@ -1098,7 +1104,7 @@ 

Functions

First Functions

Functions are mainly just for text processing. Call functions with $(fn, arguments) or ${fn, arguments}. Make has a decent amount of builtin functions.

-
bar := ${subst not,totally, "I am not superman"}
+
bar := ${subst not,"totally", "I am not superman"}
 all: 
 	@echo $(bar)
 
@@ -1116,7 +1122,7 @@

First Functions

empty:= space := $(empty) $(empty) foo := a b c -bar := $(subst $(space), $(comma) , $(foo)) +bar := $(subst $(space), $(comma) , $(foo)) # Watch out! all: # Output is ", a , b , c". Notice the spaces introduced @@ -1176,6 +1182,22 @@

The shell function

shell - This calls the shell, but it replaces newlines with spaces!

all: 
 	@echo $(shell ls -la) # Very ugly because the newlines are gone!
+

The filter function

+

The filter function is used to select certain elements from a list that match a specific pattern. For example, this will select all elements in obj_files that end with .o.

+
obj_files = foo.result bar.o lose.o
+filtered_files = $(filter %.o,$(obj_files))
+
+all:
+	@echo $(filtered_files)
+

Filter can also be used in more complex ways:

+
    +
  1. Filtering multiple patterns: You can filter multiple patterns at once. For example, $(filter %.c %.h, $(files)) will select all .c and .h files from the files list.

    +
  2. +
  3. Negation: If you want to select all elements that do not match a pattern, you can use filter-out. For example, $(filter-out %.h, $(files)) will select all files that are not .h files.

    +
  4. +
  5. Nested filter: You can nest filter functions to apply multiple filters. For example, $(filter %.o, $(filter-out test%, $(objects))) will select all object files that end with .o but don't start with test.

    +
  6. +

Other Features

Include Makefiles

The include directive tells make to read one or more other makefiles. It's a line in the makefile that looks like this:

diff --git a/src/index.md b/src/index.md index 73faf90..fbecb20 100644 --- a/src/index.md +++ b/src/index.md @@ -358,7 +358,7 @@ clean: ## Static Pattern Rules -Static pattern rules are another way to write less in a Makefile, but I'd say are more useful and a bit less "magic". Here's their syntax: +Static pattern rules are another way to write less in a Makefile. Here's their syntax: ```makefile targets...: target-pattern: prereq-patterns ... commands @@ -410,7 +410,7 @@ clean: ## Static Pattern Rules and Filter -While I introduce functions later on, I'll foreshadow what you can do with them. The `filter` function can be used in Static pattern rules to match the correct files. In this example, I made up the `.raw` and `.result` extensions. +While I introduce the [filter function](#the-filter-function) later on, it's common to use in static pattern rules, so I'll mention that here. The `filter` function can be used in Static pattern rules to match the correct files. In this example, I made up the `.raw` and `.result` extensions. ```makefile obj_files = foo.result bar.o lose.o src_files = foo.raw bar.c lose.c @@ -435,6 +435,7 @@ clean: ``` + ## Pattern Rules Pattern rules are often used but quite confusing. You can look at them as two ways: - A way to define your own implicit rules @@ -838,7 +839,7 @@ endif *Functions* are mainly just for text processing. Call functions with `$(fn, arguments)` or `${fn, arguments}`. Make has a decent amount of [builtin functions](https://www.gnu.org/software/make/manual/html_node/Functions.html). ```makefile -bar := ${subst not,totally, "I am not superman"} +bar := ${subst not,"totally", "I am not superman"} all: @echo $(bar) @@ -862,7 +863,7 @@ comma := , empty:= space := $(empty) $(empty) foo := a b c -bar := $(subst $(space), $(comma) , $(foo)) +bar := $(subst $(space), $(comma) , $(foo)) # Watch out! all: # Output is ", a , b , c". Notice the spaces introduced @@ -946,6 +947,27 @@ all: @echo $(shell ls -la) # Very ugly because the newlines are gone! ``` + +## The filter function + +The `filter` function is used to select certain elements from a list that match a specific pattern. For example, this will select all elements in `obj_files` that end with `.o`. + +```makefile +obj_files = foo.result bar.o lose.o +filtered_files = $(filter %.o,$(obj_files)) + +all: + @echo $(filtered_files) +``` + +Filter can also be used in more complex ways: + +1. **Filtering multiple patterns**: You can filter multiple patterns at once. For example, `$(filter %.c %.h, $(files))` will select all `.c` and `.h` files from the files list. + +1. **Negation**: If you want to select all elements that do not match a pattern, you can use `filter-out`. For example, `$(filter-out %.h, $(files))` will select all files that are not `.h` files. + +1. **Nested filter**: You can nest filter functions to apply multiple filters. For example, `$(filter %.o, $(filter-out test%, $(objects)))` will select all object files that end with `.o` but don't start with `test`. + # Other Features ## Include Makefiles The include directive tells make to read one or more other makefiles. It's a line in the makefile that looks like this: