首页 > 解决方案 > 使用定义、foreach 和调用的动态 Gnu Makefile 规则

问题描述

我想定义很多这样的规则:

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

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

输出文件编号:

它有几个依赖项,要么包含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.

我不知道会发生什么。

标签: makefilegnu-make

解决方案


它应该是

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)。所以你必须再有一个来生成正确的行拆分(或在调用 oprule 后foreach添加某种宏)。$(nl)

PS 而且逗号后面也不要加空格。虽然在这种特殊情况下它不会受到伤害,但总的来说,这些空间对于制作来说很重要。


推荐阅读