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