tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Changing "make clean" and "make cleandir"



I would like to make "make clean" and "make cleandir" delete leftover files in the source directory as well as the obj directory. This should help with the case that the current build uses .OBJDIR != .CURDIR, but an earlier build with .OBJDIR = .CURDIR had left some output files in the source directory.

The appended patch makes the following changes to how "make clean" and "make cleandir" work:

* The real work is done in a new include file, bsd.clean.mk.

* A new variable, CLEANDIRFILES, contains a list of files to be
  deleted by "make cleandir", analogous to the existing CLEANFILES
  variable used by "make clean".

* Other bsd.*.mk files no longer define their own "clean" or
  "cleandir" targets; instead, they append to the CLEANFILES or
  CLEANDIRFILES variables, and .include <bsd.clean.mk>.  (There were
  some cases where the CLEANFILES variable was used by a cleandir
  target; these have been changed to use the CLEANDIRFILES variable
  instead.)

* If ${.OBJDIR} and ${.CURDIR} are different, then "make clean" and
  "make cleandir" delete files from both directories.

--apb (Alan Barrett)

Index: share/mk/bsd.README
===================================================================
--- share/mk/bsd.README 30 Jun 2011 18:13:51 -0000      1.284
+++ share/mk/bsd.README 7 Sep 2011 20:06:02 -0000
@@ -809,6 +809,26 @@ TOOL_ZIC           Time zone compiler.  [zic]
 they use the same default owners etc. as the rest of the tree.
+=-=-=-=-= bsd.clean.mk =-=-=-=-=
+
+The include file <bsd.clean.mk> defines the clean and cleandir
+targets.  It uses the following variables:
+
+CLEANFILES     Files to remove for both the clean and cleandir targets.
+
+CLEANDIRFILES  Files to remove for the cleandir target, but not for
+               the clean target.
+
+To use the clean and cleandir targets defined in <bsd.clean.mk>, other
+Makefiles or bsd.*.mk files should append file names to the CLEANFILES
+or CLEANDIRFILES variables.  For example:
+
+           CLEANFILES+= a.out
+           CLEANDIRFILES+= .depend
+
+           .include <bsd.clean.mk>
+
+
 =-=-=-=-=   bsd.dep.mk   =-=-=-=-=
The include file <bsd.dep.mk> contains the default targets for building
@@ -1249,6 +1269,8 @@ BINOWN            Binary owner.
BINMODE Binary mode. +CLEANDIRFILES Additional files to remove for the cleandir target.
+
 CLEANFILES     Additional files to remove for the clean and cleandir targets.
COPTS Additional flags to the compiler when creating C objects.

Index: share/mk/bsd.dep.mk
===================================================================
--- share/mk/bsd.dep.mk 10 Apr 2011 16:52:36 -0000      1.72
+++ share/mk/bsd.dep.mk 7 Sep 2011 15:20:57 -0000
@@ -1,7 +1,6 @@
 #      $NetBSD: bsd.dep.mk,v 1.72 2011/04/10 16:52:36 joerg Exp $
##### Basic targets
-cleandir:      cleandepend
 realdepend:    beforedepend .depend afterdepend
 .ORDER:                beforedepend .depend afterdepend
