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