summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/call-function.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gnu_make/call-function.html
new repository
Diffstat (limited to 'devdocs/gnu_make/call-function.html')
-rw-r--r--devdocs/gnu_make/call-function.html20
1 files changed, 20 insertions, 0 deletions
diff --git a/devdocs/gnu_make/call-function.html b/devdocs/gnu_make/call-function.html
new file mode 100644
index 00000000..0ca79134
--- /dev/null
+++ b/devdocs/gnu_make/call-function.html
@@ -0,0 +1,20 @@
+ <h1 class="section">The call Function</h1> <p>The <code>call</code> function is unique in that it can be used to create new parameterized functions. You can write a complex expression as the value of a variable, then use <code>call</code> to expand it with different values. </p> <p>The syntax of the <code>call</code> function is: </p> <div class="example"> <pre class="example">$(call <var>variable</var>,<var>param</var>,<var>param</var>,…)
+</pre>
+</div> <p>When <code>make</code> expands this function, it assigns each <var>param</var> to temporary variables <code>$(1)</code>, <code>$(2)</code>, etc. The variable <code>$(0)</code> will contain <var>variable</var>. There is no maximum number of parameter arguments. There is no minimum, either, but it doesn’t make sense to use <code>call</code> with no parameters. </p> <p>Then <var>variable</var> is expanded as a <code>make</code> variable in the context of these temporary assignments. Thus, any reference to <code>$(1)</code> in the value of <var>variable</var> will resolve to the first <var>param</var> in the invocation of <code>call</code>. </p> <p>Note that <var>variable</var> is the <em>name</em> of a variable, not a <em>reference</em> to that variable. Therefore you would not normally use a ‘<samp>$</samp>’ or parentheses when writing it. (You can, however, use a variable reference in the name if you want the name not to be a constant.) </p> <p>If <var>variable</var> is the name of a built-in function, the built-in function is always invoked (even if a <code>make</code> variable by that name also exists). </p> <p>The <code>call</code> function expands the <var>param</var> arguments before assigning them to temporary variables. This means that <var>variable</var> values containing references to built-in functions that have special expansion rules, like <code>foreach</code> or <code>if</code>, may not work as you expect. </p> <p>Some examples may make this clearer. </p> <p>This macro simply reverses its arguments: </p> <div class="example"> <pre class="example">reverse = $(2) $(1)
+
+foo = $(call reverse,a,b)
+</pre>
+</div> <p>Here <code>foo</code> will contain ‘<samp>b a</samp>’. </p> <p>This one is slightly more interesting: it defines a macro to search for the first instance of a program in <code>PATH</code>: </p> <div class="example"> <pre class="example">pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
+
+LS := $(call pathsearch,ls)
+</pre>
+</div> <p>Now the variable <code>LS</code> contains <code>/bin/ls</code> or similar. </p> <p>The <code>call</code> function can be nested. Each recursive invocation gets its own local values for <code>$(1)</code>, etc. that mask the values of higher-level <code>call</code>. For example, here is an implementation of a <em>map</em> function: </p> <div class="example"> <pre class="example">map = $(foreach a,$(2),$(call $(1),$(a)))
+</pre>
+</div> <p>Now you can <code>map</code> a function that normally takes only one argument, such as <code>origin</code>, to multiple values in one step: </p> <div class="example"> <pre class="example">o = $(call map,origin,o map MAKE)
+</pre>
+</div> <p>and end up with <code>o</code> containing something like ‘<samp>file file default</samp>’. </p> <p>A final caution: be careful when adding whitespace to the arguments to <code>call</code>. As with other functions, any whitespace contained in the second and subsequent arguments is kept; this can cause strange effects. It’s generally safest to remove all extraneous whitespace when providing parameters to <code>call</code>. </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/Call-Function.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Call-Function.html</a>
+ </p>
+</div>