Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.215
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.215 2009/10/14 19:03:12 apb 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_modules=false
    235 	do_install=false
    236 	do_sets=false
    237 	do_sourcesets=false
    238 	do_syspkgs=false
    239 	do_iso_image=false
    240 	do_iso_image_source=false
    241 	do_params=false
    242 
    243 	# done_{operation}=true if given operation has been done.
    244 	#
    245 	done_rebuildmake=false
    246 
    247 	# Create scratch directory
    248 	#
    249 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    250 	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
    251 	trap "cd /; rm -r -f \"${tmpdir}\"" 0
    252 	results="${tmpdir}/build.sh.results"
    253 
    254 	# Set source directories
    255 	#
    256 	setmakeenv NETBSDSRCDIR "${TOP}"
    257 
    258 	# Find the version of NetBSD
    259 	#
    260 	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
    261 
    262 	# Set the BUILDSEED to NetBSD-"N"
    263 	#
    264 	setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
    265 
    266 	# Set MKARZERO to "yes"
    267 	#
    268 	setmakeenv MKARZERO "yes"
    269 
    270 	# Set various environment variables to known defaults,
    271 	# to minimize (cross-)build problems observed "in the field".
    272 	#
    273 	unsetmakeenv INFODIR
    274 	unsetmakeenv LESSCHARSET
    275 	setmakeenv LC_ALL C
    276 }
    277 
    278 getarch()
    279 {
    280 	# Translate some MACHINE name aliases (known only to build.sh)
    281 	# into proper MACHINE and MACHINE_ARCH names.  Save the alias
    282 	# name in makewrappermachine.
    283 	#
    284 	case "${MACHINE}" in
    285 
    286 	evbarm-e[bl])
    287 		makewrappermachine=${MACHINE}
    288 		# MACHINE_ARCH is "arm" or "armeb", not "armel"
    289 		MACHINE_ARCH=arm${MACHINE##*-}
    290 		MACHINE_ARCH=${MACHINE_ARCH%el}
    291 		MACHINE=${MACHINE%-e[bl]}
    292 		;;
    293 
    294 	evbmips-e[bl]|sbmips-e[bl])
    295 		makewrappermachine=${MACHINE}
    296 		MACHINE_ARCH=mips${MACHINE##*-}
    297 		MACHINE=${MACHINE%-e[bl]}
    298 		;;
    299 
    300 	evbmips64-e[bl]|sbmips64-e[bl])
    301 		makewrappermachine=${MACHINE}
    302 		MACHINE_ARCH=mips64${MACHINE##*-}
    303 		MACHINE=${MACHINE%64-e[bl]}
    304 		;;
    305 
    306 	evbsh3-e[bl])
    307 		makewrappermachine=${MACHINE}
    308 		MACHINE_ARCH=sh3${MACHINE##*-}
    309 		MACHINE=${MACHINE%-e[bl]}
    310 		;;
    311 
    312 	esac
    313 
    314 	# Translate a MACHINE into a default MACHINE_ARCH.
    315 	#
    316 	case "${MACHINE}" in
    317 
    318 	acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
    319 		MACHINE_ARCH=arm
    320 		;;
    321 
    322 	evbarm)		# unspecified MACHINE_ARCH gets LE
    323 		MACHINE_ARCH=${MACHINE_ARCH:=arm}
    324 		;;
    325 
    326 	hp700)
    327 		MACHINE_ARCH=hppa
    328 		;;
    329 
    330 	sun2)
    331 		MACHINE_ARCH=m68000
    332 		;;
    333 
    334 	amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
    335 		MACHINE_ARCH=m68k
    336 		;;
    337 
    338 	evbmips|sbmips)		# no default MACHINE_ARCH
    339 		;;
    340 
    341 	ews4800mips|mipsco|newsmips|sgimips)
    342 		MACHINE_ARCH=mipseb
    343 		;;
    344 
    345 	algor|arc|cobalt|hpcmips|playstation2|pmax)
    346 		MACHINE_ARCH=mipsel
    347 		;;
    348 
    349 	evbppc64|macppc64|ofppc64)
    350 		makewrappermachine=${MACHINE}
    351 		MACHINE=${MACHINE%64}
    352 		MACHINE_ARCH=powerpc64
    353 		;;
    354 
    355 	amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
    356 		MACHINE_ARCH=powerpc
    357 		;;
    358 
    359 	evbsh3)			# no default MACHINE_ARCH
    360 		;;
    361 
    362 	mmeye)
    363 		MACHINE_ARCH=sh3eb
    364 		;;
    365 
    366 	dreamcast|hpcsh|landisk)
    367 		MACHINE_ARCH=sh3el
    368 		;;
    369 
    370 	amd64)
    371 		MACHINE_ARCH=x86_64
    372 		;;
    373 
    374 	alpha|i386|sparc|sparc64|vax|ia64)
    375 		MACHINE_ARCH=${MACHINE}
    376 		;;
    377 
    378 	*)
    379 		bomb "Unknown target MACHINE: ${MACHINE}"
    380 		;;
    381 
    382 	esac
    383 }
    384 
    385 validatearch()
    386 {
    387 	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
    388 	#
    389 	case "${MACHINE_ARCH}" in
    390 
    391 	alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
    392 		;;
    393 
    394 	"")
    395 		bomb "No MACHINE_ARCH provided"
    396 		;;
    397 
    398 	*)
    399 		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
    400 		;;
    401 
    402 	esac
    403 
    404 	# Determine valid MACHINE_ARCHs for MACHINE
    405 	#
    406 	case "${MACHINE}" in
    407 
    408 	evbarm)
    409 		arches="arm armeb"
    410 		;;
    411 
    412 	evbmips|sbmips)
    413 		arches="mipseb mipsel mips64eb mips64el"
    414 		;;
    415 
    416 	sgimips)
    417 		arches="mipseb mips64eb"
    418 		;;
    419 
    420 	evbsh3)
    421 		arches="sh3eb sh3el"
    422 		;;
    423 
    424 	macppc|evbppc|ofppc)
    425 		arches="powerpc powerpc64"
    426 		;;
    427 	*)
    428 		oma="${MACHINE_ARCH}"
    429 		getarch
    430 		arches="${MACHINE_ARCH}"
    431 		MACHINE_ARCH="${oma}"
    432 		;;
    433 
    434 	esac
    435 
    436 	# Ensure that MACHINE_ARCH supports MACHINE
    437 	#
    438 	archok=false
    439 	for a in ${arches}; do
    440 		if [ "${a}" = "${MACHINE_ARCH}" ]; then
    441 			archok=true
    442 			break
    443 		fi
    444 	done
    445 	${archok} ||
    446 	    bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
    447 }
    448 
    449 # nobomb_getmakevar --
    450 # Given the name of a make variable in $1, print make's idea of the
    451 # value of that variable, or return 1 if there's an error.
    452 #
    453 nobomb_getmakevar()
    454 {
    455 	[ -x "${make}" ] || return 1
    456 	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
    457 _x_:
    458 	echo \${$1}
    459 .include <bsd.prog.mk>
    460 .include <bsd.kernobj.mk>
    461 EOF
    462 }
    463 
    464 # nobomb_getmakevar --
    465 # Given the name of a make variable in $1, print make's idea of the
    466 # value of that variable, or bomb if there's an error.
    467 #
    468 bomb_getmakevar()
    469 {
    470 	[ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
    471 	nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
    472 }
    473 
    474 # nobomb_getmakevar --
    475 # Given the name of a make variable in $1, print make's idea of the
    476 # value of that variable, or print a literal '$' followed by the
    477 # variable name if ${make} is not executable.  This is intended for use in
    478 # messages that need to be readable even if $make hasn't been built,
    479 # such as when build.sh is run with the "-n" option.
    480 #
    481 getmakevar()
    482 {
    483 	if [ -x "${make}" ]; then
    484 		bomb_getmakevar "$1"
    485 	else
    486 		echo "\$$1"
    487 	fi
    488 }
    489 
    490 setmakeenv()
    491 {
    492 	eval "$1='$2'; export $1"
    493 	makeenv="${makeenv} $1"
    494 }
    495 
    496 unsetmakeenv()
    497 {
    498 	eval "unset $1"
    499 	makeenv="${makeenv} $1"
    500 }
    501 
    502 # Given a variable name in $1, modify the variable in place as follows:
    503 # For each space-separated word in the variable, call resolvepath.
    504 resolvepaths()
    505 {
    506 	local var="$1"
    507 	local val
    508 	eval val=\"\${${var}}\"
    509 	local newval=''
    510 	local word
    511 	for word in ${val}; do
    512 		resolvepath word
    513 		newval="${newval}${newval:+ }${word}"
    514 	done
    515 	eval ${var}=\"\${newval}\"
    516 }
    517 
    518 # Given a variable name in $1, modify the variable in place as follows:
    519 # Convert possibly-relative path to absolute path by prepending
    520 # ${TOP} if necessary.  Also delete trailing "/", if any.
    521 resolvepath()
    522 {
    523 	local var="$1"
    524 	local val
    525 	eval val=\"\${${var}}\"
    526 	case "${val}" in
    527 	/)
    528 		;;
    529 	/*)
    530 		val="${val%/}"
    531 		;;
    532 	*)
    533 		val="${TOP}/${val%/}"
    534 		;;
    535 	esac
    536 	eval ${var}=\"\${val}\"
    537 }
    538 
    539 usage()
    540 {
    541 	if [ -n "$*" ]; then
    542 		echo ""
    543 		echo "${progname}: $*"
    544 	fi
    545 	cat <<_usage_
    546 
    547 Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-C cdextras]
    548                 [-D dest] [-j njob] [-M obj] [-m mach] [-N noisy]
    549                 [-O obj] [-R release] [-S seed] [-T tools]
    550                 [-V var=[value]] [-w wrapper] [-X x11src] [-Z var]
    551                 operation [...]
    552 
    553  Build operations (all imply "obj" and "tools"):
    554     build               Run "make build".
    555     distribution        Run "make distribution" (includes DESTDIR/etc/ files).
    556     release             Run "make release" (includes kernels & distrib media).
    557 
    558  Other operations:
    559     help                Show this message and exit.
    560     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
    561                         Always performed.
    562     cleandir            Run "make cleandir".  [Default unless -u is used]
    563     obj                 Run "make obj".  [Default unless -o is used]
    564     tools               Build and install tools.
    565     install=idir        Run "make installworld" to \`idir' to install all sets
    566                         except \`etc'.  Useful after "distribution" or "release"
    567     kernel=conf         Build kernel with config file \`conf'
    568     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
    569     modules             Build and install kernel modules.
    570     sets                Create binary sets in
    571                         RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
    572                         DESTDIR should be populated beforehand.
    573     sourcesets          Create source sets in RELEASEDIR/source/sets.
    574     syspkgs             Create syspkgs in
    575                         RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
    576     iso-image           Create CD-ROM image in RELEASEDIR/iso.
    577     iso-image-source    Create CD-ROM image with source in RELEASEDIR/iso.
    578     params              Display various make(1) parameters.
    579 
    580  Options:
    581     -a arch     Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
    582     -B buildId  Set BUILDID to buildId.
    583     -C cdextras Append cdextras to CDEXTRA variable for inclusion on CD-ROM.
    584     -D dest     Set DESTDIR to dest.  [Default: destdir.MACHINE]
    585     -E          Set "expert" mode; disables various safety checks.
    586                 Should not be used without expert knowledge of the build system.
    587     -h          Print this help message.
    588     -j njob     Run up to njob jobs in parallel; see make(1) -j.
    589     -M obj      Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
    590                 Unsets MAKEOBJDIR.
    591     -m mach     Set MACHINE to mach; not required if NetBSD native.
    592     -N noisy    Set the noisyness (MAKEVERBOSE) level of the build:
    593                     0   Minimal output ("quiet")
    594                     1   Describe what is occurring
    595                     2   Describe what is occurring and echo the actual command
    596                     3   Ignore the effect of the "@" prefix in make commands
    597                     4   Trace shell commands using the shell's -x flag
    598                 [Default: 2]
    599     -n          Show commands that would be executed, but do not execute them.
    600     -O obj      Set obj root directory to obj; sets a MAKEOBJDIR pattern.
    601                 Unsets MAKEOBJDIRPREFIX.
    602     -o          Set MKOBJDIRS=no; do not create objdirs at start of build.
    603     -R release  Set RELEASEDIR to release.  [Default: releasedir]
    604     -r          Remove contents of TOOLDIR and DESTDIR before building.
    605     -S seed     Set BUILDSEED to seed.  [Default: NetBSD-majorversion]
    606     -T tools    Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
    607                 the environment, ${toolprefix}make will be (re)built unconditionally.
    608     -U          Set MKUNPRIVED=yes; build without requiring root privileges,
    609                 install from an UNPRIVED build with proper file permissions.
    610     -u          Set MKUPDATE=yes; do not run "make cleandir" first.
    611                 Without this, everything is rebuilt, including the tools.
    612     -V v=[val]  Set variable \`v' to \`val'.
    613     -w wrapper  Create ${toolprefix}make script as wrapper.
    614                 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
    615     -X x11src   Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
    616     -x          Set MKX11=yes; build X11 from X11SRCDIR
    617     -Z v        Unset ("zap") variable \`v'.
    618 
    619 _usage_
    620 	exit 1
    621 }
    622 
    623 parseoptions()
    624 {
    625 	opts='a:B:C:D:Ehj:M:m:N:nO:oR:rS:T:UuV:w:xX:Z:'
    626 	opt_a=no
    627 
    628 	if type getopts >/dev/null 2>&1; then
    629 		# Use POSIX getopts.
    630 		#
    631 		getoptcmd='getopts ${opts} opt && opt=-${opt}'
    632 		optargcmd=':'
    633 		optremcmd='shift $((${OPTIND} -1))'
    634 	else
    635 		type getopt >/dev/null 2>&1 ||
    636 		    bomb "/bin/sh shell is too old; try ksh or bash"
    637 
    638 		# Use old-style getopt(1) (doesn't handle whitespace in args).
    639 		#
    640 		args="$(getopt ${opts} $*)"
    641 		[ $? = 0 ] || usage
    642 		set -- ${args}
    643 
    644 		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    645 		optargcmd='OPTARG="$1"; shift'
    646 		optremcmd=':'
    647 	fi
    648 
    649 	# Parse command line options.
    650 	#
    651 	while eval ${getoptcmd}; do
    652 		case ${opt} in
    653 
    654 		-a)
    655 			eval ${optargcmd}
    656 			MACHINE_ARCH=${OPTARG}
    657 			opt_a=yes
    658 			;;
    659 
    660 		-B)
    661 			eval ${optargcmd}
    662 			BUILDID=${OPTARG}
    663 			;;
    664 
    665 		-C)
    666 			eval ${optargcmd}; resolvepaths OPTARG
    667 			CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
    668 			;;
    669 
    670 		-D)
    671 			eval ${optargcmd}; resolvepath OPTARG
    672 			setmakeenv DESTDIR "${OPTARG}"
    673 			;;
    674 
    675 		-E)
    676 			do_expertmode=true
    677 			;;
    678 
    679 		-j)
    680 			eval ${optargcmd}
    681 			parallel="-j ${OPTARG}"
    682 			;;
    683 
    684 		-M)
    685 			eval ${optargcmd}; resolvepath OPTARG
    686 			case "${OPTARG}" in
    687 			\$*)	usage "-M argument must not begin with '$'"
    688 				;;
    689 			*\$*)	# can use resolvepath, but can't set TOP_objdir
    690 				resolvepath OPTARG
    691 				;;
    692 			*)	resolvepath OPTARG
    693 				TOP_objdir="${OPTARG}${TOP}"
    694 				;;
    695 			esac
    696 			unsetmakeenv MAKEOBJDIR
    697 			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
    698 			;;
    699 
    700 			# -m overrides MACHINE_ARCH unless "-a" is specified
    701 		-m)
    702 			eval ${optargcmd}
    703 			MACHINE="${OPTARG}"
    704 			[ "${opt_a}" != "yes" ] && getarch
    705 			;;
    706 
    707 		-N)
    708 			eval ${optargcmd}
    709 			case "${OPTARG}" in
    710 			0|1|2|3|4)
    711 				setmakeenv MAKEVERBOSE "${OPTARG}"
    712 				;;
    713 			*)
    714 				usage "'${OPTARG}' is not a valid value for -N"
    715 				;;
    716 			esac
    717 			;;
    718 
    719 		-n)
    720 			runcmd=echo
    721 			;;
    722 
    723 		-O)
    724 			eval ${optargcmd}
    725 			case "${OPTARG}" in
    726 			*\$*)	usage "-O argument must not contain '$'"
    727 				;;
    728 			*)	resolvepath OPTARG
    729 				TOP_objdir="${OPTARG}"
    730 				;;
    731 			esac
    732 			unsetmakeenv MAKEOBJDIRPREFIX
    733 			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
    734 			;;
    735 
    736 		-o)
    737 			MKOBJDIRS=no
    738 			;;
    739 
    740 		-R)
    741 			eval ${optargcmd}; resolvepath OPTARG
    742 			setmakeenv RELEASEDIR "${OPTARG}"
    743 			;;
    744 
    745 		-r)
    746 			do_removedirs=true
    747 			do_rebuildmake=true
    748 			;;
    749 
    750 		-S)
    751 			eval ${optargcmd}
    752 			setmakeenv BUILDSEED "${OPTARG}"
    753 			;;
    754 
    755 		-T)
    756 			eval ${optargcmd}; resolvepath OPTARG
    757 			TOOLDIR="${OPTARG}"
    758 			export TOOLDIR
    759 			;;
    760 
    761 		-U)
    762 			setmakeenv MKUNPRIVED yes
    763 			;;
    764 
    765 		-u)
    766 			setmakeenv MKUPDATE yes
    767 			;;
    768 
    769 		-V)
    770 			eval ${optargcmd}
    771 			case "${OPTARG}" in
    772 		    # XXX: consider restricting which variables can be changed?
    773 			[a-zA-Z_][a-zA-Z_0-9]*=*)
    774 				setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
    775 				;;
    776 			*)
    777 				usage "-V argument must be of the form 'var=[value]'"
    778 				;;
    779 			esac
    780 			;;
    781 
    782 		-w)
    783 			eval ${optargcmd}; resolvepath OPTARG
    784 			makewrapper="${OPTARG}"
    785 			;;
    786 
    787 		-X)
    788 			eval ${optargcmd}; resolvepath OPTARG
    789 			setmakeenv X11SRCDIR "${OPTARG}"
    790 			;;
    791 
    792 		-x)
    793 			setmakeenv MKX11 yes
    794 			;;
    795 
    796 		-Z)
    797 			eval ${optargcmd}
    798 		    # XXX: consider restricting which variables can be unset?
    799 			unsetmakeenv "${OPTARG}"
    800 			;;
    801 
    802 		--)
    803 			break
    804 			;;
    805 
    806 		-'?'|-h)
    807 			usage
    808 			;;
    809 
    810 		esac
    811 	done
    812 
    813 	# Validate operations.
    814 	#
    815 	eval ${optremcmd}
    816 	while [ $# -gt 0 ]; do
    817 		op=$1; shift
    818 		operations="${operations} ${op}"
    819 
    820 		case "${op}" in
    821 
    822 		help)
    823 			usage
    824 			;;
    825 
    826 		makewrapper|cleandir|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
    827 			;;
    828 
    829 		iso-image)
    830 			op=iso_image	# used as part of a variable name
    831 			;;
    832 
    833 		iso-image-source)
    834 			op=iso_image_source   # used as part of a variable name
    835 			;;
    836 
    837 		kernel=*|releasekernel=*)
    838 			arg=${op#*=}
    839 			op=${op%%=*}
    840 			[ -n "${arg}" ] ||
    841 			    bomb "Must supply a kernel name with \`${op}=...'"
    842 			;;
    843 
    844 		modules)
    845 			op=modules
    846 			;;
    847 
    848 		install=*)
    849 			arg=${op#*=}
    850 			op=${op%%=*}
    851 			[ -n "${arg}" ] ||
    852 			    bomb "Must supply a directory with \`install=...'"
    853 			;;
    854 
    855 		*)
    856 			usage "Unknown operation \`${op}'"
    857 			;;
    858 
    859 		esac
    860 		eval do_${op}=true
    861 	done
    862 	[ -n "${operations}" ] || usage "Missing operation to perform."
    863 
    864 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
    865 	#
    866 	if [ -z "${MACHINE}" ]; then
    867 		[ "${uname_s}" = "NetBSD" ] ||
    868 		    bomb "MACHINE must be set, or -m must be used, for cross builds."
    869 		MACHINE=${uname_m}
    870 	fi
    871 	[ -n "${MACHINE_ARCH}" ] || getarch
    872 	validatearch
    873 
    874 	# Set up default make(1) environment.
    875 	#
    876 	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
    877 	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
    878 	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
    879 	export MAKEFLAGS MACHINE MACHINE_ARCH
    880 }
    881 
    882 sanitycheck()
    883 {
    884 	# If the PATH contains any non-absolute components (including,
    885 	# but not limited to, "." or ""), then complain.  As an exception,
    886 	# allow "" or "." as the last component of the PATH.  This is fatal
    887 	# if expert mode is not in effect.
    888 	#
    889 	local path="${PATH}"
    890 	path="${path%:}"	# delete trailing ":"
    891 	path="${path%:.}"	# delete trailing ":."
    892 	case ":${path}:/" in
    893 	*:[!/]*)
    894 		if ${do_expertmode}; then
    895 			warning "PATH contains non-absolute components"
    896 		else
    897 			bomb "PATH environment variable must not" \
    898 			     "contain non-absolute components"
    899 		fi
    900 		;;
    901 	esac
    902 }
    903 
    904 # print_tooldir_make --
    905 # Try to find and print a path to an existing
    906 # ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
    907 # new version of ${toolprefix}make has been built.
    908 #
    909 # * If TOOLDIR was set in the environment or on the command line, use
    910 #   that value.
    911 # * Otherwise try to guess what TOOLDIR would be if not overridden by
    912 #   /etc/mk.conf, and check whether the resulting directory contains
    913 #   a copy of ${toolprefix}make (this should work for everybody who
    914 #   doesn't override TOOLDIR via /etc/mk.conf);
    915 # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
    916 #   in the PATH (this might accidentally find a non-NetBSD version of
    917 #   make, which will lead to failure in the next step);
    918 # * If a copy of make was found above, try to use it with
    919 #   nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
    920 #   result only if it's a directory that already exists;
    921 # * If a value of TOOLDIR was found above, and if
    922 #   ${TOOLDIR}/bin/${toolprefix}make exists, print that value.
    923 #
    924 print_tooldir_make()
    925 {
    926 	local possible_TOP_OBJ
    927 	local possible_TOOLDIR
    928 	local possible_make
    929 	local tooldir_make
    930 
    931 	if [ -n "${TOOLDIR}" ]; then
    932 		echo "${TOOLDIR}/bin/${toolprefix}make"
    933 		return 0
    934 	fi
    935 
    936 	# Set host_ostype to something like "NetBSD-4.5.6-i386".  This
    937 	# is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
    938 	#
    939 	local host_ostype="${uname_s}-$(
    940 		echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
    941 		)-$(
    942 		echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
    943 		)"
    944 
    945 	# Look in a few potential locations for
    946 	# ${possible_TOOLDIR}/bin/${toolprefix}make.
    947 	# If we find it, then set possible_make.
    948 	#
    949 	# In the usual case (without interference from environment
    950 	# variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
    951 	# "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
    952 	#
    953 	# In practice it's difficult to figure out the correct value
    954 	# for _SRC_TOP_OBJ_.  In the easiest case, when the -M or -O
    955 	# options were passed to build.sh, then ${TOP_objdir} will be
    956 	# the correct value.  We also try a few other possibilities, but
    957 	# we do not replicate all the logic of <bsd.obj.mk>.
    958 	#
    959 	for possible_TOP_OBJ in \
    960 		"${TOP_objdir}" \
    961 		"${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
    962 		"${TOP}" \
    963 		"${TOP}/obj" \
    964 		"${TOP}/obj.${MACHINE}"
    965 	do
    966 		[ -n "${possible_TOP_OBJ}" ] || continue
    967 		possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
    968 		possible_make="${possible_TOOLDIR}/bin/${toolprefix}make"
    969 		if [ -x "${possible_make}" ]; then
    970 			break
    971 		else
    972 			unset possible_make
    973 		fi
    974 	done
    975 
    976 	# If the above didn't work, search the PATH for a suitable
    977 	# ${toolprefix}make, nbmake, bmake, or make.
    978 	#
    979 	: ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
    980 	: ${possible_make:=$(find_in_PATH nbmake '')}
    981 	: ${possible_make:=$(find_in_PATH bmake '')}
    982 	: ${possible_make:=$(find_in_PATH make '')}
    983 
    984 	# At this point, we don't care whether possible_make is in the
    985 	# correct TOOLDIR or not; we simply want it to be usable by
    986 	# getmakevar to help us find the correct TOOLDIR.
    987 	#
    988 	# Use ${possible_make} with nobomb_getmakevar to try to find
    989 	# the value of TOOLDIR.  Believe the result only if it's
    990 	# a directory that already exists and contains bin/${toolprefix}make.
    991 	#
    992 	if [ -x "${possible_make}" ]; then
    993 		possible_TOOLDIR="$(
    994 			make="${possible_make}" nobomb_getmakevar TOOLDIR
    995 			)"
    996 		if [ $? = 0 ] && [ -n "${possible_TOOLDIR}" ] \
    997 		    && [ -d "${possible_TOOLDIR}" ];
    998 		then
    999 			tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
   1000 			if [ -x "${tooldir_make}" ]; then
   1001 				echo "${tooldir_make}"
   1002 				return 0
   1003 			fi
   1004 		fi
   1005 	fi
   1006 	return 1
   1007 }
   1008 
   1009 # rebuildmake --
   1010 # Rebuild nbmake in a temporary directory if necessary.  Sets $make
   1011 # to a path to the nbmake executable.  Sets done_rebuildmake=true
   1012 # if nbmake was rebuilt.
   1013 #
   1014 # There is a cyclic dependency between building nbmake and choosing
   1015 # TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
   1016 # would like to use getmakevar to get the value of TOOLDIR; but we can't
   1017 # use getmakevar before we have an up to date version of nbmake; we
   1018 # might already have an up to date version of nbmake in TOOLDIR, but we
   1019 # don't yet know where TOOLDIR is.
   1020 #
   1021 # The default value of TOOLDIR also depends on the location of the top
   1022 # level object directory, so $(getmakevar TOOLDIR) invoked before or
   1023 # after making the top level object directory may produce different
   1024 # results.
   1025 #
   1026 # Strictly speaking, we should do the following:
   1027 #
   1028 #    1. build a new version of nbmake in a temporary directory;
   1029 #    2. use the temporary nbmake to create the top level obj directory;
   1030 #    3. use $(getmakevar TOOLDIR) with the temporary nbmake to
   1031 #       get the corect value of TOOLDIR;
   1032 #    4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
   1033 #
   1034 # However, people don't like building nbmake unnecessarily if their
   1035 # TOOLDIR has not changed since an earlier build.  We try to avoid
   1036 # rebuilding a temporary version of nbmake by taking some shortcuts to
   1037 # guess a value for TOOLDIR, looking for an existing version of nbmake
   1038 # in that TOOLDIR, and checking whether that nbmake is newer than the
   1039 # sources used to build it.
   1040 #
   1041 rebuildmake()
   1042 {
   1043 	make="$(print_tooldir_make)"
   1044 	if [ -n "${make}" ] && [ -x "${make}" ]; then
   1045 		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
   1046 			if [ "${f}" -nt "${make}" ]; then
   1047 				statusmsg "${make} outdated" \
   1048 					"(older than ${f}), needs building."
   1049 				do_rebuildmake=true
   1050 				break
   1051 			fi
   1052 		done
   1053 	else
   1054 		statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
   1055 		do_rebuildmake=true
   1056 	fi
   1057 
   1058 	# Build bootstrap ${toolprefix}make if needed.
   1059 	if ${do_rebuildmake}; then
   1060 		statusmsg "Bootstrapping ${toolprefix}make"
   1061 		${runcmd} cd "${tmpdir}"
   1062 		${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
   1063 			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
   1064 			${HOST_SH} "${TOP}/tools/make/configure" ||
   1065 		    bomb "Configure of ${toolprefix}make failed"
   1066 		${runcmd} ${HOST_SH} buildmake.sh ||
   1067 		    bomb "Build of ${toolprefix}make failed"
   1068 		make="${tmpdir}/${toolprefix}make"
   1069 		${runcmd} cd "${TOP}"
   1070 		${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
   1071 		done_rebuildmake=true
   1072 	fi
   1073 }
   1074 
   1075 validatemakeparams()
   1076 {
   1077 	if [ "${runcmd}" = "echo" ]; then
   1078 		TOOLCHAIN_MISSING=no
   1079 		EXTERNAL_TOOLCHAIN=""
   1080 	else
   1081 		TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
   1082 		EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
   1083 	fi
   1084 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
   1085 	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
   1086 		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
   1087 		${runcmd} echo "	MACHINE:      ${MACHINE}"
   1088 		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
   1089 		${runcmd} echo ""
   1090 		${runcmd} echo "All builds for this platform should be done via a traditional make"
   1091 		${runcmd} echo "If you wish to use an external cross-toolchain, set"
   1092 		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
   1093 		${runcmd} echo "in either the environment or mk.conf and rerun"
   1094 		${runcmd} echo "	${progname} $*"
   1095 		exit 1
   1096 	fi
   1097 
   1098 	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
   1099 	# These may be set as build.sh options or in "mk.conf".
   1100 	# Don't export them as they're only used for tests in build.sh.
   1101 	#
   1102 	MKOBJDIRS=$(getmakevar MKOBJDIRS)
   1103 	MKUNPRIVED=$(getmakevar MKUNPRIVED)
   1104 	MKUPDATE=$(getmakevar MKUPDATE)
   1105 
   1106 	if [ "${MKOBJDIRS}" != "no" ]; then
   1107 		# Create the top-level object directory.
   1108 		#
   1109 		# "make obj NOSUBDIR=" can handle most cases, but it
   1110 		# can't handle the case where MAKEOBJDIRPREFIX is set
   1111 		# while the corresponding directory does not exist
   1112 		# (rules in <bsd.obj.mk> would abort the build).  We
   1113 		# therefore have to handle the MAKEOBJDIRPREFIX case
   1114 		# without invoking "make obj".  The MAKEOBJDIR case
   1115 		# could be handled either way, but we choose to handle
   1116 		# it similarly to MAKEOBJDIRPREFIX.
   1117 		#
   1118 		if [ -n "${TOP_obj}" ]; then
   1119 			# It must have been set by the "-M" or "-O"
   1120 			# command line options, so there's no need to
   1121 			# use getmakevar
   1122 			:
   1123 		elif [ -n "$MAKEOBJDIRPREFIX" ]; then
   1124 			TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
   1125 		elif [ -n "$MAKEOBJDIR" ]; then
   1126 			TOP_obj="$(getmakevar MAKEOBJDIR)"
   1127 		fi
   1128 		if [ -n "$TOP_obj" ]; then
   1129 			${runcmd} mkdir -p "${TOP_obj}" ||
   1130 			    bomb "Can't create top level object directory" \
   1131 					"${TOP_obj}"
   1132 		else
   1133 			${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
   1134 			    bomb "Can't create top level object directory" \
   1135 					"using make obj"
   1136 		fi
   1137 
   1138 		# make obj in tools to ensure that the objdir for "tools"
   1139 		# is available.
   1140 		#
   1141 		${runcmd} cd tools
   1142 		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
   1143 		    bomb "Failed to make obj in tools"
   1144 		${runcmd} cd "${TOP}"
   1145 	fi
   1146 
   1147 	# Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
   1148 	# and bomb if they have changed from the values we had from the
   1149 	# command line or environment.
   1150 	#
   1151 	# This must be done after creating the top-level object directory.
   1152 	#
   1153 	for var in TOOLDIR DESTDIR RELEASEDIR
   1154 	do
   1155 		eval oldval=\"\$${var}\"
   1156 		newval="$(getmakevar $var)"
   1157 		if ! $do_expertmode; then
   1158 			: ${__SRC_TOP_OBJ__:=$(getmakevar __SRC_TOP_OBJ__)}
   1159 			case "$var" in
   1160 			DESTDIR)
   1161 				: ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
   1162 				;;
   1163 			RELEASEDIR)
   1164 				: ${newval:=${_SRC_TOP_OBJ_}/releasedir}
   1165 				;;
   1166 			esac
   1167 		fi
   1168 		if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]; then
   1169 			bomb "Value of ${var} has changed" \
   1170 				"(was \"${oldval}\", now \"${newval}\")"
   1171 		fi
   1172 		eval ${var}=\"\${newval}\"
   1173 		eval export ${var}
   1174 		statusmsg "${var} path:     ${newval}"
   1175 	done
   1176 
   1177 	# RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
   1178 	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
   1179 
   1180 	# Check validity of TOOLDIR and DESTDIR.
   1181 	#
   1182 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
   1183 		bomb "TOOLDIR '${TOOLDIR}' invalid"
   1184 	fi
   1185 	removedirs="${TOOLDIR}"
   1186 
   1187 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
   1188 		if ${do_build} || ${do_distribution} || ${do_release}; then
   1189 			if ! ${do_build} || \
   1190 			   [ "${uname_s}" != "NetBSD" ] || \
   1191 			   [ "${uname_m}" != "${MACHINE}" ]; then
   1192 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
   1193 			fi
   1194 			if ! ${do_expertmode}; then
   1195 				bomb "DESTDIR must != / for non -E (expert) builds"
   1196 			fi
   1197 			statusmsg "WARNING: Building to /, in expert mode."
   1198 			statusmsg "         This may cause your system to break!  Reasons include:"
   1199 			statusmsg "            - your kernel is not up to date"
   1200 			statusmsg "            - the libraries or toolchain have changed"
   1201 			statusmsg "         YOU HAVE BEEN WARNED!"
   1202 		fi
   1203 	else
   1204 		removedirs="${removedirs} ${DESTDIR}"
   1205 	fi
   1206 	if ${do_build} || ${do_distribution} || ${do_release}; then
   1207 		if ! ${do_expertmode} && \
   1208 		    [ "$id_u" -ne 0 ] && \
   1209 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1210 			bomb "-U or -E must be set for build as an unprivileged user."
   1211 		fi
   1212 	fi
   1213 	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
   1214 		bomb "Must set RELEASEDIR with \`releasekernel=...'"
   1215 	fi
   1216 
   1217 	# Install as non-root is a bad idea.
   1218 	#
   1219 	if ${do_install} && [ "$id_u" -ne 0 ] ; then
   1220 		if ${do_expertmode}; then
   1221 			warning "Will install as an unprivileged user."
   1222 		else
   1223 			bomb "-E must be set for install as an unprivileged user."
   1224 		fi
   1225 	fi
   1226 
   1227 	# If a previous build.sh run used -U (and therefore created a
   1228 	# METALOG file), then most subsequent build.sh runs must also
   1229 	# use -U.  If DESTDIR is about to be removed, then don't perform
   1230 	# this check.
   1231 	#
   1232 	case "${do_removedirs} ${removedirs} " in
   1233 	true*" ${DESTDIR} "*)
   1234 		# DESTDIR is about to be removed
   1235 		;;
   1236 	*)
   1237 		if ( ${do_build} || ${do_distribution} || ${do_release} || \
   1238 		    ${do_install} ) && \
   1239 		    [ -e "${DESTDIR}/METALOG" ] && \
   1240 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1241 			if $do_expertmode; then
   1242 				warning "A previous build.sh run specified -U."
   1243 			else
   1244 				bomb "A previous build.sh run specified -U; you must specify it again now."
   1245 			fi
   1246 		fi
   1247 		;;
   1248 	esac
   1249 }
   1250 
   1251 
   1252 createmakewrapper()
   1253 {
   1254 	# Remove the target directories.
   1255 	#
   1256 	if ${do_removedirs}; then
   1257 		for f in ${removedirs}; do
   1258 			statusmsg "Removing ${f}"
   1259 			${runcmd} rm -r -f "${f}"
   1260 		done
   1261 	fi
   1262 
   1263 	# Recreate $TOOLDIR.
   1264 	#
   1265 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
   1266 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
   1267 
   1268 	# If we did not previously rebuild ${toolprefix}make, then
   1269 	# check whether $make is still valid and the same as the output
   1270 	# from print_tooldir_make.  If not, then rebuild make now.  A
   1271 	# possible reason for this being necessary is that the actual
   1272 	# value of TOOLDIR might be different from the value guessed
   1273 	# before the top level obj dir was created.
   1274 	#
   1275 	if ! ${done_rebuildmake} && \
   1276 	    ( [ ! -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] )
   1277 	then
   1278 		rebuildmake
   1279 	fi
   1280 
   1281 	# Install ${toolprefix}make if it was built.
   1282 	#
   1283 	if ${done_rebuildmake}; then
   1284 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
   1285 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
   1286 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
   1287 		make="${TOOLDIR}/bin/${toolprefix}make"
   1288 		statusmsg "Created ${make}"
   1289 	fi
   1290 
   1291 	# Build a ${toolprefix}make wrapper script, usable by hand as
   1292 	# well as by build.sh.
   1293 	#
   1294 	if [ -z "${makewrapper}" ]; then
   1295 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
   1296 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
   1297 	fi
   1298 
   1299 	${runcmd} rm -f "${makewrapper}"
   1300 	if [ "${runcmd}" = "echo" ]; then
   1301 		echo 'cat <<EOF >'${makewrapper}
   1302 		makewrapout=
   1303 	else
   1304 		makewrapout=">>\${makewrapper}"
   1305 	fi
   1306 
   1307 	case "${KSH_VERSION:-${SH_VERSION}}" in
   1308 	*PD\ KSH*|*MIRBSD\ KSH*)
   1309 		set +o braceexpand
   1310 		;;
   1311 	esac
   1312 
   1313 	eval cat <<EOF ${makewrapout}
   1314 #! ${HOST_SH}
   1315 # Set proper variables to allow easy "make" building of a NetBSD subtree.
   1316 # Generated from:  \$NetBSD: build.sh,v 1.215 2009/10/14 19:03:12 apb Exp $
   1317 # with these arguments: ${_args}
   1318 #
   1319 
   1320 EOF
   1321 	{
   1322 		for f in ${makeenv}; do
   1323 			if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
   1324 				eval echo "unset ${f}"
   1325 			else
   1326 				eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
   1327 			fi
   1328 		done
   1329 
   1330 		eval cat <<EOF
   1331 MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
   1332 USETOOLS=yes; export USETOOLS
   1333 EOF
   1334 	} | eval sort -u "${makewrapout}"
   1335 	eval cat <<EOF "${makewrapout}"
   1336 
   1337 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
   1338 EOF
   1339 	[ "${runcmd}" = "echo" ] && echo EOF
   1340 	${runcmd} chmod +x "${makewrapper}"
   1341 	statusmsg "makewrapper:      ${makewrapper}"
   1342 	statusmsg "Updated ${makewrapper}"
   1343 }
   1344 
   1345 make_in_dir()
   1346 {
   1347 	dir="$1"
   1348 	op="$2"
   1349 	${runcmd} cd "${dir}" ||
   1350 	    bomb "Failed to cd to \"${dir}\""
   1351 	${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1352 	    bomb "Failed to make ${op} in \"${dir}\""
   1353 	${runcmd} cd "${TOP}" ||
   1354 	    bomb "Failed to cd back to \"${TOP}\""
   1355 }
   1356 
   1357 buildtools()
   1358 {
   1359 	if [ "${MKOBJDIRS}" != "no" ]; then
   1360 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
   1361 		    bomb "Failed to make obj-tools"
   1362 	fi
   1363 	if [ "${MKUPDATE}" = "no" ]; then
   1364 		make_in_dir tools cleandir
   1365 	fi
   1366 	make_in_dir tools dependall
   1367 	make_in_dir tools install
   1368 	statusmsg "Tools built to ${TOOLDIR}"
   1369 }
   1370 
   1371 getkernelconf()
   1372 {
   1373 	kernelconf="$1"
   1374 	if [ "${MKOBJDIRS}" != "no" ]; then
   1375 		# The correct value of KERNOBJDIR might
   1376 		# depend on a prior "make obj" in
   1377 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
   1378 		#
   1379 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
   1380 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
   1381 		make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
   1382 	fi
   1383 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
   1384 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
   1385 	case "${kernelconf}" in
   1386 	*/*)
   1387 		kernelconfpath="${kernelconf}"
   1388 		kernelconfname="${kernelconf##*/}"
   1389 		;;
   1390 	*)
   1391 		kernelconfpath="${KERNCONFDIR}/${kernelconf}"
   1392 		kernelconfname="${kernelconf}"
   1393 		;;
   1394 	esac
   1395 	kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
   1396 }
   1397 
   1398 buildkernel()
   1399 {
   1400 	if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
   1401 		# Building tools every time we build a kernel is clearly
   1402 		# unnecessary.  We could try to figure out whether rebuilding
   1403 		# the tools is necessary this time, but it doesn't seem worth
   1404 		# the trouble.  Instead, we say it's the user's responsibility
   1405 		# to rebuild the tools if necessary.
   1406 		#
   1407 		statusmsg "Building kernel without building new tools"
   1408 		buildkernelwarned=true
   1409 	fi
   1410 	getkernelconf $1
   1411 	statusmsg "Building kernel:  ${kernelconf}"
   1412 	statusmsg "Build directory:  ${kernelbuildpath}"
   1413 	${runcmd} mkdir -p "${kernelbuildpath}" ||
   1414 	    bomb "Cannot mkdir: ${kernelbuildpath}"
   1415 	if [ "${MKUPDATE}" = "no" ]; then
   1416 		make_in_dir "${kernelbuildpath}" cleandir
   1417 	fi
   1418 	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
   1419 	|| bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
   1420 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
   1421 		-s "${TOP}/sys" "${kernelconfpath}" ||
   1422 	    bomb "${toolprefix}config failed for ${kernelconf}"
   1423 	make_in_dir "${kernelbuildpath}" depend
   1424 	make_in_dir "${kernelbuildpath}" all
   1425 
   1426 	if [ "${runcmd}" != "echo" ]; then
   1427 		statusmsg "Kernels built from ${kernelconf}:"
   1428 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1429 		for kern in ${kernlist:-netbsd}; do
   1430 			[ -f "${kernelbuildpath}/${kern}" ] && \
   1431 			    echo "  ${kernelbuildpath}/${kern}"
   1432 		done | tee -a "${results}"
   1433 	fi
   1434 }
   1435 
   1436 releasekernel()
   1437 {
   1438 	getkernelconf $1
   1439 	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
   1440 	${runcmd} mkdir -p "${kernelreldir}"
   1441 	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1442 	for kern in ${kernlist:-netbsd}; do
   1443 		builtkern="${kernelbuildpath}/${kern}"
   1444 		[ -f "${builtkern}" ] || continue
   1445 		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
   1446 		statusmsg "Kernel copy:      ${releasekern}"
   1447 		if [ "${runcmd}" = "echo" ]; then
   1448 			echo "gzip -c -9 < ${builtkern} > ${releasekern}"
   1449 		else
   1450 			gzip -c -9 < "${builtkern}" > "${releasekern}"
   1451 		fi
   1452 	done
   1453 }
   1454 
   1455 buildmodules()
   1456 {
   1457 	if ! ${do_tools} && ! ${buildmoduleswarned:-false}; then
   1458 		# Building tools every time we build modules is clearly
   1459 		# unnecessary as well as a kernel.
   1460 		#
   1461 		statusmsg "Building modules without building new tools"
   1462 		buildmoduleswarned=true
   1463 	fi
   1464 
   1465 	statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
   1466 	if [ "${MKOBJDIRS}" != "no" ]; then
   1467 		make_in_dir sys/modules obj ||
   1468 		    bomb "Failed to make obj in sys/modules"
   1469 	fi
   1470 	if [ "${MKUPDATE}" = "no" ]; then
   1471 		make_in_dir sys/modules cleandir
   1472 	fi
   1473 	${runcmd} "${makewrapper}" ${parallel} do-sys-modules ||
   1474 	    bomb "Failed to make do-sys-modules"
   1475 
   1476 	statusmsg "Successful build kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
   1477 }
   1478 
   1479 installworld()
   1480 {
   1481 	dir="$1"
   1482 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
   1483 	    bomb "Failed to make installworld to ${dir}"
   1484 	statusmsg "Successful installworld to ${dir}"
   1485 }
   1486 
   1487 
   1488 main()
   1489 {
   1490 	initdefaults
   1491 	_args=$@
   1492 	parseoptions "$@"
   1493 
   1494 	sanitycheck
   1495 
   1496 	build_start=$(date)
   1497 	statusmsg "${progname} command: $0 $@"
   1498 	statusmsg "${progname} started: ${build_start}"
   1499 	statusmsg "NetBSD version:   ${DISTRIBVER}"
   1500 	statusmsg "MACHINE:          ${MACHINE}"
   1501 	statusmsg "MACHINE_ARCH:     ${MACHINE_ARCH}"
   1502 	statusmsg "Build platform:   ${uname_s} ${uname_r} ${uname_m}"
   1503 	statusmsg "HOST_SH:          ${HOST_SH}"
   1504 
   1505 	rebuildmake
   1506 	validatemakeparams
   1507 	createmakewrapper
   1508 
   1509 	# Perform the operations.
   1510 	#
   1511 	for op in ${operations}; do
   1512 		case "${op}" in
   1513 
   1514 		makewrapper)
   1515 			# no-op
   1516 			;;
   1517 
   1518 		tools)
   1519 			buildtools
   1520 			;;
   1521 
   1522 		sets)
   1523 			statusmsg "Building sets from pre-populated ${DESTDIR}"
   1524 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1525 			    bomb "Failed to make ${op}"
   1526 			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
   1527 			statusmsg "Built sets to ${setdir}"
   1528 			;;
   1529 
   1530 		cleandir|obj|build|distribution|release|sourcesets|syspkgs|params)
   1531 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1532 			    bomb "Failed to make ${op}"
   1533 			statusmsg "Successful make ${op}"
   1534 			;;
   1535 
   1536 		iso-image|iso-image-source)
   1537 			${runcmd} "${makewrapper}" ${parallel} \
   1538 			    CDEXTRA="$CDEXTRA" ${op} ||
   1539 			    bomb "Failed to make ${op}"
   1540 			statusmsg "Successful make ${op}"
   1541 			;;
   1542 
   1543 		kernel=*)
   1544 			arg=${op#*=}
   1545 			buildkernel "${arg}"
   1546 			;;
   1547 
   1548 		releasekernel=*)
   1549 			arg=${op#*=}
   1550 			releasekernel "${arg}"
   1551 			;;
   1552 
   1553 		modules)
   1554 			buildmodules
   1555 			;;
   1556 
   1557 		install=*)
   1558 			arg=${op#*=}
   1559 			if [ "${arg}" = "/" ] && \
   1560 			    (	[ "${uname_s}" != "NetBSD" ] || \
   1561 				[ "${uname_m}" != "${MACHINE}" ] ); then
   1562 				bomb "'${op}' must != / for cross builds."
   1563 			fi
   1564 			installworld "${arg}"
   1565 			;;
   1566 
   1567 		*)
   1568 			bomb "Unknown operation \`${op}'"
   1569 			;;
   1570 
   1571 		esac
   1572 	done
   1573 
   1574 	statusmsg "${progname} ended:   $(date)"
   1575 	if [ -s "${results}" ]; then
   1576 		echo "===> Summary of results:"
   1577 		sed -e 's/^===>//;s/^/	/' "${results}"
   1578 		echo "===> ."
   1579 	fi
   1580 }
   1581 
   1582 main "$@"
   1583