Is it possible to perform a make clean from the parent directory which also recursively cleans all sub-directories without having to include a makefile in each sub-directory?
Instead of using recursion, you could shell out to find
to get a list of directories and do a single iteration to generate the wildcards:
SUBDIR_ROOTS := foo bar
DIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
GARBAGE_PATTERNS := *.o *~ core .depend .*.cmd *.ko *.mod.c
GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
clean:
rm -rf $(GARBAGE)
A variation of @Christoph answer:
# Exclude directory from find . command
# https://stackoverflow.com/questions/4210042/exclude-directory-from-find-command
GARBAGE_TYPES := "*.gz(busy)" *.aux *.log *.pdf *.aux *.bbl *.log *.out *.synctex.gz *.fls
DIRECTORIES_TO_CLEAN := $(shell find -not -path "./.git**" -not -path "./images**" -type d)
GARBAGE_TYPED_FOLDERS := $(foreach DIR, $(DIRECTORIES_TO_CLEAN), $(addprefix $(DIR)/,$(GARBAGE_TYPES)))
clean:
$(RM) -rf $(GARBAGE_TYPED_FOLDERS)
# echo $(GARBAGE_TYPED_FOLDERS)
This is an example for latex files. The first pattern on GARBAGE_TYPES
has the double quotes around it because of the parenthesis on the file type name. Without it, rm
cannot remove them. The other patterns does not need the quotes.
The second DIRECTORIES_TO_CLEAN
uses the opposite of a list of directories to clean, i.e., a list of directories to not clean. This is useful when you have only one or two directories as .git
and images
which you do not want to clean, but want to clean everything else.
You cannot without help of an external program. The best is a shell script that does the recursion and calls make in each of the subdirectories (look at my comment to @robert in his response) Something like this will do the work (and does not depend on GNU make features)
#!/bin/sh
ROOTDIR=`/bin/pwd`
for dir in `find . -type d -print`
do
make -C "${dir}" -f "${ROOTDIR}"/Makefile clean
done
of course, you can put this sequence (in target cleanrec
) inside your Makefile
cleanrec:
ROOT=`/bin/pwd`; \
for dir in `find . -type d -print`; \
do \
make -C "$${dir}" -f "$${ROOTDIR}"/Makefile clean; \
done
and conserve your clean
target for local cleaning of a single directory. The reason is that Makefile has only static info to do the make, and you have to get some external help to know what subdirectories you have in each directory. So, in case you are going to get external help, you'd better to use a good tool as find(1) and sh(1)
I agree that you could just have the rm command operate on subdirs. But something like the following allows recursive make using only a single makefile:
SUBDIRS = . src src1
SUBDIRSCLEAN=$(addsuffix clean,$(SUBDIRS))
clean: $(SUBDIRSCLEAN)
clean_curdir:
rm -rfv *.o *~ core .depend .*.cmd *.ko *.mod.c
%clean: %
$(MAKE) -C $< -f $(PWD)/Makefile clean_curdir
ALL_MOD = $(shell find . -maxdepth 1 -type d )
ALL_MOD_CLEAN = $(addsuffix .clean, $(ALL_MOD))
.PHONY: $(ALL_MOD_CLEAN) $(ALL_MOD) default all clean
$(ALL_MOD_CLEAN):
$(E) "cleaning $(basename $@)"
if [ -e $(basename $@)/Makefile ] ; then \
$(MAKE) -C $(basename $@) clean ;\
fi
clean: $(ALL_MOD_CLEAN)
$(E) "cleaning complete: " $(ALL_MOD)
add '.' to the suffix and use $(basename ...) instead of original target otherwise will be re-making all files every time need just to clean the project