Makefile:在配方中定义规则和前提条件 [英] Makefile: defining rules and prerequisites in recipes
问题描述
我有一个设置,要处理的文件依赖于另一个程序的输出.构建程序及其所有先决条件 这也是一项复杂的任务,因此我也想使用make.现在我的问题是,似乎无法在Makefile配方中生成规则和先决条件.考虑以下代码:
I have a setup where the files I want to process with make are dependent on the output of another program. Building the program and all its prerequisites is also a complicated task so I would like to use make for this as well. Now my problem is, that it doesn't seem that one can generate rules and prerequisites in Makefile recipes. Consider the following code:
bar:
echo target1 target2 target3 > bar
foo: bar
$(eval BAR := $(shell cat bar))
define FUN
$(1):
touch a$(1)
endef
ifdef BAR
$(foreach i,$BAR,$(eval $(call FUN,$(i))))
endif
blub: foo $(BAR)
我替换了一大套复杂的配方,这些配方最终导致我想通过bar
配方生成文件列表.实际上,产生bar
的内容非常复杂,应该通过一组Makefile配方来完成,而不能仅仅通过(如上所述)来完成:
I replaced a big set of complicated recipes that lead to the generation of the list of files I want to have in the end by the bar
recipe. In reality, producing the content of bar
is very complicated and should be done by a set of Makefile recipes and cannot just be done by (as the above suggests):
BAR:=$(shell echo target1 target2 target3)
我想将foreach
循环放入foo
的配方中,但是以prerequisites cannot be defined in recipes
失败,这很有意义,并且也在
I would like to put the foreach
loop into the recipe for foo
but that fails with prerequisites cannot be defined in recipes
which makes sense and is also explained in function define in makefile
但是,当我执行make blub
时,当foo
eval的BAR
设置为其他值时,blub
的前提条件没有重新评估.
But it seems that when I do make blub
that at the time when foo
eval's BAR
to a different value, the prerequisites for blub
are not re-evaluated.
所以我认为最终我正在寻找两件事:
So I think ultimately I'm looking for two things:
-
如何基于(并取决于)另一个配方(在这种情况下为
bar
)输出的内容在运行时动态生成配方?
how do I generate recipes dynamically at runtime, based on (and dependent on) what another recipe (
bar
in this case) outputs?
如何基于(并取决于)另一个配方(在本例中为bar
)输出的内容在运行时动态更新目标(在本例中为blub
)的前提条件?
how do I update the prerequisites of a target (blub
in this case) dynamically at runtime, based on (and dependent on) what another recipe (bar
in this case) outputs?
谢谢!
解决方案
在@ user657267的帮助下,以下内容似乎可以解决我的问题:
With the help of @user657267 the following seems to solve my problem:
.PHONY: all
all: blub
-include bar.make
.PHONY: blub
blub: $(BAR)
echo $^
bar.make: Makefile
printf 'BAR=target1 target2 target3\n' > $@
printf 'target1 target2 target3:\n' >>$@
printf '\ttouch $$@' >> $@
.PHONY: clean
clean:
rm -f target1 target2 target3 bar.make
推荐答案
听起来像应该使用make的自我重制功能
Sounds like you should be using make's self-remaking features
-include bar.make
blub: $(BAR)
@echo $^
bar.make:
@echo BAR := target1 target2 target3 > $@
@echo target1 target2 target3: ; touch $$@ >> $@
显然,bar.make
的配方是人为设计的,在现实世界中,它们可能会调用某种脚本来输出有效的makefile.
Obviously the recipes for bar.make
are contrived, in the real world they'd probably invoke some kind of script that outputs a valid makefile.
这篇关于Makefile:在配方中定义规则和前提条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!