Using Makefile to clean subdirectories

后端 未结 5 1145
孤独总比滥情好
孤独总比滥情好 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:47

    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)

提交回复
热议问题