summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/eval-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/eval-function.html
new repository
Diffstat (limited to 'devdocs/gnu_make/eval-function.html')
-rw-r--r--devdocs/gnu_make/eval-function.html32
1 files changed, 32 insertions, 0 deletions
diff --git a/devdocs/gnu_make/eval-function.html b/devdocs/gnu_make/eval-function.html
new file mode 100644
index 00000000..a826411f
--- /dev/null
+++ b/devdocs/gnu_make/eval-function.html
@@ -0,0 +1,32 @@
+ <h1 class="section">The eval Function</h1> <p>The <code>eval</code> function is very special: it allows you to define new makefile constructs that are not constant; which are the result of evaluating other variables and functions. The argument to the <code>eval</code> function is expanded, then the results of that expansion are parsed as makefile syntax. The expanded results can define new <code>make</code> variables, targets, implicit or explicit rules, etc. </p> <p>The result of the <code>eval</code> function is always the empty string; thus, it can be placed virtually anywhere in a makefile without causing syntax errors. </p> <p>It’s important to realize that the <code>eval</code> argument is expanded <em>twice</em>; first by the <code>eval</code> function, then the results of that expansion are expanded again when they are parsed as makefile syntax. This means you may need to provide extra levels of escaping for “$” characters when using <code>eval</code>. The <code>value</code> function (see <a href="value-function">Value Function</a>) can sometimes be useful in these situations, to circumvent unwanted expansions. </p> <p>Here is an example of how <code>eval</code> can be used; this example combines a number of concepts and other functions. Although it might seem overly complex to use <code>eval</code> in this example, rather than just writing out the rules, consider two things: first, the template definition (in <code>PROGRAM_template</code>) could need to be much more complex than it is here; and second, you might put the complex, “generic” part of this example into another makefile, then include it in all the individual makefiles. Now your individual makefiles are quite straightforward. </p> <div class="example"> <pre class="example">PROGRAMS = server client
+
+server_OBJS = server.o server_priv.o server_access.o
+server_LIBS = priv protocol
+
+client_OBJS = client.o client_api.o client_mem.o
+client_LIBS = protocol
+
+# Everything after this is generic
+
+.PHONY: all
+all: $(PROGRAMS)
+
+define PROGRAM_template =
+ $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
+ ALL_OBJS += $$($(1)_OBJS)
+endef
+
+$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
+
+$(PROGRAMS):
+ $(LINK.o) $^ $(LDLIBS) -o $@
+
+clean:
+ rm -f $(ALL_OBJS) $(PROGRAMS)
+</pre>
+</div><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/Eval-Function.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Eval-Function.html</a>
+ </p>
+</div>