Skip to content

Commit

Permalink
Add the filter function, thank you @SkillfulElectro
Browse files Browse the repository at this point in the history
  • Loading branch information
chaselambda committed Jun 19, 2024
1 parent 8ccad30 commit bf796dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
30 changes: 26 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@

</ol>

<ol>
<li><a class="toc-item" href="#the-filter-function">The filter function</a>
</li>

</ol>



</ol>
Expand Down Expand Up @@ -729,7 +735,7 @@ <h2 id="implicit-rules">Implicit Rules</h2>
rm -f blah*</code></pre>
<h2 id="static-pattern-rules">Static Pattern Rules</h2>
<!-- (Section 4.10) -->
<p>Static pattern rules are another way to write less in a Makefile, but I&#39;d say are more useful and a bit less &quot;magic&quot;. Here&#39;s their syntax:</p>
<p>Static pattern rules are another way to write less in a Makefile. Here&#39;s their syntax:</p>
<pre><code class="hljs makefile"><span class="hljs-section">targets...: target-pattern: prereq-patterns ...</span>
commands</code></pre>
<p>The essence is that the given <code>target</code> is matched by the <code>target-pattern</code> (via a <code>%</code> wildcard). Whatever was matched is called the <em>stem</em>. The stem is then substituted into the <code>prereq-pattern</code>, to generate the target&#39;s prereqs.</p>
Expand Down Expand Up @@ -771,7 +777,7 @@ <h2 id="static-pattern-rules">Static Pattern Rules</h2>
rm -f *.c *.o all</code></pre>
<h2 id="static-pattern-rules-and-filter">Static Pattern Rules and Filter</h2>
<!-- (Section 4.10) -->
<p>While I introduce functions later on, I&#39;ll foreshadow what you can do with them. The <code>filter</code> function can be used in Static pattern rules to match the correct files. In this example, I made up the <code>.raw</code> and <code>.result</code> extensions.</p>
<p>While I introduce the <a href="#the-filter-function">filter function</a> later on, it&#39;s common to use in static pattern rules, so I&#39;ll mention that here. The <code>filter</code> function can be used in Static pattern rules to match the correct files. In this example, I made up the <code>.raw</code> and <code>.result</code> extensions.</p>
<pre><code class="hljs makefile">obj_files = foo.result bar.o lose.o
src_files = foo.raw bar.c lose.c

Expand Down Expand Up @@ -1098,7 +1104,7 @@ <h1 id="functions">Functions</h1>
<h2 id="first-functions">First Functions</h2>
<!-- (Section 8.1) -->
<p><em>Functions</em> are mainly just for text processing. Call functions with <code>$(fn, arguments)</code> or <code>${fn, arguments}</code>. Make has a decent amount of <a href="https://www.gnu.org/software/make/manual/html_node/Functions.html">builtin functions</a>.</p>
<pre><code class="hljs makefile">bar := ${subst not,totally, <span class="hljs-string">"I am not superman"</span>}
<pre><code class="hljs makefile">bar := ${subst not,<span class="hljs-string">"totally"</span>, <span class="hljs-string">"I am not superman"</span>}
<span class="hljs-section">all: </span>
@echo <span class="hljs-variable">$(bar)</span>
</code></pre>
Expand All @@ -1116,7 +1122,7 @@ <h2 id="first-functions">First Functions</h2>
empty:=
space := <span class="hljs-variable">$(empty)</span> <span class="hljs-variable">$(empty)</span>
foo := a b c
bar := <span class="hljs-variable">$(<span class="hljs-built_in">subst</span> <span class="hljs-variable">$(space)</span>, <span class="hljs-variable">$(comma)</span> , <span class="hljs-variable">$(foo)</span>)</span>
bar := <span class="hljs-variable">$(<span class="hljs-built_in">subst</span> <span class="hljs-variable">$(space)</span>, <span class="hljs-variable">$(comma)</span> , <span class="hljs-variable">$(foo)</span>)</span> <span class="hljs-comment"># Watch out!</span>

<span class="hljs-section">all: </span>
<span class="hljs-comment"># Output is ", a , b , c". Notice the spaces introduced</span>
Expand Down Expand Up @@ -1176,6 +1182,22 @@ <h2 id="the-shell-function">The shell function</h2>
<p>shell - This calls the shell, but it replaces newlines with spaces!</p>
<pre><code class="hljs makefile"><span class="hljs-section">all: </span>
@echo <span class="hljs-variable">$(<span class="hljs-built_in">shell</span> ls -la)</span> <span class="hljs-comment"># Very ugly because the newlines are gone!</span></code></pre>
<h2 id="the-filter-function">The filter function</h2>
<p>The <code>filter</code> function is used to select certain elements from a list that match a specific pattern. For example, this will select all elements in <code>obj_files</code> that end with <code>.o</code>.</p>
<pre><code class="hljs makefile">obj_files = foo.result bar.o lose.o
filtered_files = <span class="hljs-variable">$(<span class="hljs-built_in">filter</span> %.o,<span class="hljs-variable">$(obj_files)</span>)</span>

<span class="hljs-section">all:</span>
@echo <span class="hljs-variable">$(filtered_files)</span></code></pre>
<p>Filter can also be used in more complex ways:</p>
<ol>
<li><p><strong>Filtering multiple patterns</strong>: You can filter multiple patterns at once. For example, <code>$(filter %.c %.h, $(files))</code> will select all <code>.c</code> and <code>.h</code> files from the files list.</p>
</li>
<li><p><strong>Negation</strong>: If you want to select all elements that do not match a pattern, you can use <code>filter-out</code>. For example, <code>$(filter-out %.h, $(files))</code> will select all files that are not <code>.h</code> files.</p>
</li>
<li><p><strong>Nested filter</strong>: You can nest filter functions to apply multiple filters. For example, <code>$(filter %.o, $(filter-out test%, $(objects)))</code> will select all object files that end with <code>.o</code> but don&#39;t start with <code>test</code>.</p>
</li>
</ol>
<h1 id="other-features">Other Features</h1>
<h2 id="include-makefiles">Include Makefiles</h2>
<p>The include directive tells make to read one or more other makefiles. It&#39;s a line in the makefile that looks like this:</p>
Expand Down
30 changes: 26 additions & 4 deletions src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ clean:

## Static Pattern Rules
<!-- (Section 4.10) -->
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
Expand Down Expand Up @@ -410,7 +410,7 @@ clean:

## Static Pattern Rules and Filter
<!-- (Section 4.10) -->
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
Expand All @@ -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
Expand Down Expand Up @@ -838,7 +839,7 @@ endif
<!-- (Section 8.1) -->
*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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit bf796dc

Please sign in to comment.