build.sh revision 1.89
1#! /usr/bin/env sh
2#	$NetBSD: build.sh,v 1.89 2003/01/31 01:38:35 lukem Exp $
3#
4# Copyright (c) 2001-2003 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Todd Vierling and Luke Mewburn.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18# 3. All advertising materials mentioning features or use of this software
19#    must display the following acknowledgement:
20#        This product includes software developed by the NetBSD
21#        Foundation, Inc. and its contributors.
22# 4. Neither the name of The NetBSD Foundation nor the names of its
23#    contributors may be used to endorse or promote products derived
24#    from this software without specific prior written permission.
25#
26# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36# POSSIBILITY OF SUCH DAMAGE.
37#
38#
39# Top level build wrapper, for a system containing no tools.
40#
41# This script should run on any POSIX-compliant shell.  For systems
42# with a strange /bin/sh, "ksh" or "bash" may be an ample alternative.
43#
44# Note, however, that due to the way the interpreter is invoked above,
45# if a POSIX-compliant shell is the first in the PATH, you won't have
46# to take any further action.
47#
48
49progname=${0##*/}
50toppid=$$
51trap "exit 1" 1 2 3 15
52
53bomb()
54{
55	cat >&2 <<ERRORMESSAGE
56
57ERROR: $@
58*** BUILD ABORTED ***
59ERRORMESSAGE
60	kill $toppid		# in case we were invoked from a subshell
61	exit 1
62}
63
64
65initdefaults()
66{
67	cd "$(dirname $0)"
68	[ -d usr.bin/make ] ||
69	    bomb "build.sh must be run from the top source level"
70	[ -f share/mk/bsd.own.mk ] ||
71	    bomb "src/share/mk is missing; please re-fetch the source tree"
72
73	uname_s=$(uname -s 2>/dev/null)
74	uname_m=$(uname -m 2>/dev/null)
75
76	# If $PWD is a valid name of the current directory, POSIX mandates
77	# that pwd return it by default which causes problems in the
78	# presence of symlinks.  Unsetting PWD is simpler than changing
79	# every occurrence of pwd to use -P.
80	#
81	# XXX Except that doesn't work on Solaris.
82	unset PWD
83	if [ "${uname_s}" = "SunOS" ]; then
84		TOP=$(pwd -P)
85	else
86		TOP=$(pwd)
87	fi
88
89	# Set defaults.
90	toolprefix=nb
91	MAKEFLAGS=
92	makeenv=
93	makewrapper=
94	runcmd=
95	operations=
96	removedirs=
97	do_expertmode=false
98	do_rebuildmake=false
99	do_removedirs=false
100
101	# do_{operation}=true if given operation is requested.
102	#
103	do_tools=false
104	do_obj=false
105	do_build=false
106	do_distribution=false
107	do_release=false
108	do_kernel=false
109	do_install=false
110	do_sets=false
111}
112
113getarch()
114{
115	# Translate a MACHINE into a default MACHINE_ARCH.
116	#
117	case $MACHINE in
118
119	acorn26|acorn32|cats|netwinder|shark|*arm)
120		MACHINE_ARCH=arm
121		;;
122
123	sun2)
124		MACHINE_ARCH=m68000
125		;;
126
127	amiga|atari|cesfic|hp300|sun3|*68k)
128		MACHINE_ARCH=m68k
129		;;
130
131	mipsco|newsmips|sbmips|sgimips)
132		MACHINE_ARCH=mipseb
133		;;
134
135	algor|arc|cobalt|evbmips|hpcmips|playstation2|pmax)
136		MACHINE_ARCH=mipsel
137		;;
138
139	pc532)
140		MACHINE_ARCH=ns32k
141		;;
142
143	bebox|prep|sandpoint|*ppc)
144		MACHINE_ARCH=powerpc
145		;;
146
147	evbsh3|mmeye)
148		MACHINE_ARCH=sh3eb
149		;;
150
151	dreamcast|hpcsh)
152		MACHINE_ARCH=sh3el
153		;;
154
155	hp700)
156		MACHINE_ARCH=hppa
157		;;
158
159	evbsh5)
160		MACHINE_ARCH=sh5el
161		;;
162
163	alpha|i386|sparc|sparc64|vax|x86_64)
164		MACHINE_ARCH=$MACHINE
165		;;
166
167	*)
168		bomb "unknown target MACHINE: $MACHINE"
169		;;
170
171	esac
172}
173
174validatearch()
175{
176	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
177	#
178	case $MACHINE_ARCH in
179
180	alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|ns32k|powerpc|sh[35]e[bl]|sparc|sparc64|vax|x86_64)
181		;;
182
183	*)
184		bomb "unknown target MACHINE_ARCH: $MACHINE_ARCH"
185		;;
186
187	esac
188}
189
190getmakevar()
191{
192	[ -x $make ] || bomb "getmakevar $1: $make is not executable"
193	$make -m ${TOP}/share/mk -s -f- _x_ <<EOF
194_x_:
195	echo \${$1}
196.include <bsd.prog.mk>
197.include <bsd.kernobj.mk>
198EOF
199}
200
201safe_getmakevar()
202{
203	# getmakevar() doesn't work properly if $make hasn't yet been built,
204	# which can happen when running with the "-n" option.
205	# safe_getmakevar() deals with this by emitting a literal '$'
206	# followed by the variable name, instead of trying to find the
207	# variable's value.
208	#
209
210	if [ -x $make ]; then
211		getmakevar "$1"
212	else
213		echo "\$$1"
214	fi
215}
216
217resolvepath()
218{
219	case $OPTARG in
220	/*)
221		;;
222	*)
223		OPTARG="$TOP/$OPTARG"
224		;;
225	esac
226}
227
228usage()
229{
230	if [ -n "$*" ]; then
231		echo ""
232		echo "${progname}: $*"
233	fi
234	cat <<_usage_
235
236Usage: ${progname} [-EnorUu] [-a arch] [-B buildid] [-D dest] [-j njob] [-M obj]
237		[-m mach] [-O obj] [-R release] [-T tools] [-V var=[value]]
238		[-w wrapper]   operation [...]
239
240 Build operations (all imply "obj" and "tools"):
241    build		Run "make build"
242    distribution	Run "make distribution" (includes DESTDIR/etc/ files)
243    release		Run "make release" (includes kernels & distrib media)
244
245 Other operations:
246    help		Show this message (and exit)
247    makewrapper		Create ${toolprefix}make-${MACHINE} wrapper and ${toolprefix}make.  (Always done)
248    obj			Run "make obj" (default unless -o is used)
249    tools 		Build and install tools
250    kernel=conf		Build kernel with config file \`conf'
251    install=idir	Run "make installworld" to \`idir'
252			(useful after 'distribution' or 'release')
253    sets		Create distribution sets in RELEASEDIR
254
255 Options:
256    -a arch	Set MACHINE_ARCH to arch (otherwise deduced from MACHINE)
257    -B buildId	Set BUILDID to buildId
258    -D dest	Set DESTDIR to dest
259    -E		Set "expert" mode; disables some DESTDIR checks
260    -j njob	Run up to njob jobs in parallel; see make(1)
261    -M obj	Set obj root directory to obj (sets MAKEOBJDIRPREFIX)
262    -m mach	Set MACHINE to mach (not required if NetBSD native)
263    -n		Show commands that would be executed, but do not execute them
264    -O obj	Set obj root directory to obj (sets a MAKEOBJDIR pattern)
265    -o		Set MKOBJDIRS=no (do not create objdirs at start of build)
266    -R release	Set RELEASEDIR to release
267    -r		Remove contents of TOOLDIR and DESTDIR before building
268    -T tools	Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
269		the environment, ${toolprefix}make will be (re)built unconditionally
270    -U		Set UNPRIVED (build without requiring root privileges)
271    -u		Set UPDATE (do not run "make clean" first)
272    -V v=[val]	Set variable \`v' to \`val'
273    -w wrapper	Create ${toolprefix}make script as wrapper
274		(Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE})
275
276_usage_
277	exit 1
278}
279
280parseoptions()
281{
282	opts='a:B:bD:dEhi:j:k:M:m:nO:oR:rT:tUuV:w:'
283	opt_a=no
284
285	if type getopts >/dev/null 2>&1; then
286		# Use POSIX getopts.
287		getoptcmd='getopts $opts opt && opt=-$opt'
288		optargcmd=':'
289		optremcmd='shift $(($OPTIND -1))'
290	else
291		type getopt >/dev/null 2>&1 ||
292		    bomb "/bin/sh shell is too old; try ksh or bash"
293
294		# Use old-style getopt(1) (doesn't handle whitespace in args).
295		args="$(getopt $opts $*)"
296		[ $? = 0 ] || usage
297		set -- $args
298
299		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
300		optargcmd='OPTARG="$1"; shift'
301		optremcmd=':'
302	fi
303
304	# Parse command line options.
305	#
306	while eval $getoptcmd; do
307		case $opt in
308
309		-a)
310			eval $optargcmd
311			MACHINE_ARCH=$OPTARG
312			opt_a=yes
313			;;
314
315		-B)
316			eval $optargcmd
317			BUILDID=$OPTARG
318			;;
319
320		-b)
321			usage "'-b' has been replaced by 'makewrapper'"
322			;;
323
324		-D)
325			eval $optargcmd; resolvepath
326			DESTDIR="$OPTARG"
327			export DESTDIR
328			makeenv="$makeenv DESTDIR"
329			;;
330
331		-d)
332			usage "'-d' has been replaced by 'distribution'"
333			;;
334
335		-E)
336			do_expertmode=true
337			;;
338
339		-i)
340			usage "'-i idir' has been replaced by 'install=idir'"
341			;;
342
343		-j)
344			eval $optargcmd
345			parallel="-j $OPTARG"
346			;;
347
348		-k)
349			usage "'-k conf' has been replaced by 'kernel=conf'"
350			;;
351
352		-M)
353			eval $optargcmd; resolvepath
354			MAKEOBJDIRPREFIX="$OPTARG"
355			export MAKEOBJDIRPREFIX
356			makeobjdir=$OPTARG
357			makeenv="$makeenv MAKEOBJDIRPREFIX"
358			;;
359
360			# -m overrides MACHINE_ARCH unless "-a" is specified
361		-m)
362			eval $optargcmd
363			MACHINE=$OPTARG
364			[ "$opt_a" != "yes" ] && getarch
365			;;
366
367		-n)
368			runcmd=echo
369			;;
370
371		-O)
372			eval $optargcmd; resolvepath
373			MAKEOBJDIR="\${.CURDIR:C,^$TOP,$OPTARG,}"
374			export MAKEOBJDIR
375			makeobjdir=$OPTARG
376			makeenv="$makeenv MAKEOBJDIR"
377			;;
378
379		-o)
380			MKOBJDIRS=no
381			;;
382
383		-R)
384			eval $optargcmd; resolvepath
385			RELEASEDIR=$OPTARG
386			export RELEASEDIR
387			makeenv="$makeenv RELEASEDIR"
388			;;
389
390		-r)
391			do_removedirs=true
392			do_rebuildmake=true
393			;;
394
395		-T)
396			eval $optargcmd; resolvepath
397			TOOLDIR="$OPTARG"
398			export TOOLDIR
399			;;
400
401		-t)
402			usage "'-t' has been replaced by 'tools'"
403			;;
404
405		-U)
406			UNPRIVED=yes
407			export UNPRIVED
408			makeenv="$makeenv UNPRIVED"
409			;;
410
411		-u)
412			UPDATE=yes
413			export UPDATE
414			makeenv="$makeenv UPDATE"
415			;;
416
417		-V)
418			eval $optargcmd
419			case "${OPTARG}" in
420		    # XXX: consider restricting which variables can be changed?
421			[a-zA-Z_][a-zA-Z_0-9]*=*)
422				var=${OPTARG%%=*}
423				value=${OPTARG#*=}
424				eval "${var}=\"${value}\"; export ${var}"
425				makeenv="$makeenv ${var}"
426				;;
427			*)
428				usage "-V argument must be of the form 'var=[value]'"
429				;;
430			esac
431			;;
432
433		-w)
434			eval $optargcmd; resolvepath
435			makewrapper="$OPTARG"
436			;;
437
438		--)
439			break
440			;;
441
442		-'?'|-h)
443			usage
444			;;
445
446		esac
447	done
448
449	# Validate operations.
450	#
451	eval $optremcmd
452	while [ $# -gt 0 ]; do
453		op=$1; shift
454		operations="$operations $op"
455
456		case "$op" in
457
458		help)
459			usage
460			;;
461
462		makewrapper|obj|tools|build|distribution|release|sets)
463			;;
464
465		kernel=*)
466			arg=${op#*=}
467			op=${op%%=*}
468			if [ -z "${arg}" ]; then
469				bomb "Must supply a kernel name with \`kernel=...'"
470			fi
471			;;
472
473		install=*)
474			arg=${op#*=}
475			op=${op%%=*}
476			if [ -z "${arg}" ]; then
477				bomb "Must supply a directory with \`install=...'"
478			fi
479			;;
480
481		*)
482			usage "Unknown operation \`${op}'"
483			;;
484
485		esac
486		eval do_$op=true
487	done
488	if [ -z "${operations}" ]; then
489		usage "Missing operation to perform."
490	fi
491
492	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
493	#
494	if [ -z "$MACHINE" ]; then
495		if [ "${uname_s}" != "NetBSD" ]; then
496			bomb "MACHINE must be set, or -m must be used, for cross builds."
497		fi
498		MACHINE=${uname_m}
499	fi
500	[ -n "$MACHINE_ARCH" ] || getarch
501	validatearch
502
503	# Set up default make(1) environment.
504	#
505	makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
506	if [ ! -z "$BUILDID" ]; then
507		makeenv="$makeenv BUILDID"
508	fi
509	MAKEFLAGS="-m $TOP/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
510	export MAKEFLAGS MACHINE MACHINE_ARCH
511}
512
513rebuildmake()
514{
515	# Test make source file timestamps against installed ${toolprefix}make
516	# binary, if TOOLDIR is pre-set.
517	#
518	# Note that we do NOT try to grovel "mk.conf" here to find out if
519	# TOOLDIR is set there, because it can contain make variable
520	# expansions and other stuff only parseable *after* we have a working
521	# ${toolprefix}make.  So this logic can only work if the user has
522	# pre-set TOOLDIR in the environment or used the -T option to build.sh.
523	#
524	make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
525	if [ -x $make ]; then
526		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
527			if [ $f -nt $make ]; then
528				do_rebuildmake=true
529				break
530			fi
531		done
532	else
533		do_rebuildmake=true
534	fi
535
536	# Build bootstrap ${toolprefix}make if needed.
537	if $do_rebuildmake; then
538		$runcmd echo "===> Bootstrapping ${toolprefix}make"
539		tmpdir="${TMPDIR-/tmp}/nbbuild$$"
540
541		$runcmd mkdir "$tmpdir" || bomb "cannot mkdir: $tmpdir"
542		trap "cd /; rm -r -f \"$tmpdir\"" 0
543		$runcmd cd "$tmpdir"
544
545		$runcmd env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
546			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
547			"$TOP/tools/make/configure" ||
548		    bomb "configure of ${toolprefix}make failed"
549		$runcmd sh buildmake.sh ||
550		    bomb "build of ${toolprefix}make failed"
551
552		make="$tmpdir/${toolprefix}make"
553		$runcmd cd "$TOP"
554		$runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
555	fi
556}
557
558validatemakeparams()
559{
560	if [ "$runcmd" = "echo" ]; then
561		TOOLCHAIN_MISSING=no
562		EXTERNAL_TOOLCHAIN=""
563	else
564		TOOLCHAIN_MISSING=$(getmakevar TOOLCHAIN_MISSING)
565		EXTERNAL_TOOLCHAIN=$(getmakevar EXTERNAL_TOOLCHAIN)
566	fi
567	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
568	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
569		$runcmd echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
570		$runcmd echo "	MACHINE:      ${MACHINE}"
571		$runcmd echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
572		$runcmd echo ""
573		$runcmd echo "All builds for this platform should be done via a traditional make"
574		$runcmd echo "If you wish to use an external cross-toolchain, set"
575		$runcmd echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
576		$runcmd echo "in either the environment or mk.conf and rerun"
577		$runcmd echo "	$progname $*"
578		exit 1
579	fi
580
581	# If TOOLDIR isn't already set, make objdirs in "tools" in case the
582	# default setting from <bsd.own.mk> is used.
583	#
584	if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
585		$runcmd cd tools
586		$runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= ||
587		    bomb "make obj failed in tools"
588		$runcmd cd "$TOP"
589	fi
590
591	# If setting -M or -O to root an obj dir make sure the base directory
592	# is made before continuing as bsd.own.mk will need this to pick up
593	# _SRC_TOP_OBJ_
594	#
595	if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
596		$runcmd mkdir -p "$makeobjdir"
597	fi
598
599	# Find DESTDIR and TOOLDIR.
600	#
601	DESTDIR=$(safe_getmakevar DESTDIR)
602	$runcmd echo "===> DESTDIR path: $DESTDIR"
603	TOOLDIR=$(safe_getmakevar TOOLDIR)
604	$runcmd echo "===> TOOLDIR path: $TOOLDIR"
605	export DESTDIR TOOLDIR
606
607	# Check validity of TOOLDIR and DESTDIR.
608	#
609	if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
610		bomb "TOOLDIR '$TOOLDIR' invalid"
611	fi
612	removedirs="$TOOLDIR"
613
614	if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
615		if $do_build || $do_distribution || $do_release; then
616			if ! $do_build || \
617			   [ "${uname_s}" != "NetBSD" ] || \
618			   [ "${uname_m}" != "$MACHINE" ]; then
619				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
620			fi
621			if ! $do_expertmode; then
622				bomb "DESTDIR must != / for non -E (expert) builds"
623			fi
624			$runcmd echo "===> WARNING: Building to /, in expert mode."
625			$runcmd echo "===>          This may cause your system to break!  Reasons include:"
626			$runcmd echo "===>             - your kernel is not up to date"
627			$runcmd echo "===>             - the libraries or toolchain have changed"
628			$runcmd echo "===>          YOU HAVE BEEN WARNED!"
629		fi
630	else
631		removedirs="$removedirs $DESTDIR"
632	fi
633	if $do_build || $do_distribution || $do_release; then
634		if ! $do_expertmode && \
635		    [ $(id -u 2>/dev/null) -ne 0 ] &&\
636		    [ -z "$UNPRIVED" ] ; then
637			bomb "-U or -E must be set for build as an unprivileged user."
638		fi
639        fi
640}
641
642
643createmakewrapper()
644{
645	# Remove the target directories.
646	#
647	if $do_removedirs; then
648		for f in $removedirs; do
649			$runcmd echo "===> Removing $f"
650			$runcmd rm -r -f $f
651		done
652	fi
653
654	# Recreate $TOOLDIR.
655	#
656	$runcmd mkdir -p "$TOOLDIR/bin" || bomb "mkdir of '$TOOLDIR/bin' failed"
657
658	# Install ${toolprefix}make if it was built.
659	#
660	if $do_rebuildmake; then
661		$runcmd rm -f "$TOOLDIR/bin/${toolprefix}make"
662		$runcmd cp $make "$TOOLDIR/bin/${toolprefix}make" ||
663		    bomb "failed to install \$TOOLDIR/bin/${toolprefix}make"
664		make="$TOOLDIR/bin/${toolprefix}make"
665		$runcmd echo "===> Created ${make}"
666		$runcmd rm -r -f "$tmpdir"
667		trap 0
668	fi
669
670	# Build a ${toolprefix}make wrapper script, usable by hand as
671	# well as by build.sh.
672	#
673	if [ -z "$makewrapper" ]; then
674		makewrapper="$TOOLDIR/bin/${toolprefix}make-$MACHINE"
675		if [ ! -z "$BUILDID" ]; then
676			makewrapper="$makewrapper-$BUILDID"
677		fi
678	fi
679
680	$runcmd rm -f "$makewrapper"
681	if [ "$runcmd" = "echo" ]; then
682		echo 'cat <<EOF >'$makewrapper
683		makewrapout=
684	else
685		makewrapout=">>\$makewrapper"
686	fi
687
688	eval cat <<EOF $makewrapout
689#! /bin/sh
690# Set proper variables to allow easy "make" building of a NetBSD subtree.
691# Generated from:  \$NetBSD: build.sh,v 1.89 2003/01/31 01:38:35 lukem Exp $
692#
693
694EOF
695	for f in $makeenv; do
696		eval echo "$f=\'\$$(echo $f)\'\;\ export\ $f" $makewrapout
697	done
698	eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
699
700	eval cat <<EOF $makewrapout
701
702exec "\$TOOLDIR/bin/${toolprefix}make" \${1+"\$@"}
703EOF
704	[ "$runcmd" = "echo" ] && echo EOF
705	$runcmd chmod +x "$makewrapper"
706	$runcmd echo "===> Updated ${makewrapper}"
707}
708
709buildtools()
710{
711	if [ "$MKOBJDIRS" != "no" ]; then
712		$runcmd "$makewrapper" $parallel obj-tools ||
713		    bomb "failed to make obj-tools"
714	fi
715	$runcmd cd tools
716	if [ -z "$UPDATE" ]; then
717		cleandir=cleandir
718	else
719		cleandir=
720	fi
721	$runcmd "$makewrapper" ${cleandir} dependall install ||
722	    bomb "failed to make tools"
723}
724
725buildkernel()
726{
727	kernconf="$1"
728	if ! $do_tools; then
729		# Building tools every time we build a kernel is clearly
730		# unnecessary.  We could try to figure out whether rebuilding
731		# the tools is necessary this time, but it doesn't seem worth
732		# the trouble.  Instead, we say it's the user's responsibility
733		# to rebuild the tools if necessary.
734		#
735		$runcmd echo "===> Building kernel without building new tools"
736	fi
737	$runcmd echo "===> Building kernel ${kernconf}"
738	if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
739		# The correct value of KERNOBJDIR might
740		# depend on a prior "make obj" in
741		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
742		#
743		KERNSRCDIR="$(safe_getmakevar KERNSRCDIR)"
744		KERNARCHDIR="$(safe_getmakevar KERNARCHDIR)"
745		$runcmd cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
746		$runcmd "$makewrapper" obj ||
747		    bomb "failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
748		$runcmd cd "$TOP"
749	fi
750	KERNCONFDIR="$(safe_getmakevar KERNCONFDIR)"
751	KERNOBJDIR="$(safe_getmakevar KERNOBJDIR)"
752	case "${kernconf}" in
753	*/*)
754		kernconfpath="${kernconf}"
755		kernconfbase="$(basename "${kernconf}")"
756		;;
757	*)
758		kernconfpath="${KERNCONFDIR}/${kernconf}"
759		kernconfbase="${kernconf}"
760		;;
761	esac
762	kernbuilddir="${KERNOBJDIR}/${kernconfbase}"
763	$runcmd echo "===> Kernel build directory: ${kernbuilddir}"
764	$runcmd mkdir -p "${kernbuilddir}" ||
765	    bomb "cannot mkdir: ${kernbuilddir}"
766	if [ -z "$UPDATE" ]; then
767		$runcmd cd "${kernbuilddir}"
768		$runcmd "$makewrapper" cleandir ||
769		    bomb "make cleandir failed in ${kernbuilddir}"
770		$runcmd cd "$TOP"
771	fi
772	$runcmd "${TOOLDIR}/bin/${toolprefix}config" -b "${kernbuilddir}" \
773		-s "${TOP}/sys" "${kernconfpath}" ||
774	    bomb "${toolprefix}config failed for ${kernconf}"
775	$runcmd cd "${kernbuilddir}"
776	$runcmd "$makewrapper" depend ||
777	    bomb "make depend failed in ${kernbuilddir}"
778	$runcmd "$makewrapper" $parallel all ||
779	    bomb "make all failed in ${kernbuilddir}"
780	$runcmd echo "===> New kernel should be in:"
781	$runcmd echo "	${kernbuilddir}"
782}
783
784installworld()
785{
786	dir="$1"
787	${runcmd} "$makewrapper" INSTALLWORLDDIR="${dir}" installworld ||
788	    bomb "failed to make installworld to ${dir}"
789}
790
791
792main()
793{
794	initdefaults
795	parseoptions "$@"
796	rebuildmake
797	validatemakeparams
798	createmakewrapper
799
800	# Perform the operations.
801	#
802	for op in $operations; do
803		case "$op" in
804
805		makewrapper)
806			# no-op
807			;;
808
809		tools)
810			buildtools
811			;;
812
813		obj|build|distribution|release|sets)
814			${runcmd} "$makewrapper" $parallel $op ||
815			    bomb "failed to make $op"
816			;;
817
818		kernel=*)
819			arg=${op#*=}
820			buildkernel "${arg}"
821			;;
822
823		install=*)
824			arg=${op#*=}
825			if [ "${arg}" = "/" ] && \
826			    (	[ "${uname_s}" != "NetBSD" ] || \
827				[ "${uname_m}" != "$MACHINE" ] ); then
828				bomb "'${op}' must != / for cross builds."
829			fi
830			installworld "${arg}"
831			;;
832
833		*)
834			bomb "Unknown operation \`${op}'"
835			;;
836
837		esac
838	done
839}
840
841main "$@"
842