Wildcard targets in a Makefile

MakefileGnu Make

Makefile Problem Overview


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.

Makefile Solutions


Solution 1 - Makefile

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)

Solution 2 - Makefile

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.

Solution 3 - Makefile

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobert MunteanuView Question on Stackoverflow
Solution 1 - MakefileP ShvedView Answer on Stackoverflow
Solution 2 - MakefileMetaphoxView Answer on Stackoverflow
Solution 3 - MakefileCarl NorumView Answer on Stackoverflow