Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.255
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.255 2012/08/05 04:39:09 matt Exp $
      3 #
      4 # Copyright (c) 2001-2011 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, to build or cross-build NetBSD.
     33 #
     34 
     35 #
     36 # {{{ Begin shell feature tests.
     37 #
     38 # We try to determine whether or not this script is being run under
     39 # a shell that supports the features that we use.  If not, we try to
     40 # re-exec the script under another shell.  If we can't find another
     41 # suitable shell, then we print a message and exit.
     42 #
     43 
     44 errmsg=''		# error message, if not empty
     45 shelltest=false		# if true, exit after testing the shell
     46 re_exec_allowed=true	# if true, we may exec under another shell
     47 
     48 # Parse special command line options in $1.  These special options are
     49 # for internal use only, are not documented, and are not valid anywhere
     50 # other than $1.
     51 case "$1" in
     52 "--shelltest")
     53     shelltest=true
     54     re_exec_allowed=false
     55     shift
     56     ;;
     57 "--no-re-exec")
     58     re_exec_allowed=false
     59     shift
     60     ;;
     61 esac
     62 
     63 # Solaris /bin/sh, and other SVR4 shells, do not support "!".
     64 # This is the first feature that we test, because subsequent
     65 # tests use "!".
     66 #
     67 if test -z "$errmsg"; then
     68     if ( eval '! false' ) >/dev/null 2>&1 ; then
     69 	:
     70     else
     71 	errmsg='Shell does not support "!".'
     72     fi
     73 fi
     74 
     75 # Does the shell support functions?
     76 #
     77 if test -z "$errmsg"; then
     78     if ! (
     79 	eval 'somefunction() { : ; }'
     80 	) >/dev/null 2>&1
     81     then
     82 	errmsg='Shell does not support functions.'
     83     fi
     84 fi
     85 
     86 # Does the shell support the "local" keyword for variables in functions?
     87 #
     88 # Local variables are not required by SUSv3, but some scripts run during
     89 # the NetBSD build use them.
     90 #
     91 # ksh93 fails this test; it uses an incompatible syntax involving the
     92 # keywords 'function' and 'typeset'.
     93 #
     94 if test -z "$errmsg"; then
     95     if ! (
     96 	eval 'f() { local v=2; }; v=1; f && test x"$v" = x"1"'
     97 	) >/dev/null 2>&1
     98     then
     99 	errmsg='Shell does not support the "local" keyword in functions.'
    100     fi
    101 fi
    102 
    103 # Does the shell support ${var%suffix}, ${var#prefix}, and their variants?
    104 #
    105 # We don't bother testing for ${var+value}, ${var-value}, or their variants,
    106 # since shells without those are sure to fail other tests too.
    107 #
    108 if test -z "$errmsg"; then
    109     if ! (
    110 	eval 'var=a/b/c ;
    111 	      test x"${var#*/};${var##*/};${var%/*};${var%%/*}" = \
    112 		   x"b/c;c;a/b;a" ;'
    113 	) >/dev/null 2>&1
    114     then
    115 	errmsg='Shell does not support "${var%suffix}" or "${var#prefix}".'
    116     fi
    117 fi
    118 
    119 # Does the shell support IFS?
    120 #
    121 # zsh in normal mode (as opposed to "emulate sh" mode) fails this test.
    122 #
    123 if test -z "$errmsg"; then
    124     if ! (
    125 	eval 'IFS=: ; v=":a b::c" ; set -- $v ; IFS=+ ;
    126 		test x"$#;$1,$2,$3,$4;$*" = x"4;,a b,,c;+a b++c"'
    127 	) >/dev/null 2>&1
    128     then
    129 	errmsg='Shell does not support IFS word splitting.'
    130     fi
    131 fi
    132 
    133 # Does the shell support ${1+"$@"}?
    134 #
    135 # Some versions of zsh fail this test, even in "emulate sh" mode.
    136 #
    137 if test -z "$errmsg"; then
    138     if ! (
    139 	eval 'set -- "a a a" "b b b"; set -- ${1+"$@"};
    140 	      test x"$#;$1;$2" = x"2;a a a;b b b";'
    141 	) >/dev/null 2>&1
    142     then
    143 	errmsg='Shell does not support ${1+"$@"}.'
    144     fi
    145 fi
    146 
    147 # Does the shell support $(...) command substitution?
    148 #
    149 if test -z "$errmsg"; then
    150     if ! (
    151 	eval 'var=$(echo abc); test x"$var" = x"abc"'
    152 	) >/dev/null 2>&1
    153     then
    154 	errmsg='Shell does not support "$(...)" command substitution.'
    155     fi
    156 fi
    157 
    158 # Does the shell support $(...) command substitution with
    159 # unbalanced parentheses?
    160 #
    161 # Some shells known to fail this test are:  NetBSD /bin/ksh (as of 2009-12),
    162 # bash-3.1, pdksh-5.2.14, zsh-4.2.7 in "emulate sh" mode.
    163 #
    164 if test -z "$errmsg"; then
    165     if ! (
    166 	eval 'var=$(case x in x) echo abc;; esac); test x"$var" = x"abc"'
    167 	) >/dev/null 2>&1
    168     then
    169 	# XXX: This test is ignored because so many shells fail it; instead,
    170 	#      the NetBSD build avoids using the problematic construct.
    171 	: ignore 'Shell does not support "$(...)" with unbalanced ")".'
    172     fi
    173 fi
    174 
    175 # Does the shell support getopts or getopt?
    176 #
    177 if test -z "$errmsg"; then
    178     if ! (
    179 	eval 'type getopts || type getopt'
    180 	) >/dev/null 2>&1
    181     then
    182 	errmsg='Shell does not support getopts or getopt.'
    183     fi
    184 fi
    185 
    186 #
    187 # If shelltest is true, exit now, reporting whether or not the shell is good.
    188 #
    189 if $shelltest; then
    190     if test -n "$errmsg"; then
    191 	echo >&2 "$0: $errmsg"
    192 	exit 1
    193     else
    194 	exit 0
    195     fi
    196 fi
    197 
    198 #
    199 # If the shell was bad, try to exec a better shell, or report an error.
    200 #
    201 # Loops are broken by passing an extra "--no-re-exec" flag to the new
    202 # instance of this script.
    203 #
    204 if test -n "$errmsg"; then
    205     if $re_exec_allowed; then
    206 	for othershell in \
    207 	    "${HOST_SH}" /usr/xpg4/bin/sh ksh ksh88 mksh pdksh bash dash
    208 	    # NOTE: some shells known not to work are:
    209 	    # any shell using csh syntax;
    210 	    # Solaris /bin/sh (missing many modern features);
    211 	    # ksh93 (incompatible syntax for local variables);
    212 	    # zsh (many differences, unless run in compatibility mode).
    213 	do
    214 	    test -n "$othershell" || continue
    215 	    if eval 'type "$othershell"' >/dev/null 2>&1 \
    216 		&& "$othershell" "$0" --shelltest >/dev/null 2>&1
    217 	    then
    218 		cat <<EOF
    219 $0: $errmsg
    220 $0: Retrying under $othershell
    221 EOF
    222 		HOST_SH="$othershell"
    223 		export HOST_SH
    224 		exec $othershell "$0" --no-re-exec "$@" # avoid ${1+"$@"}
    225 	    fi
    226 	    # If HOST_SH was set, but failed the test above,
    227 	    # then give up without trying any other shells.
    228 	    test x"${othershell}" = x"${HOST_SH}" && break
    229 	done
    230     fi
    231 
    232     #
    233     # If we get here, then the shell is bad, and we either could not
    234     # find a replacement, or were not allowed to try a replacement.
    235     #
    236     cat <<EOF
    237 $0: $errmsg
    238 
    239 The NetBSD build system requires a shell that supports modern POSIX
    240 features, as well as the "local" keyword in functions (which is a
    241 widely-implemented but non-standardised feature).
    242 
    243 Please re-run this script under a suitable shell.  For example:
    244 
    245 	/path/to/suitable/shell $0 ...
    246 
    247 The above command will usually enable build.sh to automatically set
    248 HOST_SH=/path/to/suitable/shell, but if that fails, then you may also
    249 need to explicitly set the HOST_SH environment variable, as follows:
    250 
    251 	HOST_SH=/path/to/suitable/shell
    252 	export HOST_SH
    253 	\${HOST_SH} $0 ...
    254 EOF
    255     exit 1
    256 fi
    257 
    258 #
    259 # }}} End shell feature tests.
    260 #
    261 
    262 progname=${0##*/}
    263 toppid=$$
    264 results=/dev/null
    265 tab='	'
    266 trap "exit 1" 1 2 3 15
    267 
    268 bomb()
    269 {
    270 	cat >&2 <<ERRORMESSAGE
    271 
    272 ERROR: $@
    273 *** BUILD ABORTED ***
    274 ERRORMESSAGE
    275 	kill ${toppid}		# in case we were invoked from a subshell
    276 	exit 1
    277 }
    278 
    279 
    280 statusmsg()
    281 {
    282 	${runcmd} echo "===> $@" | tee -a "${results}"
    283 }
    284 
    285 statusmsg2()
    286 {
    287 	local msg
    288 
    289 	msg="${1}"
    290 	shift
    291 	case "${msg}" in
    292 	????????????????*)	;;
    293 	??????????*)		msg="${msg}      ";;
    294 	?????*)			msg="${msg}           ";;
    295 	*)			msg="${msg}                ";;
    296 	esac
    297 	case "${msg}" in
    298 	?????????????????????*)	;;
    299 	????????????????????)	msg="${msg} ";;
    300 	???????????????????)	msg="${msg}  ";;
    301 	??????????????????)	msg="${msg}   ";;
    302 	?????????????????)	msg="${msg}    ";;
    303 	????????????????)	msg="${msg}     ";;
    304 	esac
    305 	statusmsg "${msg}$*"
    306 }
    307 
    308 warning()
    309 {
    310 	statusmsg "Warning: $@"
    311 }
    312 
    313 # Find a program in the PATH, and print the result.  If not found,
    314 # print a default.  If $2 is defined (even if it is an empty string),
    315 # then that is the default; otherwise, $1 is used as the default.
    316 find_in_PATH()
    317 {
    318 	local prog="$1"
    319 	local result="${2-"$1"}"
    320 	local oldIFS="${IFS}"
    321 	local dir
    322 	IFS=":"
    323 	for dir in ${PATH}; do
    324 		if [ -x "${dir}/${prog}" ]; then
    325 			result="${dir}/${prog}"
    326 			break
    327 		fi
    328 	done
    329 	IFS="${oldIFS}"
    330 	echo "${result}"
    331 }
    332 
    333 # Try to find a working POSIX shell, and set HOST_SH to refer to it.
    334 # Assumes that uname_s, uname_m, and PWD have been set.
    335 set_HOST_SH()
    336 {
    337 	# Even if ${HOST_SH} is already defined, we still do the
    338 	# sanity checks at the end.
    339 
    340 	# Solaris has /usr/xpg4/bin/sh.
    341 	#
    342 	[ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
    343 		[ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
    344 
    345 	# Try to get the name of the shell that's running this script,
    346 	# by parsing the output from "ps".  We assume that, if the host
    347 	# system's ps command supports -o comm at all, it will do so
    348 	# in the usual way: a one-line header followed by a one-line
    349 	# result, possibly including trailing white space.  And if the
    350 	# host system's ps command doesn't support -o comm, we assume
    351 	# that we'll get an error message on stderr and nothing on
    352 	# stdout.  (We don't try to use ps -o 'comm=' to suppress the
    353 	# header line, because that is less widely supported.)
    354 	#
    355 	# If we get the wrong result here, the user can override it by
    356 	# specifying HOST_SH in the environment.
    357 	#
    358 	[ -z "${HOST_SH}" ] && HOST_SH="$(
    359 		(ps -p $$ -o comm | sed -ne "2s/[ ${tab}]*\$//p") 2>/dev/null )"
    360 
    361 	# If nothing above worked, use "sh".  We will later find the
    362 	# first directory in the PATH that has a "sh" program.
    363 	#
    364 	[ -z "${HOST_SH}" ] && HOST_SH="sh"
    365 
    366 	# If the result so far is not an absolute path, try to prepend
    367 	# PWD or search the PATH.
    368 	#
    369 	case "${HOST_SH}" in
    370 	/*)	:
    371 		;;
    372 	*/*)	HOST_SH="${PWD}/${HOST_SH}"
    373 		;;
    374 	*)	HOST_SH="$(find_in_PATH "${HOST_SH}")"
    375 		;;
    376 	esac
    377 
    378 	# If we don't have an absolute path by now, bomb.
    379 	#
    380 	case "${HOST_SH}" in
    381 	/*)	:
    382 		;;
    383 	*)	bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
    384 		;;
    385 	esac
    386 
    387 	# If HOST_SH is not executable, bomb.
    388 	#
    389 	[ -x "${HOST_SH}" ] ||
    390 	    bomb "HOST_SH=\"${HOST_SH}\" is not executable."
    391 
    392 	# If HOST_SH fails tests, bomb.
    393 	# ("$0" may be a path that is no longer valid, because we have
    394 	# performed "cd $(dirname $0)", so don't use $0 here.)
    395 	#
    396 	"${HOST_SH}" build.sh --shelltest ||
    397 	    bomb "HOST_SH=\"${HOST_SH}\" failed functionality tests."
    398 }
    399 
    400 # initdefaults --
    401 # Set defaults before parsing command line options.
    402 #
    403 initdefaults()
    404 {
    405 	makeenv=
    406 	makewrapper=
    407 	makewrappermachine=
    408 	runcmd=
    409 	operations=
    410 	removedirs=
    411 
    412 	[ -d usr.bin/make ] || cd "$(dirname $0)"
    413 	[ -d usr.bin/make ] ||
    414 	    bomb "build.sh must be run from the top source level"
    415 	[ -f share/mk/bsd.own.mk ] ||
    416 	    bomb "src/share/mk is missing; please re-fetch the source tree"
    417 
    418 	# Set various environment variables to known defaults,
    419 	# to minimize (cross-)build problems observed "in the field".
    420 	#
    421 	# LC_ALL=C must be set before we try to parse the output from
    422 	# any command.  Other variables are set (or unset) here, before
    423 	# we parse command line arguments.
    424 	#
    425 	# These variables can be overridden via "-V var=value" if
    426 	# you know what you are doing.
    427 	#
    428 	unsetmakeenv INFODIR
    429 	unsetmakeenv LESSCHARSET
    430 	unsetmakeenv MAKEFLAGS
    431 	setmakeenv LC_ALL C
    432 
    433 	# Find information about the build platform.  This should be
    434 	# kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
    435 	# variables in share/mk/bsd.sys.mk.
    436 	#
    437 	# Note that "uname -p" is not part of POSIX, but we want uname_p
    438 	# to be set to the host MACHINE_ARCH, if possible.  On systems
    439 	# where "uname -p" fails, prints "unknown", or prints a string
    440 	# that does not look like an identifier, fall back to using the
    441 	# output from "uname -m" instead.
    442 	#
    443 	uname_s=$(uname -s 2>/dev/null)
    444 	uname_r=$(uname -r 2>/dev/null)
    445 	uname_m=$(uname -m 2>/dev/null)
    446 	uname_p=$(uname -p 2>/dev/null || echo "unknown")
    447 	case "${uname_p}" in
    448 	''|unknown|*[^-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
    449 	esac
    450 
    451 	id_u=$(id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null)
    452 
    453 	# If $PWD is a valid name of the current directory, POSIX mandates
    454 	# that pwd return it by default which causes problems in the
    455 	# presence of symlinks.  Unsetting PWD is simpler than changing
    456 	# every occurrence of pwd to use -P.
    457 	#
    458 	# XXX Except that doesn't work on Solaris. Or many Linuces.
    459 	#
    460 	unset PWD
    461 	TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
    462 
    463 	# The user can set HOST_SH in the environment, or we try to
    464 	# guess an appropriate value.  Then we set several other
    465 	# variables from HOST_SH.
    466 	#
    467 	set_HOST_SH
    468 	setmakeenv HOST_SH "${HOST_SH}"
    469 	setmakeenv BSHELL "${HOST_SH}"
    470 	setmakeenv CONFIG_SHELL "${HOST_SH}"
    471 
    472 	# Set defaults.
    473 	#
    474 	toolprefix=nb
    475 
    476 	# Some systems have a small ARG_MAX.  -X prevents make(1) from
    477 	# exporting variables in the environment redundantly.
    478 	#
    479 	case "${uname_s}" in
    480 	Darwin | FreeBSD | CYGWIN*)
    481 		MAKEFLAGS="-X ${MAKEFLAGS}"
    482 		;;
    483 	esac
    484 
    485 	# do_{operation}=true if given operation is requested.
    486 	#
    487 	do_expertmode=false
    488 	do_rebuildmake=false
    489 	do_removedirs=false
    490 	do_tools=false
    491 	do_cleandir=false
    492 	do_obj=false
    493 	do_build=false
    494 	do_distribution=false
    495 	do_release=false
    496 	do_kernel=false
    497 	do_releasekernel=false
    498 	do_modules=false
    499 	do_installmodules=false
    500 	do_install=false
    501 	do_sets=false
    502 	do_sourcesets=false
    503 	do_syspkgs=false
    504 	do_iso_image=false
    505 	do_iso_image_source=false
    506 	do_live_image=false
    507 	do_install_image=false
    508 	do_params=false
    509 	do_rump=false
    510 
    511 	# done_{operation}=true if given operation has been done.
    512 	#
    513 	done_rebuildmake=false
    514 
    515 	# Create scratch directory
    516 	#
    517 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    518 	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
    519 	trap "cd /; rm -r -f \"${tmpdir}\"" 0
    520 	results="${tmpdir}/build.sh.results"
    521 
    522 	# Set source directories
    523 	#
    524 	setmakeenv NETBSDSRCDIR "${TOP}"
    525 
    526 	# Make sure KERNOBJDIR is an absolute path if defined
    527 	#
    528 	case "${KERNOBJDIR}" in
    529 	''|/*)	;;
    530 	*)	KERNOBJDIR="${TOP}/${KERNOBJDIR}"
    531 		setmakeenv KERNOBJDIR "${KERNOBJDIR}"
    532 		;;
    533 	esac
    534 
    535 	# Find the version of NetBSD
    536 	#
    537 	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
    538 
    539 	# Set the BUILDSEED to NetBSD-"N"
    540 	#
    541 	setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
    542 
    543 	# Set MKARZERO to "yes"
    544 	#
    545 	setmakeenv MKARZERO "yes"
    546 
    547 }
    548 
    549 getarch()
    550 {
    551 	# Translate some MACHINE name aliases (known only to build.sh)
    552 	# into proper MACHINE and MACHINE_ARCH names.  Save the alias
    553 	# name in makewrappermachine.
    554 	#
    555 	case "${MACHINE}" in
    556 
    557 	evbearm-e[bl])
    558 		makewrappermachine=${MACHINE}
    559 		# MACHINE_ARCH is "arm" or "armeb", not "armel"
    560 		MACHINE_ARCH=earm${MACHINE##*-}
    561 		MACHINE_ARCH=${MACHINE_ARCH%el}
    562 		MACHINE=evbarm
    563 		;;
    564 
    565 	evbarm-e[bl])
    566 		makewrappermachine=${MACHINE}
    567 		# MACHINE_ARCH is "arm" or "armeb", not "armel"
    568 		MACHINE_ARCH=arm${MACHINE##*-}
    569 		MACHINE_ARCH=${MACHINE_ARCH%el}
    570 		MACHINE=${MACHINE%-e[bl]}
    571 		;;
    572 
    573 	evbmips-e[bl]|sbmips-e[bl])
    574 		makewrappermachine=${MACHINE}
    575 		MACHINE_ARCH=mips${MACHINE##*-}
    576 		MACHINE=${MACHINE%-e[bl]}
    577 		;;
    578 
    579 	evbmips64-e[bl]|sbmips64-e[bl])
    580 		makewrappermachine=${MACHINE}
    581 		MACHINE_ARCH=mips64${MACHINE##*-}
    582 		MACHINE=${MACHINE%64-e[bl]}
    583 		;;
    584 
    585 	evbsh3-e[bl])
    586 		makewrappermachine=${MACHINE}
    587 		MACHINE_ARCH=sh3${MACHINE##*-}
    588 		MACHINE=${MACHINE%-e[bl]}
    589 		;;
    590 
    591 	esac
    592 
    593 	# Translate a MACHINE into a default MACHINE_ARCH.
    594 	#
    595 	case "${MACHINE}" in
    596 
    597 	acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
    598 		MACHINE_ARCH=arm
    599 		;;
    600 
    601 	evbarm)		# unspecified MACHINE_ARCH gets LE
    602 		MACHINE_ARCH=${MACHINE_ARCH:=arm}
    603 		;;
    604 
    605 	hp700)
    606 		MACHINE_ARCH=hppa
    607 		;;
    608 
    609 	sun2)
    610 		MACHINE_ARCH=m68000
    611 		;;
    612 
    613 	amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
    614 		MACHINE_ARCH=m68k
    615 		;;
    616 
    617 	evbmips|sbmips)		# no default MACHINE_ARCH
    618 		;;
    619 
    620 	sgimips64)
    621 		makewrappermachine=${MACHINE}
    622 		MACHINE=${MACHINE%64}
    623 		MACHINE_ARCH=mips64eb
    624 		;;
    625 
    626 	ews4800mips|mipsco|newsmips|sgimips|emips)
    627 		MACHINE_ARCH=mipseb
    628 		;;
    629 
    630 	algor64|arc64|cobalt64|pmax64)
    631 		makewrappermachine=${MACHINE}
    632 		MACHINE=${MACHINE%64}
    633 		MACHINE_ARCH=mips64el
    634 		;;
    635 
    636 	algor|arc|cobalt|hpcmips|pmax)
    637 		MACHINE_ARCH=mipsel
    638 		;;
    639 
    640 	evbppc64|macppc64|ofppc64)
    641 		makewrappermachine=${MACHINE}
    642 		MACHINE=${MACHINE%64}
    643 		MACHINE_ARCH=powerpc64
    644 		;;
    645 
    646 	amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
    647 		MACHINE_ARCH=powerpc
    648 		;;
    649 
    650 	evbsh3)			# no default MACHINE_ARCH
    651 		;;
    652 
    653 	mmeye)
    654 		MACHINE_ARCH=sh3eb
    655 		;;
    656 
    657 	dreamcast|hpcsh|landisk)
    658 		MACHINE_ARCH=sh3el
    659 		;;
    660 
    661 	amd64)
    662 		MACHINE_ARCH=x86_64
    663 		;;
    664 
    665 	alpha|i386|sparc|sparc64|vax|ia64)
    666 		MACHINE_ARCH=${MACHINE}
    667 		;;
    668 
    669 	*)
    670 		bomb "Unknown target MACHINE: ${MACHINE}"
    671 		;;
    672 
    673 	esac
    674 }
    675 
    676 validatearch()
    677 {
    678 	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
    679 	#
    680 	case "${MACHINE_ARCH}" in
    681 
    682 	alpha|arm|armeb|earm|earmeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
    683 		;;
    684 
    685 	"")
    686 		bomb "No MACHINE_ARCH provided"
    687 		;;
    688 
    689 	*)
    690 		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
    691 		;;
    692 
    693 	esac
    694 
    695 	# Determine valid MACHINE_ARCHs for MACHINE
    696 	#
    697 	case "${MACHINE}" in
    698 
    699 	evbarm)
    700 		arches="arm armeb earm earmeb"
    701 		;;
    702 
    703 	cats|iyonix|netwinder|shark|zaurus)
    704 		arches="arm earm"
    705 		;;
    706 
    707 	algor|arc|cobalt|pmax)
    708 		arches="mipsel mips64el"
    709 		;;
    710 
    711 	evbmips|sbmips)
    712 		arches="mipseb mipsel mips64eb mips64el"
    713 		;;
    714 
    715 	sgimips)
    716 		arches="mipseb mips64eb"
    717 		;;
    718 
    719 	evbsh3)
    720 		arches="sh3eb sh3el"
    721 		;;
    722 
    723 	macppc|evbppc|ofppc)
    724 		arches="powerpc powerpc64"
    725 		;;
    726 	*)
    727 		oma="${MACHINE_ARCH}"
    728 		getarch
    729 		arches="${MACHINE_ARCH}"
    730 		MACHINE_ARCH="${oma}"
    731 		;;
    732 
    733 	esac
    734 
    735 	# Ensure that MACHINE_ARCH supports MACHINE
    736 	#
    737 	archok=false
    738 	for a in ${arches}; do
    739 		if [ "${a}" = "${MACHINE_ARCH}" ]; then
    740 			archok=true
    741 			break
    742 		fi
    743 	done
    744 	${archok} ||
    745 	    bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
    746 }
    747 
    748 # nobomb_getmakevar --
    749 # Given the name of a make variable in $1, print make's idea of the
    750 # value of that variable, or return 1 if there's an error.
    751 #
    752 nobomb_getmakevar()
    753 {
    754 	[ -x "${make}" ] || return 1
    755 	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
    756 _x_:
    757 	echo \${$1}
    758 .include <bsd.prog.mk>
    759 .include <bsd.kernobj.mk>
    760 EOF
    761 }
    762 
    763 # bomb_getmakevar --
    764 # Given the name of a make variable in $1, print make's idea of the
    765 # value of that variable, or bomb if there's an error.
    766 #
    767 bomb_getmakevar()
    768 {
    769 	[ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
    770 	nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
    771 }
    772 
    773 # getmakevar --
    774 # Given the name of a make variable in $1, print make's idea of the
    775 # value of that variable, or print a literal '$' followed by the
    776 # variable name if ${make} is not executable.  This is intended for use in
    777 # messages that need to be readable even if $make hasn't been built,
    778 # such as when build.sh is run with the "-n" option.
    779 #
    780 getmakevar()
    781 {
    782 	if [ -x "${make}" ]; then
    783 		bomb_getmakevar "$1"
    784 	else
    785 		echo "\$$1"
    786 	fi
    787 }
    788 
    789 setmakeenv()
    790 {
    791 	eval "$1='$2'; export $1"
    792 	makeenv="${makeenv} $1"
    793 }
    794 
    795 unsetmakeenv()
    796 {
    797 	eval "unset $1"
    798 	makeenv="${makeenv} $1"
    799 }
    800 
    801 # Given a variable name in $1, modify the variable in place as follows:
    802 # For each space-separated word in the variable, call resolvepath.
    803 resolvepaths()
    804 {
    805 	local var="$1"
    806 	local val
    807 	eval val=\"\${${var}}\"
    808 	local newval=''
    809 	local word
    810 	for word in ${val}; do
    811 		resolvepath word
    812 		newval="${newval}${newval:+ }${word}"
    813 	done
    814 	eval ${var}=\"\${newval}\"
    815 }
    816 
    817 # Given a variable name in $1, modify the variable in place as follows:
    818 # Convert possibly-relative path to absolute path by prepending
    819 # ${TOP} if necessary.  Also delete trailing "/", if any.
    820 resolvepath()
    821 {
    822 	local var="$1"
    823 	local val
    824 	eval val=\"\${${var}}\"
    825 	case "${val}" in
    826 	/)
    827 		;;
    828 	/*)
    829 		val="${val%/}"
    830 		;;
    831 	*)
    832 		val="${TOP}/${val%/}"
    833 		;;
    834 	esac
    835 	eval ${var}=\"\${val}\"
    836 }
    837 
    838 usage()
    839 {
    840 	if [ -n "$*" ]; then
    841 		echo ""
    842 		echo "${progname}: $*"
    843 	fi
    844 	cat <<_usage_
    845 
    846 Usage: ${progname} [-EhnorUuxy] [-a arch] [-B buildid] [-C cdextras]
    847                 [-D dest] [-j njob] [-M obj] [-m mach] [-N noisy]
    848                 [-O obj] [-R release] [-S seed] [-T tools]
    849                 [-V var=[value]] [-w wrapper] [-X x11src] [-Y extsrcsrc]
    850                 [-Z var]
    851                 operation [...]
    852 
    853  Build operations (all imply "obj" and "tools"):
    854     build               Run "make build".
    855     distribution        Run "make distribution" (includes DESTDIR/etc/ files).
    856     release             Run "make release" (includes kernels & distrib media).
    857 
    858  Other operations:
    859     help                Show this message and exit.
    860     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
    861                         Always performed.
    862     cleandir            Run "make cleandir".  [Default unless -u is used]
    863     obj                 Run "make obj".  [Default unless -o is used]
    864     tools               Build and install tools.
    865     install=idir        Run "make installworld" to \`idir' to install all sets
    866                         except \`etc'.  Useful after "distribution" or "release"
    867     kernel=conf         Build kernel with config file \`conf'
    868     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
    869     installmodules=idir Run "make installmodules" to \`idir' to install all
    870                         kernel modules.
    871     modules             Build kernel modules.
    872     rumptest            Do a linktest for rump (for developers).
    873     sets                Create binary sets in
    874                         RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
    875                         DESTDIR should be populated beforehand.
    876     sourcesets          Create source sets in RELEASEDIR/source/sets.
    877     syspkgs             Create syspkgs in
    878                         RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
    879     iso-image           Create CD-ROM image in RELEASEDIR/iso.
    880     iso-image-source    Create CD-ROM image with source in RELEASEDIR/iso.
    881     live-image          Create bootable live image in
    882                         RELEASEDIR/RELEASEMACHINEDIR/installation/liveimage.
    883     install-image       Create bootable installation image in
    884                         RELEASEDIR/RELEASEMACHINEDIR/installation/installimage.
    885     params              Display various make(1) parameters.
    886 
    887  Options:
    888     -a arch        Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
    889     -B buildid     Set BUILDID to buildid.
    890     -C cdextras    Append cdextras to CDEXTRA variable for inclusion on CD-ROM.
    891     -D dest        Set DESTDIR to dest.  [Default: destdir.MACHINE]
    892     -E             Set "expert" mode; disables various safety checks.
    893                    Should not be used without expert knowledge of the build system.
    894     -h             Print this help message.
    895     -j njob        Run up to njob jobs in parallel; see make(1) -j.
    896     -M obj         Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
    897                    Unsets MAKEOBJDIR.
    898     -m mach        Set MACHINE to mach; not required if NetBSD native.
    899     -N noisy       Set the noisyness (MAKEVERBOSE) level of the build:
    900                        0   Minimal output ("quiet")
    901                        1   Describe what is occurring
    902                        2   Describe what is occurring and echo the actual command
    903                        3   Ignore the effect of the "@" prefix in make commands
    904                        4   Trace shell commands using the shell's -x flag
    905                    [Default: 2]
    906     -n             Show commands that would be executed, but do not execute them.
    907     -O obj         Set obj root directory to obj; sets a MAKEOBJDIR pattern.
    908                    Unsets MAKEOBJDIRPREFIX.
    909     -o             Set MKOBJDIRS=no; do not create objdirs at start of build.
    910     -R release     Set RELEASEDIR to release.  [Default: releasedir]
    911     -r             Remove contents of TOOLDIR and DESTDIR before building.
    912     -S seed        Set BUILDSEED to seed.  [Default: NetBSD-majorversion]
    913     -T tools       Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
    914                    the environment, ${toolprefix}make will be (re)built
    915                    unconditionally.
    916     -U             Set MKUNPRIVED=yes; build without requiring root privileges,
    917                    install from an UNPRIVED build with proper file permissions.
    918     -u             Set MKUPDATE=yes; do not run "make cleandir" first.
    919                    Without this, everything is rebuilt, including the tools.
    920     -V var=[value] Set variable \`var' to \`value'.
    921     -w wrapper     Create ${toolprefix}make script as wrapper.
    922                    [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
    923     -X x11src      Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
    924     -x             Set MKX11=yes; build X11 from X11SRCDIR
    925     -Y extsrcsrc   Set EXTSRCSRCDIR to extsrcsrc.  [Default: /usr/extsrc]
    926     -y             Set MKEXTSRC=yes; build extsrc from EXTSRCSRCDIR
    927     -Z var         Unset ("zap") variable \`var'.
    928 
    929 _usage_
    930 	exit 1
    931 }
    932 
    933 parseoptions()
    934 {
    935 	opts='a:B:C:D:Ehj:M:m:N:nO:oR:rS:T:UuV:w:X:xY:yZ:'
    936 	opt_a=no
    937 
    938 	if type getopts >/dev/null 2>&1; then
    939 		# Use POSIX getopts.
    940 		#
    941 		getoptcmd='getopts ${opts} opt && opt=-${opt}'
    942 		optargcmd=':'
    943 		optremcmd='shift $((${OPTIND} -1))'
    944 	else
    945 		type getopt >/dev/null 2>&1 ||
    946 		    bomb "Shell does not support getopts or getopt"
    947 
    948 		# Use old-style getopt(1) (doesn't handle whitespace in args).
    949 		#
    950 		args="$(getopt ${opts} $*)"
    951 		[ $? = 0 ] || usage
    952 		set -- ${args}
    953 
    954 		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    955 		optargcmd='OPTARG="$1"; shift'
    956 		optremcmd=':'
    957 	fi
    958 
    959 	# Parse command line options.
    960 	#
    961 	while eval ${getoptcmd}; do
    962 		case ${opt} in
    963 
    964 		-a)
    965 			eval ${optargcmd}
    966 			MACHINE_ARCH=${OPTARG}
    967 			opt_a=yes
    968 			;;
    969 
    970 		-B)
    971 			eval ${optargcmd}
    972 			BUILDID=${OPTARG}
    973 			;;
    974 
    975 		-C)
    976 			eval ${optargcmd}; resolvepaths OPTARG
    977 			CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
    978 			;;
    979 
    980 		-D)
    981 			eval ${optargcmd}; resolvepath OPTARG
    982 			setmakeenv DESTDIR "${OPTARG}"
    983 			;;
    984 
    985 		-E)
    986 			do_expertmode=true
    987 			;;
    988 
    989 		-j)
    990 			eval ${optargcmd}
    991 			parallel="-j ${OPTARG}"
    992 			;;
    993 
    994 		-M)
    995 			eval ${optargcmd}; resolvepath OPTARG
    996 			case "${OPTARG}" in
    997 			\$*)	usage "-M argument must not begin with '\$'"
    998 				;;
    999 			*\$*)	# can use resolvepath, but can't set TOP_objdir
   1000 				resolvepath OPTARG
   1001 				;;
   1002 			*)	resolvepath OPTARG
   1003 				TOP_objdir="${OPTARG}${TOP}"
   1004 				;;
   1005 			esac
   1006 			unsetmakeenv MAKEOBJDIR
   1007 			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
   1008 			;;
   1009 
   1010 			# -m overrides MACHINE_ARCH unless "-a" is specified
   1011 		-m)
   1012 			eval ${optargcmd}
   1013 			MACHINE="${OPTARG}"
   1014 			[ "${opt_a}" != "yes" ] && getarch
   1015 			;;
   1016 
   1017 		-N)
   1018 			eval ${optargcmd}
   1019 			case "${OPTARG}" in
   1020 			0|1|2|3|4)
   1021 				setmakeenv MAKEVERBOSE "${OPTARG}"
   1022 				;;
   1023 			*)
   1024 				usage "'${OPTARG}' is not a valid value for -N"
   1025 				;;
   1026 			esac
   1027 			;;
   1028 
   1029 		-n)
   1030 			runcmd=echo
   1031 			;;
   1032 
   1033 		-O)
   1034 			eval ${optargcmd}
   1035 			case "${OPTARG}" in
   1036 			*\$*)	usage "-O argument must not contain '\$'"
   1037 				;;
   1038 			*)	resolvepath OPTARG
   1039 				TOP_objdir="${OPTARG}"
   1040 				;;
   1041 			esac
   1042 			unsetmakeenv MAKEOBJDIRPREFIX
   1043 			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
   1044 			;;
   1045 
   1046 		-o)
   1047 			MKOBJDIRS=no
   1048 			;;
   1049 
   1050 		-R)
   1051 			eval ${optargcmd}; resolvepath OPTARG
   1052 			setmakeenv RELEASEDIR "${OPTARG}"
   1053 			;;
   1054 
   1055 		-r)
   1056 			do_removedirs=true
   1057 			do_rebuildmake=true
   1058 			;;
   1059 
   1060 		-S)
   1061 			eval ${optargcmd}
   1062 			setmakeenv BUILDSEED "${OPTARG}"
   1063 			;;
   1064 
   1065 		-T)
   1066 			eval ${optargcmd}; resolvepath OPTARG
   1067 			TOOLDIR="${OPTARG}"
   1068 			export TOOLDIR
   1069 			;;
   1070 
   1071 		-U)
   1072 			setmakeenv MKUNPRIVED yes
   1073 			;;
   1074 
   1075 		-u)
   1076 			setmakeenv MKUPDATE yes
   1077 			;;
   1078 
   1079 		-V)
   1080 			eval ${optargcmd}
   1081 			case "${OPTARG}" in
   1082 		    # XXX: consider restricting which variables can be changed?
   1083 			[a-zA-Z_][a-zA-Z_0-9]*=*)
   1084 				setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
   1085 				;;
   1086 			*)
   1087 				usage "-V argument must be of the form 'var=[value]'"
   1088 				;;
   1089 			esac
   1090 			;;
   1091 
   1092 		-w)
   1093 			eval ${optargcmd}; resolvepath OPTARG
   1094 			makewrapper="${OPTARG}"
   1095 			;;
   1096 
   1097 		-X)
   1098 			eval ${optargcmd}; resolvepath OPTARG
   1099 			setmakeenv X11SRCDIR "${OPTARG}"
   1100 			;;
   1101 
   1102 		-x)
   1103 			setmakeenv MKX11 yes
   1104 			;;
   1105 
   1106 		-Y)
   1107 			eval ${optargcmd}; resolvepath OPTARG
   1108 			setmakeenv EXTSRCSRCDIR "${OPTARG}"
   1109 			;;
   1110 
   1111 		-y)
   1112 			setmakeenv MKEXTSRC yes
   1113 			;;
   1114 
   1115 		-Z)
   1116 			eval ${optargcmd}
   1117 		    # XXX: consider restricting which variables can be unset?
   1118 			unsetmakeenv "${OPTARG}"
   1119 			;;
   1120 
   1121 		--)
   1122 			break
   1123 			;;
   1124 
   1125 		-'?'|-h)
   1126 			usage
   1127 			;;
   1128 
   1129 		esac
   1130 	done
   1131 
   1132 	# Validate operations.
   1133 	#
   1134 	eval ${optremcmd}
   1135 	while [ $# -gt 0 ]; do
   1136 		op=$1; shift
   1137 		operations="${operations} ${op}"
   1138 
   1139 		case "${op}" in
   1140 
   1141 		help)
   1142 			usage
   1143 			;;
   1144 
   1145 		makewrapper|cleandir|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
   1146 			;;
   1147 
   1148 		iso-image)
   1149 			op=iso_image	# used as part of a variable name
   1150 			;;
   1151 
   1152 		iso-image-source)
   1153 			op=iso_image_source   # used as part of a variable name
   1154 			;;
   1155 
   1156 		live-image)
   1157 			op=live_image	# used as part of a variable name
   1158 			;;
   1159 
   1160 		install-image)
   1161 			op=install_image # used as part of a variable name
   1162 			;;
   1163 
   1164 		kernel=*|releasekernel=*)
   1165 			arg=${op#*=}
   1166 			op=${op%%=*}
   1167 			[ -n "${arg}" ] ||
   1168 			    bomb "Must supply a kernel name with \`${op}=...'"
   1169 			;;
   1170 
   1171 		modules)
   1172 			op=modules
   1173 			;;
   1174 
   1175 		install=*|installmodules=*)
   1176 			arg=${op#*=}
   1177 			op=${op%%=*}
   1178 			[ -n "${arg}" ] ||
   1179 			    bomb "Must supply a directory with \`install=...'"
   1180 			;;
   1181 
   1182 		rump|rumptest)
   1183 			op=${op}
   1184 			;;
   1185 
   1186 		*)
   1187 			usage "Unknown operation \`${op}'"
   1188 			;;
   1189 
   1190 		esac
   1191 		eval do_${op}=true
   1192 	done
   1193 	[ -n "${operations}" ] || usage "Missing operation to perform."
   1194 
   1195 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
   1196 	#
   1197 	if [ -z "${MACHINE}" ]; then
   1198 		[ "${uname_s}" = "NetBSD" ] ||
   1199 		    bomb "MACHINE must be set, or -m must be used, for cross builds."
   1200 		MACHINE=${uname_m}
   1201 	fi
   1202 	[ -n "${MACHINE_ARCH}" ] || getarch
   1203 	validatearch
   1204 
   1205 	# Set up default make(1) environment.
   1206 	#
   1207 	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
   1208 	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
   1209 	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS}"
   1210 	MAKEFLAGS="${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
   1211 	export MAKEFLAGS MACHINE MACHINE_ARCH
   1212 }
   1213 
   1214 # sanitycheck --
   1215 # Sanity check after parsing command line options, before rebuildmake.
   1216 #
   1217 sanitycheck()
   1218 {
   1219 	# If the PATH contains any non-absolute components (including,
   1220 	# but not limited to, "." or ""), then complain.  As an exception,
   1221 	# allow "" or "." as the last component of the PATH.  This is fatal
   1222 	# if expert mode is not in effect.
   1223 	#
   1224 	local path="${PATH}"
   1225 	path="${path%:}"	# delete trailing ":"
   1226 	path="${path%:.}"	# delete trailing ":."
   1227 	case ":${path}:/" in
   1228 	*:[!/]*)
   1229 		if ${do_expertmode}; then
   1230 			warning "PATH contains non-absolute components"
   1231 		else
   1232 			bomb "PATH environment variable must not" \
   1233 			     "contain non-absolute components"
   1234 		fi
   1235 		;;
   1236 	esac
   1237 }
   1238 
   1239 # print_tooldir_make --
   1240 # Try to find and print a path to an existing
   1241 # ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
   1242 # new version of ${toolprefix}make has been built.
   1243 #
   1244 # * If TOOLDIR was set in the environment or on the command line, use
   1245 #   that value.
   1246 # * Otherwise try to guess what TOOLDIR would be if not overridden by
   1247 #   /etc/mk.conf, and check whether the resulting directory contains
   1248 #   a copy of ${toolprefix}make (this should work for everybody who
   1249 #   doesn't override TOOLDIR via /etc/mk.conf);
   1250 # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
   1251 #   in the PATH (this might accidentally find a version of make that
   1252 #   does not understand the syntax used by NetBSD make, and that will
   1253 #   lead to failure in the next step);
   1254 # * If a copy of make was found above, try to use it with
   1255 #   nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
   1256 #   result only if it's a directory that already exists;
   1257 # * If a value of TOOLDIR was found above, and if
   1258 #   ${TOOLDIR}/bin/${toolprefix}make exists, print that value.
   1259 #
   1260 print_tooldir_make()
   1261 {
   1262 	local possible_TOP_OBJ
   1263 	local possible_TOOLDIR
   1264 	local possible_make
   1265 	local tooldir_make
   1266 
   1267 	if [ -n "${TOOLDIR}" ]; then
   1268 		echo "${TOOLDIR}/bin/${toolprefix}make"
   1269 		return 0
   1270 	fi
   1271 
   1272 	# Set host_ostype to something like "NetBSD-4.5.6-i386".  This
   1273 	# is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
   1274 	#
   1275 	local host_ostype="${uname_s}-$(
   1276 		echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
   1277 		)-$(
   1278 		echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
   1279 		)"
   1280 
   1281 	# Look in a few potential locations for
   1282 	# ${possible_TOOLDIR}/bin/${toolprefix}make.
   1283 	# If we find it, then set possible_make.
   1284 	#
   1285 	# In the usual case (without interference from environment
   1286 	# variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
   1287 	# "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
   1288 	#
   1289 	# In practice it's difficult to figure out the correct value
   1290 	# for _SRC_TOP_OBJ_.  In the easiest case, when the -M or -O
   1291 	# options were passed to build.sh, then ${TOP_objdir} will be
   1292 	# the correct value.  We also try a few other possibilities, but
   1293 	# we do not replicate all the logic of <bsd.obj.mk>.
   1294 	#
   1295 	for possible_TOP_OBJ in \
   1296 		"${TOP_objdir}" \
   1297 		"${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
   1298 		"${TOP}" \
   1299 		"${TOP}/obj" \
   1300 		"${TOP}/obj.${MACHINE}"
   1301 	do
   1302 		[ -n "${possible_TOP_OBJ}" ] || continue
   1303 		possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
   1304 		possible_make="${possible_TOOLDIR}/bin/${toolprefix}make"
   1305 		if [ -x "${possible_make}" ]; then
   1306 			break
   1307 		else
   1308 			unset possible_make
   1309 		fi
   1310 	done
   1311 
   1312 	# If the above didn't work, search the PATH for a suitable
   1313 	# ${toolprefix}make, nbmake, bmake, or make.
   1314 	#
   1315 	: ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
   1316 	: ${possible_make:=$(find_in_PATH nbmake '')}
   1317 	: ${possible_make:=$(find_in_PATH bmake '')}
   1318 	: ${possible_make:=$(find_in_PATH make '')}
   1319 
   1320 	# At this point, we don't care whether possible_make is in the
   1321 	# correct TOOLDIR or not; we simply want it to be usable by
   1322 	# getmakevar to help us find the correct TOOLDIR.
   1323 	#
   1324 	# Use ${possible_make} with nobomb_getmakevar to try to find
   1325 	# the value of TOOLDIR.  Believe the result only if it's
   1326 	# a directory that already exists and contains bin/${toolprefix}make.
   1327 	#
   1328 	if [ -x "${possible_make}" ]; then
   1329 		possible_TOOLDIR="$(
   1330 			make="${possible_make}" \
   1331 			nobomb_getmakevar TOOLDIR 2>/dev/null
   1332 			)"
   1333 		if [ $? = 0 ] && [ -n "${possible_TOOLDIR}" ] \
   1334 		    && [ -d "${possible_TOOLDIR}" ];
   1335 		then
   1336 			tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
   1337 			if [ -x "${tooldir_make}" ]; then
   1338 				echo "${tooldir_make}"
   1339 				return 0
   1340 			fi
   1341 		fi
   1342 	fi
   1343 	return 1
   1344 }
   1345 
   1346 # rebuildmake --
   1347 # Rebuild nbmake in a temporary directory if necessary.  Sets $make
   1348 # to a path to the nbmake executable.  Sets done_rebuildmake=true
   1349 # if nbmake was rebuilt.
   1350 #
   1351 # There is a cyclic dependency between building nbmake and choosing
   1352 # TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
   1353 # would like to use getmakevar to get the value of TOOLDIR; but we can't
   1354 # use getmakevar before we have an up to date version of nbmake; we
   1355 # might already have an up to date version of nbmake in TOOLDIR, but we
   1356 # don't yet know where TOOLDIR is.
   1357 #
   1358 # The default value of TOOLDIR also depends on the location of the top
   1359 # level object directory, so $(getmakevar TOOLDIR) invoked before or
   1360 # after making the top level object directory may produce different
   1361 # results.
   1362 #
   1363 # Strictly speaking, we should do the following:
   1364 #
   1365 #    1. build a new version of nbmake in a temporary directory;
   1366 #    2. use the temporary nbmake to create the top level obj directory;
   1367 #    3. use $(getmakevar TOOLDIR) with the temporary nbmake to
   1368 #       get the corect value of TOOLDIR;
   1369 #    4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
   1370 #
   1371 # However, people don't like building nbmake unnecessarily if their
   1372 # TOOLDIR has not changed since an earlier build.  We try to avoid
   1373 # rebuilding a temporary version of nbmake by taking some shortcuts to
   1374 # guess a value for TOOLDIR, looking for an existing version of nbmake
   1375 # in that TOOLDIR, and checking whether that nbmake is newer than the
   1376 # sources used to build it.
   1377 #
   1378 rebuildmake()
   1379 {
   1380 	make="$(print_tooldir_make)"
   1381 	if [ -n "${make}" ] && [ -x "${make}" ]; then
   1382 		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
   1383 			if [ "${f}" -nt "${make}" ]; then
   1384 				statusmsg "${make} outdated" \
   1385 					"(older than ${f}), needs building."
   1386 				do_rebuildmake=true
   1387 				break
   1388 			fi
   1389 		done
   1390 	else
   1391 		statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
   1392 		do_rebuildmake=true
   1393 	fi
   1394 
   1395 	# Build bootstrap ${toolprefix}make if needed.
   1396 	if ${do_rebuildmake}; then
   1397 		statusmsg "Bootstrapping ${toolprefix}make"
   1398 		${runcmd} cd "${tmpdir}"
   1399 		${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
   1400 			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
   1401 			${HOST_SH} "${TOP}/tools/make/configure" ||
   1402 		    bomb "Configure of ${toolprefix}make failed"
   1403 		${runcmd} ${HOST_SH} buildmake.sh ||
   1404 		    bomb "Build of ${toolprefix}make failed"
   1405 		make="${tmpdir}/${toolprefix}make"
   1406 		${runcmd} cd "${TOP}"
   1407 		${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
   1408 		done_rebuildmake=true
   1409 	fi
   1410 }
   1411 
   1412 # validatemakeparams --
   1413 # Perform some late sanity checks, after rebuildmake,
   1414 # but before createmakewrapper or any real work.
   1415 #
   1416 # Also create the top-level obj directory.
   1417 #
   1418 validatemakeparams()
   1419 {
   1420 	if [ "${runcmd}" = "echo" ]; then
   1421 		TOOLCHAIN_MISSING=no
   1422 		EXTERNAL_TOOLCHAIN=""
   1423 	else
   1424 		TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
   1425 		EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
   1426 	fi
   1427 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
   1428 	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
   1429 		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
   1430 		${runcmd} echo "	MACHINE:      ${MACHINE}"
   1431 		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
   1432 		${runcmd} echo ""
   1433 		${runcmd} echo "All builds for this platform should be done via a traditional make"
   1434 		${runcmd} echo "If you wish to use an external cross-toolchain, set"
   1435 		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
   1436 		${runcmd} echo "in either the environment or mk.conf and rerun"
   1437 		${runcmd} echo "	${progname} $*"
   1438 		exit 1
   1439 	fi
   1440 
   1441 	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
   1442 	# These may be set as build.sh options or in "mk.conf".
   1443 	# Don't export them as they're only used for tests in build.sh.
   1444 	#
   1445 	MKOBJDIRS=$(getmakevar MKOBJDIRS)
   1446 	MKUNPRIVED=$(getmakevar MKUNPRIVED)
   1447 	MKUPDATE=$(getmakevar MKUPDATE)
   1448 
   1449 	if [ "${MKOBJDIRS}" != "no" ]; then
   1450 		# Create the top-level object directory.
   1451 		#
   1452 		# "make obj NOSUBDIR=" can handle most cases, but it
   1453 		# can't handle the case where MAKEOBJDIRPREFIX is set
   1454 		# while the corresponding directory does not exist
   1455 		# (rules in <bsd.obj.mk> would abort the build).  We
   1456 		# therefore have to handle the MAKEOBJDIRPREFIX case
   1457 		# without invoking "make obj".  The MAKEOBJDIR case
   1458 		# could be handled either way, but we choose to handle
   1459 		# it similarly to MAKEOBJDIRPREFIX.
   1460 		#
   1461 		if [ -n "${TOP_obj}" ]; then
   1462 			# It must have been set by the "-M" or "-O"
   1463 			# command line options, so there's no need to
   1464 			# use getmakevar
   1465 			:
   1466 		elif [ -n "$MAKEOBJDIRPREFIX" ]; then
   1467 			TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
   1468 		elif [ -n "$MAKEOBJDIR" ]; then
   1469 			TOP_obj="$(getmakevar MAKEOBJDIR)"
   1470 		fi
   1471 		if [ -n "$TOP_obj" ]; then
   1472 			${runcmd} mkdir -p "${TOP_obj}" ||
   1473 			    bomb "Can't create top level object directory" \
   1474 					"${TOP_obj}"
   1475 		else
   1476 			${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
   1477 			    bomb "Can't create top level object directory" \
   1478 					"using make obj"
   1479 		fi
   1480 
   1481 		# make obj in tools to ensure that the objdir for "tools"
   1482 		# is available.
   1483 		#
   1484 		${runcmd} cd tools
   1485 		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
   1486 		    bomb "Failed to make obj in tools"
   1487 		${runcmd} cd "${TOP}"
   1488 	fi
   1489 
   1490 	# Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
   1491 	# and bomb if they have changed from the values we had from the
   1492 	# command line or environment.
   1493 	#
   1494 	# This must be done after creating the top-level object directory.
   1495 	#
   1496 	for var in TOOLDIR DESTDIR RELEASEDIR
   1497 	do
   1498 		eval oldval=\"\$${var}\"
   1499 		newval="$(getmakevar $var)"
   1500 		if ! $do_expertmode; then
   1501 			: ${_SRC_TOP_OBJ_:=$(getmakevar _SRC_TOP_OBJ_)}
   1502 			case "$var" in
   1503 			DESTDIR)
   1504 				: ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
   1505 				makeenv="${makeenv} DESTDIR"
   1506 				;;
   1507 			RELEASEDIR)
   1508 				: ${newval:=${_SRC_TOP_OBJ_}/releasedir}
   1509 				makeenv="${makeenv} RELEASEDIR"
   1510 				;;
   1511 			esac
   1512 		fi
   1513 		if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]; then
   1514 			bomb "Value of ${var} has changed" \
   1515 				"(was \"${oldval}\", now \"${newval}\")"
   1516 		fi
   1517 		eval ${var}=\"\${newval}\"
   1518 		eval export ${var}
   1519 		statusmsg2 "${var} path:" "${newval}"
   1520 	done
   1521 
   1522 	# RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
   1523 	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
   1524 
   1525 	# Check validity of TOOLDIR and DESTDIR.
   1526 	#
   1527 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
   1528 		bomb "TOOLDIR '${TOOLDIR}' invalid"
   1529 	fi
   1530 	removedirs="${TOOLDIR}"
   1531 
   1532 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
   1533 		if ${do_build} || ${do_distribution} || ${do_release}; then
   1534 			if ! ${do_build} || \
   1535 			   [ "${uname_s}" != "NetBSD" ] || \
   1536 			   [ "${uname_m}" != "${MACHINE}" ]; then
   1537 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
   1538 			fi
   1539 			if ! ${do_expertmode}; then
   1540 				bomb "DESTDIR must != / for non -E (expert) builds"
   1541 			fi
   1542 			statusmsg "WARNING: Building to /, in expert mode."
   1543 			statusmsg "         This may cause your system to break!  Reasons include:"
   1544 			statusmsg "            - your kernel is not up to date"
   1545 			statusmsg "            - the libraries or toolchain have changed"
   1546 			statusmsg "         YOU HAVE BEEN WARNED!"
   1547 		fi
   1548 	else
   1549 		removedirs="${removedirs} ${DESTDIR}"
   1550 	fi
   1551 	if ${do_build} || ${do_distribution} || ${do_release}; then
   1552 		if ! ${do_expertmode} && \
   1553 		    [ "$id_u" -ne 0 ] && \
   1554 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1555 			bomb "-U or -E must be set for build as an unprivileged user."
   1556 		fi
   1557 	fi
   1558 	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
   1559 		bomb "Must set RELEASEDIR with \`releasekernel=...'"
   1560 	fi
   1561 
   1562 	# Install as non-root is a bad idea.
   1563 	#
   1564 	if ${do_install} && [ "$id_u" -ne 0 ] ; then
   1565 		if ${do_expertmode}; then
   1566 			warning "Will install as an unprivileged user."
   1567 		else
   1568 			bomb "-E must be set for install as an unprivileged user."
   1569 		fi
   1570 	fi
   1571 
   1572 	# If a previous build.sh run used -U (and therefore created a
   1573 	# METALOG file), then most subsequent build.sh runs must also
   1574 	# use -U.  If DESTDIR is about to be removed, then don't perform
   1575 	# this check.
   1576 	#
   1577 	case "${do_removedirs} ${removedirs} " in
   1578 	true*" ${DESTDIR} "*)
   1579 		# DESTDIR is about to be removed
   1580 		;;
   1581 	*)
   1582 		if ( ${do_build} || ${do_distribution} || ${do_release} || \
   1583 		    ${do_install} ) && \
   1584 		    [ -e "${DESTDIR}/METALOG" ] && \
   1585 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1586 			if $do_expertmode; then
   1587 				warning "A previous build.sh run specified -U."
   1588 			else
   1589 				bomb "A previous build.sh run specified -U; you must specify it again now."
   1590 			fi
   1591 		fi
   1592 		;;
   1593 	esac
   1594 
   1595 	# live-image and install-image targets require binary sets
   1596 	# (actually DESTDIR/etc/mtree/set.* files) built with MKUNPRIVED.
   1597 	# If release operation is specified with live-image or install-image,
   1598 	# the release op should be performed with -U for later image ops.
   1599 	#
   1600 	if ${do_release} && ( ${do_live_image} || ${do_install_image} ) && \
   1601 	    [ "${MKUNPRIVED}" = "no" ] ; then
   1602 		bomb "-U must be specified on building release to create images later."
   1603 	fi
   1604 }
   1605 
   1606 
   1607 createmakewrapper()
   1608 {
   1609 	# Remove the target directories.
   1610 	#
   1611 	if ${do_removedirs}; then
   1612 		for f in ${removedirs}; do
   1613 			statusmsg "Removing ${f}"
   1614 			${runcmd} rm -r -f "${f}"
   1615 		done
   1616 	fi
   1617 
   1618 	# Recreate $TOOLDIR.
   1619 	#
   1620 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
   1621 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
   1622 
   1623 	# If we did not previously rebuild ${toolprefix}make, then
   1624 	# check whether $make is still valid and the same as the output
   1625 	# from print_tooldir_make.  If not, then rebuild make now.  A
   1626 	# possible reason for this being necessary is that the actual
   1627 	# value of TOOLDIR might be different from the value guessed
   1628 	# before the top level obj dir was created.
   1629 	#
   1630 	if ! ${done_rebuildmake} && \
   1631 	    ( [ ! -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] )
   1632 	then
   1633 		rebuildmake
   1634 	fi
   1635 
   1636 	# Install ${toolprefix}make if it was built.
   1637 	#
   1638 	if ${done_rebuildmake}; then
   1639 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
   1640 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
   1641 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
   1642 		make="${TOOLDIR}/bin/${toolprefix}make"
   1643 		statusmsg "Created ${make}"
   1644 	fi
   1645 
   1646 	# Build a ${toolprefix}make wrapper script, usable by hand as
   1647 	# well as by build.sh.
   1648 	#
   1649 	if [ -z "${makewrapper}" ]; then
   1650 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
   1651 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
   1652 	fi
   1653 
   1654 	${runcmd} rm -f "${makewrapper}"
   1655 	if [ "${runcmd}" = "echo" ]; then
   1656 		echo 'cat <<EOF >'${makewrapper}
   1657 		makewrapout=
   1658 	else
   1659 		makewrapout=">>\${makewrapper}"
   1660 	fi
   1661 
   1662 	case "${KSH_VERSION:-${SH_VERSION}}" in
   1663 	*PD\ KSH*|*MIRBSD\ KSH*)
   1664 		set +o braceexpand
   1665 		;;
   1666 	esac
   1667 
   1668 	eval cat <<EOF ${makewrapout}
   1669 #! ${HOST_SH}
   1670 # Set proper variables to allow easy "make" building of a NetBSD subtree.
   1671 # Generated from:  \$NetBSD: build.sh,v 1.255 2012/08/05 04:39:09 matt Exp $
   1672 # with these arguments: ${_args}
   1673 #
   1674 
   1675 EOF
   1676 	{
   1677 		for f in ${makeenv}; do
   1678 			if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
   1679 				eval echo "unset ${f}"
   1680 			else
   1681 				eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
   1682 			fi
   1683 		done
   1684 
   1685 		eval cat <<EOF
   1686 MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
   1687 USETOOLS=yes; export USETOOLS
   1688 EOF
   1689 	} | eval sort -u "${makewrapout}"
   1690 	eval cat <<EOF "${makewrapout}"
   1691 
   1692 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
   1693 EOF
   1694 	[ "${runcmd}" = "echo" ] && echo EOF
   1695 	${runcmd} chmod +x "${makewrapper}"
   1696 	statusmsg2 "Updated makewrapper:" "${makewrapper}"
   1697 }
   1698 
   1699 make_in_dir()
   1700 {
   1701 	dir="$1"
   1702 	op="$2"
   1703 	${runcmd} cd "${dir}" ||
   1704 	    bomb "Failed to cd to \"${dir}\""
   1705 	${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1706 	    bomb "Failed to make ${op} in \"${dir}\""
   1707 	${runcmd} cd "${TOP}" ||
   1708 	    bomb "Failed to cd back to \"${TOP}\""
   1709 }
   1710 
   1711 buildtools()
   1712 {
   1713 	if [ "${MKOBJDIRS}" != "no" ]; then
   1714 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
   1715 		    bomb "Failed to make obj-tools"
   1716 	fi
   1717 	if [ "${MKUPDATE}" = "no" ]; then
   1718 		make_in_dir tools cleandir
   1719 	fi
   1720 	make_in_dir tools dependall
   1721 	make_in_dir tools install
   1722 	statusmsg "Tools built to ${TOOLDIR}"
   1723 }
   1724 
   1725 getkernelconf()
   1726 {
   1727 	kernelconf="$1"
   1728 	if [ "${MKOBJDIRS}" != "no" ]; then
   1729 		# The correct value of KERNOBJDIR might
   1730 		# depend on a prior "make obj" in
   1731 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
   1732 		#
   1733 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
   1734 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
   1735 		make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
   1736 	fi
   1737 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
   1738 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
   1739 	case "${kernelconf}" in
   1740 	*/*)
   1741 		kernelconfpath="${kernelconf}"
   1742 		kernelconfname="${kernelconf##*/}"
   1743 		;;
   1744 	*)
   1745 		kernelconfpath="${KERNCONFDIR}/${kernelconf}"
   1746 		kernelconfname="${kernelconf}"
   1747 		;;
   1748 	esac
   1749 	kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
   1750 }
   1751 
   1752 buildkernel()
   1753 {
   1754 	if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
   1755 		# Building tools every time we build a kernel is clearly
   1756 		# unnecessary.  We could try to figure out whether rebuilding
   1757 		# the tools is necessary this time, but it doesn't seem worth
   1758 		# the trouble.  Instead, we say it's the user's responsibility
   1759 		# to rebuild the tools if necessary.
   1760 		#
   1761 		statusmsg "Building kernel without building new tools"
   1762 		buildkernelwarned=true
   1763 	fi
   1764 	getkernelconf $1
   1765 	statusmsg2 "Building kernel:" "${kernelconf}"
   1766 	statusmsg2 "Build directory:" "${kernelbuildpath}"
   1767 	${runcmd} mkdir -p "${kernelbuildpath}" ||
   1768 	    bomb "Cannot mkdir: ${kernelbuildpath}"
   1769 	if [ "${MKUPDATE}" = "no" ]; then
   1770 		make_in_dir "${kernelbuildpath}" cleandir
   1771 	fi
   1772 	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
   1773 	|| bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
   1774 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
   1775 		-s "${TOP}/sys" "${kernelconfpath}" ||
   1776 	    bomb "${toolprefix}config failed for ${kernelconf}"
   1777 	make_in_dir "${kernelbuildpath}" depend
   1778 	make_in_dir "${kernelbuildpath}" all
   1779 
   1780 	if [ "${runcmd}" != "echo" ]; then
   1781 		statusmsg "Kernels built from ${kernelconf}:"
   1782 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1783 		for kern in ${kernlist:-netbsd}; do
   1784 			[ -f "${kernelbuildpath}/${kern}" ] && \
   1785 			    echo "  ${kernelbuildpath}/${kern}"
   1786 		done | tee -a "${results}"
   1787 	fi
   1788 }
   1789 
   1790 releasekernel()
   1791 {
   1792 	getkernelconf $1
   1793 	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
   1794 	${runcmd} mkdir -p "${kernelreldir}"
   1795 	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1796 	for kern in ${kernlist:-netbsd}; do
   1797 		builtkern="${kernelbuildpath}/${kern}"
   1798 		[ -f "${builtkern}" ] || continue
   1799 		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
   1800 		statusmsg2 "Kernel copy:" "${releasekern}"
   1801 		if [ "${runcmd}" = "echo" ]; then
   1802 			echo "gzip -c -9 < ${builtkern} > ${releasekern}"
   1803 		else
   1804 			gzip -c -9 < "${builtkern}" > "${releasekern}"
   1805 		fi
   1806 	done
   1807 }
   1808 
   1809 buildmodules()
   1810 {
   1811 	setmakeenv MKBINUTILS no
   1812 	if ! ${do_tools} && ! ${buildmoduleswarned:-false}; then
   1813 		# Building tools every time we build modules is clearly
   1814 		# unnecessary as well as a kernel.
   1815 		#
   1816 		statusmsg "Building modules without building new tools"
   1817 		buildmoduleswarned=true
   1818 	fi
   1819 
   1820 	statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
   1821 	if [ "${MKOBJDIRS}" != "no" ]; then
   1822 		make_in_dir sys/modules obj ||
   1823 		    bomb "Failed to make obj in sys/modules"
   1824 	fi
   1825 	if [ "${MKUPDATE}" = "no" ]; then
   1826 		make_in_dir sys/modules cleandir
   1827 	fi
   1828 	${runcmd} "${makewrapper}" ${parallel} do-sys-modules ||
   1829 	    bomb "Failed to make do-sys-modules"
   1830 
   1831 	statusmsg "Successful build of kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
   1832 }
   1833 
   1834 installmodules()
   1835 {
   1836 	dir="$1"
   1837 	${runcmd} "${makewrapper}" INSTALLMODULESDIR="${dir}" installmodules ||
   1838 	    bomb "Failed to make installmodules to ${dir}"
   1839 	statusmsg "Successful installmodules to ${dir}"
   1840 }
   1841 
   1842 installworld()
   1843 {
   1844 	dir="$1"
   1845 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
   1846 	    bomb "Failed to make installworld to ${dir}"
   1847 	statusmsg "Successful installworld to ${dir}"
   1848 }
   1849 
   1850 # Run rump build&link tests.
   1851 #
   1852 # To make this feasible for running without having to install includes and
   1853 # libraries into destdir (i.e. quick), we only run ld.  This is possible
   1854 # since the rump kernel is a closed namespace apart from calls to rumpuser.
   1855 # Therefore, if ld complains only about rumpuser symbols, rump kernel
   1856 # linking was successful.
   1857 #
   1858 # We test that rump links with a number of component configurations.
   1859 # These attempt to mimic what is encountered in the full build.
   1860 # See list below.  The list should probably be either autogenerated
   1861 # or managed elsewhere; keep it here until a better idea arises.
   1862 #
   1863 # Above all, note that THIS IS NOT A SUBSTITUTE FOR A FULL BUILD.
   1864 #
   1865 
   1866 RUMP_LIBSETS='
   1867 	-lrump,
   1868 	-lrumpvfs -lrump,
   1869 	-lrumpvfs -lrumpdev -lrump,
   1870 	-lrumpnet -lrump,
   1871 	-lrumpkern_tty -lrumpvfs -lrump,
   1872 	-lrumpfs_tmpfs -lrumpvfs -lrump,
   1873 	-lrumpfs_ffs -lrumpfs_msdos -lrumpvfs -lrumpdev_disk -lrumpdev -lrump,
   1874 	-lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet -lrump,
   1875 	-lrumpnet_sockin -lrumpfs_smbfs -lrumpdev_netsmb
   1876 	    -lrumpkern_crypto -lrumpdev -lrumpnet -lrumpvfs -lrump,
   1877 	-lrumpnet_sockin -lrumpfs_nfs -lrumpnet -lrumpvfs -lrump,
   1878 	-lrumpdev_cgd -lrumpdev_raidframe -lrumpdev_disk -lrumpdev_rnd
   1879 	    -lrumpdev_dm -lrumpdev -lrumpvfs -lrumpkern_crypto -lrump'
   1880 dorump()
   1881 {
   1882 	local doclean=""
   1883 	local doobjs=""
   1884 
   1885 	# we cannot link libs without building csu, and that leads to lossage
   1886 	[ "${1}" != "rumptest" ] && bomb 'build.sh rump not yet functional. ' \
   1887 	    'did you mean "rumptest"?'
   1888 
   1889 	# create obj and distrib dirs
   1890 	if [ "${MKOBJDIRS}" != "no" ]; then
   1891 		make_in_dir "${NETBSDSRCDIR}/etc/mtree" obj
   1892 		make_in_dir "${NETBSDSRCDIR}/sys/rump" obj
   1893 	fi
   1894 	${runcmd} "${makewrapper}" ${parallel} do-distrib-dirs \
   1895 	    || bomb 'could not create distrib-dirs'
   1896 
   1897 	[ "${MKUPDATE}" = "no" ] && doclean="cleandir"
   1898 	targlist="${doclean} ${doobjs} dependall install"
   1899 	# optimize: for test we build only static libs (3x test speedup)
   1900 	if [ "${1}" = "rumptest" ] ; then
   1901 		setmakeenv NOPIC 1
   1902 		setmakeenv NOPROFILE 1
   1903 	fi
   1904 	for cmd in ${targlist} ; do
   1905 		make_in_dir "${NETBSDSRCDIR}/sys/rump" ${cmd}
   1906 	done
   1907 
   1908 	# if we just wanted to build & install rump, we're done
   1909 	[ "${1}" != "rumptest" ] && return
   1910 
   1911 	${runcmd} cd "${NETBSDSRCDIR}/sys/rump/librump/rumpkern" \
   1912 	    || bomb "cd to rumpkern failed"
   1913 	md_quirks=`${runcmd} "${makewrapper}" -V '${_SYMQUIRK}'`
   1914 	# one little, two little, three little backslashes ...
   1915 	md_quirks="$(echo ${md_quirks} | sed 's,\\,\\\\,g'";s/'//g" )"
   1916 	${runcmd} cd "${TOP}" || bomb "cd to ${TOP} failed"
   1917 	tool_ld=`${runcmd} "${makewrapper}" -V '${LD}'`
   1918 
   1919 	local oIFS="${IFS}"
   1920 	IFS=","
   1921 	for set in ${RUMP_LIBSETS} ; do
   1922 		IFS="${oIFS}"
   1923 		${runcmd} ${tool_ld} -nostdlib -L${DESTDIR}/usr/lib	\
   1924 		    -static --whole-archive ${set} 2>&1 -o /tmp/rumptest.$$ | \
   1925 		      awk -v quirks="${md_quirks}" '
   1926 			/undefined reference/ &&
   1927 			    !/more undefined references.*follow/{
   1928 				if (match($NF,
   1929 				    "`(rumpuser_|__" quirks ")") == 0)
   1930 					fails[NR] = $0
   1931 			}
   1932 			/cannot find -l/{fails[NR] = $0}
   1933 			/cannot open output file/{fails[NR] = $0}
   1934 			END{
   1935 				for (x in fails)
   1936 					print fails[x]
   1937 				exit x!=0
   1938 			}'
   1939 		[ $? -ne 0 ] && bomb "Testlink of rump failed: ${set}"
   1940 	done
   1941 	statusmsg "Rump build&link tests successful"
   1942 }
   1943 
   1944 main()
   1945 {
   1946 	initdefaults
   1947 	_args=$@
   1948 	parseoptions "$@"
   1949 
   1950 	sanitycheck
   1951 
   1952 	build_start=$(date)
   1953 	statusmsg2 "${progname} command:" "$0 $*"
   1954 	statusmsg2 "${progname} started:" "${build_start}"
   1955 	statusmsg2 "NetBSD version:"   "${DISTRIBVER}"
   1956 	statusmsg2 "MACHINE:"          "${MACHINE}"
   1957 	statusmsg2 "MACHINE_ARCH:"     "${MACHINE_ARCH}"
   1958 	statusmsg2 "Build platform:"   "${uname_s} ${uname_r} ${uname_m}"
   1959 	statusmsg2 "HOST_SH:"          "${HOST_SH}"
   1960 
   1961 	rebuildmake
   1962 	validatemakeparams
   1963 	createmakewrapper
   1964 
   1965 	# Perform the operations.
   1966 	#
   1967 	for op in ${operations}; do
   1968 		case "${op}" in
   1969 
   1970 		makewrapper)
   1971 			# no-op
   1972 			;;
   1973 
   1974 		tools)
   1975 			buildtools
   1976 			;;
   1977 
   1978 		sets)
   1979 			statusmsg "Building sets from pre-populated ${DESTDIR}"
   1980 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1981 			    bomb "Failed to make ${op}"
   1982 			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
   1983 			statusmsg "Built sets to ${setdir}"
   1984 			;;
   1985 
   1986 		cleandir|obj|build|distribution|release|sourcesets|syspkgs|params)
   1987 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1988 			    bomb "Failed to make ${op}"
   1989 			statusmsg "Successful make ${op}"
   1990 			;;
   1991 
   1992 		iso-image|iso-image-source)
   1993 			${runcmd} "${makewrapper}" ${parallel} \
   1994 			    CDEXTRA="$CDEXTRA" ${op} ||
   1995 			    bomb "Failed to make ${op}"
   1996 			statusmsg "Successful make ${op}"
   1997 			;;
   1998 
   1999 		live-image|install-image)
   2000 			# install-image and live-image require mtree spec files
   2001 			# built with UNPRIVED.  Assume UNPRIVED build has been
   2002 			# performed if METALOG file is created in DESTDIR.
   2003 			if [ ! -e "${DESTDIR}/METALOG" ] ; then
   2004 				bomb "The release binaries must have been built with -U to create images."
   2005 			fi
   2006 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   2007 			    bomb "Failed to make ${op}"
   2008 			statusmsg "Successful make ${op}"
   2009 			;;
   2010 		kernel=*)
   2011 			arg=${op#*=}
   2012 			buildkernel "${arg}"
   2013 			;;
   2014 
   2015 		releasekernel=*)
   2016 			arg=${op#*=}
   2017 			releasekernel "${arg}"
   2018 			;;
   2019 
   2020 		modules)
   2021 			buildmodules
   2022 			;;
   2023 
   2024 		installmodules=*)
   2025 			arg=${op#*=}
   2026 			if [ "${arg}" = "/" ] && \
   2027 			    (	[ "${uname_s}" != "NetBSD" ] || \
   2028 				[ "${uname_m}" != "${MACHINE}" ] ); then
   2029 				bomb "'${op}' must != / for cross builds."
   2030 			fi
   2031 			installmodules "${arg}"
   2032 			;;
   2033 
   2034 		install=*)
   2035 			arg=${op#*=}
   2036 			if [ "${arg}" = "/" ] && \
   2037 			    (	[ "${uname_s}" != "NetBSD" ] || \
   2038 				[ "${uname_m}" != "${MACHINE}" ] ); then
   2039 				bomb "'${op}' must != / for cross builds."
   2040 			fi
   2041 			installworld "${arg}"
   2042 			;;
   2043 
   2044 		rump|rumptest)
   2045 			dorump "${op}"
   2046 			;;
   2047 
   2048 		*)
   2049 			bomb "Unknown operation \`${op}'"
   2050 			;;
   2051 
   2052 		esac
   2053 	done
   2054 
   2055 	statusmsg2 "${progname} ended:" "$(date)"
   2056 	if [ -s "${results}" ]; then
   2057 		echo "===> Summary of results:"
   2058 		sed -e 's/^===>//;s/^/	/' "${results}"
   2059 		echo "===> ."
   2060 	fi
   2061 }
   2062 
   2063 main "$@"
   2064