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