altendky
altendky

Reputation: 4354

Pattern rules with multiple parameters within a Makefile

%.600x.png: %.svg
    inkscape --export-png=$@ --export-width=600 --export-area-drawing $<

%.300x.png: %.svg
    inkscape --export-png=$@ --export-width=300 --export-area-drawing $<

How would you avoid the repetition shown in the above Makefile? To get my point across, I'll invent some new syntax.

%(1).%(2)x.png: %(1).svg
    inkscape --export-png=$@ --export-width=%(2) --export-area-drawing $<

Upvotes: 1

Views: 143

Answers (1)

Beta
Beta

Reputation: 99172

Primitive wildcard handling is one of Make's glaring shortcomings. Here is a way to do what you want, but it isn't exactly elegant:

define pngrule
%.$(1)x.png: %.svg
    inkscape --export-png=$$@ --export-width=$(1) --export-area-drawing $$<
endef

$(eval $(call pngrule,300))

$(eval $(call pngrule,600))

Note the $$@ and $$< in the command, and the lack of whitespace in the call statements.

If you have a lot of these widths, it may be worthwhile to remove a little more redundancy:

WIDTHS := 300 600

$(foreach width,$(WIDTHS),$(eval $(call pngrule,$(width))))

Upvotes: 1

Related Questions