makefile评估模板调用中的变量

时间:2015-06-22 15:39:23

标签: c++ makefile

考虑一下这个做得很少的简单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 

我认为这会创建两个规则,rule_DEBUGrule_OPT,其配方会回显他们的名字和arg。此外,我认为info行只会打印DEBUG DEBUG,然后打印OPT OPT。但是,我在两个帐户上都错了。 info行日志:

DEBUG 
OPT DEBUG

当我跑make时,我只回到空行。如果我运行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  

看起来@VAR具有我想要和期望的值 - 但为什么echo没有正确?看起来好像我有食谱:

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

那回声我想要的,但我仍然不确定如何info copy。为什么呢?

1 个答案:

答案 0 :(得分:1)

$(info)输出问题是我在this answer的评论中解释的。您有一个评估性订单问题。在eval处理定义扩展之前,定义中的分配不会发生,因此您指定的变量值在以后才可见。您需要额外的eval或后eval扩展才能执行此操作。

无论

$(eval copy      := $(1))

在模板中或将内容拆分为多个定义。

@echo $(VAR)的问题是一回事。这是由eval扩展的define扩展而且$(VAR)在此时未设置,因此您需要模板中的$$(VAR)才能获得字面值扩展配方中的$(VAR),以便它正常工作和配方执行时间。