上例子
define func foo: @echo "at foo" endef all: foo @echo "final" $(eval $(call func, foo,abc.c))
先不整那些函数参数传递之类的幺蛾子,做一个个简单的例子,
运行 make
结果是:
at foo
final
然后再变化下:
define func $1: @echo "at foo" endef all: foo @echo "final" $(eval $(call func, foo,abc.c))
这次,传递 foo 作为 $1,得到结果相同。
基本上可以看到,eval 函数是具备一定的运行时刻改变makefile 能力。
函数功能:函数“eval”是一个比较特殊的函数。使用它我们可以在我们的Makefile中构造一个可变的规则结构关系(依赖关系链),其中可以使用其它变量和函数。函数“eval”对它的参数进行展开,展开的结果作为Makefile的一部分,make可以对展开内容进行语法解析。展开的结果可以包含一个新变量、目标、隐含规则或者是明确规则等。也就是说此函数的功能主要是:根据其参数的关系、结构,对它们进行替换展开。
返回值:函数“eval”的返回值时空,也可以说没有返回值。
函数说明:“eval”函数执行时会对它的参数进行两次展开。第一次展开过程发是由函数本身完成的,第二次是函数展开后的结果被作为Makefile内容时由make解析时展开的。明确这一点对于使用“eval”函数非常重要。在理解了函数“eval”二次展开的过程后。实际使用时,当函数的展开结果中存在引用(格式为:$(x))时,那么在函数的参数中应该使用“$$”来代替“$”。因为这一点,所以通常它的参数中会使用函数“value”来取一个变量的文本值。
"It's important to realize that the eval argument is expanded twice; first by the eval function, then the results of that expansion are expanded again when they are parsed as makefile syntax."
Take the example from GNU Make for 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)
--------------------------------
How to understand the twice replace for eval:
In foreach:
"eval $(call PROGRAM_template, server)"
1st time replacement, 'PROGRAM_template' is executed as shell:
"server: $(server_OBJS) $(server_LIBS:%=-l%)
2nd time replacement, execute as makefile:
server: $server_OBJS -l$server_LIBS