Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.187.2.1
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.187.2.1 2008/05/18 12:28:44 yamt Exp $
      3 #
      4 # Copyright (c) 2001-2005 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 #
     19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29 # POSSIBILITY OF SUCH DAMAGE.
     30 #
     31 #
     32 # Top level build wrapper, for a system containing no tools.
     33 #
     34 # This script should run on any POSIX-compliant shell.  If the
     35 # first "sh" found in the PATH is a POSIX-compliant shell, then
     36 # you should not need to take any special action.  Otherwise, you
     37 # should set the environment variable HOST_SH to a POSIX-compliant
     38 # shell, and invoke build.sh with that shell.  (Depending on your
     39 # system, one of /bin/ksh, /usr/local/bin/bash, or /usr/xpg4/bin/sh
     40 # might be a suitable shell.)
     41 #
     42 
     43 progname=${0##*/}
     44 toppid=$$
     45 results=/dev/null
     46 trap "exit 1" 1 2 3 15
     47 
     48 bomb()
     49 {
     50 	cat >&2 <<ERRORMESSAGE
     51 
     52 ERROR: $@
     53 *** BUILD ABORTED ***
     54 ERRORMESSAGE
     55 	kill ${toppid}		# in case we were invoked from a subshell
     56 	exit 1
     57 }
     58 
     59 
     60 statusmsg()
     61 {
     62 	${runcmd} echo "===> $@" | tee -a "${results}"
     63 }
     64 
     65 warning()
     66 {
     67 	statusmsg "Warning: $@"
     68 }
     69 
     70 # Find a program in the PATH, and print the result.  If not found,
     71 # print a default.  If $2 is defined (even if it is an empty string),
     72 # then that is the default; otherwise, $1 is used as the default.
     73 find_in_PATH()
     74 {
     75 	local prog="$1"
     76 	local result="${2-"$1"}"
     77 	local oldIFS="${IFS}"
     78 	local dir
     79 	IFS=":"
     80 	for dir in ${PATH}; do
     81 		if [ -x "${dir}/${prog}" ]; then
     82 			result="${dir}/${prog}"
     83 			break
     84 		fi
     85 	done
     86 	IFS="${oldIFS}"
     87 	echo "${result}"
     88 }
     89 
     90 # Try to find a working POSIX shell, and set HOST_SH to refer to it.
     91 # Assumes that uname_s, uname_m, and PWD have been set.
     92 set_HOST_SH()
     93 {
     94 	# Even if ${HOST_SH} is already defined, we still do the
     95 	# sanity checks at the end.
     96 
     97 	# Solaris has /usr/xpg4/bin/sh.
     98 	#
     99 	[ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
    100 		[ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
    101 
    102 	# Try to get the name of the shell that's running this script,
    103 	# by parsing the output from "ps".  We assume that, if the host
    104 	# system's ps command supports -o comm at all, it will do so
    105 	# in the usual way: a one-line header followed by a one-line
    106 	# result, possibly including trailing white space.  And if the
    107 	# host system's ps command doesn't support -o comm, we assume
    108 	# that we'll get an error message on stderr and nothing on
    109 	# stdout.  (We don't try to use ps -o 'comm=' to suppress the
    110 	# header line, because that is less widely supported.)
    111 	#
    112 	# If we get the wrong result here, the user can override it by
    113 	# specifying HOST_SH in the environment.
    114 	#
    115 	[ -z "${HOST_SH}" ] && HOST_SH="$(
    116 		(ps -p $$ -o comm | sed -ne '2s/[ \t]*$//p') 2>/dev/null )"
    117 
    118 	# If nothing above worked, use "sh".  We will later find the
    119 	# first directory in the PATH that has a "sh" program.
    120 	#
    121 	[ -z "${HOST_SH}" ] && HOST_SH="sh"
    122 
    123 	# If the result so far is not an absolute path, try to prepend
    124 	# PWD or search the PATH.
    125 	#
    126 	case "${HOST_SH}" in
    127 	/*)	:
    128 		;;
    129 	*/*)	HOST_SH="${PWD}/${HOST_SH}"
    130 		;;
    131 	*)	HOST_SH="$(find_in_PATH "${HOST_SH}")"
    132 		;;
    133 	esac
    134 
    135 	# If we don't have an absolute path by now, bomb.
    136 	#
    137 	case "${HOST_SH}" in
    138 	/*)	:
    139 		;;
    140 	*)	bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
    141 		;;
    142 	esac
    143 
    144 	# If HOST_SH is not executable, bomb.
    145 	#
    146 	[ -x "${HOST_SH}" ] ||
    147 	    bomb "HOST_SH=\"${HOST_SH}\" is not executable."
    148 }
    149 
    150 initdefaults()
    151 {
    152 	makeenv=
    153 	makewrapper=
    154 	makewrappermachine=
    155 	runcmd=
    156 	operations=
    157 	removedirs=
    158 
    159 	[ -d usr.bin/make ] || cd "$(dirname $0)"
    160 	[ -d usr.bin/make ] ||
    161 	    bomb "build.sh must be run from the top source level"
    162 	[ -f share/mk/bsd.own.mk ] ||
    163 	    bomb "src/share/mk is missing; please re-fetch the source tree"
    164 
    165 	# Find information about the build platform.  Note that "uname -p"
    166 	# is not part of POSIX, but NetBSD's uname -p prints MACHINE_ARCH,
    167 	# while uname -m prints MACHINE.
    168 	#
    169 	uname_s=$(uname -s 2>/dev/null)
    170 	uname_r=$(uname -r 2>/dev/null)
    171 	uname_m=$(uname -m 2>/dev/null)
    172 	uname_p=$(uname -p 2>/dev/null || uname -m 2>/dev/null)
    173 
    174 	# If $PWD is a valid name of the current directory, POSIX mandates
    175 	# that pwd return it by default which causes problems in the
    176 	# presence of symlinks.  Unsetting PWD is simpler than changing
    177 	# every occurrence of pwd to use -P.
    178 	#
    179 	# XXX Except that doesn't work on Solaris. Or many Linuces.
    180 	#
    181 	unset PWD
    182 	TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
    183 
    184 	# The user can set HOST_SH in the environment, or we try to
    185 	# guess an appropriate value.  Then we set several other
    186 	# variables from HOST_SH.
    187 	#
    188 	set_HOST_SH
    189 	setmakeenv HOST_SH "${HOST_SH}"
    190 	setmakeenv BSHELL "${HOST_SH}"
    191 	setmakeenv CONFIG_SHELL "${HOST_SH}"
    192 
    193 	# Set defaults.
    194 	#
    195 	toolprefix=nb
    196 
    197 	# Some systems have a small ARG_MAX.  -X prevents make(1) from
    198 	# exporting variables in the environment redundantly.
    199 	#
    200 	case "${uname_s}" in
    201 	Darwin | FreeBSD | CYGWIN*)
    202 		MAKEFLAGS=-X
    203 		;;
    204 	*)
    205 		MAKEFLAGS=
    206 		;;
    207 	esac
    208 
    209 	# do_{operation}=true if given operation is requested.
    210 	#
    211 	do_expertmode=false
    212 	do_rebuildmake=false
    213 	do_removedirs=false
    214 	do_tools=false
    215 	do_obj=false
    216 	do_build=false
    217 	do_distribution=false
    218 	do_release=false
    219 	do_kernel=false
    220 	do_releasekernel=false
    221 	do_install=false
    222 	do_sets=false
    223 	do_sourcesets=false
    224 	do_syspkgs=false
    225 	do_iso_image=false
    226 	do_iso_image_source=false
    227 	do_params=false
    228 
    229 	# Create scratch directory
    230 	#
    231 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    232 	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
    233 	trap "cd /; rm -r -f \"${tmpdir}\"" 0
    234 	results="${tmpdir}/build.sh.results"
    235 
    236 	# Set source directories
    237 	#
    238 	setmakeenv NETBSDSRCDIR "${TOP}"
    239 
    240 	# Find the version of NetBSD
    241 	#
    242 	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
    243 
    244 	# Set various environment variables to known defaults,
    245 	# to minimize (cross-)build problems observed "in the field".
    246 	#
    247 	unsetmakeenv INFODIR
    248 	unsetmakeenv LESSCHARSET
    249 	setmakeenv LC_ALL C
    250 }
    251 
    252 getarch()
    253 {
    254 	# Translate some MACHINE name aliases (known only to build.sh)
    255 	# into proper MACHINE and MACHINE_ARCH names.  Save the alias
    256 	# name in makewrappermachine.
    257 	#
    258 	case "${MACHINE}" in
    259 
    260 	evbarm-e[bl])
    261 		makewrappermachine=${MACHINE}
    262 		# MACHINE_ARCH is "arm" or "armeb", not "armel"
    263 		MACHINE_ARCH=arm${MACHINE##*-}
    264 		MACHINE_ARCH=${MACHINE_ARCH%el}
    265 		MACHINE=${MACHINE%-e[bl]}
    266 		;;
    267 
    268 	evbmips-e[bl]|sbmips-e[bl])
    269 		makewrappermachine=${MACHINE}
    270 		MACHINE_ARCH=mips${MACHINE##*-}
    271 		MACHINE=${MACHINE%-e[bl]}
    272 		;;
    273 
    274 	evbmips64-e[bl]|sbmips64-e[bl])
    275 		makewrappermachine=${MACHINE}
    276 		MACHINE_ARCH=mips64${MACHINE##*-}
    277 		MACHINE=${MACHINE%64-e[bl]}
    278 		;;
    279 
    280 	evbsh3-e[bl])
    281 		makewrappermachine=${MACHINE}
    282 		MACHINE_ARCH=sh3${MACHINE##*-}
    283 		MACHINE=${MACHINE%-e[bl]}
    284 		;;
    285 
    286 	esac
    287 
    288 	# Translate a MACHINE into a default MACHINE_ARCH.
    289 	#
    290 	case "${MACHINE}" in
    291 
    292 	acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
    293 		MACHINE_ARCH=arm
    294 		;;
    295 
    296 	evbarm)		# unspecified MACHINE_ARCH gets LE
    297 		MACHINE_ARCH=${MACHINE_ARCH:=arm}
    298 		;;
    299 
    300 	hp700)
    301 		MACHINE_ARCH=hppa
    302 		;;
    303 
    304 	sun2)
    305 		MACHINE_ARCH=m68000
    306 		;;
    307 
    308 	amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
    309 		MACHINE_ARCH=m68k
    310 		;;
    311 
    312 	evbmips|sbmips)		# no default MACHINE_ARCH
    313 		;;
    314 
    315 	ews4800mips|mipsco|newsmips|sgimips)
    316 		MACHINE_ARCH=mipseb
    317 		;;
    318 
    319 	algor|arc|cobalt|hpcmips|playstation2|pmax)
    320 		MACHINE_ARCH=mipsel
    321 		;;
    322 
    323 	evbppc64|macppc64|ofppc64)
    324 		makewrappermachine=${MACHINE}
    325 		MACHINE=${MACHINE%64}
    326 		MACHINE_ARCH=powerpc64
    327 		;;
    328 
    329 	amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
    330 		MACHINE_ARCH=powerpc
    331 		;;
    332 
    333 	evbsh3)			# no default MACHINE_ARCH
    334 		;;
    335 
    336 	mmeye)
    337 		MACHINE_ARCH=sh3eb
    338 		;;
    339 
    340 	dreamcast|hpcsh|landisk)
    341 		MACHINE_ARCH=sh3el
    342 		;;
    343 
    344 	amd64)
    345 		MACHINE_ARCH=x86_64
    346 		;;
    347 
    348 	alpha|i386|sparc|sparc64|vax|ia64)
    349 		MACHINE_ARCH=${MACHINE}
    350 		;;
    351 
    352 	*)
    353 		bomb "Unknown target MACHINE: ${MACHINE}"
    354 		;;
    355 
    356 	esac
    357 }
    358 
    359 validatearch()
    360 {
    361 	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
    362 	#
    363 	case "${MACHINE_ARCH}" in
    364 
    365 	alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
    366 		;;
    367 
    368 	"")
    369 		bomb "No MACHINE_ARCH provided"
    370 		;;
    371 
    372 	*)
    373 		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
    374 		;;
    375 
    376 	esac
    377 
    378 	# Determine valid MACHINE_ARCHs for MACHINE
    379 	#
    380 	case "${MACHINE}" in
    381 
    382 	evbarm)
    383 		arches="arm armeb"
    384 		;;
    385 
    386 	evbmips|sbmips)
    387 		arches="mipseb mipsel mips64eb mips64el"
    388 		;;
    389 
    390 	sgimips)
    391 		arches="mipseb mips64eb"
    392 		;;
    393 
    394 	evbsh3)
    395 		arches="sh3eb sh3el"
    396 		;;
    397 
    398 	macppc|evbppc|ofppc)
    399 		arches="powerpc powerpc64"
    400 		;;
    401 	*)
    402 		oma="${MACHINE_ARCH}"
    403 		getarch
    404 		arches="${MACHINE_ARCH}"
    405 		MACHINE_ARCH="${oma}"
    406 		;;
    407 
    408 	esac
    409 
    410 	# Ensure that MACHINE_ARCH supports MACHINE
    411 	#
    412 	archok=false
    413 	for a in ${arches}; do
    414 		if [ "${a}" = "${MACHINE_ARCH}" ]; then
    415 			archok=true
    416 			break
    417 		fi
    418 	done
    419 	${archok} ||
    420 	    bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
    421 }
    422 
    423 nobomb_getmakevar()
    424 {
    425 	[ -x "${make}" ] || return 1
    426 	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
    427 _x_:
    428 	echo \${$1}
    429 .include <bsd.prog.mk>
    430 .include <bsd.kernobj.mk>
    431 EOF
    432 }
    433 
    434 raw_getmakevar()
    435 {
    436 	[ -x "${make}" ] || bomb "raw_getmakevar $1: ${make} is not executable"
    437 	nobomb_getmakevar "$1" || bomb "raw_getmakevar $1: ${make} failed"
    438 }
    439 
    440 getmakevar()
    441 {
    442 	# raw_getmakevar() doesn't work properly if $make hasn't yet been
    443 	# built, which can happen when running with the "-n" option.
    444 	# getmakevar() deals with this by emitting a literal '$'
    445 	# followed by the variable name, instead of trying to find the
    446 	# variable's value.
    447 	#
    448 	if [ -x "${make}" ]; then
    449 		raw_getmakevar "$1"
    450 	else
    451 		echo "\$$1"
    452 	fi
    453 }
    454 
    455 setmakeenv()
    456 {
    457 	eval "$1='$2'; export $1"
    458 	makeenv="${makeenv} $1"
    459 }
    460 
    461 unsetmakeenv()
    462 {
    463 	eval "unset $1"
    464 	makeenv="${makeenv} $1"
    465 }
    466 
    467 # Convert possibly-relative path to absolute path by prepending
    468 # ${TOP} if necessary.  Also delete trailing "/", if any.
    469 resolvepath()
    470 {
    471 	case "${OPTARG}" in
    472 	/)
    473 		;;
    474 	/*)
    475 		OPTARG="${OPTARG%/}"
    476 		;;
    477 	*)
    478 		OPTARG="${TOP}/${OPTARG%/}"
    479 		;;
    480 	esac
    481 }
    482 
    483 usage()
    484 {
    485 	if [ -n "$*" ]; then
    486 		echo ""
    487 		echo "${progname}: $*"
    488 	fi
    489 	cat <<_usage_
    490 
    491 Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-C cdextras] [-D dest]
    492 		[-j njob] [-M obj] [-m mach] [-N noisy] [-O obj] [-R release]
    493 		[-T tools] [-V var=[value]] [-w wrapper] [-X x11src] [-Z var]
    494 		operation [...]
    495 
    496  Build operations (all imply "obj" and "tools"):
    497     build               Run "make build".
    498     distribution        Run "make distribution" (includes DESTDIR/etc/ files).
    499     release             Run "make release" (includes kernels & distrib media).
    500 
    501  Other operations:
    502     help                Show this message and exit.
    503     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
    504                         Always performed.
    505     obj                 Run "make obj".  [Default unless -o is used]
    506     tools               Build and install tools.
    507     install=idir        Run "make installworld" to \`idir' to install all sets
    508 			except \`etc'.  Useful after "distribution" or "release"
    509     kernel=conf         Build kernel with config file \`conf'
    510     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
    511     sets                Create binary sets in
    512 			RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
    513 			DESTDIR should be populated beforehand.
    514     sourcesets          Create source sets in RELEASEDIR/source/sets.
    515     syspkgs             Create syspkgs in
    516 			RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
    517     iso-image           Create CD-ROM image in RELEASEDIR/iso.
    518     iso-image-source    Create CD-ROM image with source in RELEASEDIR/iso.
    519     params              Display various make(1) parameters.
    520 
    521  Options:
    522     -a arch     Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
    523     -B buildId  Set BUILDID to buildId.
    524     -C cdextras Set CDEXTRA to cdextras
    525     -D dest     Set DESTDIR to dest.  [Default: destdir.MACHINE]
    526     -E          Set "expert" mode; disables various safety checks.
    527                 Should not be used without expert knowledge of the build system.
    528     -h          Print this help message.
    529     -j njob     Run up to njob jobs in parallel; see make(1) -j.
    530     -M obj      Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
    531                 Unsets MAKEOBJDIR.
    532     -m mach     Set MACHINE to mach; not required if NetBSD native.
    533     -N noisy	Set the noisyness (MAKEVERBOSE) level of the build:
    534 		    0	Quiet
    535 		    1	Operations are described, commands are suppressed
    536 		    2	Full output
    537 		[Default: 2]
    538     -n          Show commands that would be executed, but do not execute them.
    539     -O obj      Set obj root directory to obj; sets a MAKEOBJDIR pattern.
    540                 Unsets MAKEOBJDIRPREFIX.
    541     -o          Set MKOBJDIRS=no; do not create objdirs at start of build.
    542     -R release  Set RELEASEDIR to release.  [Default: releasedir]
    543     -r          Remove contents of TOOLDIR and DESTDIR before building.
    544     -T tools    Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
    545                 the environment, ${toolprefix}make will be (re)built unconditionally.
    546     -U          Set MKUNPRIVED=yes; build without requiring root privileges,
    547     		install from an UNPRIVED build with proper file permissions.
    548     -u          Set MKUPDATE=yes; do not run "make cleandir" first.
    549 		Without this, everything is rebuilt, including the tools.
    550     -V v=[val]  Set variable \`v' to \`val'.
    551     -w wrapper  Create ${toolprefix}make script as wrapper.
    552                 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
    553     -X x11src   Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
    554     -x          Set MKX11=yes; build X11R6 from X11SRCDIR
    555     -Z v        Unset ("zap") variable \`v'.
    556 
    557 _usage_
    558 	exit 1
    559 }
    560 
    561 parseoptions()
    562 {
    563 	opts='a:B:bC:D:dEhi:j:k:M:m:N:nO:oR:rT:tUuV:w:xX:Z:'
    564 	opt_a=no
    565 
    566 	if type getopts >/dev/null 2>&1; then
    567 		# Use POSIX getopts.
    568 		#
    569 		getoptcmd='getopts ${opts} opt && opt=-${opt}'
    570 		optargcmd=':'
    571 		optremcmd='shift $((${OPTIND} -1))'
    572 	else
    573 		type getopt >/dev/null 2>&1 ||
    574 		    bomb "/bin/sh shell is too old; try ksh or bash"
    575 
    576 		# Use old-style getopt(1) (doesn't handle whitespace in args).
    577 		#
    578 		args="$(getopt ${opts} $*)"
    579 		[ $? = 0 ] || usage
    580 		set -- ${args}
    581 
    582 		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    583 		optargcmd='OPTARG="$1"; shift'
    584 		optremcmd=':'
    585 	fi
    586 
    587 	# Parse command line options.
    588 	#
    589 	while eval ${getoptcmd}; do
    590 		case ${opt} in
    591 
    592 		-a)
    593 			eval ${optargcmd}
    594 			MACHINE_ARCH=${OPTARG}
    595 			opt_a=yes
    596 			;;
    597 
    598 		-B)
    599 			eval ${optargcmd}
    600 			BUILDID=${OPTARG}
    601 			;;
    602 
    603 		-b)
    604 			usage "'-b' has been replaced by 'makewrapper'"
    605 			;;
    606 
    607 		-C)
    608 			eval ${optargcmd}; resolvepath
    609 			iso_dir=${OPTARG}
    610 			;;
    611 
    612 		-D)
    613 			eval ${optargcmd}; resolvepath
    614 			setmakeenv DESTDIR "${OPTARG}"
    615 			;;
    616 
    617 		-d)
    618 			usage "'-d' has been replaced by 'distribution'"
    619 			;;
    620 
    621 		-E)
    622 			do_expertmode=true
    623 			;;
    624 
    625 		-i)
    626 			usage "'-i idir' has been replaced by 'install=idir'"
    627 			;;
    628 
    629 		-j)
    630 			eval ${optargcmd}
    631 			parallel="-j ${OPTARG}"
    632 			;;
    633 
    634 		-k)
    635 			usage "'-k conf' has been replaced by 'kernel=conf'"
    636 			;;
    637 
    638 		-M)
    639 			eval ${optargcmd}; resolvepath
    640 			makeobjdir="${OPTARG}"
    641 			unsetmakeenv MAKEOBJDIR
    642 			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
    643 			;;
    644 
    645 			# -m overrides MACHINE_ARCH unless "-a" is specified
    646 		-m)
    647 			eval ${optargcmd}
    648 			MACHINE="${OPTARG}"
    649 			[ "${opt_a}" != "yes" ] && getarch
    650 			;;
    651 
    652 		-N)
    653 			eval ${optargcmd}
    654 			case "${OPTARG}" in
    655 			0|1|2)
    656 				setmakeenv MAKEVERBOSE "${OPTARG}"
    657 				;;
    658 			*)
    659 				usage "'${OPTARG}' is not a valid value for -N"
    660 				;;
    661 			esac
    662 			;;
    663 
    664 		-n)
    665 			runcmd=echo
    666 			;;
    667 
    668 		-O)
    669 			eval ${optargcmd}; resolvepath
    670 			makeobjdir="${OPTARG}"
    671 			unsetmakeenv MAKEOBJDIRPREFIX
    672 			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
    673 			;;
    674 
    675 		-o)
    676 			MKOBJDIRS=no
    677 			;;
    678 
    679 		-R)
    680 			eval ${optargcmd}; resolvepath
    681 			setmakeenv RELEASEDIR "${OPTARG}"
    682 			;;
    683 
    684 		-r)
    685 			do_removedirs=true
    686 			do_rebuildmake=true
    687 			;;
    688 
    689 		-T)
    690 			eval ${optargcmd}; resolvepath
    691 			TOOLDIR="${OPTARG}"
    692 			export TOOLDIR
    693 			;;
    694 
    695 		-t)
    696 			usage "'-t' has been replaced by 'tools'"
    697 			;;
    698 
    699 		-U)
    700 			setmakeenv MKUNPRIVED yes
    701 			;;
    702 
    703 		-u)
    704 			setmakeenv MKUPDATE yes
    705 			;;
    706 
    707 		-V)
    708 			eval ${optargcmd}
    709 			case "${OPTARG}" in
    710 		    # XXX: consider restricting which variables can be changed?
    711 			[a-zA-Z_][a-zA-Z_0-9]*=*)
    712 				setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
    713 				;;
    714 			*)
    715 				usage "-V argument must be of the form 'var=[value]'"
    716 				;;
    717 			esac
    718 			;;
    719 
    720 		-w)
    721 			eval ${optargcmd}; resolvepath
    722 			makewrapper="${OPTARG}"
    723 			;;
    724 
    725 		-X)
    726 			eval ${optargcmd}; resolvepath
    727 			setmakeenv X11SRCDIR "${OPTARG}"
    728 			;;
    729 
    730 		-x)
    731 			setmakeenv MKX11 yes
    732 			;;
    733 
    734 		-Z)
    735 			eval ${optargcmd}
    736 		    # XXX: consider restricting which variables can be unset?
    737 			unsetmakeenv "${OPTARG}"
    738 			;;
    739 
    740 		--)
    741 			break
    742 			;;
    743 
    744 		-'?'|-h)
    745 			usage
    746 			;;
    747 
    748 		esac
    749 	done
    750 
    751 	# Validate operations.
    752 	#
    753 	eval ${optremcmd}
    754 	while [ $# -gt 0 ]; do
    755 		op=$1; shift
    756 		operations="${operations} ${op}"
    757 
    758 		case "${op}" in
    759 
    760 		help)
    761 			usage
    762 			;;
    763 
    764 		makewrapper|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
    765 			;;
    766 
    767 		iso-image)
    768 			op=iso_image	# used as part of a variable name
    769 			;;
    770 
    771 		iso-image-source)
    772 			op=iso_image_source   # used as part of a variable name
    773 			;;
    774 
    775 		kernel=*|releasekernel=*)
    776 			arg=${op#*=}
    777 			op=${op%%=*}
    778 			[ -n "${arg}" ] ||
    779 			    bomb "Must supply a kernel name with \`${op}=...'"
    780 			;;
    781 
    782 		install=*)
    783 			arg=${op#*=}
    784 			op=${op%%=*}
    785 			[ -n "${arg}" ] ||
    786 			    bomb "Must supply a directory with \`install=...'"
    787 			;;
    788 
    789 		*)
    790 			usage "Unknown operation \`${op}'"
    791 			;;
    792 
    793 		esac
    794 		eval do_${op}=true
    795 	done
    796 	[ -n "${operations}" ] || usage "Missing operation to perform."
    797 
    798 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
    799 	#
    800 	if [ -z "${MACHINE}" ]; then
    801 		[ "${uname_s}" = "NetBSD" ] ||
    802 		    bomb "MACHINE must be set, or -m must be used, for cross builds."
    803 		MACHINE=${uname_m}
    804 	fi
    805 	[ -n "${MACHINE_ARCH}" ] || getarch
    806 	validatearch
    807 
    808 	# Set up default make(1) environment.
    809 	#
    810 	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
    811 	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
    812 	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
    813 	export MAKEFLAGS MACHINE MACHINE_ARCH
    814 }
    815 
    816 sanitycheck()
    817 {
    818 	# If the PATH contains any non-absolute components (including,
    819 	# but not limited to, "." or ""), then complain.  As an exception,
    820 	# allow "" or "." as the last component of the PATH.  This is fatal
    821 	# if expert mode is not in effect.
    822 	#
    823 	local path="${PATH}"
    824 	path="${path%:}"	# delete trailing ":"
    825 	path="${path%:.}"	# delete trailing ":."
    826 	case ":${path}:/" in
    827 	*:[!/]*)
    828 		if ${do_expertmode}; then
    829 			warning "PATH contains non-absolute components"
    830 		else
    831 			bomb "PATH environment variable must not" \
    832 			     "contain non-absolute components"
    833 		fi
    834 		;;
    835 	esac
    836 }
    837 
    838 # Try to set a value for TOOLDIR.  This is difficult because of a cyclic
    839 # dependency: TOOLDIR may be affected by settings in /etc/mk.conf, so
    840 # we would like to use getmakevar to get the value of TOOLDIR, but we
    841 # can't use getmakevar before we have an up to date version of nbmake;
    842 # we might already have an up to date version of nbmake in TOOLDIR, but
    843 # we don't yet know where TOOLDIR is.
    844 #
    845 # In principle, we could break the cycle by building a copy of nbmake
    846 # in a temporary directory.  However, people who use the default value
    847 # of TOOLDIR do not like to have nbmake rebuilt every time they run
    848 # build.sh.
    849 #
    850 # We try to please everybody as follows:
    851 #
    852 # * If TOOLDIR was set in the environment or on the command line, use
    853 #   that value.
    854 # * Otherwise try to guess what TOOLDIR would be if not overridden by
    855 #   /etc/mk.conf, and check whether the resulting directory contains
    856 #   a copy of ${toolprefix}make (this should work for everybody who
    857 #   doesn't override TOOLDIR via /etc/mk.conf);
    858 # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
    859 #   in the PATH (this might accidentally find a non-NetBSD version of
    860 #   make, which will lead to failure in the next step);
    861 # * If a copy of make was found above, try to use it with
    862 #   nobomb_getmakevar to find the correct value for TOOLDIR;
    863 # * If all else fails, leave TOOLDIR unset.  Our caller is expected to
    864 #   be able to cope with this.
    865 #
    866 try_set_TOOLDIR()
    867 {
    868 	[ -n "${TOOLDIR}" ] && return
    869 
    870 	# Set guess_TOOLDIR, in the same way that <bsd.own.mk> would set
    871 	# TOOLDIR if /etc/mk.conf sisn't interfere.
    872 	local topobjdir="${TOP}"
    873 	[ -n "${makeobjdir}" ] && topobjdir="${topobjdir}/${makeobjdir}"
    874 	local host_ostype="${uname_s}-$(
    875 		echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
    876 		)$(
    877 		echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
    878 		)"
    879 	local guess_TOOLDIR="${topobjdir}/tooldir.${host_ostype}"
    880 
    881 	# Look for a suitable ${toolprefix}make, nbmake, bmake, or make.
    882 	guess_make="${guess_TOOLDIR}/bin/${toolprefix}make"
    883 	[ -x "${guess_make}" ] || guess_make=""
    884 	: ${guess_make:=$(find_in_PATH ${toolprefix}make '')}
    885 	: ${guess_make:=$(find_in_PATH nbmake '')}
    886 	: ${guess_make:=$(find_in_PATH bmake '')}
    887 	: ${guess_make:=$(find_in_PATH make '')}
    888 
    889 	# Use ${guess_make} with nobomb_getmakevar
    890 	if [ -x "${guess_make}" ]; then
    891 		TOOLDIR=$(make="${guess_make}" nobomb_getmakevar TOOLDIR)
    892 		[ -n "${TOOLDIR}" ] || unset TOOLDIR
    893 	fi
    894 }
    895 
    896 rebuildmake()
    897 {
    898 	# Test make source file timestamps against installed ${toolprefix}make
    899 	# binary, if TOOLDIR is pre-set or if try_set_TOOLDIR can set it.
    900 	#
    901 	try_set_TOOLDIR
    902 	make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
    903 	if [ -x "${make}" ]; then
    904 		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
    905 			if [ "${f}" -nt "${make}" ]; then
    906 				statusmsg "${make} outdated (older than ${f}), needs building."
    907 				do_rebuildmake=true
    908 				break
    909 			fi
    910 		done
    911 	else
    912 		statusmsg "No ${make}, needs building."
    913 		do_rebuildmake=true
    914 	fi
    915 
    916 	# Build bootstrap ${toolprefix}make if needed.
    917 	if ${do_rebuildmake}; then
    918 		statusmsg "Bootstrapping ${toolprefix}make"
    919 		${runcmd} cd "${tmpdir}"
    920 		${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
    921 			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
    922 			${HOST_SH} "${TOP}/tools/make/configure" ||
    923 		    bomb "Configure of ${toolprefix}make failed"
    924 		${runcmd} ${HOST_SH} buildmake.sh ||
    925 		    bomb "Build of ${toolprefix}make failed"
    926 		make="${tmpdir}/${toolprefix}make"
    927 		${runcmd} cd "${TOP}"
    928 		${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
    929 	fi
    930 }
    931 
    932 validatemakeparams()
    933 {
    934 	if [ "${runcmd}" = "echo" ]; then
    935 		TOOLCHAIN_MISSING=no
    936 		EXTERNAL_TOOLCHAIN=""
    937 	else
    938 		TOOLCHAIN_MISSING=$(raw_getmakevar TOOLCHAIN_MISSING)
    939 		EXTERNAL_TOOLCHAIN=$(raw_getmakevar EXTERNAL_TOOLCHAIN)
    940 	fi
    941 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
    942 	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
    943 		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
    944 		${runcmd} echo "	MACHINE:      ${MACHINE}"
    945 		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
    946 		${runcmd} echo ""
    947 		${runcmd} echo "All builds for this platform should be done via a traditional make"
    948 		${runcmd} echo "If you wish to use an external cross-toolchain, set"
    949 		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
    950 		${runcmd} echo "in either the environment or mk.conf and rerun"
    951 		${runcmd} echo "	${progname} $*"
    952 		exit 1
    953 	fi
    954 
    955 	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
    956 	# These may be set as build.sh options or in "mk.conf".
    957 	# Don't export them as they're only used for tests in build.sh.
    958 	#
    959 	MKOBJDIRS=$(getmakevar MKOBJDIRS)
    960 	MKUNPRIVED=$(getmakevar MKUNPRIVED)
    961 	MKUPDATE=$(getmakevar MKUPDATE)
    962 
    963 	if [ "${MKOBJDIRS}" != "no" ]; then
    964 		# If setting -M or -O to the root of an obj dir, make sure
    965 		# the base directory is made before continuing as <bsd.own.mk>
    966 		# will need this to pick up _SRC_TOP_OBJ_
    967 		#
    968 		if [ ! -z "${makeobjdir}" ]; then
    969 			${runcmd} mkdir -p "${makeobjdir}"
    970 		fi
    971 
    972 		# make obj in tools to ensure that the objdir for the top-level
    973 		# of the source tree and for "tools" is available, in case the
    974 		# default TOOLDIR setting from <bsd.own.mk> is used, or the
    975 		# build.sh default DESTDIR and RELEASEDIR is to be used.
    976 		#
    977 		${runcmd} cd tools
    978 		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
    979 		    bomb "Failed to make obj in tools"
    980 		${runcmd} cd "${TOP}"
    981 	fi
    982 
    983 	# Find TOOLDIR, DESTDIR, RELEASEDIR, and RELEASEMACHINEDIR.
    984 	#
    985 	TOOLDIR=$(getmakevar TOOLDIR)
    986 	statusmsg "TOOLDIR path:     ${TOOLDIR}"
    987 	DESTDIR=$(getmakevar DESTDIR)
    988 	RELEASEDIR=$(getmakevar RELEASEDIR)
    989 	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
    990 	if ! $do_expertmode; then
    991 		_SRC_TOP_OBJ_=$(getmakevar _SRC_TOP_OBJ_)
    992 		: ${DESTDIR:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
    993 		: ${RELEASEDIR:=${_SRC_TOP_OBJ_}/releasedir}
    994 		makeenv="${makeenv} DESTDIR RELEASEDIR"
    995 	fi
    996 	export TOOLDIR DESTDIR RELEASEDIR
    997 	statusmsg "DESTDIR path:     ${DESTDIR}"
    998 	statusmsg "RELEASEDIR path:  ${RELEASEDIR}"
    999 
   1000 	# Check validity of TOOLDIR and DESTDIR.
   1001 	#
   1002 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
   1003 		bomb "TOOLDIR '${TOOLDIR}' invalid"
   1004 	fi
   1005 	removedirs="${TOOLDIR}"
   1006 
   1007 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
   1008 		if ${do_build} || ${do_distribution} || ${do_release}; then
   1009 			if ! ${do_build} || \
   1010 			   [ "${uname_s}" != "NetBSD" ] || \
   1011 			   [ "${uname_m}" != "${MACHINE}" ]; then
   1012 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
   1013 			fi
   1014 			if ! ${do_expertmode}; then
   1015 				bomb "DESTDIR must != / for non -E (expert) builds"
   1016 			fi
   1017 			statusmsg "WARNING: Building to /, in expert mode."
   1018 			statusmsg "         This may cause your system to break!  Reasons include:"
   1019 			statusmsg "            - your kernel is not up to date"
   1020 			statusmsg "            - the libraries or toolchain have changed"
   1021 			statusmsg "         YOU HAVE BEEN WARNED!"
   1022 		fi
   1023 	else
   1024 		removedirs="${removedirs} ${DESTDIR}"
   1025 	fi
   1026 	if ${do_build} || ${do_distribution} || ${do_release}; then
   1027 		if ! ${do_expertmode} && \
   1028 		    [ "$(id -u 2>/dev/null)" -ne 0 ] && \
   1029 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1030 			bomb "-U or -E must be set for build as an unprivileged user."
   1031 		fi
   1032 	fi
   1033 	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
   1034 		bomb "Must set RELEASEDIR with \`releasekernel=...'"
   1035 	fi
   1036 
   1037 	# Install as non-root is a bad idea.
   1038 	#
   1039 	if ${do_install} && [ "$(id -u 2>/dev/null)" -ne 0 ] ; then
   1040 		if ${do_expertmode}; then
   1041 			warning "Will install as an unprivileged user."
   1042 		else
   1043 			bomb "-E must be set for install as an unprivileged user."
   1044 		fi
   1045 	fi
   1046 
   1047 	# If a previous build.sh run used -U (and therefore created a
   1048 	# METALOG file), then most subsequent build.sh runs must also
   1049 	# use -U.  If DESTDIR is about to be removed, then don't perform
   1050 	# this check.
   1051 	#
   1052 	case "${do_removedirs} ${removedirs} " in
   1053 	true*" ${DESTDIR} "*)
   1054 		# DESTDIR is about to be removed
   1055 		;;
   1056 	*)
   1057 		if ( ${do_build} || ${do_distribution} || ${do_release} || \
   1058 		    ${do_install} ) && \
   1059 		    [ -e "${DESTDIR}/METALOG" ] && \
   1060 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1061 			if $do_expertmode; then
   1062 				warning "A previous build.sh run specified -U."
   1063 			else
   1064 				bomb "A previous build.sh run specified -U; you must specify it again now."
   1065 			fi
   1066 		fi
   1067 		;;
   1068 	esac
   1069 }
   1070 
   1071 
   1072 createmakewrapper()
   1073 {
   1074 	# Remove the target directories.
   1075 	#
   1076 	if ${do_removedirs}; then
   1077 		for f in ${removedirs}; do
   1078 			statusmsg "Removing ${f}"
   1079 			${runcmd} rm -r -f "${f}"
   1080 		done
   1081 	fi
   1082 
   1083 	# Recreate $TOOLDIR.
   1084 	#
   1085 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
   1086 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
   1087 
   1088 	# Install ${toolprefix}make if it was built.
   1089 	#
   1090 	if ${do_rebuildmake}; then
   1091 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
   1092 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
   1093 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
   1094 		make="${TOOLDIR}/bin/${toolprefix}make"
   1095 		statusmsg "Created ${make}"
   1096 	fi
   1097 
   1098 	# Build a ${toolprefix}make wrapper script, usable by hand as
   1099 	# well as by build.sh.
   1100 	#
   1101 	if [ -z "${makewrapper}" ]; then
   1102 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
   1103 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
   1104 	fi
   1105 
   1106 	${runcmd} rm -f "${makewrapper}"
   1107 	if [ "${runcmd}" = "echo" ]; then
   1108 		echo 'cat <<EOF >'${makewrapper}
   1109 		makewrapout=
   1110 	else
   1111 		makewrapout=">>\${makewrapper}"
   1112 	fi
   1113 
   1114 	case "${KSH_VERSION:-${SH_VERSION}}" in
   1115 	*PD\ KSH*|*MIRBSD\ KSH*)
   1116 		set +o braceexpand
   1117 		;;
   1118 	esac
   1119 
   1120 	eval cat <<EOF ${makewrapout}
   1121 #! ${HOST_SH}
   1122 # Set proper variables to allow easy "make" building of a NetBSD subtree.
   1123 # Generated from:  \$NetBSD: build.sh,v 1.187.2.1 2008/05/18 12:28:44 yamt Exp $
   1124 # with these arguments: ${_args}
   1125 #
   1126 
   1127 EOF
   1128 	{
   1129 		for f in ${makeenv}; do
   1130 			if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
   1131 				eval echo "unset ${f}"
   1132 			else
   1133 				eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
   1134 			fi
   1135 		done
   1136 
   1137 		eval cat <<EOF
   1138 MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
   1139 USETOOLS=yes; export USETOOLS
   1140 EOF
   1141 	} | eval sort -u "${makewrapout}"
   1142 	eval cat <<EOF "${makewrapout}"
   1143 
   1144 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
   1145 EOF
   1146 	[ "${runcmd}" = "echo" ] && echo EOF
   1147 	${runcmd} chmod +x "${makewrapper}"
   1148 	statusmsg "makewrapper:      ${makewrapper}"
   1149 	statusmsg "Updated ${makewrapper}"
   1150 }
   1151 
   1152 buildtools()
   1153 {
   1154 	if [ "${MKOBJDIRS}" != "no" ]; then
   1155 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
   1156 		    bomb "Failed to make obj-tools"
   1157 	fi
   1158 	${runcmd} cd tools
   1159 	if [ "${MKUPDATE}" = "no" ]; then
   1160 		${runcmd} "${makewrapper}" ${parallel} cleandir ||
   1161 		    bomb "Failed to make cleandir tools"
   1162 	fi
   1163 	${runcmd} "${makewrapper}" ${parallel} dependall ||
   1164 	    bomb "Failed to make dependall tools"
   1165 	${runcmd} "${makewrapper}" ${parallel} install ||
   1166 	    bomb "Failed to make install tools"
   1167 	statusmsg "Tools built to ${TOOLDIR}"
   1168 	${runcmd} cd "${TOP}"
   1169 }
   1170 
   1171 getkernelconf()
   1172 {
   1173 	kernelconf="$1"
   1174 	if [ "${MKOBJDIRS}" != "no" ]; then
   1175 		# The correct value of KERNOBJDIR might
   1176 		# depend on a prior "make obj" in
   1177 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
   1178 		#
   1179 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
   1180 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
   1181 		${runcmd} cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
   1182 		${runcmd} "${makewrapper}" ${parallel} obj ||
   1183 		    bomb "Failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
   1184 		${runcmd} cd "${TOP}"
   1185 	fi
   1186 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
   1187 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
   1188 	case "${kernelconf}" in
   1189 	*/*)
   1190 		kernelconfpath="${kernelconf}"
   1191 		kernelconfname="${kernelconf##*/}"
   1192 		;;
   1193 	*)
   1194 		kernelconfpath="${KERNCONFDIR}/${kernelconf}"
   1195 		kernelconfname="${kernelconf}"
   1196 		;;
   1197 	esac
   1198 	kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
   1199 }
   1200 
   1201 buildkernel()
   1202 {
   1203 	if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
   1204 		# Building tools every time we build a kernel is clearly
   1205 		# unnecessary.  We could try to figure out whether rebuilding
   1206 		# the tools is necessary this time, but it doesn't seem worth
   1207 		# the trouble.  Instead, we say it's the user's responsibility
   1208 		# to rebuild the tools if necessary.
   1209 		#
   1210 		statusmsg "Building kernel without building new tools"
   1211 		buildkernelwarned=true
   1212 	fi
   1213 	getkernelconf $1
   1214 	statusmsg "Building kernel:  ${kernelconf}"
   1215 	statusmsg "Build directory:  ${kernelbuildpath}"
   1216 	${runcmd} mkdir -p "${kernelbuildpath}" ||
   1217 	    bomb "Cannot mkdir: ${kernelbuildpath}"
   1218 	if [ "${MKUPDATE}" = "no" ]; then
   1219 		${runcmd} cd "${kernelbuildpath}"
   1220 		${runcmd} "${makewrapper}" ${parallel} cleandir ||
   1221 		    bomb "Failed to make cleandir in ${kernelbuildpath}"
   1222 		${runcmd} cd "${TOP}"
   1223 	fi
   1224 	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
   1225 	|| bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
   1226 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
   1227 		-s "${TOP}/sys" "${kernelconfpath}" ||
   1228 	    bomb "${toolprefix}config failed for ${kernelconf}"
   1229 	${runcmd} cd "${kernelbuildpath}"
   1230 	${runcmd} "${makewrapper}" ${parallel} depend ||
   1231 	    bomb "Failed to make depend in ${kernelbuildpath}"
   1232 	${runcmd} "${makewrapper}" ${parallel} all ||
   1233 	    bomb "Failed to make all in ${kernelbuildpath}"
   1234 	${runcmd} cd "${TOP}"
   1235 
   1236 	if [ "${runcmd}" != "echo" ]; then
   1237 		statusmsg "Kernels built from ${kernelconf}:"
   1238 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1239 		for kern in ${kernlist:-netbsd}; do
   1240 			[ -f "${kernelbuildpath}/${kern}" ] && \
   1241 			    echo "  ${kernelbuildpath}/${kern}"
   1242 		done | tee -a "${results}"
   1243 	fi
   1244 }
   1245 
   1246 releasekernel()
   1247 {
   1248 	getkernelconf $1
   1249 	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
   1250 	${runcmd} mkdir -p "${kernelreldir}"
   1251 	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1252 	for kern in ${kernlist:-netbsd}; do
   1253 		builtkern="${kernelbuildpath}/${kern}"
   1254 		[ -f "${builtkern}" ] || continue
   1255 		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
   1256 		statusmsg "Kernel copy:      ${releasekern}"
   1257 		${runcmd} gzip -c -9 < "${builtkern}" > "${releasekern}"
   1258 	done
   1259 }
   1260 
   1261 installworld()
   1262 {
   1263 	dir="$1"
   1264 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
   1265 	    bomb "Failed to make installworld to ${dir}"
   1266 	statusmsg "Successful installworld to ${dir}"
   1267 }
   1268 
   1269 
   1270 main()
   1271 {
   1272 	initdefaults
   1273 	_args=$@
   1274 	parseoptions "$@"
   1275 
   1276 	sanitycheck
   1277 
   1278 	build_start=$(date)
   1279 	statusmsg "${progname} command: $0 $@"
   1280 	statusmsg "${progname} started: ${build_start}"
   1281 	statusmsg "NetBSD version:   ${DISTRIBVER}"
   1282 	statusmsg "MACHINE:          ${MACHINE}"
   1283 	statusmsg "MACHINE_ARCH:     ${MACHINE_ARCH}"
   1284 	statusmsg "Build platform:   ${uname_s} ${uname_r} ${uname_m}"
   1285 	statusmsg "HOST_SH:          ${HOST_SH}"
   1286 
   1287 	rebuildmake
   1288 	validatemakeparams
   1289 	createmakewrapper
   1290 
   1291 	# Perform the operations.
   1292 	#
   1293 	for op in ${operations}; do
   1294 		case "${op}" in
   1295 
   1296 		makewrapper)
   1297 			# no-op
   1298 			;;
   1299 
   1300 		tools)
   1301 			buildtools
   1302 			;;
   1303 
   1304 		sets)
   1305 			statusmsg "Building sets from pre-populated ${DESTDIR}"
   1306 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1307 			    bomb "Failed to make ${op}"
   1308 			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
   1309 			statusmsg "Built sets to ${setdir}"
   1310 			;;
   1311 
   1312 		obj|build|distribution|release|sourcesets|syspkgs|params)
   1313 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1314 			    bomb "Failed to make ${op}"
   1315 			statusmsg "Successful make ${op}"
   1316 			;;
   1317 
   1318 		iso-image|iso-image-source)
   1319 			${runcmd} "${makewrapper}" ${parallel} \
   1320 			    CDEXTRA=$iso_dir ${op} ||
   1321 			    bomb "Failed to make ${op}"
   1322 			statusmsg "Successful make ${op}"
   1323 			;;
   1324 
   1325 		kernel=*)
   1326 			arg=${op#*=}
   1327 			buildkernel "${arg}"
   1328 			;;
   1329 
   1330 		releasekernel=*)
   1331 			arg=${op#*=}
   1332 			releasekernel "${arg}"
   1333 			;;
   1334 
   1335 		install=*)
   1336 			arg=${op#*=}
   1337 			if [ "${arg}" = "/" ] && \
   1338 			    (	[ "${uname_s}" != "NetBSD" ] || \
   1339 				[ "${uname_m}" != "${MACHINE}" ] ); then
   1340 				bomb "'${op}' must != / for cross builds."
   1341 			fi
   1342 			installworld "${arg}"
   1343 			;;
   1344 
   1345 		*)
   1346 			bomb "Unknown operation \`${op}'"
   1347 			;;
   1348 
   1349 		esac
   1350 	done
   1351 
   1352 	statusmsg "${progname} ended:   $(date)"
   1353 	if [ -s "${results}" ]; then
   1354 		echo "===> Summary of results:"
   1355 		sed -e 's/^===>//;s/^/	/' "${results}"
   1356 		echo "===> ."
   1357 	fi
   1358 }
   1359 
   1360 main "$@"
   1361