Search code examples
makefilegnu-maketarget

Unable to do make clean despite .PHONY


I have created a Makefile, to convert markdown to other formats. I have used .PHONY: clean but I am still unable to do make clean. It searches for clean.md file. I know its because of $(MAKECMDGOALS) but I need it to convert just one file.

SOURCE= $(wildcard *.md)
## Pattern Substitution
HTML=$(SOURCE:.md=.gen.html) 
PDF=$(SOURCE:.md=.gen.pdf) 

## Targets and dependencies
.PHONY: all
all : $(HTML) $(PDF)

html:   clean $(HTML)
pdf:    clean $(PDF)

.PHONY: clean
clean:
    - $(RM) -f *.gen.*

.PHONY: $(MAKECMDGOALS)
$(MAKECMDGOALS): $(MAKECMDGOALS:%=%.html) $(MAKECMDGOALS:%=%.gen.pdf)

%.gen.html : %.md
    $(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_HTML_OPTIONS) -o $@ $<

%.gen.pdf : %.md
    $(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_PDF_OPTIONS) -o $@ $<

Please suggest changes.


Solution

  • GOALS := $(filter-out clean, $(MAKECMDGOALS))
    
    .PHONY: $(GOALS)
    $(GOALS): $(GOALS:%=%.html) $(GOALS:%=%.gen.pdf)