Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.84
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.84 2003/01/23 16:24:08 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 			if [ "${arg}" = "/" ] && \
    472 			    (	[ "${uname_s}" != "NetBSD" ] || \
    473 				[ "${uname_m}" != "$MACHINE" ] ); then
    474 				bomb "'install=${arg}' must != / for cross builds."
    475 			fi
    476 			;;
    477 
    478 		*)
    479 			usage "Unknown operation \`${op}'"
    480 			;;
    481 
    482 		esac
    483 		eval do_$op=true
    484 	done
    485 
    486 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
    487 	#
    488 	if [ -z "$MACHINE" ]; then
    489 		if [ "${uname_s}" != "NetBSD" ]; then
    490 			bomb "MACHINE must be set, or -m must be used, for cross builds."
    491 		fi
    492 		MACHINE=${uname_m}
    493 	fi
    494 	[ -n "$MACHINE_ARCH" ] || getarch
    495 	validatearch
    496 
    497 	# Set up default make(1) environment.
    498 	#
    499 	makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
    500 	if [ ! -z "$BUILDID" ]; then
    501 		makeenv="$makeenv BUILDID"
    502 	fi
    503 	MAKEFLAGS="-m $TOP/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
    504 	export MAKEFLAGS MACHINE MACHINE_ARCH
    505 }
    506 
    507 rebuildmake()
    508 {
    509 	# Test make source file timestamps against installed ${toolprefix}make
    510 	# binary, if TOOLDIR is pre-set.
    511 	#
    512 	# Note that we do NOT try to grovel "mk.conf" here to find out if
    513 	# TOOLDIR is set there, because it can contain make variable
    514 	# expansions and other stuff only parseable *after* we have a working
    515 	# ${toolprefix}make.  So this logic can only work if the user has
    516 	# pre-set TOOLDIR in the environment or used the -T option to build.sh.
    517 	#
    518 	make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
    519 	if [ -x $make ]; then
    520 		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
    521 			if [ $f -nt $make ]; then
    522 				do_rebuildmake=true
    523 				break
    524 			fi
    525 		done
    526 	else
    527 		do_rebuildmake=true
    528 	fi
    529 
    530 	# Build bootstrap ${toolprefix}make if needed.
    531 	if $do_rebuildmake; then
    532 		$runcmd echo "===> Bootstrapping ${toolprefix}make"
    533 		tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    534 
    535 		$runcmd mkdir "$tmpdir" || bomb "cannot mkdir: $tmpdir"
    536 		trap "cd /; rm -r -f \"$tmpdir\"" 0
    537 		$runcmd cd "$tmpdir"
    538 
    539 		$runcmd env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
    540 			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
    541 			"$TOP/tools/make/configure" ||
    542 		    bomb "configure of ${toolprefix}make failed"
    543 		$runcmd sh buildmake.sh ||
    544 		    bomb "build of ${toolprefix}make failed"
    545 
    546 		make="$tmpdir/${toolprefix}make"
    547 		$runcmd cd "$TOP"
    548 		$runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
    549 	fi
    550 }
    551 
    552 validatemakeparams()
    553 {
    554 	if [ "$runcmd" = "echo" ]; then
    555 		TOOLCHAIN_MISSING=no
    556 		EXTERNAL_TOOLCHAIN=""
    557 	else
    558 		TOOLCHAIN_MISSING=$(getmakevar TOOLCHAIN_MISSING)
    559 		EXTERNAL_TOOLCHAIN=$(getmakevar EXTERNAL_TOOLCHAIN)
    560 	fi
    561 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
    562 	   [ "${EXTERNAL_TOOLCHAIN}" = "" ]; then
    563 		$runcmd echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
    564 		$runcmd echo "	MACHINE:      ${MACHINE}"
    565 		$runcmd echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
    566 		$runcmd echo ""
    567 		$runcmd echo "All builds for this platform should be done via a traditional make"
    568 		$runcmd echo "If you wish to use an external cross-toolchain, set"
    569 		$runcmd echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
    570 		$runcmd echo "in either the environment or mk.conf and rerun"
    571 		$runcmd echo "	$progname $*"
    572 		exit 1
    573 	fi
    574 
    575 	# If TOOLDIR isn't already set, make objdirs in "tools" in case the
    576 	# default setting from <bsd.own.mk> is used.
    577 	#
    578 	if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
    579 		$runcmd cd tools
    580 		$runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= ||
    581 		    bomb "make obj failed in tools"
    582 		$runcmd cd "$TOP"
    583 	fi
    584 
    585 	# If setting -M or -O to root an obj dir make sure the base directory
    586 	# is made before continuing as bsd.own.mk will need this to pick up
    587 	# _SRC_TOP_OBJ_
    588 	#
    589 	if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
    590 		$runcmd mkdir -p "$makeobjdir"
    591 	fi
    592 
    593 	# Find DESTDIR and TOOLDIR.
    594 	#
    595 	DESTDIR=$(safe_getmakevar DESTDIR)
    596 	$runcmd echo "===> DESTDIR path: $DESTDIR"
    597 	TOOLDIR=$(safe_getmakevar TOOLDIR)
    598 	$runcmd echo "===> TOOLDIR path: $TOOLDIR"
    599 	export DESTDIR TOOLDIR
    600 
    601 	# Check validity of TOOLDIR and DESTDIR.
    602 	#
    603 	if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
    604 		bomb "TOOLDIR '$TOOLDIR' invalid"
    605 	fi
    606 	removedirs="$TOOLDIR"
    607 
    608 	if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
    609 		if $do_build || $do_distribution || $do_release; then
    610 			if ! $do_build || \
    611 			   [ "${uname_s}" != "NetBSD" ] || \
    612 			   [ "${uname_m}" != "$MACHINE" ]; then
    613 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
    614 			fi
    615 			if ! $do_expertmode; then
    616 				bomb "DESTDIR must != / for non -E (expert) builds"
    617 			fi
    618 			$runcmd echo "===> WARNING: Building to /, in expert mode."
    619 			$runcmd echo "===>          This may cause your system to break!  Reasons include:"
    620 			$runcmd echo "===>             - your kernel is not up to date"
    621 			$runcmd echo "===>             - the libraries or toolchain have changed"
    622 			$runcmd echo "===>          YOU HAVE BEEN WARNED!"
    623 		fi
    624 	else
    625 		removedirs="$removedirs $DESTDIR"
    626 	fi
    627 }
    628 
    629 
    630 createmakewrapper()
    631 {
    632 	# Remove the target directories.
    633 	#
    634 	if $do_removedirs; then
    635 		for f in $removedirs; do
    636 			$runcmd echo "===> Removing $f"
    637 			$runcmd rm -r -f $f
    638 		done
    639 	fi
    640 
    641 	# Recreate $TOOLDIR.
    642 	#
    643 	$runcmd mkdir -p "$TOOLDIR/bin" || bomb "mkdir of '$TOOLDIR/bin' failed"
    644 
    645 	# Install ${toolprefix}make if it was built.
    646 	#
    647 	if $do_rebuildmake; then
    648 		$runcmd rm -f "$TOOLDIR/bin/${toolprefix}make"
    649 		$runcmd cp $make "$TOOLDIR/bin/${toolprefix}make" ||
    650 		    bomb "failed to install \$TOOLDIR/bin/${toolprefix}make"
    651 		make="$TOOLDIR/bin/${toolprefix}make"
    652 		$runcmd echo "===> Created ${make}"
    653 		$runcmd rm -r -f "$tmpdir"
    654 		trap 0
    655 	fi
    656 
    657 	# Build a ${toolprefix}make wrapper script, usable by hand as
    658 	# well as by build.sh.
    659 	#
    660 	if [ -z "$makewrapper" ]; then
    661 		makewrapper="$TOOLDIR/bin/${toolprefix}make-$MACHINE"
    662 		if [ ! -z "$BUILDID" ]; then
    663 			makewrapper="$makewrapper-$BUILDID"
    664 		fi
    665 	fi
    666 
    667 	$runcmd rm -f "$makewrapper"
    668 	if [ "$runcmd" = "echo" ]; then
    669 		echo 'cat <<EOF >'$makewrapper
    670 		makewrapout=
    671 	else
    672 		makewrapout=">>\$makewrapper"
    673 	fi
    674 
    675 	eval cat <<EOF $makewrapout
    676 #! /bin/sh
    677 # Set proper variables to allow easy "make" building of a NetBSD subtree.
    678 # Generated from:  \$NetBSD: build.sh,v 1.84 2003/01/23 16:24:08 lukem Exp $
    679 #
    680 
    681 EOF
    682 	for f in $makeenv; do
    683 		eval echo "$f=\'\$$(echo $f)\'\;\ export\ $f" $makewrapout
    684 	done
    685 	eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
    686 
    687 	eval cat <<EOF $makewrapout
    688 
    689 exec "\$TOOLDIR/bin/${toolprefix}make" \${1+"\$@"}
    690 EOF
    691 	[ "$runcmd" = "echo" ] && echo EOF
    692 	$runcmd chmod +x "$makewrapper"
    693 	$runcmd echo "===> Updated ${makewrapper}"
    694 }
    695 
    696 buildtools()
    697 {
    698 	if [ "$MKOBJDIRS" != "no" ]; then
    699 		$runcmd "$makewrapper" $parallel obj-tools ||
    700 		    bomb "failed to make obj-tools"
    701 	fi
    702 	$runcmd cd tools
    703 	if [ "$UPDATE" = "" ]; then
    704 		cleandir=cleandir
    705 	else
    706 		cleandir=
    707 	fi
    708 	$runcmd "$makewrapper" ${cleandir} dependall install ||
    709 	    bomb "failed to make tools"
    710 }
    711 
    712 buildkernel()
    713 {
    714 	kernconf="$1"
    715 	if ! $do_tools; then
    716 		# Building tools every time we build a kernel is clearly
    717 		# unnecessary.  We could try to figure out whether rebuilding
    718 		# the tools is necessary this time, but it doesn't seem worth
    719 		# the trouble.  Instead, we say it's the user's responsibility
    720 		# to rebuild the tools if necessary.
    721 		#
    722 		$runcmd echo "===> Building kernel without building new tools"
    723 	fi
    724 	$runcmd echo "===> Building kernel ${kernconf}"
    725 	if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
    726 		# The correct value of KERNOBJDIR might
    727 		# depend on a prior "make obj" in
    728 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
    729 		#
    730 		KERNSRCDIR="$(safe_getmakevar KERNSRCDIR)"
    731 		KERNARCHDIR="$(safe_getmakevar KERNARCHDIR)"
    732 		$runcmd cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
    733 		$runcmd "$makewrapper" obj ||
    734 		    bomb "failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
    735 		$runcmd cd "$TOP"
    736 	fi
    737 	KERNCONFDIR="$(safe_getmakevar KERNCONFDIR)"
    738 	KERNOBJDIR="$(safe_getmakevar KERNOBJDIR)"
    739 	case "${kernconf}" in
    740 	*/*)
    741 		kernconfpath="${kernconf}"
    742 		kernconfbase="$(basename "${kernconf}")"
    743 		;;
    744 	*)
    745 		kernconfpath="${KERNCONFDIR}/${kernconf}"
    746 		kernconfbase="${kernconf}"
    747 		;;
    748 	esac
    749 	kernbuilddir="${KERNOBJDIR}/${kernconfbase}"
    750 	$runcmd echo "===> Kernel build directory: ${kernbuilddir}"
    751 	$runcmd mkdir -p "${kernbuilddir}" ||
    752 	    bomb "cannot mkdir: ${kernbuilddir}"
    753 	if [ "$UPDATE" = "" ]; then
    754 		$runcmd cd "${kernbuilddir}"
    755 		$runcmd "$makewrapper" cleandir ||
    756 		    bomb "make cleandir failed in ${kernbuilddir}"
    757 		$runcmd cd "$TOP"
    758 	fi
    759 	$runcmd "${TOOLDIR}/bin/${toolprefix}config" -b "${kernbuilddir}" \
    760 		-s "${TOP}/sys" "${kernconfpath}" ||
    761 	    bomb "${toolprefix}config failed for ${kernconf}"
    762 	$runcmd cd "${kernbuilddir}"
    763 	$runcmd "$makewrapper" depend ||
    764 	    bomb "make depend failed in ${kernbuilddir}"
    765 	$runcmd "$makewrapper" $parallel all ||
    766 	    bomb "make all failed in ${kernbuilddir}"
    767 	$runcmd echo "===> New kernel should be in:"
    768 	$runcmd echo "	${kernbuilddir}"
    769 }
    770 
    771 installworld()
    772 {
    773 	dir="$1"
    774 	${runcmd} "$makewrapper" INSTALLWORLDDIR="${dir}" installworld ||
    775 	    bomb "failed to make installworld to ${dir}"
    776 }
    777 
    778 
    779 main()
    780 {
    781 	initdefaults
    782 	parseoptions "$@"
    783 	rebuildmake
    784 	validatemakeparams
    785 	createmakewrapper
    786 
    787 	# Perform the operations.
    788 	#
    789 	for op in $operations; do
    790 		case "$op" in
    791 
    792 		tools)
    793 			buildtools
    794 			;;
    795 
    796 		obj|build|distribution|release)
    797 			${runcmd} "$makewrapper" $parallel $op ||
    798 			    bomb "failed to make $op"
    799 			;;
    800 
    801 		kernel=*)
    802 			arg=${op#*=}
    803 			buildkernel "${arg}"
    804 			;;
    805 
    806 		install=*)
    807 			arg=${op#*=}
    808 			installworld "${arg}"
    809 			;;
    810 
    811 		*)
    812 			bomb "Unknown operation \`${op}'"
    813 			;;
    814 
    815 		esac
    816 	done
    817 }
    818 
    819 main "$@"
    820