-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEval-Function.html
More file actions
78 lines (62 loc) · 3.04 KB
/
Eval-Function.html
File metadata and controls
78 lines (62 loc) · 3.04 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<html lang="en">
<head>
<title>GNU `make'</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU `make'">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
</head>
<body>
<div class="node">
<p>
Node:<a name="Eval%20Function">Eval Function</a>,
Next:<a rel="next" accesskey="n" href="Origin-Function.html#Origin%20Function">Origin Function</a>,
Previous:<a rel="previous" accesskey="p" href="Value-Function.html#Value%20Function">Value Function</a>,
Up:<a rel="up" accesskey="u" href="Functions.html#Functions">Functions</a>
<hr><br>
</div>
<h3 class="section">The <code>eval</code> Function</h3>
<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>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>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.html#Value%20Function">Value Function</a>) can sometimes be useful in these
situations, to circumvent unwanted expansions.
<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.
<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)_OBJ) $$($(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>
</body></html>