summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/eval-function.html
blob: a826411fd4ddb386d77a706b1ce14b9382929ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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>