我在makefile中有许多非常相似的$(foreach..)
循环,它们的工作方式类似于下面的target_old
目标:
define NL
endef
extras=some/path/
vars=a b c
all: target_old target_new
target_old:
# foreach and some multiple evals inside and some multiple commands inside
$(foreach var, ${vars}, \
$(eval extra := ${extras}${var}) \
@echo var is ${var} and extras is ${extra}$(NL) \
)
# my try to wrap it in a function
define foreach_vars
$(foreach var, ${vars},
$(eval extra := ${extras}${var}) \
$1$(NL) \
)
endef
target_new:
@echo this is wrong:
$(call foreach_vars, \
@echo var is ${var} and extras is ${extra} \
)
我有很多这样的foreach
循环,它们在eval
中都具有相同的foreach
。所以我想在我自己的函数foreach
中用eval
包裹foreach_vars
循环。因此,我不必在每个$(eval extra := ${extras}${var})
调用中都使用foreach
。我创建了target_new
目标进行测试。我希望两个目标的输出相同,make target_old
打印:
$ make target_old
var is a and extras is some/path/a
var is b and extras is some/path/b
var is c and extras is some/path/c
但是target_new
并没有从循环内部选择${var}
,${var}
只是扩展为空:
$ make target_new
this is wrong:
var is and extras is
var is and extras is
var is and extras is
我想这是因为扩展发生在输入$(call...)
之前。有什么方法可以用来“推迟” $(call...)
调用内部参数的扩展,直到函数内部的foreach
内部?是否可以在make中编写自定义的类似于foreach的宏?还有其他用于实现这种功能的方法吗?
是的,您的问题来自于您想要的扩展和您想要的顺序不会发生的扩展。