Subject: toolchain/18739: add "-k kernel" option to build.sh
To: None <gnats-bugs@gnats.netbsd.org>
From: Alan Barrett <apb@cequrux.com>
List: netbsd-bugs
Date: 10/20/2002 15:26:27
>Number:         18739
>Category:       toolchain
>Synopsis:       add "-k kernel" option to build.sh
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    toolchain-manager
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun Oct 20 06:28:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Alan Barrett
>Release:        NetBSD 1.6I
>Organization:
	Not much
>Environment:
Machine: i386
>Description:
	The appended patch adds a "-k kernel" option to build.sh,
	to build a kernel using the new tool chain.

	It also includes an update to BUILDING.mdoc, describing
	the -k option and adding some examples.

>How-To-Repeat:
	$ cd /usr/src
	$ sh ./build.sh -t -k GENERIC
	[doesn't work, "-k" is not a recognised option]

>Fix:

Index: build.sh
--- build.sh	2002/10/02 16:57:29	1.69
+++ build.sh	2002/10/20 12:08:10
@@ -90,6 +90,7 @@
 _x_:
 	echo \${$1}
 .include <bsd.prog.mk>
+.include <bsd.kernobj.mk>
 EOF
 }
 
@@ -102,27 +103,28 @@
 
 usage () {
 	echo "Usage:"
-	echo "$0 [-bdorUu] [-a arch] [-B buildid] [-j njob] [-m mach] "
+	echo "$0 [-bdorUu] [-a arch] [-B buildid] [-j njob] [-k kernel] [-m mach] "
 	echo "   [-w wrapper] [-D dest] [-M obj] [-O obj] [-R release] [-T tools]"
 	echo ""
-	echo "    -a: set MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
-	echo "    -B: set BUILDID to buildid"
+	echo "    -a arch: set MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
+	echo "    -B buildid: set BUILDID to buildid"
 	echo "    -b: build nbmake and nbmake wrapper script, if needed"
-	echo "    -D: set DESTDIR to dest"
+	echo "    -D dest: set DESTDIR to dest"
 	echo "    -d: build a full distribution into DESTDIR (including etc files)"
-	echo "    -j: Run up to njob jobs in parallel; see make(1)"
-	echo "    -M: set obj root directory to obj (sets MAKEOBJDIRPREFIX)"
-	echo "    -m: set MACHINE to mach (not required if NetBSD native)"
+	echo "    -j njob: run up to njob jobs in parallel; see make(1)"
+	echo "    -k kernel: build a kernel using the named configuration file"
+	echo "    -M obj: set obj root directory to obj (sets MAKEOBJDIRPREFIX)"
+	echo "    -m mach: set MACHINE to mach (not required if NetBSD native)"
 	echo "    -n: show commands that would be executed, but do not execute them"
-	echo "    -O: set obj root directory to obj (sets a MAKEOBJDIR pattern)"
+	echo "    -O obj: set obj root directory to obj (sets a MAKEOBJDIR pattern)"
 	echo "    -o: set MKOBJDIRS=no (do not create objdirs at start of build)"
-	echo "    -R: build a release (and set RELEASEDIR to release)"
+	echo "    -R release: build a release (and set RELEASEDIR to release)"
 	echo "    -r: remove contents of TOOLDIR and DESTDIR before building"
-	echo "    -T: set TOOLDIR to tools"
+	echo "    -T tools: set TOOLDIR to tools"
 	echo "    -t: build and install tools only (implies -b)"
 	echo "    -U: set UNPRIVED"
 	echo "    -u: set UPDATE"
-	echo "    -w: create nbmake script at wrapper (default TOOLDIR/bin/nbmake-MACHINE)"
+	echo "    -w wrapper: create nbmake script at wrapper (default TOOLDIR/bin/nbmake-MACHINE)"
 	echo ""
 	echo "Note: if -T is unset and TOOLDIR is not set in the environment,"
 	echo "      nbmake will be [re]built unconditionally."
@@ -133,13 +135,14 @@
 MAKEFLAGS=
 buildtarget=build
 do_buildsystem=true
+do_buildonlykernel=false
 do_buildonlytools=false
 do_rebuildmake=false
 do_removedirs=false
 makeenv=
 makewrapper=
 opt_a=no
-opts='a:B:bdhj:m:nortuw:D:M:O:R:T:U'
+opts='a:B:bdhj:k:m:nortuw:D:M:O:R:T:U'
 runcmd=
 
 if type getopts >/dev/null 2>&1; then
@@ -173,6 +176,10 @@
 	-j)	eval $optargcmd
 		parallel="-j $OPTARG";;
 
+	-k)	do_buildonlykernel=true; do_buildsystem=false
+		eval $optargcmd
+		KERNCONFNAME=$OPTARG;;
+
 	# -m overrides MACHINE_ARCH unless "-a" is specified
 	-m)	eval $optargcmd
 		MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
