danijar
danijar

Reputation: 34185

How to have different rules for the same target depending on the input extension?

I want to use make to convert images into the right format for a book I'm writing. Input images are in the figure directory and can have different file formats, e.g. PNG, SVG or JPG. Depending on the file extension, I want to run a different conversion command. The results should land in output and always be of type PNG.

FIGURES := $(patsubst figure/%.svg,output/%.png,$(wildcard figure/*.svg))

figures: $(FIGURES)

output/%.png: figure/%.svg
    convert -density 600 -background none -resize 2500x $< $@

This is what I have so far, it works for SVG inputs only. How can I collect all files from the figure directory regardless of the file extension and apply different rules depending on the file extension?

Upvotes: 1

Views: 179

Answers (1)

user657267
user657267

Reputation: 21000

Something like

sources := $(wildcard $(addprefix figure/*,.png .svg .jpg))
targets := $(patsubst figure/%, output/%.png, $(basename $(sources)))

.PHONY: all
all: $(targets)

output/%.png: figure/%.png
    # whatever
output/%.png: figure/%.svg
    convert -density 600 -background none -resize 2500x $< $@
output/%.png: figure/%.jpg
    # blah

Upvotes: 1

Related Questions