您的位置:首页 > 运维架构 > Linux

Makefile中eval函数用法

2015-08-22 14:40 429 查看
函数功能:函数“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

--------------------------------

References:

* GNU make manual: http://www.gnu.org/software/make/manual/make.html#Eval-Function
* http://www.cnblogs.com/hnrainll/archive/2011/04/12/2013377.html
转载地址:http://a18945.blog.sohu.com/112037479.html

                   http://blog.sina.com.cn/s/blog_7180d4e201010umk.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  makefile linux