2011-07-08 63 views
0

我试图编写一个Makefile,它将从单个源文件生成一组“矩形”输出文件。想象一下,我们有一个单独的SVG文件,我们想将它编译成许多PNG文件。 PNG文件的创建由两个参数(因此是“矩形”字) - 分辨率(高,低)和颜色(彩色,黑白)控制。创建这样的PNG文件了SVG文件的最简单的Makefile看起来是这样的:GNU Make中的双精度%

img-highres-color.png: img.svg 
    convert --highres --color --output img-highres-color.png img.svg 

img-highres-bw.png: img.svg 
    convert --highres --bw --output img-highres-color.png img.svg 

img-lowres-color.png: img.svg 
    convert --lowres --color --output img-highres-color.png img.svg 

img-lowres-bw.png: img.svg 
    convert --lowres --bw --output img-highres-color.png img.svg 

下一个步骤是创建一个使用%静态模式规则。我能想出这样的:

RESOLUTIONS = highres lowres 
COLORS = color bw 

PNG_FILES = $(foreach r, $(RESOLUTIONS), $(foreach c, $(COLORS), img-$(r)-$(c).png)) 

$(filter img-highres-%.png, $(PNG_FILES)): img-highres-%.png: img.svg 
    convert --highres --$* --output img-highres-$*.png img.svg 

$(filter img-lowres-%.png, $(PNG_FILES)): img-lowres-%.png: img.svg 
    convert --lowres --$* --output img-lowres-$*.png img.svg 

最后,我想创建一个单一的静态模式规则,但是这需要使用双%,像这样:

RESOLUTIONS = highres lowres 
COLORS = color bw 

PNG_FILES = $(foreach r, $(RESOLUTIONS), $(foreach c, $(COLORS), img-$(r)-$(c).png)) 

$(filter img-%-%.png, $(PNG_FILES)): img-%-%.png: img.svg 
    convert --$* --$* --output img-$*-$*.png img.svg 

这当然不起作用。是否有可能编写单一规则来实现这一点?

以上情况是对我真实情况的简单描述。重要的是,RESOLUTIONSCOLORS变量的值是事先不知道的。另外,你能提供一个足以处理两个以上维度的解决方案吗?在第三个维度上面的例子可以是文件类型 - PNG,JPG,GIF等

回答

2

你可以在这里使用$(eval)

RESOLUTIONS=highres lowres 
COLORS=color bw 
PNG_FILES = $(foreach r, $(RESOLUTIONS), $(foreach c, $(COLORS), img-$(r)-$(c).png)) 

all: $(PNG_FILES) 

# Make the rules for converting the svg into each variant. 

define mkrule 
img-$1-$2.png: img.svg 
    @convert --$1 --$2 --output [email protected] $$< 
endef 
$(foreach color,$(COLORS),$(foreach res,$(RESOLUTIONS),$(eval $(call mkrule,$(res),$(color))))) 

$(eval)允许你动态地构造和注入的makefile碎片进入makefile文件被解析。你应该能够根据你的喜好以多种不同的方式来扩展它。

0

您可以使用一个单一的%和文本操作功能“解压”的$*像这样:在-

$(filter img-%.png, $(PNG_FILES)): img-%.png: img.svg 
    convert $(addprefix --,$(subst -, ,$*)) --output [email protected] $< 

$(subst)分裂$*进言,而$(addprefix)作用于依次在每个字。奖励:同样的规则也适用于超过2个维度,只要您使用的任何标志都不包含- :-)