1# $NetBSD: bsd.clean.mk,v 1.10 2023/06/03 21:21:32 lukem 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
34clean:              .PHONY __doclean
35__doclean:          .PHONY .MADE __cleanuse CLEANFILES
36cleandir: .PHONY clean __docleandir
37__docleandir:       .PHONY .MADE __cleanuse CLEANDIRFILES
38
39# __cleanuse is invoked with ${.ALLSRC} as the name of a variable
40# (such as CLEANFILES or CLEANDIRFILES), or possibly a list of
41# variable names.  ${.ALLSRC:@v@${${v}}@} will be the list of
42# files to delete.  (We pass the variable name, e.g. CLEANFILES,
43# instead of the file names, e.g. ${CLEANFILES}, because we don't
44# want make to replace any of the file names with the result of
45# searching .PATH.)
46#
47# If the list of files is empty, then the commands
48# reduce to "true", with an "@" prefix to prevent echoing.
49#
50# The use of :M* is needed to handle the case that CLEANFILES
51# or CLEANDIRFILES is not completely empty but contains spaces.
52# This can easily happen when CLEANFILES or CLEANDIRFILES is set
53# from other variables that happen to be empty.
54#
55# The use of :Q is needed to handle the case that CLEANFILES
56# or CLEANDIRFILES contains quoted strings, such as
57# CLEANFILES = "filename with spaces".
58#
59__cleanuse: .USE
60.if 0     # print "# clean CLEANFILES" for debugging
61          ${"${.ALLSRC:@v@${${v}:M*}@:Q}" == "":?@true:${_MKMSG} \
62                    "clean" ${.ALLSRC} }
63.endif
64.for _d in ${"${.OBJDIR}" == "${.CURDIR}" || "${MKCLEANSRC}" == "no" \
65                    :? ${.OBJDIR} \
66                    :  ${.OBJDIR} ${.CURDIR} }
67          ${"${.ALLSRC:@v@${${v}:M*}@:Q}" == "":?@true: \
68              (cd ${_d} && rm -f ${.ALLSRC:@v@${${v}}@} || true) }
69.if "${MKCLEANVERIFY}" == "yes"
70          @${"${.ALLSRC:@v@${${v}:M*}@:Q}" == "":?true: \
71              bad="\$(cd ${_d} && ls -1d ${.ALLSRC:@v@${${v}}@} 2>/dev/null)"; \
72              if test -n "\$bad"; then \
73                  echo "Failed to remove the following files from ${_d}:" ; \
74                  echo "\$bad" | while read file ; do \
75                        echo "          \$file" ; \
76                    done ; \
77                  false ; \
78              fi }
79.endif
80.endfor
81
82# Don't automatically load ".depend" files during "make clean"
83# or "make cleandir".
84.if make(clean) || make(cleandir)
85.MAKE.DEPENDFILE := .depend.no-such-file
86.endif
87
88.endif    # !defined(_BSD_CLEAN_MK)
89