Objetivos comodín en un Makefile
Pregunta
¿Cómo puedo compactar los siguientes objetivos Makefile?
$(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
-
Usando GNU Make 3.81
.
Solución
El concepto se llama reglas de patrón . Puede leerlo en GNU make manual .
$(GRAPHDIR)/%.png: $(GRAPHDIR)/%.dot
dot $< -Tpng -o $@
graphs: $(patsubst %,$(GRAPHDIR)/%.png, Complex Simple IFileReader McCabe)\
o simplemente
%.png: %.dot
dot $< -Tpng -o $@
graphs: $(patsubst %,$(GRAPHDIR)/%.png, Complex Simple IFileReader McCabe)
Cosas avanzadas: es divertido notar que hay una repetición allí arriba ...
PNG_pattern=$(GRAPHDIR)/%.png
$(PNG_pattern): $(GRAPHDIR)/%.dot
dot $< -Tpng -o $@
graphs: $(patsubst %,$(PNG_pattern), Complex Simple IFileReader McCabe)
Otros consejos
En caso de que realmente quiera generar un .PNG para cada .DOT dentro del directorio actual:
%.png : %.dot
dot -Tpng -o $@ $<
all: $(addsuffix .png, $(basename $(wildcard *.dot)))
Se me ocurrió este Makefile después de leer la respuesta de @Pavel.
Creo que quieres algunas reglas de patrón. Prueba esto.
TARGETS = $(GRAPHDIR)/Complex.png \
$(GRAPHDIR)/Simple.png \
$(GRAPHDIR)/IFileReader.png \
$(GRAPHDIR)/McCabe-linear.png
%.png : %.dot
dot $^ -Tpng -o $@
graphs: $(TARGETS)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow