views:

479

answers:

3

I'd like to use a makefile to convert a set of svgs to pngs. The following snippet shows what I've done so far.

%.png: origs/%.svg
    convert -resize "32x" $< $@

make foo.png works now. but I'm stuck with the all target. I'd like to convert everything (all svgs that is) with a single command.

In all examples I've found the all target does something like this:

all: ${OBJECTS}

But I'm sure there's a simpler way for my case, please help me find it ! :D

A: 

I don't remember if make can do that.

You could add a shell foreach statement to your all:

( cd origs ; for file in *.svg ; do ; convert ${file} ; done )

You need the parens to make the foreach share the same environment (shell).

I have too many semicolons; I can never remember where they're needed and not needed when turning a multiline shell command into a one-liner.

lumpynose
Remove the semicolon after "do" and you should be fine.
Nathan Kitchen
+2  A: 

Depending on the version of make you're using, you may be able to define a set of targets based on all the svgs that are present. For example, in GNU make:

SVG = $(wildcard *.svg)
PNG = $(SVG:.svg=.png)

all: $(PNG)
Nathan Kitchen
great thanks! I just found this variation:PNG = $(patsubst origs/%.svg,%.png,$(SVG))
reto
A: 

What you have up there is a pattern rule. It tells make how to make a certian kind of target (those ending with ".png"), rather than any target in particular. So what you have is something that allows you type in an arbitrary "make foo.png" and it will know how to do that.

The way I would generalise this to create a rule for making "all .png's you can make from this directory" would be using a variable. Put a line something like this at the top of your makefile:

SVGs = *.svg
PNGs = $(subst .svg,.png,$(SVGs))

The first line will create a variable SVGs that contains all the files in your directory ending in ".svg". The second creates a variable PNGs containing the same list, but with ".png" on the end instead.

Then you should create a rule to build all SVG's like so:

allsvgs : $(PNGs)

Note that I did not call it "all". The "all" target is an unnoficial standard target that should always mean "build every target my makefile supports", In your case I suppose you could make a case for putting "allsvgs" on all's list of targets, but as your makefile grows you will need to add stuff to it.

T.E.D.