问题
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