Wildcard targets in a Makefile

How can I compact the folllowing Makefile targets?

$(GRAPHDIR)/Complex.png: $(GRAPHDIR)/Complex.dot
        dot $(GRAPHDIR)/Complex.dot -Tpng -o $(GRAPHDIR)/Complex.png

$(GRAPHDIR)/Simple.png: $(GRAPHDIR)/Simple.dot
        dot $(GRAPHDIR)/Simple.dot -Tpng -o $(GRAPHDIR)/Simple.png

$(GRAPHDIR)/IFileReader.png: $(GRAPHDIR)/IFileReader.dot
        dot $(GRAPHDIR)/IFileReader.dot -Tpng -o $(GRAPHDIR)/IFileReader.png

$(GRAPHDIR)/McCabe-linear.png: $(GRAPHDIR)/McCabe-linear.dot
        dot $(GRAPHDIR)/McCabe-linear.dot -Tpng -o $(GRAPHDIR)/McCabe-linear.png

graphs: $(GRAPHDIR)/Complex.png $(GRAPHDIR)/Simple.png $(GRAPHDIR)/IFileReader.png $(GRAPHDIR)/McCabe-linear.png

--

Using GNU Make 3.81.


The concept is called pattern rules. You can read about it in GNU make manual.

$(GRAPHDIR)/%.png: $(GRAPHDIR)/%.dot
        dot $< -Tpng -o $@

graphs: $(patsubst %,$(GRAPHDIR)/%.png, Complex Simple IFileReader McCabe)\

or just

%.png: %.dot
        dot $< -Tpng -o $@

graphs: $(patsubst %,$(GRAPHDIR)/%.png, Complex Simple IFileReader McCabe)

You can also remove all repetition by extracting one of the patterns into a separate variable PNG_PATTERN like so:

PNG_pattern=$(GRAPHDIR)/%.png

$(PNG_pattern): $(GRAPHDIR)/%.dot
        dot $< -Tpng -o $@

graphs: $(patsubst %,$(PNG_pattern), Complex Simple IFileReader McCabe)

Just in case you actually want to generate a .PNG for every .DOT within current directory:

%.png : %.dot
    dot -Tpng -o $@ $<

all: $(addsuffix .png, $(basename $(wildcard *.dot)))

I came up with this Makefile after reading the answer from @Pavel.


I think you want some pattern rules. Try this out.

TARGETS = $(GRAPHDIR)/Complex.png \  
          $(GRAPHDIR)/Simple.png \ 
          $(GRAPHDIR)/IFileReader.png \
          $(GRAPHDIR)/McCabe-linear.png

%.png : %.dot
        dot $^ -Tpng -o $@

graphs: $(TARGETS)