diff options
Diffstat (limited to 'devdocs/gnu_make/foreach-function.html')
| -rw-r--r-- | devdocs/gnu_make/foreach-function.html | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/devdocs/gnu_make/foreach-function.html b/devdocs/gnu_make/foreach-function.html new file mode 100644 index 00000000..2db8c9ce --- /dev/null +++ b/devdocs/gnu_make/foreach-function.html @@ -0,0 +1,19 @@ + <h1 class="section">The foreach Function</h1> <p>The <code>foreach</code> function is similar to the <code>let</code> function, but very different from other functions. It causes one piece of text to be used repeatedly, each time with a different substitution performed on it. The <code>foreach</code> function resembles the <code>for</code> command in the shell <code>sh</code> and the <code>foreach</code> command in the C-shell <code>csh</code>. </p> <p>The syntax of the <code>foreach</code> function is: </p> <div class="example"> <pre class="example">$(foreach <var>var</var>,<var>list</var>,<var>text</var>) +</pre> +</div> <p>The first two arguments, <var>var</var> and <var>list</var>, are expanded before anything else is done; note that the last argument, <var>text</var>, is <strong>not</strong> expanded at the same time. Then for each word of the expanded value of <var>list</var>, the variable named by the expanded value of <var>var</var> is set to that word, and <var>text</var> is expanded. Presumably <var>text</var> contains references to that variable, so its expansion will be different each time. </p> <p>The result is that <var>text</var> is expanded as many times as there are whitespace-separated words in <var>list</var>. The multiple expansions of <var>text</var> are concatenated, with spaces between them, to make the result of <code>foreach</code>. </p> <p>This simple example sets the variable ‘<samp>files</samp>’ to the list of all files in the directories in the list ‘<samp>dirs</samp>’: </p> <div class="example"> <pre class="example">dirs := a b c d +files := $(foreach dir,$(dirs),$(wildcard $(dir)/*)) +</pre> +</div> <p>Here <var>text</var> is ‘<samp>$(wildcard $(dir)/*)</samp>’. The first repetition finds the value ‘<samp>a</samp>’ for <code>dir</code>, so it produces the same result as ‘<samp>$(wildcard a/*)</samp>’; the second repetition produces the result of ‘<samp>$(wildcard b/*)</samp>’; and the third, that of ‘<samp>$(wildcard c/*)</samp>’. </p> <p>This example has the same result (except for setting ‘<samp>dirs</samp>’) as the following example: </p> <div class="example"> <pre class="example">files := $(wildcard a/* b/* c/* d/*) +</pre> +</div> <p>When <var>text</var> is complicated, you can improve readability by giving it a name, with an additional variable: </p> <div class="example"> <pre class="example">find_files = $(wildcard $(dir)/*) +dirs := a b c d +files := $(foreach dir,$(dirs),$(find_files)) +</pre> +</div> <p>Here we use the variable <code>find_files</code> this way. We use plain ‘<samp>=</samp>’ to define a recursively-expanding variable, so that its value contains an actual function call to be re-expanded under the control of <code>foreach</code>; a simply-expanded variable would not do, since <code>wildcard</code> would be called only once at the time of defining <code>find_files</code>. </p> <p>Like the <code>let</code> function, the <code>foreach</code> function has no permanent effect on the variable <var>var</var>; its value and flavor after the <code>foreach</code> function call are the same as they were beforehand. The other values which are taken from <var>list</var> are in effect only temporarily, during the execution of <code>foreach</code>. The variable <var>var</var> is a simply-expanded variable during the execution of <code>foreach</code>. If <var>var</var> was undefined before the <code>foreach</code> function call, it is undefined after the call. See <a href="flavors">The Two Flavors of Variables</a>. </p> <p>You must take care when using complex variable expressions that result in variable names because many strange things are valid variable names, but are probably not what you intended. For example, </p> <div class="example"> <pre class="example">files := $(foreach Esta-escrito-en-espanol!,b c ch,$(find_files)) +</pre> +</div> <p>might be useful if the value of <code>find_files</code> references the variable whose name is ‘<samp>Esta-escrito-en-espanol!</samp>’ (es un nombre bastante largo, no?), but it is more likely to be a mistake. </p><div class="_attribution"> + <p class="_attribution-p"> + Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br> + <a href="https://www.gnu.org/software/make/manual/html_node/Foreach-Function.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Foreach-Function.html</a> + </p> +</div> |
