2011-07-08 62 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個維度,只要您使用的任何標誌都不包含- :-)