@@ -62,9 +61,8 @@ ${__DPSRCS.d}: ${__DPSRCS.notd} ${DPSRCS
 .endif # defined(SRCS)                                                 # }
##### Clean rules
-cleandepend: .PHONY
 .if defined(SRCS)
-       rm -f .depend ${__DPSRCS.d} ${.CURDIR}/tags ${CLEANDEPEND}
+CLEANDIRFILES+= .depend ${__DPSRCS.d} ${.CURDIR}/tags ${CLEANDEPEND}
 .endif
##### Custom rules
@@ -75,3 +73,6 @@ tags: ${SRCS}
            ${TOOL_SED} "s;\${.CURDIR}/;;" > tags
 .endif
 .endif
+
+##### Pull in related .mk logic
+.include <bsd.clean.mk>

Index: share/mk/bsd.files.mk
===================================================================
--- share/mk/bsd.files.mk       3 Feb 2010 15:34:44 -0000       1.41
+++ share/mk/bsd.files.mk       7 Sep 2011 14:16:28 -0000
@@ -118,9 +118,7 @@ ${_TL}: ${_SL}
realall: ${BUILDSYMLINKS.t} -cleandir: cleanbuildsymlinks
-cleanbuildsymlinks: .PHONY
-       rm -f ${BUILDSYMLINKS.t}
+CLEANDIRFILES+= ${BUILDSYMLINKS.t}
.endif # } @@ -149,13 +147,12 @@ CLEANUUDECODE_FILES=${UUDECODE_FILES} ${
 CLEANUUDECODE_FILES+=${UUDECODE_FILES_RENAME_${i}}
 .endfor
-clean: cleanuudecodefiles
-cleanuudecodefiles: .PHONY
-       rm -f ${CLEANUUDECODE_FILES}
+CLEANFILES+= ${CLEANUUDECODE_FILES}
 .endif                                                         # }
##### Pull in related .mk logic
 .include <bsd.obj.mk>
 .include <bsd.sys.mk>
+.include <bsd.clean.mk>
.endif # !defined(_BSD_FILES_MK_)

Index: share/mk/bsd.hostlib.mk
===================================================================
--- share/mk/bsd.hostlib.mk     5 Aug 2011 09:36:28 -0000       1.15
+++ share/mk/bsd.hostlib.mk     7 Sep 2011 14:18:14 -0000
@@ -4,7 +4,6 @@
 .include <bsd.sys.mk>
##### Basic targets
-clean:         cleanlib
##### Default values
 CFLAGS+=       ${COPTS}
@@ -44,9 +43,7 @@ lib${HOSTLIB}.a: ${OBJS} ${DPADD}
realall: lib${HOSTLIB}.a -cleanlib: .PHONY
-       rm -f a.out [Ee]rrs mklog core *.core \
-           lib${HOSTLIB}.a ${OBJS} ${CLEANFILES}
+CLEANFILES+= a.out [Ee]rrs mklog core *.core lib${HOSTLIB}.a ${OBJS}
beforedepend:
 CFLAGS:=       ${HOST_CFLAGS}
@@ -55,5 +52,6 @@ CPPFLAGS:=    ${HOST_CPPFLAGS}
 ##### Pull in related .mk logic
 .include <bsd.obj.mk>
 .include <bsd.dep.mk>
+.include <bsd.clean.mk>
${TARGETS}: # ensure existence

Index: share/mk/bsd.hostprog.mk
===================================================================
--- share/mk/bsd.hostprog.mk    5 Jun 2011 23:09:15 -0000       1.63
+++ share/mk/bsd.hostprog.mk    7 Sep 2011 14:18:09 -0000
@@ -5,7 +5,6 @@
 .include <bsd.sys.mk>
##### Basic targets
-clean:         cleanprog
##### Default values
 LIBATF_C?=     /usr/lib/libatf-c.a
@@ -123,9 +122,7 @@ MAN=        ${HOSTPROG}.1
realall: ${HOSTPROG} -cleanprog: .PHONY
-       rm -f a.out [Ee]rrs mklog core *.core \
-           ${HOSTPROG} ${OBJS} ${LOBJS} ${CLEANFILES}
+CLEANFILES+= a.out [Ee]rrs mklog core *.core ${HOSTPROG} ${OBJS} ${LOBJS}
beforedepend:
 CFLAGS:=       ${HOST_CFLAGS}
@@ -144,6 +141,7 @@ LINKSMODE?= ${BINMODE}
 .include <bsd.inc.mk>
 .include <bsd.links.mk>
 .include <bsd.dep.mk>
+.include <bsd.clean.mk>
${TARGETS}: # ensure existence
Index: share/mk/bsd.info.mk
===================================================================
--- share/mk/bsd.info.mk        28 Feb 2009 19:18:52 -0000      1.39
+++ share/mk/bsd.info.mk        7 Sep 2011 15:22:09 -0000
@@ -3,7 +3,6 @@
 .include <bsd.init.mk>
##### Basic targets
-cleandir:      cleaninfo
 realinstall:   infoinstall
##### Default values
@@ -78,15 +77,11 @@ infoinstall::       ${_F}
 .endif # ${MKINFO} != "no"
##### Clean rules
-CLEANFILES+=   ${INFOFILES}
-
-cleaninfo: .PHONY
-.if !empty(CLEANFILES)
-       rm -f ${CLEANFILES}
-.endif
+CLEANDIRFILES+=        ${INFOFILES}
##### Pull in related .mk logic
 .include <bsd.obj.mk>
 .include <bsd.sys.mk>
+.include <bsd.clean.mk>
${TARGETS}: # ensure existence

Index: share/mk/bsd.kmodule.mk
===================================================================
--- share/mk/bsd.kmodule.mk     14 Jul 2011 18:12:35 -0000      1.31
+++ share/mk/bsd.kmodule.mk     7 Sep 2011 14:22:07 -0000
@@ -8,7 +8,6 @@ MKPIE=no
 .include <bsd.sys.mk>
##### Basic targets
-clean:         cleankmod
 realinstall:   kmodinstall
KERN= $S/kern
@@ -124,9 +123,7 @@ kmodinstall::       ${_PROG}
 .endif # !target(kmodinstall)
##### Clean rules
-cleankmod: .PHONY
-       rm -f a.out [Ee]rrs mklog core *.core \
-               ${PROG} ${OBJS} ${LOBJS} ${CLEANFILES}
+CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
##### Custom rules
 lint: ${LOBJS}
@@ -141,6 +138,7 @@ LINKSMODE?= ${KMODULEMODE}
 .include <bsd.man.mk>
 .include <bsd.links.mk>
 .include <bsd.dep.mk>
+.include <bsd.clean.mk>
.-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
 .-include "$S/arch/${MACHINE}/include/Makefile.inc"

Index: share/mk/bsd.lib.mk
===================================================================
--- share/mk/bsd.lib.mk 27 Aug 2011 18:35:20 -0000      1.315
+++ share/mk/bsd.lib.mk 7 Sep 2011 14:24:04 -0000
@@ -37,7 +37,6 @@ MKPROFILE:=   no
 ##### Basic targets
 .PHONY:                checkver libinstall
 realinstall:   checkver libinstall
-clean:         cleanlib
##### LIB specific flags.
 # XXX: This is needed for programs that link with .a libraries
@@ -596,14 +595,13 @@ lint: ${LOBJS}
        ${LINT} ${LINTFLAGS} ${LOBJS}
 .endif
-cleanlib: .PHONY
-       rm -f a.out [Ee]rrs mklog core *.core ${CLEANFILES}
-       rm -f lib${LIB}.a ${STOBJS}
-       rm -f lib${LIB}_p.a ${POBJS}
-       rm -f lib${LIB}_g.a ${GOBJS}
-       rm -f lib${LIB}_pic.a lib${LIB}.so.* lib${LIB}.so ${_LIB.debug} ${SOBJS}
-       rm -f ${STOBJS:=.tmp} ${POBJS:=.tmp} ${SOBJS:=.tmp} ${GOBJS:=.tmp}
-       rm -f llib-l${LIB}.ln ${LOBJS}
+CLEANFILES+= a.out [Ee]rrs mklog core *.core
+CLEANFILES+= lib${LIB}.a ${STOBJS}
+CLEANFILES+= lib${LIB}_p.a ${POBJS}
+CLEANFILES+= lib${LIB}_g.a ${GOBJS}
+CLEANFILES+= lib${LIB}_pic.a lib${LIB}.so.* lib${LIB}.so ${_LIB.debug} ${SOBJS}
+CLEANFILES+= ${STOBJS:=.tmp} ${POBJS:=.tmp} ${SOBJS:=.tmp} ${GOBJS:=.tmp}
+CLEANFILES+= llib-l${LIB}.ln ${LOBJS}
.if !target(libinstall) # {
@@ -777,5 +775,6 @@ LINKSMODE?= ${LIBMODE}
 .include <bsd.links.mk>
 .include <bsd.dep.mk>
 .include <bsd.clang-analyze.mk>
+.include <bsd.clean.mk>
${TARGETS}: # ensure existence

Index: share/mk/bsd.man.mk
===================================================================
--- share/mk/bsd.man.mk 30 Jun 2011 18:13:51 -0000      1.108
+++ share/mk/bsd.man.mk 7 Sep 2011 14:26:49 -0000
@@ -245,21 +245,15 @@ htmllinks::       ${_t}
 ##### Clean rules
 .undef _F
-cleandir: cleanman
-.if !empty(CLEANFILES)
-       rm -f ${CLEANFILES}
-.endif
-
-cleanman: .PHONY
 .if !empty(MAN) && (${MKMAN} != "no")
 .if (${MKCATPAGES} != "no")
-       rm -f ${CATPAGES}
+CLEANDIRFILES+= ${CATPAGES}
 .endif
 .if !empty(MANSUFFIX)
-       rm -f ${MANPAGES} ${CATPAGES:S/${MANSUFFIX}$//}
+CLEANDIRFILES+= ${MANPAGES} ${CATPAGES:S/${MANSUFFIX}$//}
 .endif
 .if ${MKHTML} != "no"
-       rm -f ${HTMLPAGES}
+CLEANDIRFILES+= ${HTMLPAGES}
 .endif
 .endif
 # (XXX ${CATPAGES:S...} cleans up old .catN files where .catN.gz now used)
@@ -273,5 +267,6 @@ lintmanpages: ${MANPAGES}
 .include <bsd.obj.mk>
 .include <bsd.files.mk>
 .include <bsd.sys.mk>
+.include <bsd.clean.mk>
${TARGETS} catinstall maninstall htmlinstall: # ensure existence

Index: share/mk/bsd.nls.mk
===================================================================
--- share/mk/bsd.nls.mk 16 Mar 2006 18:43:34 -0000      1.46
+++ share/mk/bsd.nls.mk 7 Sep 2011 14:27:24 -0000
@@ -3,7 +3,6 @@
 .include <bsd.init.mk>
##### Basic targets
-cleandir:      cleannls
 realinstall:   nlsinstall
##### Default values
@@ -62,11 +61,11 @@ nlsinstall::        ${_F}
 .endif # ${MKNLS} != "no"
##### Clean rules
-cleannls: .PHONY
 .if ${MKNLS} != "no" && !empty(NLS)
-       rm -f ${NLSALL}
+CLEANDIRFILES+= ${NLSALL}
 .endif
##### Pull in related .mk logic
 .include <bsd.obj.mk>
 .include <bsd.sys.mk>
+.include <bsd.clean.mk>

Index: share/mk/bsd.prog.mk
===================================================================
--- share/mk/bsd.prog.mk        27 Aug 2011 18:35:20 -0000      1.266
+++ share/mk/bsd.prog.mk        7 Sep 2011 14:31:12 -0000
@@ -14,7 +14,6 @@
##### Basic targets
 realinstall:   proginstall scriptsinstall
-clean:         cleanprog
.gdbinit:
        rm -f .gdbinit
@@ -27,10 +26,7 @@ clean:               cleanprog
        echo "source ${__gdbinit}" >> .gdbinit
 .endfor
-cleanobjs: .PHONY
-
-cleanprog: .PHONY cleanobjs cleanextra
-       rm -f a.out [Ee]rrs mklog core *.core .gdbinit
+CLEANFILES+= a.out [Ee]rrs mklog core *.core .gdbinit
.if defined(SHAREDSTRINGS)
 CLEANFILES+=strings
@@ -352,14 +348,10 @@ MAN+=             ${MAN.${_P}}
realall: ${_P} ${_PROGDEBUG.${_P}} -cleanprog: cleanprog-${_P}
-cleanprog-${_P}:
-       rm -f ${_P} ${_PROGDEBUG.${_P}}
+CLEANFILES+= ${_P} ${_PROGDEBUG.${_P}}
.if defined(OBJS.${_P}) && !empty(OBJS.${_P})
-cleanobjs: cleanobjs-${_P}
-cleanobjs-${_P}:
-       rm -f ${OBJS.${_P}} ${LOBJS.${_P}}
+CLEANFILES+= ${OBJS.${_P}} ${LOBJS.${_P}}
 .endif
_PROG_INSTALL+= proginstall-${_P}
@@ -414,9 +406,7 @@ lint-${_P}: ${LOBJS.${_P}}
.if defined(OBJS) && !empty(OBJS) && \
     (empty(PROGS) && empty(PROGS_CXX))
-cleanobjs: cleanobjs-plain
-cleanobjs-plain:
-       rm -f ${OBJS} ${LOBJS}
+CLEANFILES+= ${OBJS} ${LOBJS}
 .endif
.if !target(proginstall)
@@ -476,11 +466,7 @@ LINKSMODE?= ${BINMODE}
 .include <bsd.sys.mk>
 .include <bsd.dep.mk>
 .include <bsd.clang-analyze.mk>
-
-cleanextra: .PHONY
-.if defined(CLEANFILES) && !empty(CLEANFILES)
-       rm -f ${CLEANFILES}
-.endif
+.include <bsd.clean.mk>
${TARGETS}: # ensure existence
Index: share/mk/bsd.rpc.mk
===================================================================
--- share/mk/bsd.rpc.mk 12 May 2004 16:38:01 -0000      1.11
+++ share/mk/bsd.rpc.mk 7 Sep 2011 14:31:36 -0000
@@ -72,3 +72,4 @@ CLEANFILES+=  ${RPC_CLNTFILES}
 ##### Pull in related .mk logic
 .include <bsd.obj.mk>
 .include <bsd.sys.mk>
+.include <bsd.clean.mk>

Index: share/mk/bsd.test.mk
===================================================================
--- share/mk/bsd.test.mk        14 May 2011 17:47:28 -0000      1.18
+++ share/mk/bsd.test.mk        7 Sep 2011 15:35:57 -0000
@@ -36,13 +36,13 @@ MAN.${_T}?= # empty
.if defined(TESTS_SH)
 _TESTS+=               ${TESTS_SH}
-CLEANFILES+=           ${TESTS_SH}
+CLEANDIRFILES+=                ${TESTS_SH}
. for _T in ${TESTS_SH}
 SCRIPTS+=              ${_T}
 SCRIPTSDIR_${_T}=      ${TESTSDIR}
-CLEANFILES+= ${_T}.tmp
+CLEANDIRFILES+=                ${_T}.tmp
TESTS_SH_SRC_${_T}?= ${_T}.sh
 ${_T}: ${TESTS_SH_SRC_${_T}}
@@ -61,7 +61,7 @@ FILES+=                       Atffile
 FILESDIR_Atffile=      ${TESTSDIR}
. if ${ATFFILE:tl} == "auto"
-CLEANFILES+=   Atffile Atffile.tmp
+CLEANDIRFILES+=        Atffile Atffile.tmp
realall: Atffile
 Atffile: Makefile
@@ -83,10 +83,6 @@ Atffile: Makefile
.if !empty(SCRIPTS) || !empty(PROGS) || !empty(PROGS_CXX)
 .  include <bsd.prog.mk>
-.else
-cleandir:      cleantest
-cleantest:     .PHONY
-       rm -f ${CLEANFILES}
 .endif
#
@@ -118,7 +114,7 @@ TESTS_ENV += PATH=${TESTS_PATH:tW:S/ /:/
_TESTS_FIFO = ${.OBJDIR}/atf-run.fifo
 _TESTS_LOG = ${.OBJDIR}/atf-run.log
-CLEANFILES += ${_TESTS_FIFO} ${_TESTS_LOG}
+CLEANDIRFILES += ${_TESTS_FIFO} ${_TESTS_LOG}
.PHONY: test
 .if defined(TESTSDIR)
@@ -156,3 +152,6 @@ test:
 test:
        @echo "*** No TESTSDIR defined; nothing to do."
 .endif
+
+##### Pull in related .mk logic
+.include <bsd.clean.mk>

Index: share/mk/bsd.x11.mk
===================================================================
--- share/mk/bsd.x11.mk 1 Sep 2011 16:41:48 -0000       1.102
+++ share/mk/bsd.x11.mk 7 Sep 2011 14:41:08 -0000
@@ -193,9 +193,7 @@ LDFLAGS+=           -Wl,-rpath,${X11USRLIBDIR} -L
realall: ${CPPSCRIPTS} -clean: cleancppscripts
-cleancppscripts: .PHONY
-       rm -f ${CPPSCRIPTS}
+CLEANFILES+= ${CPPSCRIPTS}
 .endif                                                         # }
#
@@ -340,9 +338,7 @@ pkgconfig-install: ${_PKGDEST.${_pkg}}
                < ${.IMPSRC} > ${.TARGET}.tmp && \
        mv -f ${.TARGET}.tmp ${.TARGET}
-cleandir: cleanpkgconfig
-cleanpkgconfig: .PHONY
-       rm -f ${_PKGCONFIG_FILES} ${_PKGCONFIG_FILES:C/$/.tmp/}
+CLEANDIRFILES+= ${_PKGCONFIG_FILES} ${_PKGCONFIG_FILES:C/$/.tmp/}
 .endif
#
@@ -369,9 +365,7 @@ realinstall: appdefsinstall
 # .man page handling
 #
 .if (${MKMAN} != "no" && (${MAN:U} != "" || ${PROG:U} != ""))    # {
-cleandir: cleanx11man
-cleanx11man: .PHONY
-       rm -f ${MAN:U${PROG:D${PROG.1}}}
+CLEANDIRFILES+= ${MAN:U${PROG:D${PROG.1}}}
 .endif                                                         # }
.SUFFIXES: .man .man.pre .1 .3 .4 .5 .7
@@ -430,3 +424,6 @@ _X11MANTRANSFORMCMD+=       ${X11EXTRAMANDEFS}
        ${_MKTARGET_CREATE}
        rm -f ${.TARGET}
        ${_X11MANTRANSFORMCMD} | ${X11TOOL_UNXCOMM} > ${.TARGET}
+
+##### Pull in related .mk logic
+.include <bsd.clean.mk>

Index: share/mk/bsd.clean.mk
===================================================================
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ share/mk/bsd.clean.mk       7 Sep 2011 21:02:21 -0000
@@ -0,0 +1,42 @@
+# $NetBSD$
+
+# <bsd.clean.mk>
+#
+# Public targets:
+#
+# clean:       Delete files listed in ${CLEANFILES}.
+# cleandir:    Delete files listed in ${CLEANFILES} and ${CLEANDIRFILES}.
+#
+# Public variables:
+#
+# CLEANFILES   Files to remove for both the clean and cleandir targets.
+#
+# CLEANDIRFILES        Files to remove for the cleandir target, but not for
+#              the clean target.
+
+# bsd.clean.mk might be included multiple times, and variables that
+# are undefined the first time might be defined later.  We avoid
+# defining commands for any target more than once, but we check
+# for non-empty variables every time.
+
+clean: .PHONY __doclean
+__doclean: .PHONY
+.if defined(CLEANFILES) && !empty(CLEANFILES)
+.if !commands(__doclean)
+       rm -f ${CLEANFILES}
+.if "${.OBJDIR}" != "${.CURDIR}"
+       ( cd ${.CURDIR} && rm -f ${CLEANFILES} )
+.endif # .OBJDIR != .CURDIR
+.endif # !commands(__doclean)
+.endif # defined && !empty(CLEANFILES)
+
+cleandir: .PHONY clean __docleandir
+__docleandir: .PHONY
+.if defined(CLEANDIRFILES) && !empty(CLEANDIRFILES)
+.if !commands(__docleandir)
+       rm -f ${CLEANDIRFILES}
+.if "${.OBJDIR}" != "${.CURDIR}"
+       ( cd ${.CURDIR} && rm -f ${CLEANDIRFILES} )
+.endif # .OBJDIR != .CURDIR
+.endif # !commands(__docleandir)
+.endif # defined && !empty(CLEANDIRFILES)


Home | Main Index | Thread Index | Old Index