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