user3313834
user3313834

Reputation: 7837

in Makefile remove folders if a filename with the same basename exist

I need to remove in the following case

.
├── a
│   └── index.html
├── a.rst
├── b
│   └── index.html
├── c
│   └── index.html
└── c.rst

folder a and c not b.

I make it work with this Makefile:

$ cat Makefile
.PHONY: clean                                                                                       
                                                                                                    
HTML_TARGETS:= $(patsubst %.rst,%.html,$(wildcard *.rst))                                           
                                                                                                    
clean: $(HTML_TARGETS)                                                                              
                                                                                                    
%.html: %.rst                                                                                       
    @echo rm $(basename $@ .html) 
$

$ make
rm a
rm c
$

Is there a better way to write it ? (the patsubst use an unneeded .html sub)

Upvotes: 0

Views: 776

Answers (1)

MadScientist
MadScientist

Reputation: 100956

What I mean is why don't you just do something like:

.PHONY: clean

HTML_DIRS := $(patsubst %/,%,$(dir $(wildcard */*.html)))
RST_FILES := $(basename $(wildcard *.rst))

clean:
        echo rm -r $(filter $(RST_FILES),$(HTML_DIRS))

Upvotes: 2

Related Questions