Subject: Auto creating depend files
To: None <tech-toolchain@netbsd.org>
From: David Laight <david@l8s.co.uk>
List: tech-toolchain
Date: 12/14/2003 21:18:58
Having got fed up of update builds failing because a .depend file
referenced a now-deleted file I looked at ways of stopping in happening.

- Mark all the .h files .OPTIONAL in the .depend file
- Regenerate the xxx.d files during the build (usually will cc -MD)
- Rebuild the .depend file after the usual target is built

This means that incorrect dependencies are used the first time a source
file is rebuilt, but after that they self correct.  It also completely
removes the need for a 'make depend' pass.

I've make it conditional on MKAUTODEP which I've defaulted to "yes"
(it will need to be "no" for anything not using gcc - host tools are ok).

I've changed bsd.lib.mk to use objcopy (not ld) to remove (un)wanted
local symbols - otherwise cc -MD generates the wrong filename.

Seems to work on i386, anyone fancy testing anything else?

	David

Index: Makefile
===================================================================
RCS file: /cvsroot/src/Makefile,v
retrieving revision 1.222
diff -u -p -r1.222 Makefile
--- Makefile	5 Dec 2003 22:33:01 -0000	1.222
+++ Makefile	7 Dec 2003 20:51:30 -0000
@@ -112,6 +112,14 @@ _SUBDIR=	tools lib include gnu bin games
 _SUBDIR+=	usr.sbin share rescue sys etc .WAIT distrib regress
 
 #
+# If MKAUTODEP is true do 'make all' instead of 'make dependall'
+#
+
+.if ${MKAUTODEP} == "no"
+depend=depend
+.endif
+
+#
 # Weed out directories that don't exist.
 #
 