@@ -308,7 +315,6 @@
 if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
 	$runcmd cd tools
 	$runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= || exit 1
-	$runcmd cd ..
 fi
 
 #
@@ -412,14 +418,38 @@
 
 if $do_buildsystem; then
 	${runcmd-exec} "$makewrapper" $parallel $buildtarget
-elif $do_buildonlytools; then
-	if [ "$MKOBJDIRS" != "no" ]; then
-		$runcmd "$makewrapper" $parallel obj-tools || exit 1
+else
+	# One or more of do_buildonlytools and do_buildonlykernel
+	# might be set.  Do them in the appropriate order.
+	if $do_buildonlytools; then
+		if [ "$MKOBJDIRS" != "no" ]; then
+			$runcmd "$makewrapper" $parallel obj-tools || exit 1
+		fi
+		$runcmd cd tools
+		if [ "$UPDATE" = "" ]; then
+			$runcmd "$makewrapper" cleandir dependall install
+		else
+			$runcmd "$makewrapper" dependall install
+		fi
 	fi
-	$runcmd cd tools
-	if [ "$UPDATE" = "" ]; then
-		${runcmd-exec} "$makewrapper" cleandir dependall install
-	else
-		${runcmd-exec} "$makewrapper" dependall install
+	if $do_buildonlykernel; then
+		if [ "$runcmd" = "echo" ]; then
+			# shown symbolically with -n
+			# because getmakevar might not work yet
+			KERNCONFDIR='${KERNCONFDIR}'
+			KERNOBJDIR='${KERNOBJDIR}'
+		else
+			KERNCONFDIR="$( getmakevar KERNCONFDIR )"
+			KERNOBJDIR="$( getmakevar KERNOBJDIR )"
+		fi
+		$runcmd cd "${KERNCONFDIR}"
+		# XXX "make obj" doesn't seem to work here
+		$runcmd mkdir -p "${KERNOBJDIR}/${KERNCONFNAME}"
+		$runcmd "${TOOLDIR}/bin/nbconfig" \
+			-b "${KERNOBJDIR}/${KERNCONFNAME}" \
+			-s "${TOP}/sys" "${KERNCONFNAME}"
+		$runcmd cd "${KERNOBJDIR}/${KERNCONFNAME}"
+		$runcmd "$makewrapper" dependall
+		echo "New kernel should be in ${KERNOBJDIR}/${KERNCONFNAME}"
 	fi
 fi
Index: doc/BUILDING.mdoc
--- doc/BUILDING.mdoc	2002/09/21 08:19:29	1.1
+++ doc/BUILDING.mdoc	2002/10/20 13:22:50
@@ -824,6 +824,36 @@
 If you see build failures with -j, please save complete build logs 
 so the failures can be analyzed.
 .
+.It Sy -k Em kernel
+Build a new kernel.
+The
+.Em kernel
+argument is the base name of a configuration file suitable
+for use by
+.Xr config 8 .
+The configuration file is expected to be found in the
+.Sy KERNCONFDIR
+directory, which is typically
+.Sy sys/arch/MACHINE/conf .
+The new kernel will be built in a subdirectory of
+.Sy KERNOBJDIR ,
+which is typically
+.Sy sys/arch/MACHINE/compile
+or an associated object directory.
+In order to ensure that the kernel is built using up-to-date tools,
+it is strongly recommended that the tools be rebuilt (using the
+.Sy -t 
+option) in a separate invocation of
+.Nm build.sh
+prior to using the
+.Sy -k
+option, or that the
+.Sy -t
+and
+.Sy -k
+options be used together in a single invocation of
+.Nm build.sh .
+.
 .It Sy -m Em mach
 Set the value of
 .Sy MACHINE
@@ -962,6 +992,28 @@
 This script can be symlinked into a directory listed in
 .Sy PATH ,
 or called with an absolute path.
+.
+.Sh EXAMPLES
+.Bl -tag -width "build.sh"
+.It Li "build.sh -t"
+Build a new toolchain.
+.It Li "cd ${KERNCONFDIR} ; ${TOOLDIR}/bin/nbconfig GENERIC"
+Use the new version of
+.Xr config 8
+to prepare to build a new GENERIC kernel.
+.It Li "cd ${KERNOBJDIR}/GENERIC ; ${TOOLDIR}/bin/nbmake-${MACHINE} dependall"
+Use the new toolchain to build a new GENERIC kernel.
+.It Li "build.sh -t -k GENERIC"
+Build a new toolchain, and use the new toolchain to
+configure and build a new GENERIC kernel.
+.It Li "build.sh -U -d"
+Using unprivileged mode,
+build a complete distribution in
+.Sy DESTDIR .
+.It Li "build.sh -U -R /some/dir/RELEASE"
+Using unprivileged mode,
+build a complete release in the specified release directory.
+.El
 .
 .Sh OBSOLETE VARIABLES
 .
>Release-Note:
>Audit-Trail:
>Unformatted: