Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Makefile to clean subdirectories

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?

For example, currently in my Makefile, I have something like:

SUBDIRS = src, src1

.PHONY: clean subdirs $(SUBDIRS)

clean: $(SUBDIRS)
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c

$(SUBDIRS):
    $(MAKE) -C $(SUBDIRS) clean 

However, this requires me to have a Makefile in both src and src1. Otherwise, I would get the error

No rule to make target clean

Since I only want to run the command "rm -rf *.o ~ core .depend ..cmd *.ko *.mod.c" in each subdirectory anyways, it seems redundant to have to include a Makefile in every subdirectory with the exact same line for clean. Is there no way to simply have the same clean command run in each of the subdirectories?

like image 735
Tony Avatar asked Sep 24 '14 01:09

Tony


1 Answers

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
like image 153
robert Avatar answered Oct 15 '22 11:10

robert