@@ -319,13 +327,13 @@ do-${targ}: ${targ}
 
 .for dir in tools tools/compat lib/csu gnu/lib/libgcc${LIBGCC_EXT} lib/libc lib/libdes lib gnu/lib
 do-${dir:S/\//-/g}:
-.for targ in dependall install
+.for targ in ${depend}all install
 	(cd ${.CURDIR}/${dir} && ${MAKE} ${targ})
 .endfor
 .endfor
 
 do-ld.so:
-.for targ in dependall install
+.for targ in ${depend}all install
 .if (${OBJECT_FMT} == "a.out")
 	(cd ${.CURDIR}/libexec/ld.aout_so && ${MAKE} ${targ})
 .endif
@@ -335,7 +343,7 @@ do-ld.so:
 .endfor
 
 do-build:
-.for targ in dependall install
+.for targ in ${depend}all install
 	(cd ${.CURDIR} && ${MAKE} ${targ} BUILD_tools=no BUILD_lib=no)
 .endfor
 
Index: build.sh
===================================================================
RCS file: /cvsroot/src/build.sh,v
retrieving revision 1.125
diff -u -p -r1.125 build.sh
--- build.sh	14 Nov 2003 12:38:12 -0000	1.125
+++ build.sh	7 Dec 2003 20:51:33 -0000
@@ -706,6 +706,7 @@ validatemakeparams()
 	MKOBJDIRS=$(getmakevar MKOBJDIRS)
 	MKUNPRIVED=$(getmakevar MKUNPRIVED)
 	MKUPDATE=$(getmakevar MKUPDATE)
+	MKAUTODEP=$(getmakevar MKAUTODEP)
 
 	if [ "${MKOBJDIRS}" != "no" ]; then
 		# If setting -M or -O to the root of an obj dir, make sure
@@ -865,7 +866,9 @@ buildtools()
 	else
 		cleandir=
 	fi
-	${runcmd} "${makewrapper}" ${cleandir} dependall install ||
+	[ "$MKAUTODEP" != "no" ] && depend= || depend=depend
+	${runcmd} "${makewrapper}" ${cleandir} ${depend}all install ||
 	    bomb "Failed to make tools"
 	statusmsg "Tools built to ${TOOLDIR}"
 	${runcmd} cd "${TOP}"
@@ -924,12 +927,19 @@ buildkernel()
 		    bomb "Failed to make cleandir in ${kernelbuildpath}"
 		${runcmd} cd "${TOP}"
 	fi
 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
 		-s "${TOP}/sys" "${kernelconfpath}" ||
 	    bomb "${toolprefix}config failed for ${kernelconf}"
 	${runcmd} cd "${kernelbuildpath}"
-	${runcmd} "${makewrapper}" depend ||
-	    bomb "Failed to make depend in ${kernelbuildpath}"
+	if [ "$MKAUTODEP" = no ]; then
+		${runcmd} "${makewrapper}" depend ||
+		    bomb "Failed to make depend in ${kernelbuildpath}"
+	fi
 	${runcmd} "${makewrapper}" ${parallel} all ||
 	    bomb "Failed to make all in ${kernelbuildpath}"
 	${runcmd} cd "${TOP}"
Index: bin/sh/Makefile
===================================================================
RCS file: /cvsroot/src/bin/sh/Makefile,v
retrieving revision 1.73
diff -u -p -r1.73 Makefile
--- bin/sh/Makefile	16 Nov 2003 14:14:18 -0000	1.73
+++ bin/sh/Makefile	7 Dec 2003 20:51:33 -0000
@@ -12,6 +12,7 @@ SHSRCS=	alias.c cd.c echo.c error.c eval
 GENSRCS=arith.c arith_lex.c builtins.c init.c nodes.c syntax.c
 GENHDRS=arith.h builtins.h nodes.h syntax.h token.h
 SRCS=	${SHSRCS} ${GENSRCS}
+HOSTSRCS= mkinit.c mknodes.c mksyntax.c
 
 DPSRCS+=${GENHDRS}
 
Index: share/mk/bsd.dep.mk
===================================================================
RCS file: /cvsroot/src/share/mk/bsd.dep.mk,v
retrieving revision 1.64
diff -u -p -r1.64 bsd.dep.mk
--- share/mk/bsd.dep.mk	11 Nov 2003 11:36:40 -0000	1.64
+++ share/mk/bsd.dep.mk	7 Dec 2003 20:51:34 -0000
@@ -21,24 +21,44 @@ __acpp_flags=	${_TRADITIONAL_CPP}
 
 __DPSRCS.all=	${SRCS:C/\.(c|m|s|S|C|cc|cpp|cxx)$/.d/} \
 		${DPSRCS:C/\.(c|m|s|S|C|cc|cpp|cxx)$/.d/}
-__DPSRCS.d=	${__DPSRCS.all:O:u:M*.d}
-__DPSRCS.notd=	${__DPSRCS.all:O:u:N*.d}
+__DPSRCS.d=	${__DPSRCS.all:O:u:M*.d} ${HOSTSRCS:.c=.ld}
+__DPSRCS.notd=	${__DPSRCS.all:O:u:N*.d:N*.ld}
 
 .NOPATH: .depend ${__DPSRCS.d}
 
+.SUFFIXES: .ld
+
+.c.ld:
+	${_MKTARGET_CREATE}
+	${HOST_MKDEP} -f ${.TARGET} -s .lo -- ${COPTS.${.IMPSRC:T}} \
+	    ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
+
+.if ${MKAUTODEP} != "no" && (!make(depend) || defined(CRUNCHEDPROG) && ${CRUNCHEDPROG} == 1)
+CPPFLAGS +=	-MD
+
+# We have to make the .depend file depend on the .o files, not the .d ones
+__DPSRCS.dd=	${__DPSRCS.d:S/d$/o/}
+.if !empty(__DPSRCS.d)
+.OPTIONAL: ${__DPSRCS.d}
+.endif
+.else									# } {
+__DPSRCS.dd=	${__DPSRCS.d}
+
 .if !empty(__DPSRCS.d)							# {
 ${__DPSRCS.d}: ${__DPSRCS.notd} ${DPSRCS}
 .endif									# }
 
-.depend: ${__DPSRCS.d}
-	${_MKTARGET_CREATE}
-	rm -f .depend
-	${MKDEP} -d -f ${.TARGET} -s ${MKDEP_SUFFIXES:Q} ${__DPSRCS.d}
-
-.SUFFIXES: .d .s .S .c .C .cc .cpp .cxx .m
+.SUFFIXES: .d .ld .s .S .c .C .cc .cpp .cxx .m
 
 .c.d:
 	${_MKTARGET_CREATE}
+.if defined(CRUNCHEDPROG)
+	echo 'defined(CRUNCHEDPROG)'
+.endif
+.if make(depend)
+	echo 'make(depend)'
+.endif
+	echo auto:${MKAUTODEP} crunch:${CRUNCHEDPROG}
 	${MKDEP} -f ${.TARGET} -- ${MKDEPFLAGS} ${CFLAGS:M-[ID]*} ${CPPFLAGS} \
 	    ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
 
@@ -58,6 +78,13 @@ ${__DPSRCS.d}: ${__DPSRCS.notd} ${DPSRCS
 	    ${DESTDIR:D-nostdinc++ ${CPPFLAG_ISYSTEMXX} \
 	    ${DESTDIR}/usr/include/g++} \
 	    ${CPPFLAGS} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
+
+.endif # MKAUTODEP != "no"						# }
+
+.depend: ${__DPSRCS.dd}
+	${_MKTARGET_CREATE}
+	rm -f .depend
+	${MKDEP} -o -d -f ${.TARGET} -s ${MKDEP_SUFFIXES:Q} ${__DPSRCS.d}
 
 .endif # defined(SRCS)							# }
 
Index: share/mk/bsd.kmod.mk
===================================================================
RCS file: /cvsroot/src/share/mk/bsd.kmod.mk,v
retrieving revision 1.71
diff -u -p -r1.71 bsd.kmod.mk
--- share/mk/bsd.kmod.mk	4 Nov 2003 14:52:22 -0000	1.71
+++ share/mk/bsd.kmod.mk	7 Dec 2003 20:51:34 -0000
@@ -54,7 +54,7 @@ ${OBJS} ${LOBJS}: ${DPSRCS}
 
 .if ${MACHINE_CPU} == "powerpc" || \
     ${MACHINE_CPU} == "arm"
-${KMOD}_tmp.o: ${OBJS} ${DPADD}
+${KMOD}_tmp.o: ${OBJS} ${DPADD} ${AUTO_DEPEND}
 	${LD} -r ${LDFLAGS} -o tmp.o ${OBJS}
 	mv tmp.o ${.TARGET}
 
@@ -63,7 +63,7 @@ ${KMOD}_tramp.S: ${KMOD}_tmp.o $S/lkm/ar
 		 awk -f $S/lkm/arch/${MACHINE_CPU}/lkmtramp.awk > tmp.S
 	mv tmp.S ${.TARGET}
 
-${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
+${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o ${AUTO_DEPEND}
 	${LD} -r ${LDFLAGS} \
 		`${OBJDUMP} --reloc ${KMOD}_tmp.o | \
 			 awk -f $S/lkm/arch/${MACHINE_CPU}/lkmwrap.awk` \
@@ -76,7 +76,7 @@ ${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
 .endif
 	mv tmp.o ${.TARGET}
 .else
-${PROG}: ${OBJS} ${DPADD}
+${PROG}: ${OBJS} ${DPADD} ${AUTO_DEPEND}
 	${LD} -r ${LDFLAGS} -o tmp.o ${OBJS}
 	mv tmp.o ${.TARGET}
 .endif
Index: share/mk/bsd.lib.mk
===================================================================
RCS file: /cvsroot/src/share/mk/bsd.lib.mk,v
retrieving revision 1.241
diff -u -p -r1.241 bsd.lib.mk
--- share/mk/bsd.lib.mk	13 Nov 2003 05:56:30 -0000	1.241
+++ share/mk/bsd.lib.mk	7 Dec 2003 20:51:35 -0000
@@ -173,32 +173,23 @@ FFLAGS+=	${FOPTS}
 
 .c.o:
 	${_MKTARGET_COMPILE}
-	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(COPTS) && !empty(COPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} -o ${.TARGET}
+.if !defined(COPTS) || empty(COPTS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .c.po:
 	${_MKTARGET_COMPILE}
-	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} -pg ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(COPTS) && !empty(COPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -X -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} -pg ${.IMPSRC} -o ${.TARGET}
+.if !defined(COPTS) || empty(COPTS:M*-g*)
+	${OBJCOPY} -X ${.TARGET}
 .endif
 
 .c.so:
 	${_MKTARGET_COMPILE}
-	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${CSHLIBFLAGS} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(COPTS) && !empty(COPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${CSHLIBFLAGS} ${.IMPSRC} -o ${.TARGET}
+.if !defined(COPTS) || empty(COPTS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .c.ln:
@@ -207,62 +198,44 @@ FFLAGS+=	${FOPTS}
 
 .cc.o .cpp.o .cxx.o .C.o:
 	${_MKTARGET_COMPILE}
-	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(COPTS) && !empty(COPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} -o ${.TARGET}
+.if !defined(COPTS) || empty(COPTS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .cc.po .cpp.po .cxx.o .C.po:
 	${_MKTARGET_COMPILE}
-	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} -pg ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(COPTS) && !empty(COPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -X -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} -pg ${.IMPSRC} -o ${.TARGET}
+.if !defined(COPTS) || empty(COPTS:M*-g*)
+	${OBJCOPY} -X ${.TARGET}
 .endif
 
 .cc.so .cpp.so .cxx.so .C.so:
 	${_MKTARGET_COMPILE}
-	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${CSHLIBFLAGS} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(COPTS) && !empty(COPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${CSHLIBFLAGS} ${.IMPSRC} -o ${.TARGET}
+.if !defined(COPTS) || empty(COPTS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .f.o:
 	${_MKTARGET_COMPILE}
-	${COMPILE.f} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(FOPTS) && !empty(FOPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.f} ${.IMPSRC} -o ${.TARGET}
+.if !defined(FOPTS) || empty(FOPTS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .f.po:
 	${_MKTARGET_COMPILE}
-	${COMPILE.f} -pg ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(FOPTS) && !empty(FOPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -X -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.f} -pg ${.IMPSRC} -o ${.TARGET}
+.if !defined(FOPTS) || empty(FOPTS:M*-g*)
+	${OBJCOPY} -X ${.TARGET}
 .endif
 
 .f.so:
 	${_MKTARGET_COMPILE}
-	${COMPILE.f} ${FPICFLAGS} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(FOPTS) && !empty(FOPTS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.f} ${FPICFLAGS} ${.IMPSRC} -o ${.TARGET}
+.if !defined(FOPTS) || empty(FOPTS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .f.ln:
@@ -271,51 +244,39 @@ FFLAGS+=	${FOPTS}
 
 .m.o:
 	${_MKTARGET_COMPILE}
-	${COMPILE.m} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(OBJCFLAGS) && !empty(OBJCFLAGS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.m} ${.IMPSRC} -o ${.TARGET}
+.if !defined(OBJCFLAGS) || empty(OBJCFLAGS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .m.po:
 	${_MKTARGET_COMPILE}
-	${COMPILE.m} -pg ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(OBJCFLAGS) && !empty(OBJCFLAGS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -X -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.m} -pg ${.IMPSRC} -o ${.TARGET}
+.if !defined(OBJCFLAGS) || empty(OBJCFLAGS:M*-g*)
+	${OBJCOPY} -X ${.TARGET}
 .endif
 
 .m.so:
 	${_MKTARGET_COMPILE}
-	${COMPILE.m} ${CSHLIBFLAGS} ${.IMPSRC} -o ${.TARGET}.tmp
-.if defined(OBJCFLAGS) && !empty(OBJCFLAGS:M*-g*)
-	mv ${.TARGET}.tmp ${.TARGET}
-.else
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.m} ${CSHLIBFLAGS} ${.IMPSRC} -o ${.TARGET}
+.if !defined(OBJCFLAGS) || empty(OBJCFLAGS:M*-g*)
+	${OBJCOPY} -x ${.TARGET}
 .endif
 
 .S.o .s.o:
 	${_MKTARGET_COMPILE}
-	${COMPILE.S} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}.tmp
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.S} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}
+	${OBJCOPY} -x ${.TARGET}
 
 .S.po .s.po:
 	${_MKTARGET_COMPILE}
-	${COMPILE.S} -DGPROF -DPROF ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}.tmp
-	${LD} -X -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.S} -DGPROF -DPROF ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}
+	${OBJCOPY} -X ${.TARGET}
 
 .S.so .s.so:
 	${_MKTARGET_COMPILE}
-	${COMPILE.S} ${CAPICFLAGS} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}.tmp
-	${LD} -x -r ${.TARGET}.tmp -o ${.TARGET}
-	rm -f ${.TARGET}.tmp
+	${COMPILE.S} ${CAPICFLAGS} ${CFLAGS:M-[ID]*} ${AINC} ${.IMPSRC} -o ${.TARGET}
+	${OBJCOPY} -x ${.TARGET}
 
 .if defined(LIB)
 .if (${MKPIC} == "no" || (defined(LDSTATIC) && ${LDSTATIC} != "") \
@@ -374,7 +335,7 @@ SOBJS=
 
 .NOPATH: ${ALLOBJS} ${_LIBS} ${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
 
-realall: ${SRCS} ${ALLOBJS:O} ${_LIBS}
+realall: ${SRCS} ${ALLOBJS:O} ${_LIBS} ${AUTO_DEP}
 
 __archivebuild: .USE
 	${_MKTARGET_BUILD}
Index: share/mk/bsd.own.mk
===================================================================
RCS file: /cvsroot/src/share/mk/bsd.own.mk,v
retrieving revision 1.400
diff -u -p -r1.400 bsd.own.mk
--- share/mk/bsd.own.mk	5 Dec 2003 12:14:42 -0000	1.400
+++ share/mk/bsd.own.mk	7 Dec 2003 20:51:37 -0000
@@ -472,27 +472,6 @@ TARGETS+=	all clean cleandir depend depe
 		beforeinstall afterinstall realinstall realdepend realall \
 		html installhtml cleanhtml subdir-all subdir-install subdir-depend
 
-.if ${NEED_OWN_INSTALL_TARGET} == "yes"
-.if !target(install)
-install:	.NOTMAIN beforeinstall subdir-install realinstall afterinstall
-beforeinstall:	.NOTMAIN
-subdir-install:	.NOTMAIN beforeinstall
-realinstall:	.NOTMAIN beforeinstall
-afterinstall:	.NOTMAIN subdir-install realinstall
-.endif
-all:		.NOTMAIN realall subdir-all
-subdir-all:	.NOTMAIN
-realall:	.NOTMAIN
-depend:		.NOTMAIN realdepend subdir-depend
-subdir-depend:	.NOTMAIN
-realdepend:	.NOTMAIN
-distclean:	.NOTMAIN cleandir
-cleandir:	.NOTMAIN clean
-
-dependall:	.NOTMAIN realdepend .MAKE
-	@cd ${.CURDIR}; ${MAKE} realall
-.endif
-
 #
 # Define MKxxx variables (which are either yes or no) for users
 # to set in /etc/mk.conf and override in the make environment.
@@ -523,7 +502,8 @@ MK${var}:=	yes
 #
 # MK* options which default to "yes".
 #
-.for var in BFD BINUTILS \
+.for var in AUTODEP \
+	BFD BINUTILS \
 	CATPAGES CRYPTO CVS \
 	DOC \
 	GCC GCCCMDS GDB \
@@ -643,6 +623,9 @@ USE_${var}?= yes
 USE_${var}?= yes
 .endfor
 
+.if ${MKAUTODEP} != "no"
+AUTO_DEPEND?=	.WAIT .depend
+.endif
 #
 # Use XFree86 4.x as default version on i386, amd64, macppc, cats, sgimips,
 # and sparc*.
@@ -704,9 +687,6 @@ _MKMSG_LEX?=		${_MKMSG} "    lex "
 _MKMSG_REMOVE?=		${_MKMSG} " remove "
 _MKMSG_YACC?=		${_MKMSG} "   yacc "
 
-_MKSHMSG_CREATE?=	${_MKSHMSG} " create "
-_MKSHMSG_INSTALL?=	${_MKSHMSG} "install "
-
 _MKTARGET_BUILD?=	${_MKMSG_BUILD} ${.CURDIR:T}/${.TARGET}
 _MKTARGET_CREATE?=	${_MKMSG_CREATE} ${.CURDIR:T}/${.TARGET}
 _MKTARGET_COMPILE?=	${_MKMSG_COMPILE} ${.CURDIR:T}/${.TARGET}
@@ -716,5 +696,36 @@ _MKTARGET_LINK?=	${_MKMSG_LINK} ${.CURDI
 _MKTARGET_LEX?=		${_MKMSG_LEX} ${.CURDIR:T}/${.TARGET}
 _MKTARGET_REMOVE?=	${_MKMSG_REMOVE} ${.TARGET}
 _MKTARGET_YACC?=	${_MKMSG_YACC} ${.CURDIR:T}/${.TARGET}
+
+_MKSHMSG_CREATE?=	${_MKSHMSG} " create "
+_MKSHMSG_INSTALL?=	${_MKSHMSG} "install "
+
+_MKSHTARGET_CREATE?=	${_MKSHMSG_CREATE} ${.OBJDIR:T}/${.TARGET}
+
+.if ${NEED_OWN_INSTALL_TARGET} == "yes"
+.if !target(install)
+install:	.NOTMAIN beforeinstall subdir-install realinstall afterinstall
+beforeinstall:	.NOTMAIN
+subdir-install:	.NOTMAIN beforeinstall
+realinstall:	.NOTMAIN beforeinstall
+afterinstall:	.NOTMAIN subdir-install realinstall
+.endif
+all:		.NOTMAIN realall .WAIT subdir-all
+subdir-all:	.NOTMAIN
+realall:	.NOTMAIN
+depend:		.NOTMAIN realdepend .WAIT subdir-depend
+subdir-depend:	.NOTMAIN
+realdepend:	.NOTMAIN
+distclean:	.NOTMAIN cleandir
+cleandir:	.NOTMAIN clean
+
+.if ${MKAUTODEP} != "no"
+dependall:	.NOTMAIN .MAKE
+	@cd ${.CURDIR}; ${MAKE} realall
+.else
+dependall:	.NOTMAIN realdepend .MAKE
+	@cd ${.CURDIR}; ${MAKE} realall
+.endif
+.endif
 
 .endif	# !defined(_BSD_OWN_MK_)
Index: share/mk/bsd.prog.mk
===================================================================
RCS file: /cvsroot/src/share/mk/bsd.prog.mk,v
retrieving revision 1.194
diff -u -p -r1.194 bsd.prog.mk
--- share/mk/bsd.prog.mk	16 Nov 2003 14:09:00 -0000	1.194
+++ share/mk/bsd.prog.mk	7 Dec 2003 20:51:38 -0000
@@ -184,7 +184,8 @@ ${PROG}: .gdbinit ${LIBCRT0} ${OBJS} ${L
 .endif	# defined(DESTDIR)
 .endif	# !commands(${PROG})
 
-${PROG}.ro: ${OBJS} ${DPADD}
+${PROG}.ro: ${OBJS} ${DPADD} ${AUTO_DEPEND}
+	${_MKTARGET_LINK}
 	${LD} -r -dc -o ${.TARGET} ${OBJS}
 
 .endif	# defined(OBJS) && !empty(OBJS)
@@ -194,7 +195,7 @@ MAN=	${PROG}.1
 .endif	# !defined(MAN)
 .endif	# defined(PROG)
 
-realall: ${PROG} ${SCRIPTS}
+realall: ${PROG} ${SCRIPTS} ${AUTO_DEPEND}
 
 cleanprog: cleanobjs cleanextra
 	rm -f a.out [Ee]rrs mklog core *.core .gdbinit ${PROG}
Index: sys/arch/i386/stand/lib/Makefile
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/stand/lib/Makefile,v
retrieving revision 1.22
diff -u -p -r1.22 Makefile
--- sys/arch/i386/stand/lib/Makefile	16 Apr 2003 22:30:40 -0000	1.22
+++ sys/arch/i386/stand/lib/Makefile	7 Dec 2003 20:51:38 -0000
@@ -44,7 +44,7 @@ SRCS+= biosmca.S biosmemps2.S
 .undef DESTDIR
 .include <bsd.lib.mk>
 
-lib${LIB}.o:: ${OBJS}
+lib${LIB}.o:: ${OBJS} ${AUTODEPEND}
 	@echo building standard ${LIB} library
 	@rm -f lib${LIB}.o
 	@${LD} -r -o lib${LIB}.o `lorder ${OBJS} | tsort`
Index: sys/conf/Makefile.kern.inc
===================================================================
RCS file: /cvsroot/src/sys/conf/Makefile.kern.inc,v
retrieving revision 1.51
diff -u -p -r1.51 Makefile.kern.inc
--- sys/conf/Makefile.kern.inc	4 Dec 2003 02:33:41 -0000	1.51
+++ sys/conf/Makefile.kern.inc	7 Dec 2003 20:51:40 -0000
@@ -167,6 +167,9 @@ ${_cfile:T:R}.o: ${_cfile}
 #	${SYSTEM_LD_TAIL}
 SYSTEM_OBJ?=	${MD_OBJS} ${MI_OBJS} ${OBJS} ${LIBCOMPAT} ${LIBKERN}
 SYSTEM_DEP?=	Makefile ${SYSTEM_OBJ} .gdbinit
+.if !target(.depend) && ${MKAUTODEP} != "no" && !make(depend)
+SYSTEM_DEP+=	.WAIT .depend
+.endif
 SYSTEM_LD_HEAD?=@rm -f $@
 SYSTEM_LD?=	@${_MKSHMSG} "   link  ${.CURDIR:T}/${.TARGET}"; \
 		${_MKSHECHO}\
@@ -220,7 +223,7 @@ vers.o: ${SYSTEM_DEP} ${SYSTEM_SWAP_DEP}
 	${_MKMSG_CREATE} vers.c
 	${HOST_SH} $S/conf/newvers.sh
 	${_MKTARGET_COMPILE}
-	${CC} ${CFLAGS} ${CPPFLAGS} ${PROF} -c vers.c
+	${CC} ${CFLAGS} ${CPPFLAGS:N-MD} ${PROF} -c vers.c
 .endif
 
 .if !target(config_time.h)
@@ -284,7 +287,33 @@ clean: __CLEANKERNEL
 depend: .depend
 dependall: depend all
 
-.if !target(.depend)
+.if !target(.depend)							# {
+.if ${MKAUTODEP} != "no"						# {
+DEP_FILES?=	${OBJS:.o=.d} assym.d
+.if make(depend)							# {
+.for f in ${MD_CFILES} ${MI_CFILES} ${CFILES}
+${f:T:R}.d: ${f}
+	${MKDEP} -f ${.TARGET} -- ${MKDEP_CFLAGS} ${CPPFLAGS} ${f}
+.endfor
+.for f in ${MD_SFILES} ${SFILES}
+${f:T:R}.d: ${f}
+	${MKDEP} -f ${.TARGET} -- ${MKDEP_AFLAGS} ${CPPFLAGS} ${f}
+.endfor
+.else									# } {
+CPPFLAGS+=	-MD
+.OPTIONAL: ${DEP_FILES}
+.endif									# }
+
+assym.dep: assym.h config_time.h
+	cat ${GENASSYM} ${GENASSYM_EXTRAS} | \
+	    sh $S/kern/genassym.sh ${MKDEP} -f assym.dep -- \
+	    ${CFLAGS} ${CPPFLAGS:N-MD}
+assym.d: assym.dep
+	sed -e 's/.*\.o:.*\.c/assym.h:/' < assym.dep >${.TARGET}
+
+.depend: ${DEP_FILES}
+	${MKDEP} -d -- ${.ALLSRC}
+.else	# {MKAUTODEP}							# } {
 SRCS?=		${MD_SFILES} ${MD_CFILES} ${MI_CFILES} ${CFILES} ${SFILES}
 MKDEP_AFLAGS?=	${AFLAGS}
 MKDEP_CFLAGS?=	${CFLAGS}
@@ -298,7 +327,8 @@ MKDEP_CFLAGS?=	${CFLAGS}
 	    ${CFLAGS} ${CPPFLAGS}
 	@sed -e 's/.*\.o:.*\.c/assym.h:/' < assym.dep >> .depend
 	@rm -f assym.dep
-.endif
+.endif									# }
+.endif									# }
 
 .if !target(lint)
 ALLSFILES?=	${MD_SFILES} ${SFILES}
Index: sys/lib/libkern/Makefile
===================================================================
RCS file: /cvsroot/src/sys/lib/libkern/Makefile,v
retrieving revision 1.71
diff -u -p -r1.71 Makefile
--- sys/lib/libkern/Makefile	7 Jul 2003 13:20:17 -0000	1.71
+++ sys/lib/libkern/Makefile	7 Dec 2003 20:51:40 -0000
@@ -88,12 +88,12 @@ libinstall::
 .undef DESTDIR
 .include <bsd.lib.mk>
 
-lib${LIB}.o:: ${OBJS}
+lib${LIB}.o:: ${OBJS} ${AUTO_DEPEND}
 	@echo building standard ${LIB} library
 	@rm -f lib${LIB}.o
 	@${LD} -r -o lib${LIB}.o `NM=${NM} ${LORDER} ${OBJS} | ${TSORT}`
 
-lib${LIB}.po:: ${POBJS}
+lib${LIB}.po:: ${POBJS} ${AUTO_DEPEND}
 	@echo building profiled ${LIB} library
 	@rm -f lib${LIB}.po
 	@${LD} -r -o lib${LIB}.po `NM=${NM} ${LORDER} ${POBJS} | ${TSORT}`
Index: sys/lib/libsa/Makefile
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/Makefile,v
retrieving revision 1.54
diff -u -p -r1.54 Makefile
--- sys/lib/libsa/Makefile	11 Apr 2003 11:31:44 -0000	1.54
+++ sys/lib/libsa/Makefile	7 Dec 2003 20:51:40 -0000
@@ -56,7 +56,7 @@ libinstall::
 .undef DESTDIR
 .include <bsd.lib.mk>
 
-lib${LIB}.o:: ${OBJS}
+lib${LIB}.o:: ${OBJS} ${AUTO_DEPEND}
 	@echo building standard ${LIB} library
 	@rm -f lib${LIB}.o
 	@${LD} -r -o lib${LIB}.o `lorder ${OBJS} | tsort`
Index: sys/lib/libz/Makefile
===================================================================
RCS file: /cvsroot/src/sys/lib/libz/Makefile,v
retrieving revision 1.13
diff -u -p -r1.13 Makefile
--- sys/lib/libz/Makefile	25 Mar 2003 22:48:43 -0000	1.13
+++ sys/lib/libz/Makefile	7 Dec 2003 20:51:40 -0000
@@ -26,7 +26,7 @@ libinstall::
 .undef DESTDIR
 .include <bsd.lib.mk>
 
-lib${LIB}.o:: ${OBJS}
+lib${LIB}.o:: ${OBJS} ${AUTO_DEPEND}
 	@echo building standard ${LIB} library
 	@rm -f lib${LIB}.o
 	@${LD} -r -o lib${LIB}.o `lorder ${OBJS} | tsort`
-- 
David Laight: david@l8s.co.uk