build.sh revision 1.91
1#! /usr/bin/env sh
2#	$NetBSD: build.sh,v 1.91 2003/02/16 04:35:03 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.
248			(Always done)
249    obj			Run "make obj" (default unless -o is used)
250    tools 		Build and install tools
251    kernel=conf		Build kernel with config file \`conf'
252    install=idir	Run "make installworld" to \`idir'
253			(useful after 'distribution' or 'release')
254    sets		Create distribution sets in RELEASEDIR
255
256 Options:
257    -a arch	Set MACHINE_ARCH to arch (otherwise deduced from MACHINE)
258    -B buildId	Set BUILDID to buildId
259    -D dest	Set DESTDIR to dest
260    -E		Set "expert" mode; disables some DESTDIR checks
261    -j njob	Run up to njob jobs in parallel; see make(1)
262    -M obj	Set obj root directory to obj (sets MAKEOBJDIRPREFIX)
263    -m mach	Set MACHINE to mach (not required if NetBSD native)
264    -n		Show commands that would be executed, but do not execute them
265    -O obj	Set obj root directory to obj (sets a MAKEOBJDIR pattern)
266    -o		Set MKOBJDIRS=no (do not create objdirs at start of build)
267    -R release	Set RELEASEDIR to release
268    -r		Remove contents of TOOLDIR and DESTDIR before building
269    -T tools	Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
270		the environment, ${toolprefix}make will be (re)built unconditionally
271    -U		Set UNPRIVED (build without requiring root privileges)
272    -u		Set UPDATE (do not run "make clean" first)
273    -V v=[val]	Set variable \`v' to \`val'
274    -w wrapper	Create ${toolprefix}make script as wrapper
275		(Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE})
276
277_usage_
278	exit 1
279}
280
281parseoptions()
282{
283	opts='a:B:bD:dEhi:j:k:M:m:nO:oR:rT:tUuV:w:'
284	opt_a=no
285
286	if type getopts >/dev/null 2>&1; then
287		# Use POSIX getopts.
288		getoptcmd='getopts $opts opt && opt=-$opt'
289		optargcmd=':'
290		optremcmd='shift $(($OPTIND -1))'
291	else
292		type getopt >/dev/null 2>&1 ||
293		    bomb "/bin/sh shell is too old; try ksh or bash"
294
295		# Use old-style getopt(1) (doesn't handle whitespace in args).
296		args="$(getopt $opts $*)"
297		[ $? = 0 ] || usage
298		set -- $args
299
300		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
301		optargcmd='OPTARG="$1"; shift'
302		optremcmd=':'
303	fi
304
305	# Parse command line options.
306	#
307	while eval $getoptcmd; do
308		case $opt in
309
310		-a)
311			eval $optargcmd
312			MACHINE_ARCH=$OPTARG
313			opt_a=yes
314			;;
315
316		-B)
317			eval $optargcmd
318			BUILDID=$OPTARG
319			;;
320
321		-b)
322			usage "'-b' has been replaced by 'makewrapper'"
323			;;
324
325		-D)
326			eval $optargcmd; resolvepath
327			DESTDIR="$OPTARG"
328			export DESTDIR
329			makeenv="$makeenv DESTDIR"
330			;;
331
332		-d)
333			usage "'-d' has been replaced by 'distribution'"
334			;;
335
336		-E)
337			do_expertmode=true
338			;;
339
340		-i)
341			usage "'-i idir' has been replaced by 'install=idir'"
342			;;
343
344		-j)
345			eval $optargcmd
346			parallel="-j $OPTARG"
347			;;
348
349		-k)
350			usage "'-k conf' has been replaced by 'kernel=conf'"
351			;;
352
353		-M)
354			eval $optargcmd; resolvepath
355			MAKEOBJDIRPREFIX="$OPTARG"
356			export MAKEOBJDIRPREFIX
357			makeobjdir=$OPTARG
358			makeenv="$makeenv MAKEOBJDIRPREFIX"
359			;;
360
361			# -m overrides MACHINE_ARCH unless "-a" is specified
362		-m)
363			eval $optargcmd
364			MACHINE=$OPTARG
365			[ "$opt_a" != "yes" ] && getarch
366			;;
367
368		-n)
369			runcmd=echo
370			;;
371
372		-O)
373			eval $optargcmd; resolvepath
374			MAKEOBJDIR="\${.CURDIR:C,^$TOP,$OPTARG,}"
375			export MAKEOBJDIR
376			makeobjdir=$OPTARG
377			makeenv="$makeenv MAKEOBJDIR"
378			;;
379
380		-o)
381			MKOBJDIRS=no
382			;;
383
384		-R)
385			eval $optargcmd; resolvepath
386			RELEASEDIR=$OPTARG
387			export RELEASEDIR
388			makeenv="$makeenv RELEASEDIR"
389			;;
390
391		-r)
392			do_removedirs=true
393			do_rebuildmake=true
394			;;
395
396		-T)
397			eval $optargcmd; resolvepath
398			TOOLDIR="$OPTARG"
399			export TOOLDIR
400			;;
401
402		-t)
403			usage "'-t' has been replaced by 'tools'"
404			;;
405
406		-U)
407			UNPRIVED=yes
408			export UNPRIVED
409			makeenv="$makeenv UNPRIVED"
410			;;
411
412		-u)
413			UPDATE=yes
414			export UPDATE
415			makeenv="$makeenv UPDATE"
416			;;
417
418		-V)
419			eval $optargcmd
420			case "${OPTARG}" in
421		    # XXX: consider restricting which variables can be changed?
422			[a-zA-Z_][a-zA-Z_0-9]*=*)
423				var=${OPTARG%%=*}
424				value=${OPTARG#*=}
425				eval "${var}=\"${value}\"; export ${var}"
426				makeenv="$makeenv ${var}"
427				;;
428			*)
429				usage "-V argument must be of the form 'var=[value]'"
430				;;
431			esac
432			;;
433
434		-w)
435			eval $optargcmd; resolvepath
436			makewrapper="$OPTARG"
437			;;
438
439		--)
440			break
441			;;
442
443		-'?'|-h)
444			usage
445			;;
446
447		esac
448	done
449
450	# Validate operations.
451	#
452	eval $optremcmd
453	while [ $# -gt 0 ]; do
454		op=$1; shift
455		operations="$operations $op"
456
457		case "$op" in
458
459		help)
460			usage
461			;;
462
463		makewrapper|obj|tools|build|distribution|release|sets)
464			;;
465
466		kernel=*)
467			arg=${op#*=}
468			op=${op%%=*}
469			if [ -z "${arg}" ]; then
470				bomb "Must supply a kernel name with \`kernel=...'"
471			fi
472			;;
473
474		install=*)
475			arg=${op#*=}
476			op=${op%%=*}
477			if [ -z "${arg}" ]; then
478				bomb "Must supply a directory with \`install=...'"
479			fi
480			;;
481
482		*)
483			usage "Unknown operation \`${op}'"
484			;;
485
486		esac
487		eval do_$op=true
488	done
489	if [ -z "${operations}" ]; then
490		usage "Missing operation to perform."
491	fi
492
493	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
494	#
495	if [ -z "$MACHINE" ]; then
496		if [ "${uname_s}" != "NetBSD" ]; then
497			bomb "MACHINE must be set, or -m must be used, for cross builds."
498		fi
499		MACHINE=${uname_m}
500	fi
501	[ -n "$MACHINE_ARCH" ] || getarch
502	validatearch
503
504	# Set up default make(1) environment.
505	#
506	makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
507	if [ ! -z "$BUILDID" ]; then
508		makeenv="$makeenv BUILDID"
509	fi
510	MAKEFLAGS="-m $TOP/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
511	export MAKEFLAGS MACHINE MACHINE_ARCH
512}
513
514rebuildmake()
515{
516	# Test make source file timestamps against installed ${toolprefix}make
517	# binary, if TOOLDIR is pre-set.
518	#
519	# Note that we do NOT try to grovel "mk.conf" here to find out if
520	# TOOLDIR is set there, because it can contain make variable
521	# expansions and other stuff only parseable *after* we have a working
522	# ${toolprefix}make.  So this logic can only work if the user has
523	# pre-set TOOLDIR in the environment or used the -T option to build.sh.
524	#
525	make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
526	if [ -x $make ]; then
527		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
528			if [ $f -nt $make ]; then
529				do_rebuildmake=true
530				break
531			fi
532		done
533	else
534		do_rebuildmake=true
535	fi
536
537	# Build bootstrap ${toolprefix}make if needed.
538	if $do_rebuildmake; then
539		$runcmd echo "===> Bootstrapping ${toolprefix}make"
540		tmpdir="${TMPDIR-/tmp}/nbbuild$$"
541
542		$runcmd mkdir "$tmpdir" || bomb "cannot mkdir: $tmpdir"
543		trap "cd /; rm -r -f \"$tmpdir\"" 0
544		$runcmd cd "$tmpdir"
545
546		$runcmd env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
547			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
548			"$TOP/tools/make/configure" ||
549		    bomb "configure of ${toolprefix}make failed"
550		$runcmd sh buildmake.sh ||
551		    bomb "build of ${toolprefix}make failed"
552
553		make="$tmpdir/${toolprefix}make"
554		$runcmd cd "$TOP"
555		$runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
556	fi
557}
558
559validatemakeparams()
560{
561	if [ "$runcmd" = "echo" ]; then
562		TOOLCHAIN_MISSING=no
563		EXTERNAL_TOOLCHAIN=""
564	else
565		TOOLCHAIN_MISSING=$(getmakevar TOOLCHAIN_MISSING)
566		EXTERNAL_TOOLCHAIN=$(getmakevar EXTERNAL_TOOLCHAIN)
567	fi
568	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
569	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
570		$runcmd echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
571		$runcmd echo "	MACHINE:      ${MACHINE}"
572		$runcmd echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
573		$runcmd echo ""
574		$runcmd echo "All builds for this platform should be done via a traditional make"
575		$runcmd echo "If you wish to use an external cross-toolchain, set"
576		$runcmd echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
577		$runcmd echo "in either the environment or mk.conf and rerun"
578		$runcmd echo "	$progname $*"
579		exit 1
580	fi
581
582	# If TOOLDIR isn't already set, make objdirs in "tools" in case the
583	# default setting from <bsd.own.mk> is used.
584	#
585	if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
586		$runcmd cd tools
587		$runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= ||
588		    bomb "make obj failed in tools"
589		$runcmd cd "$TOP"
590	fi
591
592	# If setting -M or -O to root an obj dir make sure the base directory
593	# is made before continuing as bsd.own.mk will need this to pick up
594	# _SRC_TOP_OBJ_
595	#
596	if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
597		$runcmd mkdir -p "$makeobjdir"
598	fi
599
600	# Find DESTDIR and TOOLDIR.
601	#
602	DESTDIR=$(safe_getmakevar DESTDIR)
603	$runcmd echo "===> DESTDIR path: $DESTDIR"
604	TOOLDIR=$(safe_getmakevar TOOLDIR)
605	$runcmd echo "===> TOOLDIR path: $TOOLDIR"
606	export DESTDIR TOOLDIR
607
608	# Check validity of TOOLDIR and DESTDIR.
609	#
610	if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
611		bomb "TOOLDIR '$TOOLDIR' invalid"
612	fi
613	removedirs="$TOOLDIR"
614
615	if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
616		if $do_build || $do_distribution || $do_release; then
617			if ! $do_build || \
618			   [ "${uname_s}" != "NetBSD" ] || \
619			   [ "${uname_m}" != "$MACHINE" ]; then
620				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
621			fi
622			if ! $do_expertmode; then
623				bomb "DESTDIR must != / for non -E (expert) builds"
624			fi
625			$runcmd echo "===> WARNING: Building to /, in expert mode."
626			$runcmd echo "===>          This may cause your system to break!  Reasons include:"
627			$runcmd echo "===>             - your kernel is not up to date"
628			$runcmd echo "===>             - the libraries or toolchain have changed"
629			$runcmd echo "===>          YOU HAVE BEEN WARNED!"
630		fi
631	else
632		removedirs="$removedirs $DESTDIR"
633	fi
634	if $do_build || $do_distribution || $do_release; then
635		if ! $do_expertmode && \
636		    [ $(id -u 2>/dev/null) -ne 0 ] &&\
637		    [ -z "$UNPRIVED" ] ; then
638			bomb "-U or -E must be set for build as an unprivileged user."
639		fi
640        fi
641}
642
643
644createmakewrapper()
645{
646	# Remove the target directories.
647	#
648	if $do_removedirs; then
649		for f in $removedirs; do
650			$runcmd echo "===> Removing $f"
651			$runcmd rm -r -f $f
652		done
653	fi
654
655	# Recreate $TOOLDIR.
656	#
657	$runcmd mkdir -p "$TOOLDIR/bin" || bomb "mkdir of '$TOOLDIR/bin' failed"
658
659	# Install ${toolprefix}make if it was built.
660	#
661	if $do_rebuildmake; then
662		$runcmd rm -f "$TOOLDIR/bin/${toolprefix}make"
663		$runcmd cp $make "$TOOLDIR/bin/${toolprefix}make" ||
664		    bomb "failed to install \$TOOLDIR/bin/${toolprefix}make"
665		make="$TOOLDIR/bin/${toolprefix}make"
666		$runcmd echo "===> Created ${make}"
667		$runcmd rm -r -f "$tmpdir"
668		trap 0
669	fi
670
671	# Build a ${toolprefix}make wrapper script, usable by hand as
672	# well as by build.sh.
673	#
674	if [ -z "$makewrapper" ]; then
675		makewrapper="$TOOLDIR/bin/${toolprefix}make-$MACHINE"
676		if [ ! -z "$BUILDID" ]; then
677			makewrapper="$makewrapper-$BUILDID"
678		fi
679	fi
680
681	$runcmd rm -f "$makewrapper"
682	if [ "$runcmd" = "echo" ]; then
683		echo 'cat <<EOF >'$makewrapper
684		makewrapout=
685	else
686		makewrapout=">>\$makewrapper"
687	fi
688
689	eval cat <<EOF $makewrapout
690#! /bin/sh
691# Set proper variables to allow easy "make" building of a NetBSD subtree.
692# Generated from:  \$NetBSD: build.sh,v 1.91 2003/02/16 04:35:03 lukem Exp $
693#
694
695EOF
696	for f in $makeenv; do
697		eval echo "$f=\'\$$(echo $f)\'\;\ export\ $f" $makewrapout
698	done
699	eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
700
701	eval cat <<EOF $makewrapout
702
703exec "\$TOOLDIR/bin/${toolprefix}make" \${1+"\$@"}
704EOF
705	[ "$runcmd" = "echo" ] && echo EOF
706	$runcmd chmod +x "$makewrapper"
707	$runcmd echo "===> Updated ${makewrapper}"
708}
709
710buildtools()
711{
712	if [ "$MKOBJDIRS" != "no" ]; then
713		$runcmd "$makewrapper" $parallel obj-tools ||
714		    bomb "failed to make obj-tools"
715	fi
716	$runcmd cd tools
717	if [ -z "$UPDATE" ]; then
718		cleandir=cleandir
719	else
720		cleandir=
721	fi
722	$runcmd "$makewrapper" ${cleandir} dependall install ||
723	    bomb "failed to make tools"
724}
725
726buildkernel()
727{
728	kernconf="$1"
729	if ! $do_tools; then
730		# Building tools every time we build a kernel is clearly
731		# unnecessary.  We could try to figure out whether rebuilding
732		# the tools is necessary this time, but it doesn't seem worth
733		# the trouble.  Instead, we say it's the user's responsibility
734		# to rebuild the tools if necessary.
735		#
736		$runcmd echo "===> Building kernel without building new tools"
737	fi
738	$runcmd echo "===> Building kernel ${kernconf}"
739	if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
740		# The correct value of KERNOBJDIR might
741		# depend on a prior "make obj" in
742		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
743		#
744		KERNSRCDIR="$(safe_getmakevar KERNSRCDIR)"
745		KERNARCHDIR="$(safe_getmakevar KERNARCHDIR)"
746		$runcmd cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
747		$runcmd "$makewrapper" obj ||
748		    bomb "failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
749		$runcmd cd "$TOP"
750	fi
751	KERNCONFDIR="$(safe_getmakevar KERNCONFDIR)"
752	KERNOBJDIR="$(safe_getmakevar KERNOBJDIR)"
753	case "${kernconf}" in
754	*/*)
755		kernconfpath="${kernconf}"
756		kernconfbase="$(basename "${kernconf}")"
757		;;
758	*)
759		kernconfpath="${KERNCONFDIR}/${kernconf}"
760		kernconfbase="${kernconf}"
761		;;
762	esac
763	kernbuilddir="${KERNOBJDIR}/${kernconfbase}"
764	$runcmd echo "===> Kernel build directory: ${kernbuilddir}"
765	$runcmd mkdir -p "${kernbuilddir}" ||
766	    bomb "cannot mkdir: ${kernbuilddir}"
767	if [ -z "$UPDATE" ]; then
768		$runcmd cd "${kernbuilddir}"
769		$runcmd "$makewrapper" cleandir ||
770		    bomb "make cleandir failed in ${kernbuilddir}"
771		$runcmd cd "$TOP"
772	fi
773	$runcmd "${TOOLDIR}/bin/${toolprefix}config" -b "${kernbuilddir}" \
774		-s "${TOP}/sys" "${kernconfpath}" ||
775	    bomb "${toolprefix}config failed for ${kernconf}"
776	$runcmd cd "${kernbuilddir}"
777	$runcmd "$makewrapper" depend ||
778	    bomb "make depend failed in ${kernbuilddir}"
779	$runcmd "$makewrapper" $parallel all ||
780	    bomb "make all failed in ${kernbuilddir}"
781
782	if [ "$runcmd" != "echo" ]; then
783		echo "===> New kernels built:"
784		kernlist=$(awk '$1 == "config" { print $2 }' ${kernconfpath})
785		for kern in ${kernlist:-netbsd}; do
786			[ -f "${kernbuilddir}/${kern}" ] && \
787			    echo "  ${kernbuilddir}/${kern}"
788		done
789		echo "."
790	fi
791}
792
793installworld()
794{
795	dir="$1"
796	${runcmd} "$makewrapper" INSTALLWORLDDIR="${dir}" installworld ||
797	    bomb "failed to make installworld to ${dir}"
798}
799
800
801main()
802{
803	initdefaults
804	parseoptions "$@"
805	rebuildmake
806	validatemakeparams
807	createmakewrapper
808
809	# Perform the operations.
810	#
811	for op in $operations; do
812		case "$op" in
813
814		makewrapper)
815			# no-op
816			;;
817
818		tools)
819			buildtools
820			;;
821
822		obj|build|distribution|release|sets)
823			${runcmd} "$makewrapper" $parallel $op ||
824			    bomb "failed to make $op"
825			;;
826
827		kernel=*)
828			arg=${op#*=}
829			buildkernel "${arg}"
830			;;
831
832		install=*)
833			arg=${op#*=}
834			if [ "${arg}" = "/" ] && \
835			    (	[ "${uname_s}" != "NetBSD" ] || \
836				[ "${uname_m}" != "$MACHINE" ] ); then
837				bomb "'${op}' must != / for cross builds."
838			fi
839			installworld "${arg}"
840			;;
841
842		*)
843			bomb "Unknown operation \`${op}'"
844			;;
845
846		esac
847	done
848}
849
850main "$@"
851