我喜欢Dylan给出的答案,“迭代Makefile中的列表”, Iterating over lists in Makefiles?并尝试做类似的事情,但遇到了麻烦。也许sonmeone可以提供帮助。
我的Makefile:
PARTITION_TMP:=Joe|red \
Carl:Mary|white \
Fritz|blue
partition_tmp:
@- $(foreach partition_tmp,$(PARTITION_TMP), \
$(eval N = $(word 1, $(subst |, ,$(partition_tmp)))) \
$(eval C = $(word 2, $(subst |, ,$(partition_tmp)))) \
$(call print_tmp, $N, $C) \
)
define print_tmp
@echo in print_tmp
@echo set_global -name PARTITION_NAME $(1)
@echo set_global -color PARTITION_COLOR $(2) -name PARTITION_NAME $(1)
endef
当我运行make时,我得到以下内容:
in print_tmp
set_global -name PARTITION_NAME Joe
set_global -color PARTITION_COLOR red -name PARTITION_NAME Joe @echo 在print_tmp
中set_global -name PARTITION_NAME Carl:Mary
set_global -color PARTITION_COLOR white -name PARTITION_NAME Carl:Mary @echo in print_tmp
set_global -name PARTITION_NAME Fritz
set_global -color PARTITION_COLOR蓝色-name PARTITION_NAME Fritz
我无法弄清楚为什么我在前两次调用print_tmp结束时得到“@echo in print_tmp”(或者可能是最后两次调用print_tmp的开始?)
有人可以帮忙吗?
谢谢, 标记
答案 0 :(得分:3)
这是因为你在一行上将print_tmp
块串起来 - 但这些块有内部换行符。考虑:
partition_tmp:
$(foreach color, red blue, \
$(call print_tmp, $(color)) \
)
define print_tmp
@echo first $(1)
endef
foreach
计算为@echo first red @echo first blue
,然后作为shell命令执行并生成:
first red @echo first blue
现在在宏中添加另一行:
partition_tmp:
$(foreach color, red blue, \
$(call print_tmp, $(color)) \
)
define print_tmp
@echo first $(1)
@echo second $(1)
endef
现在foreach
评估为
@echo first red
@echo second red @echo first blue
@echo second blue
产生:
first red
second red @echo first blue
second blue
要解决此问题,只需添加一个空白行:
define print_tmp
@echo in print_tmp
@echo set_global -name PARTITION_NAME $(1)
@echo set_global -color PARTITION_COLOR $(2) -name PARTITION_NAME $(1)
endef