Makefile中具有多个参数的模式规则

时间:2012-12-03 20:43:07

标签: makefile

%.600x.png: %.svg
    inkscape --export-png=$@ --export-width=600 --export-area-drawing $<

%.300x.png: %.svg
    inkscape --export-png=$@ --export-width=300 --export-area-drawing $<

您如何避免上述Makefile中显示的重复?为了解释我的观点,我将发明一些新的语法。

%(1).%(2)x.png: %(1).svg
    inkscape --export-png=$@ --export-width=%(2) --export-area-drawing $<

1 个答案:

答案 0 :(得分:1)

原始通配符处理是Make的明显缺点之一。这是一种做你想做的事情的方法,但它不是很优雅:

define pngrule
%.$(1)x.png: %.svg
    inkscape --export-png=$$@ --export-width=$(1) --export-area-drawing $$<
endef

$(eval $(call pngrule,300))

$(eval $(call pngrule,600))

请注意命令中的$$@$$<,以及call语句中缺少空格。

如果你有很多这样的宽度,那么删除一点冗余可能是值得的:

WIDTHS := 300 600

$(foreach width,$(WIDTHS),$(eval $(call pngrule,$(width))))