xref: /netbsd-src/share/mk/bsd.clean.mk (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1# $NetBSD: bsd.clean.mk,v 1.4 2011/10/05 12:34:04 apb 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.
19#
20# MKCLEANVERIFY	Whether or not to verify that the file deletion worked.
21#
22
23.if !defined(_BSD_CLEAN_MK_)
24_BSD_CLEAN_MK_=1
25
26.include <bsd.init.mk>
27
28MKCLEANSRC?=	yes
29MKCLEANVERIFY?=	yes
30
31clean:		.PHONY __doclean
32__doclean:	.PHONY .MADE __cleanuse CLEANFILES
33cleandir:	.PHONY clean __docleandir
34__docleandir:	.PHONY .MADE __cleanuse CLEANDIRFILES
35
36# __cleanuse is invoked with ${.ALLSRC} as the name of a variable
37# (such as CLEANFILES or CLEANDIRFILES), or possibly a list of
38# variable names.  ${.ALLSRC:@v@${${v}}@} will be the list of
39# files to delete.  (We pass the variable name, e.g. CLEANFILES,
40# instead of the file names, e.g. ${CLEANFILES}, because we don't
41# want make to replace any of the file names with the result of
42# searching .PATH.)
43#
44# If the list of files is empty, then the commands
45# reduce to "true", with an "@" prefix to prevent echoing.
46#
47__cleanuse: .USE
48.if 0	# print "# clean CLEANFILES" for debugging
49	${"${.ALLSRC:@v@${${v}:M*}@}" == "":?@true:${_MKMSG} \
50		"clean" ${.ALLSRC} }
51.endif
52.for _d in ${"${.OBJDIR}" == "${.CURDIR}" || "${MKCLEANSRC}" == "no" \
53		:? ${.OBJDIR} \
54		:  ${.OBJDIR} ${.CURDIR} }
55	${"${.ALLSRC:@v@${${v}:M*}@}" == "":?@true: \
56	    (cd ${_d} && rm -f ${.ALLSRC:@v@${${v}}@} || true) }
57.if "${MKCLEANVERIFY}" == "yes"
58	@${"${.ALLSRC:@v@${${v}:M*}@}" == "":?true: \
59	    bad="\$(cd ${_d} && ls -d ${.ALLSRC:@v@${${v}}@} 2>/dev/null)"; \
60	    if test -n "\$bad"; then \
61	        echo "Failed to remove files from ${_d}:" ; \
62	        echo "\$bad" ; \
63	        false ; \
64	    fi }
65.endif
66.endfor
67
68# Don't automatically load ".depend" files during "make clean"
69# or "make cleandir".
70.if make(clean) || make(cleandir)
71.MAKE.DEPENDFILE := .depend.no-such-file
72.endif
73
74.endif	# !defined(_BSD_CLEAN_MK)
75