GNU make implicit rules for batch file processing

本秂侑毒 提交于 2019-12-24 11:44:37

问题


I'd like to use GNU make to automate batch file processing, in my particular case I have a huge number of image files, I'd like to colorspace convert and reencode them to a custom file format. The file format encoder accepts only filenames on the command line, i.e. no stdio redirection.

My file and directory structure is

./sourceimages/*.tif
./destimages/*.mie
./Makefile

I wrote a peliminary Makefile with a pattern rule

%.mie : %.tif
    tmpraw := $(shell mktemp --suffix=raw)
    convert $< -colorspace YUV -resize …x… rgb:$(tmpraw)
    miecoder $(tmpraw) $@
    rm $(tmpraw)

But now I'm stuck, because I can't figure out how to make make to take all the files in sourceimages as prerequisites of implicit targets in destimages. So how can I do this?

I'd really like to use make for this, to harness its capability for parallel execution.


回答1:


First, your use of mktemp to create a unique temporary file name is unnecessary and leads to other problems; if you're converting foo.tif to foo.mie, let's just call the temporary file foo_temp.tif.

%.mie : %.tif
    convert $< -colorspace YUV -resize …x… rgb:$*_temp.tif
    miecoder $*_temp.tif $@
    rm $*_temp.tif

Next, we put in the paths so that Make can build a target in one place using a prerequisite from another:

destimages/%.mie : sourceimages/%.tif
    convert $< -colorspace YUV -resize …x… rgb:$*_temp.tif
    miecoder $*_temp.tif $@
    rm $*_temp.tif

Finally we look for sources, infer a list of desired targets, then make them prerequisites of the default taget (put all of this above the destimages/%.mie:... rule in the makefile):

SOURCES := $(wildcard sourceimages/%.tif)
TARGETS = $(patsubst sourceimages/%.tif, destimages/%.mie, $(SOURCES))

.PHONY: all
all: $(TARGETS)


来源:https://stackoverflow.com/questions/14653566/gnu-make-implicit-rules-for-batch-file-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!