Makefile “双”字;foreach“;在makepp中
我试图用makepp做一些类似于嵌套循环的事情,但我不知道如何做 这就是我想做的Makefile “双”字;foreach“;在makepp中,makefile,Makefile,我试图用makepp做一些类似于嵌套循环的事情,但我不知道如何做 这就是我想做的 MODULES= A B C TEMPLATES = X Y Z #I'd like to make a target which needs both the MODULE and TEMPLATE: $(MODULE)/$(TEMPLATE) : @echo The Module is $(MODULE) and the Template is $(TEMPLATE) #I've tried fo
MODULES= A B C
TEMPLATES = X Y Z
#I'd like to make a target which needs both the MODULE and TEMPLATE:
$(MODULE)/$(TEMPLATE) :
@echo The Module is $(MODULE) and the Template is $(TEMPLATE)
#I've tried foreach, and can do something like:
$(MODULE)/$(foreach) : : foreach $(TEMPLATES)
@echo The Module is $(MODULE) and the Template is $(foreach)
#Or I can do:
$(foreach)/$(TEMPLATE) : : foreach $(MODULES)
@echo The Module is $(foreach) and the Template is $(TEMPLATE)
如何创建一组适用于任何模块/模板目标的规则
我希望用户能够有如下目标:
makepp A/Z
不是
那么,如何制定一个目标,将所有模块和模板进行交叉积:
makepp all
The Module is A and the Template is X
The Module is A and the Template is Y
The Module is A and the Template is Z
...
The Module is C and the Template is X
The Module is C and the Template is Y
The Module is C and the Template is Z
这个很棘手。我不是makepp专家,但如果它与GNU make充分兼容(正如它所宣称的那样),那么以下内容应该与您想要的内容非常接近:
MODULES := A B C
TEMPLATES := X Y Z
ALL :=
define MODULE_TEMPLATE_rule
$(1)/$(2):
@echo The Module is $(1) and the Template is $(2)
ALL += $(1)/$(2)
endef
define MODULE_rule
$(foreach template,$(TEMPLATES),$(eval $(call MODULE_TEMPLATE_rule,$(1),$(template))))
endef
$(foreach module,$(MODULES),$(eval $(call MODULE_rule,$(module))))
all: $(ALL)
这里的魔杖是foreach
、call
和eval
的混合体。call
的第一个参数是变量名。在我的示例中,这些变量是用define endef
构造定义的,但没有任何区别call
展开变量,将其下一个参数分配给$(1),$(2).
局部变量。因此:
$(call MODULE_TEMPLATE_rule,A,X)
例如,将返回:
A/X:
@echo The Module is A and the Template is X
ALL += A/X
但返回并不是实例化。这就是eval
进入场景的地方:它展开其参数,并将结果解析为任何make语句。foreach的foreach
用于迭代模块和模板,但您已经知道了这一点
请注意,ALL
变量是由迭代器在模块和模板上逐步构建的。因此,如果您键入make all
make将在all
中生成所有单词,即打印所有组合:
The Module is A and the Template is X
The Module is A and the Template is Y
The Module is A and the Template is Z
The Module is B and the Template is X
The Module is B and the Template is Y
The Module is B and the Template is Z
The Module is C and the Template is X
The Module is C and the Template is Y
The Module is C and the Template is Z
就这些。但是要注意:为了有效地使用它,你必须了解make是如何工作的,它做什么以及以什么顺序工作。在这里,手册是强制性的。这是关于
make
(GNU或其他)还是关于makepp
?因为标签和内容似乎不一致。
The Module is A and the Template is X
The Module is A and the Template is Y
The Module is A and the Template is Z
The Module is B and the Template is X
The Module is B and the Template is Y
The Module is B and the Template is Z
The Module is C and the Template is X
The Module is C and the Template is Y
The Module is C and the Template is Z