Home | History | Annotate | Line # | Download | only in mk
bsd.clean.mk revision 1.9
      1 # $NetBSD: bsd.clean.mk,v 1.9 2021/12/07 22:40:52 rillig Exp $
      2 
      3 # <bsd.clean.mk>
      4 #
      5 # Public targets:
      6 #
      7 # clean:	Delete files listed in ${CLEANFILES}.
      8 # cleandir:	Delete files listed in ${CLEANFILES} and ${CLEANDIRFILES}.
      9 #
     10 # Public variables:
     11 #
     12 # CLEANFILES	Files to remove for both the clean and cleandir targets.
     13 #
     14 # CLEANDIRFILES	Files to remove for the cleandir target, but not for
     15 #		the clean target.
     16 #
     17 # MKCLEANSRC	Whether or not to clean the source directory
     18 # 		in addition to the object directory.  Defaults to "yes".
     19 #
     20 # MKCLEANVERIFY	Whether or not to verify that the file deletion worked.
     21 #		Defaults to "yes".
     22 #
     23 # The files listed in CLEANFILES and CLEANDIRFILES must not be
     24 # directories, because the potential risk from running "rm -rf" commands
     25 # in bsd.clean.mk is considered too great.  If you want to recursively
     26 # delete a directory as part of "make clean" or "make cleandir" then you
     27 # need to provide your own target.
     28 
     29 .if !defined(_BSD_CLEAN_MK_)
     30 _BSD_CLEAN_MK_=1
     31 
     32 .include <bsd.init.mk>
     33 
     34 MKCLEANSRC?=	yes
     35 MKCLEANVERIFY?=	yes
     36 
     37 clean:		.PHONY __doclean
     38 __doclean:	.PHONY .MADE __cleanuse CLEANFILES
     39 cleandir:	.PHONY clean __docleandir
     40 __docleandir:	.PHONY .MADE __cleanuse CLEANDIRFILES
     41 
     42 # __cleanuse is invoked with ${.ALLSRC} as the name of a variable
     43 # (such as CLEANFILES or CLEANDIRFILES), or possibly a list of
     44 # variable names.  ${.ALLSRC:@v@${${v}}@} will be the list of
     45 # files to delete.  (We pass the variable name, e.g. CLEANFILES,
     46 # instead of the file names, e.g. ${CLEANFILES}, because we don't
     47 # want make to replace any of the file names with the result of
     48 # searching .PATH.)
     49 #
     50 # If the list of files is empty, then the commands
     51 # reduce to "true", with an "@" prefix to prevent echoing.
     52 #
     53 # The use of :M* is needed to handle the case that CLEANFILES
     54 # or CLEANDIRFILES is not completely empty but contains spaces.
     55 # This can easily happen when CLEANFILES or CLEANDIRFILES is set
     56 # from other variables that happen to be empty.
     57 #
     58 # The use of :Q is needed to handle the case that CLEANFILES
     59 # or CLEANDIRFILES contains quoted strings, such as
     60 # CLEANFILES = "filename with spaces".
     61 #
     62 __cleanuse: .USE
     63 .if 0	# print "# clean CLEANFILES" for debugging
     64 	${"${.ALLSRC:@v@${${v}:M*}@:Q}" == "":?@true:${_MKMSG} \
     65 		"clean" ${.ALLSRC} }
     66 .endif
     67 .for _d in ${"${.OBJDIR}" == "${.CURDIR}" || "${MKCLEANSRC}" == "no" \
     68 		:? ${.OBJDIR} \
     69 		:  ${.OBJDIR} ${.CURDIR} }
     70 	${"${.ALLSRC:@v@${${v}:M*}@:Q}" == "":?@true: \
     71 	    (cd ${_d} && rm -f ${.ALLSRC:@v@${${v}}@} || true) }
     72 .if "${MKCLEANVERIFY}" == "yes"
     73 	@${"${.ALLSRC:@v@${${v}:M*}@:Q}" == "":?true: \
     74 	    bad="\$(cd ${_d} && ls -1d ${.ALLSRC:@v@${${v}}@} 2>/dev/null)"; \
     75 	    if test -n "\$bad"; then \
     76 	        echo "Failed to remove the following files from ${_d}:" ; \
     77 	        echo "\$bad" | while read file ; do \
     78 		    echo "	\$file" ; \
     79 		done ; \
     80 	        false ; \
     81 	    fi }
     82 .endif
     83 .endfor
     84 
     85 # Don't automatically load ".depend" files during "make clean"
     86 # or "make cleandir".
     87 .if make(clean) || make(cleandir)
     88 .MAKE.DEPENDFILE := .depend.no-such-file
     89 .endif
     90 
     91 .endif	# !defined(_BSD_CLEAN_MK)
     92