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