简体   繁体   English

模板调用中变量的 makefile 评估

[英]makefile evaluation of variables in template call

Consider this simple makefile that does very little:考虑这个简单的makefile,它的作用很小:

define template
copy      := $(1)
$(info $(1) $(copy))

.PHONY    : rule_$(1)
rule_$(1) : VAR := $(1)
rule_$(1) :
    @echo $@ $(VAR)

all       : rule_$(1)
endef

$(foreach mode,DEBUG OPT, \
    $(eval $(call template,$(mode))))

.PHONY : all 
.DEFAULT_GOAL := all 

I thought that this would create two rules, rule_DEBUG and rule_OPT , whose recipes would echo their names and arg.我认为这会创建两个规则, rule_DEBUGrule_OPT ,它们的配方会与它们的名称和参数相呼应。 Furthermore, I thought that the info line would just print DEBUG DEBUG and then OPT OPT .此外,我认为info行只会打印DEBUG DEBUG然后OPT OPT However, I am wrong on both accounts.但是,我在这两个方面都错了。 The info lines log: info行记录:

DEBUG 
OPT DEBUG

And when I run make , I get back nothing but empty lines.当我运行make ,我只得到空行。 If I run make -p , I do see this for rule_OPT :如果我运行make -p ,我确实看到了rule_OPT

rule_OPT:
#  Phony target (prerequisite of .PHONY).
#  Implicit rule search has not been done.
#  Implicit/static pattern stem: `'
#  File does not exist.
#  File has been updated.
#  Successfully updated.
# automatic
# @ := rule_OPT
# makefile (from `makefile', line 10)
# VAR := OPT
# automatic
# % := 
# automatic
# * := 
# automatic
# + := 
# automatic
# | := 
# automatic
# < := 
# automatic
# ^ := 
# automatic
# ? := 
# variable set hash-table stats:
# Load=9/32=28%, Rehash=0, Collisions=1/12=8%
#  recipe to execute (from `makefile', line 10):
        @echo  

Looks like @ and VAR have the values I want and expect - but why doesn't it echo correctly?看起来@VAR具有我想要和期望的值 - 但为什么它没有正确echo It looks like if I have the recipe:看起来如果我有食谱:

rule_$(1) :
    @echo $$@ $$(VAR)

That echos what I want, but I am still not sure how to info the copy .这与我想要的相呼应,但我仍然不确定如何为copy提供info Why?为什么?

The $(info) output issue is what I explained in the comments on this answer of mine. $(info)输出问题是我在我的这个答案的评论中所解释的。 You have an evaluative order problem.你有一个评估顺序问题。 The assignments in the define aren't happening until the define expansion is processed by eval so your assigned variable value isn't visible until later.在定义扩展由eval处理之前,定义中的赋值不会发生,因此您分配的变量值直到稍后才可见。 You need extra eval 's or post- eval expansion to do that.你需要额外的eval或 post- eval扩展来做到这一点。

Either任何一个

$(eval copy      := $(1))

in the template or splitting up the contents into multiple defines.在模板中或将内容拆分为多个定义。

The problem with @echo $(VAR) is the same thing. @echo $(VAR)是一样的。 That is being expanded by the eval of the define 's expansion and $(VAR) is unset at that point so you need $$(VAR) in the template to have a literal $(VAR) in the expanded recipe so that it works correctly and recipe execution time.这是由define的扩展的eval扩展的,并且此时$(VAR)未设置,因此您需要模板中的$$(VAR)在扩展配方中有文字$(VAR)以便它工作正确和配方执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM