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