使用define,foreach和call的动态Gnu Makefile规则

时间:2019-06-24 12:30:46

标签: makefile gnu-make

我想定义一些这样的规则:

x-9: y-9 z-9 x-8
    python gen-files.py --out-x=x-9 --out-y=y-9 --in-x=x-8
x-8: y-8 z-8 x-7
    python gen-files.py --out-x=x-8 --out-y=y-8 --in-x=x-7
x-7: y-7 z-7 x-6
    python gen-files.py --out-x=x-7 --out-y=y-7 --in-x=x-6
x-6: y-6 z-6 x-5
    python gen-files.py --out-x=x-6 --out-y=y-6 --in-x=x-5
x-5: y-5 z-5 x-4
    python gen-files.py --out-x=x-5 --out-y=y-5 --in-x=x-4

x-4:
    touch x-4

如您所见,基本思想是我有一个目标

outputfile-NUMBER:

具有两个依赖项,它们的名称中包含NUMBER或名称中包含NUMBER-MINUS-ONE

我的目标是当我尝试建立最终目标x-9时,它会自动x-8x-7 ...降到x-4

我尝试过类似的

define oprule
x-$(1): x-$(1) y-$(1) z-$(1) x-$(2)
    python gen-files.py --out-x=x-$(1) --out-y=y-$(1) --in-x=x-$(2)
endef

ttt = 9 8 7 6 5
$(foreach now, $(ttt), \
    $(call oprule, $(now), $(shell $$(( $(now)-1 )) ) ) )

我认为这会生成5条规则,但是当我尝试

make x-9

我收到消息

Makefile:93: *** multiple target patterns.  Stop.

我不知道会发生什么。

1 个答案:

答案 0 :(得分:0)

应该是

define oprule
x-$(1): y-$(1) z-$(1) x-$(2)
    python gen-files.py --out-x=x-$(1) --out-y=y-$(1) --in-x=x-$(2)

endef

endef之前的最后一个换行符总是被用光(就像define之后的第一个换行符一样)。因此,对于foreach,您还必须再有一个来生成正确的行拆分(或在调用oprule之后添加某种$(nl)宏)。

P.S。并且也不要在逗号后放置空格。尽管在这种情况下不会造成伤害,但是总的来说,空格对于make来说很重要。