1
%.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 $<

1 Answer 1

1

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))))
2
  • While the shortcoming does bother me, it's not all bad in this case. I don't completely mind having an easily enforced limited set of acceptable widths.
    – altendky
    Dec 4, 2012 at 14:42
  • @altendky: If the wildcard handling were a little better, you could have a limited set of acceptable widths without so much plumbing. Questions equivalent to "how can I have more than one wildcard in a pattern rule?" come up a lot, and the answer is always clumsy.
    – Beta
    Dec 4, 2012 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.