Using Makefile to clean subdirectories

后端 未结 5 1151
孤独总比滥情好
孤独总比滥情好 2021-02-08 16:09

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?

5条回答
  •  旧巷少年郎
    2021-02-08 16:43

    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.

提交回复
热议问题