Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.362
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.362 2022/08/14 08:51:41 lukem Exp $
      3 #
      4 # Copyright (c) 2001-2022 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 dash bash
    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 nl='
    267 '
    268 trap "exit 1" 1 2 3 15
    269 
    270 bomb()
    271 {
    272 	cat >&2 <<ERRORMESSAGE
    273 
    274 ERROR: $@
    275 *** BUILD ABORTED ***
    276 ERRORMESSAGE
    277 	kill ${toppid}		# in case we were invoked from a subshell
    278 	exit 1
    279 }
    280 
    281 # Quote args to make them safe in the shell.
    282 # Usage: quotedlist="$(shell_quote args...)"
    283 #
    284 # After building up a quoted list, use it by evaling it inside
    285 # double quotes, like this:
    286 #    eval "set -- $quotedlist"
    287 # or like this:
    288 #    eval "\$command $quotedlist \$filename"
    289 #
    290 shell_quote()
    291 {(
    292 	local result=''
    293 	local arg qarg
    294 	LC_COLLATE=C ; export LC_COLLATE # so [a-zA-Z0-9] works in ASCII
    295 	for arg in "$@" ; do
    296 		case "${arg}" in
    297 		'')
    298 			qarg="''"
    299 			;;
    300 		*[!-./a-zA-Z0-9]*)
    301 			# Convert each embedded ' to '\'',
    302 			# then insert ' at the beginning of the first line,
    303 			# and append ' at the end of the last line.
    304 			# Finally, elide unnecessary '' pairs at the
    305 			# beginning and end of the result and as part of
    306 			# '\'''\'' sequences that result from multiple
    307 			# adjacent quotes in he input.
    308 			qarg="$(printf "%s\n" "$arg" | \
    309 			    ${SED:-sed} -e "s/'/'\\\\''/g" \
    310 				-e "1s/^/'/" -e "\$s/\$/'/" \
    311 				-e "1s/^''//" -e "\$s/''\$//" \
    312 				-e "s/'''/'/g"
    313 				)"
    314 			;;
    315 		*)
    316 			# Arg is not the empty string, and does not contain
    317 			# any unsafe characters.  Leave it unchanged for
    318 			# readability.
    319 			qarg="${arg}"
    320 			;;
    321 		esac
    322 		result="${result}${result:+ }${qarg}"
    323 	done
    324 	printf "%s\n" "$result"
    325 )}
    326 
    327 statusmsg()
    328 {
    329 	${runcmd} echo "===> $@" | tee -a "${results}"
    330 }
    331 
    332 statusmsg2()
    333 {
    334 	local msg
    335 
    336 	msg="${1}"
    337 	shift
    338 	case "${msg}" in
    339 	????????????????*)	;;
    340 	??????????*)		msg="${msg}      ";;
    341 	?????*)			msg="${msg}           ";;
    342 	*)			msg="${msg}                ";;
    343 	esac
    344 	case "${msg}" in
    345 	?????????????????????*)	;;
    346 	????????????????????)	msg="${msg} ";;
    347 	???????????????????)	msg="${msg}  ";;
    348 	??????????????????)	msg="${msg}   ";;
    349 	?????????????????)	msg="${msg}    ";;
    350 	????????????????)	msg="${msg}     ";;
    351 	esac
    352 	statusmsg "${msg}$*"
    353 }
    354 
    355 warning()
    356 {
    357 	statusmsg "Warning: $@"
    358 }
    359 
    360 # Find a program in the PATH, and print the result.  If not found,
    361 # print a default.  If $2 is defined (even if it is an empty string),
    362 # then that is the default; otherwise, $1 is used as the default.
    363 #
    364 find_in_PATH()
    365 {
    366 	local prog="$1"
    367 	local result="${2-"$1"}"
    368 	local oldIFS="${IFS}"
    369 	local dir
    370 	IFS=":"
    371 	for dir in ${PATH}; do
    372 		if [ -x "${dir}/${prog}" ]; then
    373 			result="${dir}/${prog}"
    374 			break
    375 		fi
    376 	done
    377 	IFS="${oldIFS}"
    378 	echo "${result}"
    379 }
    380 
    381 # Try to find a working POSIX shell, and set HOST_SH to refer to it.
    382 # Assumes that uname_s, uname_m, and PWD have been set.
    383 #
    384 set_HOST_SH()
    385 {
    386 	# Even if ${HOST_SH} is already defined, we still do the
    387 	# sanity checks at the end.
    388 
    389 	# Solaris has /usr/xpg4/bin/sh.
    390 	#
    391 	[ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
    392 		[ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
    393 
    394 	# Try to get the name of the shell that's running this script,
    395 	# by parsing the output from "ps".  We assume that, if the host
    396 	# system's ps command supports -o comm at all, it will do so
    397 	# in the usual way: a one-line header followed by a one-line
    398 	# result, possibly including trailing white space.  And if the
    399 	# host system's ps command doesn't support -o comm, we assume
    400 	# that we'll get an error message on stderr and nothing on
    401 	# stdout.  (We don't try to use ps -o 'comm=' to suppress the
    402 	# header line, because that is less widely supported.)
    403 	#
    404 	# If we get the wrong result here, the user can override it by
    405 	# specifying HOST_SH in the environment.
    406 	#
    407 	[ -z "${HOST_SH}" ] && HOST_SH="$(
    408 		(ps -p $$ -o comm | sed -ne "2s/[ ${tab}]*\$//p") 2>/dev/null )"
    409 
    410 	# If nothing above worked, use "sh".  We will later find the
    411 	# first directory in the PATH that has a "sh" program.
    412 	#
    413 	[ -z "${HOST_SH}" ] && HOST_SH="sh"
    414 
    415 	# If the result so far is not an absolute path, try to prepend
    416 	# PWD or search the PATH.
    417 	#
    418 	case "${HOST_SH}" in
    419 	/*)	:
    420 		;;
    421 	*/*)	HOST_SH="${PWD}/${HOST_SH}"
    422 		;;
    423 	*)	HOST_SH="$(find_in_PATH "${HOST_SH}")"
    424 		;;
    425 	esac
    426 
    427 	# If we don't have an absolute path by now, bomb.
    428 	#
    429 	case "${HOST_SH}" in
    430 	/*)	:
    431 		;;
    432 	*)	bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
    433 		;;
    434 	esac
    435 
    436 	# If HOST_SH is not executable, bomb.
    437 	#
    438 	[ -x "${HOST_SH}" ] ||
    439 	    bomb "HOST_SH=\"${HOST_SH}\" is not executable."
    440 
    441 	# If HOST_SH fails tests, bomb.
    442 	# ("$0" may be a path that is no longer valid, because we have
    443 	# performed "cd $(dirname $0)", so don't use $0 here.)
    444 	#
    445 	"${HOST_SH}" build.sh --shelltest ||
    446 	    bomb "HOST_SH=\"${HOST_SH}\" failed functionality tests."
    447 }
    448 
    449 # initdefaults --
    450 # Set defaults before parsing command line options.
    451 #
    452 initdefaults()
    453 {
    454 	makeenv=
    455 	makewrapper=
    456 	makewrappermachine=
    457 	runcmd=
    458 	operations=
    459 	removedirs=
    460 
    461 	[ -d usr.bin/make ] || cd "$(dirname $0)"
    462 	[ -d usr.bin/make ] ||
    463 	    bomb "usr.bin/make not found; build.sh must be run from the top \
    464 level of source directory"
    465 	[ -f share/mk/bsd.own.mk ] ||
    466 	    bomb "src/share/mk is missing; please re-fetch the source tree"
    467 
    468 	# Set various environment variables to known defaults,
    469 	# to minimize (cross-)build problems observed "in the field".
    470 	#
    471 	# LC_ALL=C must be set before we try to parse the output from
    472 	# any command.  Other variables are set (or unset) here, before
    473 	# we parse command line arguments.
    474 	#
    475 	# These variables can be overridden via "-V var=value" if
    476 	# you know what you are doing.
    477 	#
    478 	unsetmakeenv C_INCLUDE_PATH
    479 	unsetmakeenv CPLUS_INCLUDE_PATH
    480 	unsetmakeenv INFODIR
    481 	unsetmakeenv LESSCHARSET
    482 	unsetmakeenv MAKEFLAGS
    483 	unsetmakeenv TERMINFO
    484 	setmakeenv LC_ALL C
    485 
    486 	# Find information about the build platform.  This should be
    487 	# kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
    488 	# variables in share/mk/bsd.sys.mk.
    489 	#
    490 	# Note that "uname -p" is not part of POSIX, but we want uname_p
    491 	# to be set to the host MACHINE_ARCH, if possible.  On systems
    492 	# where "uname -p" fails, prints "unknown", or prints a string
    493 	# that does not look like an identifier, fall back to using the
    494 	# output from "uname -m" instead.
    495 	#
    496 	uname_s=$(uname -s 2>/dev/null)
    497 	uname_r=$(uname -r 2>/dev/null)
    498 	uname_m=$(uname -m 2>/dev/null)
    499 	uname_p=$(uname -p 2>/dev/null || echo "unknown")
    500 	case "${uname_p}" in
    501 	''|unknown|*[!-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
    502 	esac
    503 
    504 	id_u=$(id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null)
    505 
    506 	# If $PWD is a valid name of the current directory, POSIX mandates
    507 	# that pwd return it by default which causes problems in the
    508 	# presence of symlinks.  Unsetting PWD is simpler than changing
    509 	# every occurrence of pwd to use -P.
    510 	#
    511 	# XXX Except that doesn't work on Solaris. Or many Linuces.
    512 	#
    513 	unset PWD
    514 	TOP=$( (exec pwd -P 2>/dev/null) || (exec pwd 2>/dev/null) )
    515 
    516 	# The user can set HOST_SH in the environment, or we try to
    517 	# guess an appropriate value.  Then we set several other
    518 	# variables from HOST_SH.
    519 	#
    520 	set_HOST_SH
    521 	setmakeenv HOST_SH "${HOST_SH}"
    522 	setmakeenv BSHELL "${HOST_SH}"
    523 	setmakeenv CONFIG_SHELL "${HOST_SH}"
    524 
    525 	# Set defaults.
    526 	#
    527 	toolprefix=nb
    528 
    529 	# Some systems have a small ARG_MAX.  -X prevents make(1) from
    530 	# exporting variables in the environment redundantly.
    531 	#
    532 	case "${uname_s}" in
    533 	Darwin | FreeBSD | CYGWIN*)
    534 		MAKEFLAGS="-X ${MAKEFLAGS}"
    535 		;;
    536 	esac
    537 
    538 	# do_{operation}=true if given operation is requested.
    539 	#
    540 	do_expertmode=false
    541 	do_rebuildmake=false
    542 	do_removedirs=false
    543 	do_tools=false
    544 	do_libs=false
    545 	do_cleandir=false
    546 	do_obj=false
    547 	do_build=false
    548 	do_distribution=false
    549 	do_release=false
    550 	do_kernel=false
    551 	do_releasekernel=false
    552 	do_kernels=false
    553 	do_modules=false
    554 	do_installmodules=false
    555 	do_install=false
    556 	do_sets=false
    557 	do_sourcesets=false
    558 	do_syspkgs=false
    559 	do_iso_image=false
    560 	do_iso_image_source=false
    561 	do_live_image=false
    562 	do_install_image=false
    563 	do_disk_image=false
    564 	do_params=false
    565 	do_rump=false
    566 	do_dtb=false
    567 
    568 	# done_{operation}=true if given operation has been done.
    569 	#
    570 	done_rebuildmake=false
    571 
    572 	# Create scratch directory
    573 	#
    574 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    575 	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
    576 	trap "cd /; rm -r -f \"${tmpdir}\"" 0
    577 	results="${tmpdir}/build.sh.results"
    578 
    579 	# Set source directories
    580 	#
    581 	setmakeenv NETBSDSRCDIR "${TOP}"
    582 
    583 	# Make sure KERNOBJDIR is an absolute path if defined
    584 	#
    585 	case "${KERNOBJDIR}" in
    586 	''|/*)	;;
    587 	*)	KERNOBJDIR="${TOP}/${KERNOBJDIR}"
    588 		setmakeenv KERNOBJDIR "${KERNOBJDIR}"
    589 		;;
    590 	esac
    591 
    592 	# Find the version of NetBSD
    593 	#
    594 	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
    595 
    596 	# Set the BUILDSEED to NetBSD-"N"
    597 	#
    598 	setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
    599 
    600 	# Set MKARZERO to "yes"
    601 	#
    602 	setmakeenv MKARZERO "yes"
    603 
    604 }
    605 
    606 # valid_MACHINE_ARCH -- A multi-line string, listing all valid
    607 # MACHINE/MACHINE_ARCH pairs.
    608 #
    609 # Each line contains a MACHINE and MACHINE_ARCH value, an optional ALIAS
    610 # which may be used to refer to the MACHINE/MACHINE_ARCH pair, and an
    611 # optional DEFAULT or NO_DEFAULT keyword.
    612 #
    613 # When a MACHINE corresponds to multiple possible values of
    614 # MACHINE_ARCH, then this table should list all allowed combinations.
    615 # If the MACHINE is associated with a default MACHINE_ARCH (to be
    616 # used when the user specifies the MACHINE but fails to specify the
    617 # MACHINE_ARCH), then one of the lines should have the "DEFAULT"
    618 # keyword.  If there is no default MACHINE_ARCH for a particular
    619 # MACHINE, then there should be a line with the "NO_DEFAULT" keyword,
    620 # and with a blank MACHINE_ARCH.
    621 #
    622 valid_MACHINE_ARCH='
    623 MACHINE=acorn32		MACHINE_ARCH=earmv4	ALIAS=eacorn32 DEFAULT
    624 MACHINE=algor		MACHINE_ARCH=mips64el	ALIAS=algor64
    625 MACHINE=algor		MACHINE_ARCH=mipsel	DEFAULT
    626 MACHINE=alpha		MACHINE_ARCH=alpha
    627 MACHINE=amd64		MACHINE_ARCH=x86_64
    628 MACHINE=amiga		MACHINE_ARCH=m68k
    629 MACHINE=amigappc	MACHINE_ARCH=powerpc
    630 MACHINE=arc		MACHINE_ARCH=mips64el	ALIAS=arc64
    631 MACHINE=arc		MACHINE_ARCH=mipsel	DEFAULT
    632 MACHINE=atari		MACHINE_ARCH=m68k
    633 MACHINE=bebox		MACHINE_ARCH=powerpc
    634 MACHINE=cats		MACHINE_ARCH=earmv4	ALIAS=ecats DEFAULT
    635 MACHINE=cesfic		MACHINE_ARCH=m68k
    636 MACHINE=cobalt		MACHINE_ARCH=mips64el	ALIAS=cobalt64
    637 MACHINE=cobalt		MACHINE_ARCH=mipsel	DEFAULT
    638 MACHINE=dreamcast	MACHINE_ARCH=sh3el
    639 MACHINE=emips		MACHINE_ARCH=mipseb
    640 MACHINE=epoc32		MACHINE_ARCH=earmv4	ALIAS=eepoc32 DEFAULT
    641 MACHINE=evbarm		MACHINE_ARCH=		NO_DEFAULT
    642 MACHINE=evbarm		MACHINE_ARCH=earmv4	ALIAS=evbearmv4-el	ALIAS=evbarmv4-el
    643 MACHINE=evbarm		MACHINE_ARCH=earmv4eb	ALIAS=evbearmv4-eb	ALIAS=evbarmv4-eb
    644 MACHINE=evbarm		MACHINE_ARCH=earmv5	ALIAS=evbearmv5-el	ALIAS=evbarmv5-el
    645 MACHINE=evbarm		MACHINE_ARCH=earmv5hf	ALIAS=evbearmv5hf-el	ALIAS=evbarmv5hf-el
    646 MACHINE=evbarm		MACHINE_ARCH=earmv5eb	ALIAS=evbearmv5-eb	ALIAS=evbarmv5-eb
    647 MACHINE=evbarm		MACHINE_ARCH=earmv5hfeb	ALIAS=evbearmv5hf-eb	ALIAS=evbarmv5hf-eb
    648 MACHINE=evbarm		MACHINE_ARCH=earmv6	ALIAS=evbearmv6-el	ALIAS=evbarmv6-el
    649 MACHINE=evbarm		MACHINE_ARCH=earmv6hf	ALIAS=evbearmv6hf-el	ALIAS=evbarmv6hf-el
    650 MACHINE=evbarm		MACHINE_ARCH=earmv6eb	ALIAS=evbearmv6-eb	ALIAS=evbarmv6-eb
    651 MACHINE=evbarm		MACHINE_ARCH=earmv6hfeb	ALIAS=evbearmv6hf-eb	ALIAS=evbarmv6hf-eb
    652 MACHINE=evbarm		MACHINE_ARCH=earmv7	ALIAS=evbearmv7-el	ALIAS=evbarmv7-el
    653 MACHINE=evbarm		MACHINE_ARCH=earmv7eb	ALIAS=evbearmv7-eb	ALIAS=evbarmv7-eb
    654 MACHINE=evbarm		MACHINE_ARCH=earmv7hf	ALIAS=evbearmv7hf-el	ALIAS=evbarmv7hf-el
    655 MACHINE=evbarm		MACHINE_ARCH=earmv7hfeb	ALIAS=evbearmv7hf-eb	ALIAS=evbarmv7hf-eb
    656 MACHINE=evbarm		MACHINE_ARCH=aarch64	ALIAS=evbarm64-el	ALIAS=evbarm64
    657 MACHINE=evbarm		MACHINE_ARCH=aarch64eb	ALIAS=evbarm64-eb
    658 MACHINE=evbcf		MACHINE_ARCH=coldfire
    659 MACHINE=evbmips		MACHINE_ARCH=		NO_DEFAULT
    660 MACHINE=evbmips		MACHINE_ARCH=mips64eb	ALIAS=evbmips64-eb
    661 MACHINE=evbmips		MACHINE_ARCH=mips64el	ALIAS=evbmips64-el
    662 MACHINE=evbmips		MACHINE_ARCH=mipseb	ALIAS=evbmips-eb
    663 MACHINE=evbmips		MACHINE_ARCH=mipsel	ALIAS=evbmips-el
    664 MACHINE=evbmips		MACHINE_ARCH=mipsn64eb	ALIAS=evbmipsn64-eb
    665 MACHINE=evbmips		MACHINE_ARCH=mipsn64el	ALIAS=evbmipsn64-el
    666 MACHINE=evbppc		MACHINE_ARCH=powerpc	DEFAULT
    667 MACHINE=evbppc		MACHINE_ARCH=powerpc64	ALIAS=evbppc64
    668 MACHINE=evbsh3		MACHINE_ARCH=		NO_DEFAULT
    669 MACHINE=evbsh3		MACHINE_ARCH=sh3eb	ALIAS=evbsh3-eb
    670 MACHINE=evbsh3		MACHINE_ARCH=sh3el	ALIAS=evbsh3-el
    671 MACHINE=ews4800mips	MACHINE_ARCH=mipseb
    672 MACHINE=hp300		MACHINE_ARCH=m68k
    673 MACHINE=hppa		MACHINE_ARCH=hppa
    674 MACHINE=hpcarm		MACHINE_ARCH=earmv4	ALIAS=hpcearm DEFAULT
    675 MACHINE=hpcmips		MACHINE_ARCH=mipsel
    676 MACHINE=hpcsh		MACHINE_ARCH=sh3el
    677 MACHINE=i386		MACHINE_ARCH=i386
    678 MACHINE=ia64		MACHINE_ARCH=ia64
    679 MACHINE=ibmnws		MACHINE_ARCH=powerpc
    680 MACHINE=iyonix		MACHINE_ARCH=earm	ALIAS=eiyonix DEFAULT
    681 MACHINE=landisk		MACHINE_ARCH=sh3el
    682 MACHINE=luna68k		MACHINE_ARCH=m68k
    683 MACHINE=mac68k		MACHINE_ARCH=m68k
    684 MACHINE=macppc		MACHINE_ARCH=powerpc	DEFAULT
    685 MACHINE=macppc		MACHINE_ARCH=powerpc64	ALIAS=macppc64
    686 MACHINE=mipsco		MACHINE_ARCH=mipseb
    687 MACHINE=mmeye		MACHINE_ARCH=sh3eb
    688 MACHINE=mvme68k		MACHINE_ARCH=m68k
    689 MACHINE=mvmeppc		MACHINE_ARCH=powerpc
    690 MACHINE=netwinder	MACHINE_ARCH=earmv4	ALIAS=enetwinder DEFAULT
    691 MACHINE=news68k		MACHINE_ARCH=m68k
    692 MACHINE=newsmips	MACHINE_ARCH=mipseb
    693 MACHINE=next68k		MACHINE_ARCH=m68k
    694 MACHINE=ofppc		MACHINE_ARCH=powerpc	DEFAULT
    695 MACHINE=ofppc		MACHINE_ARCH=powerpc64	ALIAS=ofppc64
    696 MACHINE=or1k		MACHINE_ARCH=or1k
    697 MACHINE=playstation2	MACHINE_ARCH=mipsel
    698 MACHINE=pmax		MACHINE_ARCH=mips64el	ALIAS=pmax64
    699 MACHINE=pmax		MACHINE_ARCH=mipsel	DEFAULT
    700 MACHINE=prep		MACHINE_ARCH=powerpc
    701 MACHINE=riscv		MACHINE_ARCH=riscv64	ALIAS=riscv64 DEFAULT
    702 MACHINE=riscv		MACHINE_ARCH=riscv32	ALIAS=riscv32
    703 MACHINE=rs6000		MACHINE_ARCH=powerpc
    704 MACHINE=sandpoint	MACHINE_ARCH=powerpc
    705 MACHINE=sbmips		MACHINE_ARCH=		NO_DEFAULT
    706 MACHINE=sbmips		MACHINE_ARCH=mips64eb	ALIAS=sbmips64-eb
    707 MACHINE=sbmips		MACHINE_ARCH=mips64el	ALIAS=sbmips64-el
    708 MACHINE=sbmips		MACHINE_ARCH=mipseb	ALIAS=sbmips-eb
    709 MACHINE=sbmips		MACHINE_ARCH=mipsel	ALIAS=sbmips-el
    710 MACHINE=sgimips		MACHINE_ARCH=mips64eb	ALIAS=sgimips64
    711 MACHINE=sgimips		MACHINE_ARCH=mipseb	DEFAULT
    712 MACHINE=shark		MACHINE_ARCH=earmv4	ALIAS=eshark DEFAULT
    713 MACHINE=sparc		MACHINE_ARCH=sparc
    714 MACHINE=sparc64		MACHINE_ARCH=sparc64
    715 MACHINE=sun2		MACHINE_ARCH=m68000
    716 MACHINE=sun3		MACHINE_ARCH=m68k
    717 MACHINE=vax		MACHINE_ARCH=vax
    718 MACHINE=x68k		MACHINE_ARCH=m68k
    719 MACHINE=zaurus		MACHINE_ARCH=earm	ALIAS=ezaurus DEFAULT
    720 '
    721 
    722 # getarch -- find the default MACHINE_ARCH for a MACHINE,
    723 # or convert an alias to a MACHINE/MACHINE_ARCH pair.
    724 #
    725 # Saves the original value of MACHINE in makewrappermachine before
    726 # alias processing.
    727 #
    728 # Sets MACHINE and MACHINE_ARCH if the input MACHINE value is
    729 # recognised as an alias, or recognised as a machine that has a default
    730 # MACHINE_ARCH (or that has only one possible MACHINE_ARCH).
    731 #
    732 # Leaves MACHINE and MACHINE_ARCH unchanged if MACHINE is recognised
    733 # as being associated with multiple MACHINE_ARCH values with no default.
    734 #
    735 # Bombs if MACHINE is not recognised.
    736 #
    737 getarch()
    738 {
    739 	local IFS
    740 	local found=""
    741 	local line
    742 
    743 	IFS="${nl}"
    744 	makewrappermachine="${MACHINE}"
    745 	for line in ${valid_MACHINE_ARCH}; do
    746 		line="${line%%#*}" # ignore comments
    747 		line="$( IFS=" ${tab}" ; echo $line )" # normalise white space
    748 		case "${line} " in
    749 		" ")
    750 			# skip blank lines or comment lines
    751 			continue
    752 			;;
    753 		*" ALIAS=${MACHINE} "*)
    754 			# Found a line with a matching ALIAS=<alias>.
    755 			found="$line"
    756 			break
    757 			;;
    758 		"MACHINE=${MACHINE} "*" NO_DEFAULT"*)
    759 			# Found an explicit "NO_DEFAULT" for this MACHINE.
    760 			found="$line"
    761 			break
    762 			;;
    763 		"MACHINE=${MACHINE} "*" DEFAULT"*)
    764 			# Found an explicit "DEFAULT" for this MACHINE.
    765 			found="$line"
    766 			break
    767 			;;
    768 		"MACHINE=${MACHINE} "*)
    769 			# Found a line for this MACHINE.  If it's the
    770 			# first such line, then tentatively accept it.
    771 			# If it's not the first matching line, then
    772 			# remember that there was more than one match.
    773 			case "$found" in
    774 			'')	found="$line" ;;
    775 			*)	found="MULTIPLE_MATCHES" ;;
    776 			esac
    777 			;;
    778 		esac
    779 	done
    780 
    781 	case "$found" in
    782 	*NO_DEFAULT*|*MULTIPLE_MATCHES*)
    783 		# MACHINE is OK, but MACHINE_ARCH is still unknown
    784 		return
    785 		;;
    786 	"MACHINE="*" MACHINE_ARCH="*)
    787 		# Obey the MACHINE= and MACHINE_ARCH= parts of the line.
    788 		IFS=" "
    789 		for frag in ${found}; do
    790 			case "$frag" in
    791 			MACHINE=*|MACHINE_ARCH=*)
    792 				eval "$frag"
    793 				;;
    794 			esac
    795 		done
    796 		;;
    797 	*)
    798 		bomb "Unknown target MACHINE: ${MACHINE}"
    799 		;;
    800 	esac
    801 }
    802 
    803 # validatearch -- check that the MACHINE/MACHINE_ARCH pair is supported.
    804 #
    805 # Bombs if the pair is not supported.
    806 #
    807 validatearch()
    808 {
    809 	local IFS
    810 	local line
    811 	local foundpair=false foundmachine=false foundarch=false
    812 
    813 	case "${MACHINE_ARCH}" in
    814 	"")
    815 		bomb "No MACHINE_ARCH provided. Use 'build.sh -m ${MACHINE} list-arch' to show options."
    816 		;;
    817 	esac
    818 
    819 	IFS="${nl}"
    820 	for line in ${valid_MACHINE_ARCH}; do
    821 		line="${line%%#*}" # ignore comments
    822 		line="$( IFS=" ${tab}" ; echo $line )" # normalise white space
    823 		case "${line} " in
    824 		" ")
    825 			# skip blank lines or comment lines
    826 			continue
    827 			;;
    828 		"MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH} "*)
    829 			foundpair=true
    830 			;;
    831 		"MACHINE=${MACHINE} "*)
    832 			foundmachine=true
    833 			;;
    834 		*"MACHINE_ARCH=${MACHINE_ARCH} "*)
    835 			foundarch=true
    836 			;;
    837 		esac
    838 	done
    839 
    840 	case "${foundpair}:${foundmachine}:${foundarch}" in
    841 	true:*)
    842 		: OK
    843 		;;
    844 	*:false:*)
    845 		bomb "Unknown target MACHINE: ${MACHINE}"
    846 		;;
    847 	*:*:false)
    848 		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
    849 		;;
    850 	*)
    851 		bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
    852 		;;
    853 	esac
    854 }
    855 
    856 # listarch -- list valid MACHINE/MACHINE_ARCH/ALIAS values,
    857 # optionally restricted to those where the MACHINE and/or MACHINE_ARCH
    858 # match specified glob patterns.
    859 #
    860 listarch()
    861 {
    862 	local machglob="$1" archglob="$2"
    863 	local IFS
    864 	local wildcard="*"
    865 	local line xline frag
    866 	local line_matches_machine line_matches_arch
    867 	local found=false
    868 
    869 	# Empty machglob or archglob should match anything
    870 	: "${machglob:=${wildcard}}"
    871 	: "${archglob:=${wildcard}}"
    872 
    873 	IFS="${nl}"
    874 	for line in ${valid_MACHINE_ARCH}; do
    875 		line="${line%%#*}" # ignore comments
    876 		xline="$( IFS=" ${tab}" ; echo $line )" # normalise white space
    877 		[ -z "${xline}" ] && continue # skip blank or comment lines
    878 
    879 		line_matches_machine=false
    880 		line_matches_arch=false
    881 
    882 		IFS=" "
    883 		for frag in ${xline}; do
    884 			case "${frag}" in
    885 			MACHINE=${machglob})
    886 				line_matches_machine=true ;;
    887 			ALIAS=${machglob})
    888 				line_matches_machine=true ;;
    889 			MACHINE_ARCH=${archglob})
    890 				line_matches_arch=true ;;
    891 			esac
    892 		done
    893 
    894 		if $line_matches_machine && $line_matches_arch; then
    895 			found=true
    896 			echo "$line"
    897 		fi
    898 	done
    899 	if ! $found; then
    900 		echo >&2 "No match for" \
    901 		    "MACHINE=${machglob} MACHINE_ARCH=${archglob}"
    902 		return 1
    903 	fi
    904 	return 0
    905 }
    906 
    907 # nobomb_getmakevar --
    908 # Given the name of a make variable in $1, print make's idea of the
    909 # value of that variable, or return 1 if there's an error.
    910 #
    911 nobomb_getmakevar()
    912 {
    913 	[ -x "${make}" ] || return 1
    914 	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
    915 _x_:
    916 	echo \${$1}
    917 .include <bsd.prog.mk>
    918 .include <bsd.kernobj.mk>
    919 EOF
    920 }
    921 
    922 # bomb_getmakevar --
    923 # Given the name of a make variable in $1, print make's idea of the
    924 # value of that variable, or bomb if there's an error.
    925 #
    926 bomb_getmakevar()
    927 {
    928 	[ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
    929 	nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
    930 }
    931 
    932 # getmakevar --
    933 # Given the name of a make variable in $1, print make's idea of the
    934 # value of that variable, or print a literal '$' followed by the
    935 # variable name if ${make} is not executable.  This is intended for use in
    936 # messages that need to be readable even if $make hasn't been built,
    937 # such as when build.sh is run with the "-n" option.
    938 #
    939 getmakevar()
    940 {
    941 	if [ -x "${make}" ]; then
    942 		bomb_getmakevar "$1"
    943 	else
    944 		echo "\$$1"
    945 	fi
    946 }
    947 
    948 setmakeenv()
    949 {
    950 	eval "$1='$2'; export $1"
    951 	makeenv="${makeenv} $1"
    952 }
    953 
    954 safe_setmakeenv()
    955 {
    956 	case "$1" in
    957 
    958 	#	Look for any vars we want to prohibit here, like:
    959 	# Bad | Dangerous)	usage "Cannot override $1 with -V";;
    960 
    961 	# That first char is OK has already been verified.
    962 	*[!A-Za-z0-9_]*)	usage "Bad variable name (-V): '$1'";;
    963 	esac
    964 	setmakeenv "$@"
    965 }
    966 
    967 unsetmakeenv()
    968 {
    969 	eval "unset $1"
    970 	makeenv="${makeenv} $1"
    971 }
    972 
    973 safe_unsetmakeenv()
    974 {
    975 	case "$1" in
    976 
    977 	#	Look for any vars user should not be able to unset
    978 	# Needed | Must_Have)	usage "Variable $1 cannot be unset";;
    979 
    980 	[!A-Za-z_]* | *[!A-Za-z0-9_]*)	usage "Bad variable name (-Z): '$1'";;
    981 	esac
    982 	unsetmakeenv "$1"
    983 }
    984 
    985 # Given a variable name in $1, modify the variable in place as follows:
    986 # For each space-separated word in the variable, call resolvepath.
    987 #
    988 resolvepaths()
    989 {
    990 	local var="$1"
    991 	local val
    992 	eval val=\"\${${var}}\"
    993 	local newval=''
    994 	local word
    995 	for word in ${val}; do
    996 		resolvepath word
    997 		newval="${newval}${newval:+ }${word}"
    998 	done
    999 	eval ${var}=\"\${newval}\"
   1000 }
   1001 
   1002 # Given a variable name in $1, modify the variable in place as follows:
   1003 # Convert possibly-relative path to absolute path by prepending
   1004 # ${TOP} if necessary.  Also delete trailing "/", if any.
   1005 #
   1006 resolvepath()
   1007 {
   1008 	local var="$1"
   1009 	local val
   1010 	eval val=\"\${${var}}\"
   1011 	case "${val}" in
   1012 	/)
   1013 		;;
   1014 	/*)
   1015 		val="${val%/}"
   1016 		;;
   1017 	*)
   1018 		val="${TOP}/${val%/}"
   1019 		;;
   1020 	esac
   1021 	eval ${var}=\"\${val}\"
   1022 }
   1023 
   1024 # Display help to stdout.
   1025 #
   1026 help()
   1027 {
   1028 	cat <<_usage_
   1029 
   1030 Usage: ${progname} [-EhnoPRrUuxy] [-a arch] [-B buildid] [-C cdextras]
   1031                 [-c compiler] [-D dest] [-j njob] [-M obj] [-m mach]
   1032                 [-N noisy] [-O obj] [-R release] [-S seed] [-T tools]
   1033                 [-V var=[value]] [-w wrapper] [-X x11src] [-Y extsrcsrc]
   1034                 [-Z var]
   1035                 operation [...]
   1036 
   1037  Build operations (all imply "obj" and "tools"):
   1038     build               Run "make build".
   1039     distribution        Run "make distribution" (includes DESTDIR/etc/ files).
   1040     release             Run "make release" (includes kernels & distrib media).
   1041 
   1042  Other operations:
   1043     help                Show this message and exit.
   1044     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
   1045                         Always performed.
   1046     cleandir            Run "make cleandir".  [Default unless -u is used]
   1047     dtb			Build devicetree blobs.
   1048     obj                 Run "make obj".  [Default unless -o is used]
   1049     tools               Build and install tools.
   1050     install=idir        Run "make installworld" to \`idir' to install all sets
   1051                         except \`etc'.  Useful after "distribution" or "release"
   1052     kernel=conf         Build kernel with config file \`conf'
   1053     kernel.gdb=conf     Build kernel (including netbsd.gdb) with config
   1054                         file \`conf'
   1055     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
   1056     kernels             Build all kernels
   1057     installmodules=idir Run "make installmodules" to \`idir' to install all
   1058                         kernel modules.
   1059     modules             Build kernel modules.
   1060     rumptest            Do a linktest for rump (for developers).
   1061     sets                Create binary sets in
   1062                         RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
   1063                         DESTDIR should be populated beforehand.
   1064     distsets            Same as "distribution sets".
   1065     sourcesets          Create source sets in RELEASEDIR/source/sets.
   1066     syspkgs             Create syspkgs in
   1067                         RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
   1068     iso-image           Create CD-ROM image in RELEASEDIR/images.
   1069     iso-image-source    Create CD-ROM image with source in RELEASEDIR/images.
   1070     live-image          Create bootable live image in
   1071                         RELEASEDIR/RELEASEMACHINEDIR/installation/liveimage.
   1072     install-image       Create bootable installation image in
   1073                         RELEASEDIR/RELEASEMACHINEDIR/installation/installimage.
   1074     disk-image=target   Create bootable disk image in
   1075                         RELEASEDIR/RELEASEMACHINEDIR/binary/gzimg/target.img.gz.
   1076     params              Display various make(1) parameters.
   1077     list-arch           Display a list of valid MACHINE/MACHINE_ARCH values,
   1078                         and exit.  The list may be narrowed by passing glob
   1079                         patterns or exact values in MACHINE or MACHINE_ARCH.
   1080     mkrepro-timestamp   Show the latest source timestamp used for reproducable
   1081                         builds and exit.  Requires -P or -V MKREPRO=yes.
   1082 
   1083  Options:
   1084     -a arch        Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
   1085     -B buildid     Set BUILDID to buildid.
   1086     -C cdextras    Append cdextras to CDEXTRA variable for inclusion on CD-ROM.
   1087     -c compiler    Select compiler:
   1088                        clang
   1089                        gcc
   1090                    [Default: gcc]
   1091     -D dest        Set DESTDIR to dest.  [Default: destdir.MACHINE]
   1092     -E             Set "expert" mode; disables various safety checks.
   1093                    Should not be used without expert knowledge of the build
   1094                    system.
   1095     -h             Print this help message.
   1096     -j njob        Run up to njob jobs in parallel; see make(1) -j.
   1097     -M obj         Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
   1098                    Unsets MAKEOBJDIR.
   1099     -m mach        Set MACHINE to mach.  Some mach values are actually
   1100                    aliases that set MACHINE/MACHINE_ARCH pairs.
   1101                    [Default: deduced from the host system if the host
   1102                    OS is NetBSD]
   1103     -N noisy       Set the noisyness (MAKEVERBOSE) level of the build:
   1104                        0   Minimal output ("quiet")
   1105                        1   Describe what is occurring
   1106                        2   Describe what is occurring and echo the actual
   1107                            command
   1108                        3   Ignore the effect of the "@" prefix in make commands
   1109                        4   Trace shell commands using the shell's -x flag
   1110                    [Default: 2]
   1111     -n             Show commands that would be executed, but do not execute them.
   1112     -O obj         Set obj root directory to obj; sets a MAKEOBJDIR pattern.
   1113                    Unsets MAKEOBJDIRPREFIX.
   1114     -o             Set MKOBJDIRS=no; do not create objdirs at start of build.
   1115     -P             Set MKREPRO and MKREPRO_TIMESTAMP to the latest source
   1116                    CVS timestamp for reproducible builds.
   1117     -R release     Set RELEASEDIR to release.  [Default: releasedir]
   1118     -r             Remove contents of TOOLDIR and DESTDIR before building.
   1119     -S seed        Set BUILDSEED to seed.  [Default: NetBSD-majorversion]
   1120     -T tools       Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
   1121                    the environment, ${toolprefix}make will be (re)built
   1122                    unconditionally.
   1123     -U             Set MKUNPRIVED=yes; build without requiring root privileges,
   1124                    install from an UNPRIVED build with proper file permissions.
   1125     -u             Set MKUPDATE=yes; do not run "make cleandir" first.
   1126                    Without this, everything is rebuilt, including the tools.
   1127     -V var=[value] Set variable \`var' to \`value'.
   1128     -w wrapper     Create ${toolprefix}make script as wrapper.
   1129                    [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
   1130     -X x11src      Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
   1131     -x             Set MKX11=yes; build X11 from X11SRCDIR
   1132     -Y extsrcsrc   Set EXTSRCSRCDIR to extsrcsrc.  [Default: /usr/extsrc]
   1133     -y             Set MKEXTSRC=yes; build extsrc from EXTSRCSRCDIR
   1134     -Z var         Unset ("zap") variable \`var'.
   1135 
   1136 _usage_
   1137 }
   1138 
   1139 # Display optional error message, help to stderr, and exit 1.
   1140 #
   1141 usage()
   1142 {
   1143 	if [ -n "$*" ]; then
   1144 		echo ""
   1145 		echo "${progname}: $*"
   1146 	fi
   1147 	help 1>&2
   1148 	exit 1
   1149 }
   1150 
   1151 parseoptions()
   1152 {
   1153 	opts='a:B:C:c:D:Ehj:M:m:N:nO:oPR:rS:T:UuV:w:X:xY:yZ:'
   1154 	opt_a=false
   1155 	opt_m=false
   1156 
   1157 	if type getopts >/dev/null 2>&1; then
   1158 		# Use POSIX getopts.
   1159 		#
   1160 		getoptcmd='getopts ${opts} opt && opt=-${opt}'
   1161 		optargcmd=':'
   1162 		optremcmd='shift $((${OPTIND} -1))'
   1163 	else
   1164 		type getopt >/dev/null 2>&1 ||
   1165 		    bomb "Shell does not support getopts or getopt"
   1166 
   1167 		# Use old-style getopt(1) (doesn't handle whitespace in args).
   1168 		#
   1169 		args="$(getopt ${opts} $*)"
   1170 		[ $? = 0 ] || usage
   1171 		set -- ${args}
   1172 
   1173 		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
   1174 		optargcmd='OPTARG="$1"; shift'
   1175 		optremcmd=':'
   1176 	fi
   1177 
   1178 	# Parse command line options.
   1179 	#
   1180 	while eval ${getoptcmd}; do
   1181 		case ${opt} in
   1182 
   1183 		-a)
   1184 			eval ${optargcmd}
   1185 			MACHINE_ARCH=${OPTARG}
   1186 			opt_a=true
   1187 			;;
   1188 
   1189 		-B)
   1190 			eval ${optargcmd}
   1191 			BUILDID=${OPTARG}
   1192 			;;
   1193 
   1194 		-C)
   1195 			eval ${optargcmd}; resolvepaths OPTARG
   1196 			CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
   1197 			;;
   1198 
   1199 		-c)
   1200 			eval ${optargcmd}
   1201 			case "${OPTARG}" in
   1202 			gcc)	# default, no variables needed
   1203 				;;
   1204 			clang)	setmakeenv HAVE_LLVM yes
   1205 				setmakeenv MKLLVM yes
   1206 				setmakeenv MKGCC no
   1207 				;;
   1208 			#pcc)	...
   1209 			#	;;
   1210 			*)	bomb "Unknown compiler: ${OPTARG}"
   1211 			esac
   1212 			;;
   1213 
   1214 		-D)
   1215 			eval ${optargcmd}; resolvepath OPTARG
   1216 			setmakeenv DESTDIR "${OPTARG}"
   1217 			;;
   1218 
   1219 		-E)
   1220 			do_expertmode=true
   1221 			;;
   1222 
   1223 		-j)
   1224 			eval ${optargcmd}
   1225 			parallel="-j ${OPTARG}"
   1226 			;;
   1227 
   1228 		-M)
   1229 			eval ${optargcmd}; resolvepath OPTARG
   1230 			case "${OPTARG}" in
   1231 			\$*)	usage "-M argument must not begin with '\$'"
   1232 				;;
   1233 			*\$*)	# can use resolvepath, but can't set TOP_objdir
   1234 				resolvepath OPTARG
   1235 				;;
   1236 			*)	resolvepath OPTARG
   1237 				TOP_objdir="${OPTARG}${TOP}"
   1238 				;;
   1239 			esac
   1240 			unsetmakeenv MAKEOBJDIR
   1241 			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
   1242 			;;
   1243 
   1244 			# -m overrides MACHINE_ARCH unless "-a" is specified
   1245 		-m)
   1246 			eval ${optargcmd}
   1247 			MACHINE="${OPTARG}"
   1248 			opt_m=true
   1249 			;;
   1250 
   1251 		-N)
   1252 			eval ${optargcmd}
   1253 			case "${OPTARG}" in
   1254 			0|1|2|3|4)
   1255 				setmakeenv MAKEVERBOSE "${OPTARG}"
   1256 				;;
   1257 			*)
   1258 				usage "'${OPTARG}' is not a valid value for -N"
   1259 				;;
   1260 			esac
   1261 			;;
   1262 
   1263 		-n)
   1264 			runcmd=echo
   1265 			;;
   1266 
   1267 		-O)
   1268 			eval ${optargcmd}
   1269 			case "${OPTARG}" in
   1270 			*\$*)	usage "-O argument must not contain '\$'"
   1271 				;;
   1272 			*)	resolvepath OPTARG
   1273 				TOP_objdir="${OPTARG}"
   1274 				;;
   1275 			esac
   1276 			unsetmakeenv MAKEOBJDIRPREFIX
   1277 			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
   1278 			;;
   1279 
   1280 		-o)
   1281 			MKOBJDIRS=no
   1282 			;;
   1283 
   1284 		-P)
   1285 			MKREPRO=yes
   1286 			;;
   1287 
   1288 		-R)
   1289 			eval ${optargcmd}; resolvepath OPTARG
   1290 			setmakeenv RELEASEDIR "${OPTARG}"
   1291 			;;
   1292 
   1293 		-r)
   1294 			do_removedirs=true
   1295 			do_rebuildmake=true
   1296 			;;
   1297 
   1298 		-S)
   1299 			eval ${optargcmd}
   1300 			setmakeenv BUILDSEED "${OPTARG}"
   1301 			;;
   1302 
   1303 		-T)
   1304 			eval ${optargcmd}; resolvepath OPTARG
   1305 			TOOLDIR="${OPTARG}"
   1306 			export TOOLDIR
   1307 			;;
   1308 
   1309 		-U)
   1310 			setmakeenv MKUNPRIVED yes
   1311 			;;
   1312 
   1313 		-u)
   1314 			setmakeenv MKUPDATE yes
   1315 			;;
   1316 
   1317 		-V)
   1318 			eval ${optargcmd}
   1319 			case "${OPTARG}" in
   1320 		    # XXX: consider restricting which variables can be changed?
   1321 			[a-zA-Z_]*=*)
   1322 				safe_setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
   1323 				;;
   1324 			[a-zA-Z_]*)
   1325 				safe_setmakeenv "${OPTARG}" ""
   1326 				;;
   1327 			*)
   1328 				usage "-V argument must be of the form 'var[=value]'"
   1329 				;;
   1330 			esac
   1331 			;;
   1332 
   1333 		-w)
   1334 			eval ${optargcmd}; resolvepath OPTARG
   1335 			makewrapper="${OPTARG}"
   1336 			;;
   1337 
   1338 		-X)
   1339 			eval ${optargcmd}; resolvepath OPTARG
   1340 			setmakeenv X11SRCDIR "${OPTARG}"
   1341 			;;
   1342 
   1343 		-x)
   1344 			setmakeenv MKX11 yes
   1345 			;;
   1346 
   1347 		-Y)
   1348 			eval ${optargcmd}; resolvepath OPTARG
   1349 			setmakeenv EXTSRCSRCDIR "${OPTARG}"
   1350 			;;
   1351 
   1352 		-y)
   1353 			setmakeenv MKEXTSRC yes
   1354 			;;
   1355 
   1356 		-Z)
   1357 			eval ${optargcmd}
   1358 		    # XXX: consider restricting which variables can be unset?
   1359 			safe_unsetmakeenv "${OPTARG}"
   1360 			;;
   1361 
   1362 		--)
   1363 			break
   1364 			;;
   1365 
   1366 		-'?'|-h)
   1367 			help
   1368 			exit 0
   1369 			;;
   1370 
   1371 		esac
   1372 	done
   1373 
   1374 	# Validate operations.
   1375 	#
   1376 	eval ${optremcmd}
   1377 	while [ $# -gt 0 ]; do
   1378 		op=$1; shift
   1379 		operations="${operations} ${op}"
   1380 
   1381 		case "${op}" in
   1382 
   1383 		help)
   1384 			help
   1385 			exit 0
   1386 			;;
   1387 
   1388 		list-arch)
   1389 			listarch "${MACHINE}" "${MACHINE_ARCH}"
   1390 			exit
   1391 			;;
   1392 		mkrepro-timestamp)
   1393 			setup_mkrepro quiet
   1394 			echo ${MKREPRO_TIMESTAMP:-0}
   1395 			[ ${MKREPRO_TIMESTAMP:-0} -ne 0 ]; exit
   1396 			;;
   1397 
   1398 		kernel=*|releasekernel=*|kernel.gdb=*)
   1399 			arg=${op#*=}
   1400 			op=${op%%=*}
   1401 			[ -n "${arg}" ] ||
   1402 			    bomb "Must supply a kernel name with \`${op}=...'"
   1403 			;;
   1404 
   1405 		disk-image=*)
   1406 			arg=${op#*=}
   1407 			op=disk_image
   1408 			[ -n "${arg}" ] ||
   1409 			    bomb "Must supply a target name with \`${op}=...'"
   1410 
   1411 			;;
   1412 
   1413 		install=*|installmodules=*)
   1414 			arg=${op#*=}
   1415 			op=${op%%=*}
   1416 			[ -n "${arg}" ] ||
   1417 			    bomb "Must supply a directory with \`install=...'"
   1418 			;;
   1419 
   1420 		distsets)
   1421 			operations="$(echo "$operations" | sed 's/distsets/distribution sets/')"
   1422 			do_sets=true
   1423 			op=distribution
   1424 			;;
   1425 
   1426 		build|\
   1427 		cleandir|\
   1428 		distribution|\
   1429 		dtb|\
   1430 		install-image|\
   1431 		iso-image-source|\
   1432 		iso-image|\
   1433 		kernels|\
   1434 		libs|\
   1435 		live-image|\
   1436 		makewrapper|\
   1437 		modules|\
   1438 		obj|\
   1439 		params|\
   1440 		release|\
   1441 		rump|\
   1442 		rumptest|\
   1443 		sets|\
   1444 		sourcesets|\
   1445 		syspkgs|\
   1446 		tools)
   1447 			;;
   1448 
   1449 		*)
   1450 			usage "Unknown operation \`${op}'"
   1451 			;;
   1452 
   1453 		esac
   1454 		# ${op} may contain chars that are not allowed in variable
   1455 		# names.  Replace them with '_' before setting do_${op}.
   1456 		op="$( echo "$op" | tr -s '.-' '__')"
   1457 		eval do_${op}=true
   1458 	done
   1459 	[ -n "${operations}" ] || usage "Missing operation to perform."
   1460 
   1461 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
   1462 	#
   1463 	if [ -z "${MACHINE}" ]; then
   1464 		[ "${uname_s}" = "NetBSD" ] ||
   1465 		    bomb "MACHINE must be set, or -m must be used, for cross builds."
   1466 		MACHINE=${uname_m}
   1467 		MACHINE_ARCH=${uname_p}
   1468 	fi
   1469 	if $opt_m && ! $opt_a; then
   1470 		# Settings implied by the command line -m option
   1471 		# override MACHINE_ARCH from the environment (if any).
   1472 		getarch
   1473 	fi
   1474 	[ -n "${MACHINE_ARCH}" ] || getarch
   1475 	validatearch
   1476 
   1477 	# Set up default make(1) environment.
   1478 	#
   1479 	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
   1480 	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
   1481 	[ -z "${BUILDINFO}" ] || makeenv="${makeenv} BUILDINFO"
   1482 	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS}"
   1483 	MAKEFLAGS="${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
   1484 	export MAKEFLAGS MACHINE MACHINE_ARCH
   1485 	setmakeenv USETOOLS "yes"
   1486 	setmakeenv MAKEWRAPPERMACHINE "${makewrappermachine:-${MACHINE}}"
   1487 	setmakeenv MAKE_OBJDIR_CHECK_WRITABLE no
   1488 }
   1489 
   1490 # sanitycheck --
   1491 # Sanity check after parsing command line options, before rebuildmake.
   1492 #
   1493 sanitycheck()
   1494 {
   1495 	# Install as non-root is a bad idea.
   1496 	#
   1497 	if ${do_install} && [ "$id_u" -ne 0 ] ; then
   1498 		if ${do_expertmode}; then
   1499 			warning "Will install as an unprivileged user."
   1500 		else
   1501 			bomb "-E must be set for install as an unprivileged user."
   1502 		fi
   1503 	fi
   1504 
   1505 	# If the PATH contains any non-absolute components (including,
   1506 	# but not limited to, "." or ""), then complain.  As an exception,
   1507 	# allow "" or "." as the last component of the PATH.  This is fatal
   1508 	# if expert mode is not in effect.
   1509 	#
   1510 	local path="${PATH}"
   1511 	path="${path%:}"	# delete trailing ":"
   1512 	path="${path%:.}"	# delete trailing ":."
   1513 	case ":${path}:/" in
   1514 	*:[!/~]*)
   1515 		if ${do_expertmode}; then
   1516 			warning "PATH contains non-absolute components"
   1517 		else
   1518 			bomb "PATH environment variable must not" \
   1519 			     "contain non-absolute components"
   1520 		fi
   1521 		;;
   1522 	esac
   1523 
   1524 	while [ ${MKX11-no} = "yes" ]; do		# not really a loop
   1525 		test -n "${X11SRCDIR}" && {
   1526 		    test -d "${X11SRCDIR}" ||
   1527 		    	bomb "X11SRCDIR (${X11SRCDIR}) does not exist (with -x)"
   1528 		    break
   1529 		}
   1530 		for _xd in \
   1531 		    "${NETBSDSRCDIR%/*}/xsrc" \
   1532 		    "${NETBSDSRCDIR}/xsrc" \
   1533 		    /usr/xsrc
   1534 		do
   1535 		    test -d "${_xd}" &&
   1536 			setmakeenv X11SRCDIR "${_xd}" &&
   1537 			break 2
   1538 		done
   1539 		bomb "Asked to build X11 but no xsrc"
   1540 	done
   1541 }
   1542 
   1543 # print_tooldir_make --
   1544 # Try to find and print a path to an existing
   1545 # ${TOOLDIR}/bin/${toolprefix}program
   1546 print_tooldir_program()
   1547 {
   1548 	local possible_TOP_OBJ
   1549 	local possible_TOOLDIR
   1550 	local possible_program
   1551 	local tooldir_program
   1552 	local program=${1}
   1553 
   1554 	if [ -n "${TOOLDIR}" ]; then
   1555 		echo "${TOOLDIR}/bin/${toolprefix}${program}"
   1556 		return
   1557 	fi
   1558 
   1559 	# Set host_ostype to something like "NetBSD-4.5.6-i386".  This
   1560 	# is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
   1561 	#
   1562 	local host_ostype="${uname_s}-$(
   1563 		echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
   1564 		)-$(
   1565 		echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
   1566 		)"
   1567 
   1568 	# Look in a few potential locations for
   1569 	# ${possible_TOOLDIR}/bin/${toolprefix}${program}.
   1570 	# If we find it, then set possible_program.
   1571 	#
   1572 	# In the usual case (without interference from environment
   1573 	# variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
   1574 	# "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
   1575 	#
   1576 	# In practice it's difficult to figure out the correct value
   1577 	# for _SRC_TOP_OBJ_.  In the easiest case, when the -M or -O
   1578 	# options were passed to build.sh, then ${TOP_objdir} will be
   1579 	# the correct value.  We also try a few other possibilities, but
   1580 	# we do not replicate all the logic of <bsd.obj.mk>.
   1581 	#
   1582 	for possible_TOP_OBJ in \
   1583 		"${TOP_objdir}" \
   1584 		"${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
   1585 		"${TOP}" \
   1586 		"${TOP}/obj" \
   1587 		"${TOP}/obj.${MACHINE}"
   1588 	do
   1589 		[ -n "${possible_TOP_OBJ}" ] || continue
   1590 		possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
   1591 		possible_program="${possible_TOOLDIR}/bin/${toolprefix}${program}"
   1592 		if [ -x "${possible_make}" ]; then
   1593 			echo ${possible_program}
   1594 			return;
   1595 		fi
   1596 	done
   1597 	echo ""
   1598 }
   1599 # print_tooldir_make --
   1600 # Try to find and print a path to an existing
   1601 # ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
   1602 # new version of ${toolprefix}make has been built.
   1603 #
   1604 # * If TOOLDIR was set in the environment or on the command line, use
   1605 #   that value.
   1606 # * Otherwise try to guess what TOOLDIR would be if not overridden by
   1607 #   /etc/mk.conf, and check whether the resulting directory contains
   1608 #   a copy of ${toolprefix}make (this should work for everybody who
   1609 #   doesn't override TOOLDIR via /etc/mk.conf);
   1610 # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
   1611 #   in the PATH (this might accidentally find a version of make that
   1612 #   does not understand the syntax used by NetBSD make, and that will
   1613 #   lead to failure in the next step);
   1614 # * If a copy of make was found above, try to use it with
   1615 #   nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
   1616 #   result only if it's a directory that already exists;
   1617 # * If a value of TOOLDIR was found above, and if
   1618 #   ${TOOLDIR}/bin/${toolprefix}make exists, print that value.
   1619 #
   1620 print_tooldir_make()
   1621 {
   1622 	local possible_make
   1623 	local possible_TOOLDIR
   1624 	local tooldir_make
   1625 
   1626 	possible_make=$(print_tooldir_program make)
   1627 	# If the above didn't work, search the PATH for a suitable
   1628 	# ${toolprefix}make, nbmake, bmake, or make.
   1629 	#
   1630 	: ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
   1631 	: ${possible_make:=$(find_in_PATH nbmake '')}
   1632 	: ${possible_make:=$(find_in_PATH bmake '')}
   1633 	: ${possible_make:=$(find_in_PATH make '')}
   1634 
   1635 	# At this point, we don't care whether possible_make is in the
   1636 	# correct TOOLDIR or not; we simply want it to be usable by
   1637 	# getmakevar to help us find the correct TOOLDIR.
   1638 	#
   1639 	# Use ${possible_make} with nobomb_getmakevar to try to find
   1640 	# the value of TOOLDIR.  Believe the result only if it's
   1641 	# a directory that already exists and contains bin/${toolprefix}make.
   1642 	#
   1643 	if [ -x "${possible_make}" ]; then
   1644 		possible_TOOLDIR="$(
   1645 			make="${possible_make}" \
   1646 			nobomb_getmakevar TOOLDIR 2>/dev/null
   1647 			)"
   1648 		if [ $? = 0 ] && [ -n "${possible_TOOLDIR}" ] \
   1649 		    && [ -d "${possible_TOOLDIR}" ];
   1650 		then
   1651 			tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
   1652 			if [ -x "${tooldir_make}" ]; then
   1653 				echo "${tooldir_make}"
   1654 				return 0
   1655 			fi
   1656 		fi
   1657 	fi
   1658 	return 1
   1659 }
   1660 
   1661 # rebuildmake --
   1662 # Rebuild nbmake in a temporary directory if necessary.  Sets $make
   1663 # to a path to the nbmake executable.  Sets done_rebuildmake=true
   1664 # if nbmake was rebuilt.
   1665 #
   1666 # There is a cyclic dependency between building nbmake and choosing
   1667 # TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
   1668 # would like to use getmakevar to get the value of TOOLDIR; but we can't
   1669 # use getmakevar before we have an up to date version of nbmake; we
   1670 # might already have an up to date version of nbmake in TOOLDIR, but we
   1671 # don't yet know where TOOLDIR is.
   1672 #
   1673 # The default value of TOOLDIR also depends on the location of the top
   1674 # level object directory, so $(getmakevar TOOLDIR) invoked before or
   1675 # after making the top level object directory may produce different
   1676 # results.
   1677 #
   1678 # Strictly speaking, we should do the following:
   1679 #
   1680 #    1. build a new version of nbmake in a temporary directory;
   1681 #    2. use the temporary nbmake to create the top level obj directory;
   1682 #    3. use $(getmakevar TOOLDIR) with the temporary nbmake to
   1683 #       get the correct value of TOOLDIR;
   1684 #    4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
   1685 #
   1686 # However, people don't like building nbmake unnecessarily if their
   1687 # TOOLDIR has not changed since an earlier build.  We try to avoid
   1688 # rebuilding a temporary version of nbmake by taking some shortcuts to
   1689 # guess a value for TOOLDIR, looking for an existing version of nbmake
   1690 # in that TOOLDIR, and checking whether that nbmake is newer than the
   1691 # sources used to build it.
   1692 #
   1693 rebuildmake()
   1694 {
   1695 	make="$(print_tooldir_make)"
   1696 	if [ -n "${make}" ] && [ -x "${make}" ]; then
   1697 		for f in usr.bin/make/*.[ch]; do
   1698 			if [ "${f}" -nt "${make}" ]; then
   1699 				statusmsg "${make} outdated" \
   1700 					"(older than ${f}), needs building."
   1701 				do_rebuildmake=true
   1702 				break
   1703 			fi
   1704 		done
   1705 	else
   1706 		statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
   1707 		do_rebuildmake=true
   1708 	fi
   1709 
   1710 	# Build bootstrap ${toolprefix}make if needed.
   1711 	if ! ${do_rebuildmake}; then
   1712 		return
   1713 	fi
   1714 
   1715 	# Silent configure with MAKEVERBOSE==0
   1716 	if [ ${MAKEVERBOSE:-2} -eq 0 ]; then
   1717 		configure_args=--silent
   1718 	fi
   1719 
   1720 	statusmsg "Bootstrapping ${toolprefix}make"
   1721 	${runcmd} cd "${tmpdir}"
   1722 	${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
   1723 		CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
   1724 	    ${HOST_SH} "${TOP}/tools/make/configure" ${configure_args} ||
   1725 	( cp ${tmpdir}/config.log ${tmpdir}-config.log
   1726 	      bomb "Configure of ${toolprefix}make failed, see ${tmpdir}-config.log for details" )
   1727 	${runcmd} ${HOST_SH} buildmake.sh ||
   1728 	    bomb "Build of ${toolprefix}make failed"
   1729 	make="${tmpdir}/${toolprefix}make"
   1730 	${runcmd} cd "${TOP}"
   1731 	${runcmd} rm -f usr.bin/make/*.o
   1732 	done_rebuildmake=true
   1733 }
   1734 
   1735 # validatemakeparams --
   1736 # Perform some late sanity checks, after rebuildmake,
   1737 # but before createmakewrapper or any real work.
   1738 #
   1739 # Creates the top-level obj directory, because that
   1740 # is needed by some of the sanity checks.
   1741 #
   1742 # Prints status messages reporting the values of several variables.
   1743 #
   1744 validatemakeparams()
   1745 {
   1746 	# MAKECONF (which defaults to /etc/mk.conf in share/mk/bsd.own.mk)
   1747 	# can affect many things, so mention it in an early status message.
   1748 	#
   1749 	MAKECONF=$(getmakevar MAKECONF)
   1750 	if [ -e "${MAKECONF}" ]; then
   1751 		statusmsg2 "MAKECONF file:" "${MAKECONF}"
   1752 	else
   1753 		statusmsg2 "MAKECONF file:" "${MAKECONF} (File not found)"
   1754 	fi
   1755 
   1756 	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE.
   1757 	# These may be set as build.sh options or in "mk.conf".
   1758 	# Don't export them as they're only used for tests in build.sh.
   1759 	#
   1760 	MKOBJDIRS=$(getmakevar MKOBJDIRS)
   1761 	MKUNPRIVED=$(getmakevar MKUNPRIVED)
   1762 	MKUPDATE=$(getmakevar MKUPDATE)
   1763 
   1764 	# Non-root should always use either the -U or -E flag.
   1765 	#
   1766 	if ! ${do_expertmode} && \
   1767 	    [ "$id_u" -ne 0 ] && \
   1768 	    [ "${MKUNPRIVED}" = "no" ] ; then
   1769 		bomb "-U or -E must be set for build as an unprivileged user."
   1770 	fi
   1771 
   1772 	if [ "${runcmd}" = "echo" ]; then
   1773 		TOOLCHAIN_MISSING=no
   1774 		EXTERNAL_TOOLCHAIN=""
   1775 	else
   1776 		TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
   1777 		EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
   1778 	fi
   1779 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
   1780 	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
   1781 		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
   1782 		${runcmd} echo "	MACHINE:      ${MACHINE}"
   1783 		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
   1784 		${runcmd} echo ""
   1785 		${runcmd} echo "All builds for this platform should be done via a traditional make"
   1786 		${runcmd} echo "If you wish to use an external cross-toolchain, set"
   1787 		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
   1788 		${runcmd} echo "in either the environment or mk.conf and rerun"
   1789 		${runcmd} echo "	${progname} $*"
   1790 		exit 1
   1791 	fi
   1792 
   1793 	if [ "${MKOBJDIRS}" != "no" ]; then
   1794 		# Create the top-level object directory.
   1795 		#
   1796 		# "make obj NOSUBDIR=" can handle most cases, but it
   1797 		# can't handle the case where MAKEOBJDIRPREFIX is set
   1798 		# while the corresponding directory does not exist
   1799 		# (rules in <bsd.obj.mk> would abort the build).  We
   1800 		# therefore have to handle the MAKEOBJDIRPREFIX case
   1801 		# without invoking "make obj".  The MAKEOBJDIR case
   1802 		# could be handled either way, but we choose to handle
   1803 		# it similarly to MAKEOBJDIRPREFIX.
   1804 		#
   1805 		if [ -n "${TOP_obj}" ]; then
   1806 			# It must have been set by the "-M" or "-O"
   1807 			# command line options, so there's no need to
   1808 			# use getmakevar
   1809 			:
   1810 		elif [ -n "$MAKEOBJDIRPREFIX" ]; then
   1811 			TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
   1812 		elif [ -n "$MAKEOBJDIR" ]; then
   1813 			TOP_obj="$(getmakevar MAKEOBJDIR)"
   1814 		fi
   1815 		if [ -n "$TOP_obj" ]; then
   1816 			${runcmd} mkdir -p "${TOP_obj}" ||
   1817 			    bomb "Can't create top level object directory" \
   1818 					"${TOP_obj}"
   1819 		else
   1820 			${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
   1821 			    bomb "Can't create top level object directory" \
   1822 					"using make obj"
   1823 		fi
   1824 
   1825 		# make obj in tools to ensure that the objdir for "tools"
   1826 		# is available.
   1827 		#
   1828 		${runcmd} cd tools
   1829 		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
   1830 		    bomb "Failed to make obj in tools"
   1831 		${runcmd} cd "${TOP}"
   1832 	fi
   1833 
   1834 	# Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
   1835 	# and bomb if they have changed from the values we had from the
   1836 	# command line or environment.
   1837 	#
   1838 	# This must be done after creating the top-level object directory.
   1839 	#
   1840 	for var in TOOLDIR DESTDIR RELEASEDIR
   1841 	do
   1842 		eval oldval=\"\$${var}\"
   1843 		newval="$(getmakevar $var)"
   1844 		if ! $do_expertmode; then
   1845 			: ${_SRC_TOP_OBJ_:=$(getmakevar _SRC_TOP_OBJ_)}
   1846 			case "$var" in
   1847 			DESTDIR)
   1848 				: ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
   1849 				makeenv="${makeenv} DESTDIR"
   1850 				;;
   1851 			RELEASEDIR)
   1852 				: ${newval:=${_SRC_TOP_OBJ_}/releasedir}
   1853 				makeenv="${makeenv} RELEASEDIR"
   1854 				;;
   1855 			esac
   1856 		fi
   1857 		if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]; then
   1858 			bomb "Value of ${var} has changed" \
   1859 				"(was \"${oldval}\", now \"${newval}\")"
   1860 		fi
   1861 		eval ${var}=\"\${newval}\"
   1862 		eval export ${var}
   1863 		statusmsg2 "${var} path:" "${newval}"
   1864 	done
   1865 
   1866 	# RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
   1867 	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
   1868 
   1869 	# Check validity of TOOLDIR and DESTDIR.
   1870 	#
   1871 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
   1872 		bomb "TOOLDIR '${TOOLDIR}' invalid"
   1873 	fi
   1874 	removedirs="${TOOLDIR}"
   1875 
   1876 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
   1877 		if ${do_distribution} || ${do_release} || \
   1878 		   [ "${uname_s}" != "NetBSD" ] || \
   1879 		   [ "${uname_m}" != "${MACHINE}" ]; then
   1880 			bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
   1881 		fi
   1882 		if ! ${do_expertmode}; then
   1883 			bomb "DESTDIR must != / for non -E (expert) builds"
   1884 		fi
   1885 		statusmsg "WARNING: Building to /, in expert mode."
   1886 		statusmsg "         This may cause your system to break!  Reasons include:"
   1887 		statusmsg "            - your kernel is not up to date"
   1888 		statusmsg "            - the libraries or toolchain have changed"
   1889 		statusmsg "         YOU HAVE BEEN WARNED!"
   1890 	else
   1891 		removedirs="${removedirs} ${DESTDIR}"
   1892 	fi
   1893 	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
   1894 		bomb "Must set RELEASEDIR with \`releasekernel=...'"
   1895 	fi
   1896 
   1897 	# If a previous build.sh run used -U (and therefore created a
   1898 	# METALOG file), then most subsequent build.sh runs must also
   1899 	# use -U.  If DESTDIR is about to be removed, then don't perform
   1900 	# this check.
   1901 	#
   1902 	case "${do_removedirs} ${removedirs} " in
   1903 	true*" ${DESTDIR} "*)
   1904 		# DESTDIR is about to be removed
   1905 		;;
   1906 	*)
   1907 		if [ -e "${DESTDIR}/METALOG" ] && \
   1908 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1909 			if $do_expertmode; then
   1910 				warning "A previous build.sh run specified -U."
   1911 			else
   1912 				bomb "A previous build.sh run specified -U; you must specify it again now."
   1913 			fi
   1914 		fi
   1915 		;;
   1916 	esac
   1917 
   1918 	# live-image and install-image targets require binary sets
   1919 	# (actually DESTDIR/etc/mtree/set.* files) built with MKUNPRIVED.
   1920 	# If release operation is specified with live-image or install-image,
   1921 	# the release op should be performed with -U for later image ops.
   1922 	#
   1923 	if ${do_release} && ( ${do_live_image} || ${do_install_image} ) && \
   1924 	    [ "${MKUNPRIVED}" = "no" ] ; then
   1925 		bomb "-U must be specified on building release to create images later."
   1926 	fi
   1927 }
   1928 
   1929 
   1930 createmakewrapper()
   1931 {
   1932 	# Remove the target directories.
   1933 	#
   1934 	if ${do_removedirs}; then
   1935 		for f in ${removedirs}; do
   1936 			statusmsg "Removing ${f}"
   1937 			${runcmd} rm -r -f "${f}"
   1938 		done
   1939 	fi
   1940 
   1941 	# Recreate $TOOLDIR.
   1942 	#
   1943 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
   1944 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
   1945 
   1946 	# If we did not previously rebuild ${toolprefix}make, then
   1947 	# check whether $make is still valid and the same as the output
   1948 	# from print_tooldir_make.  If not, then rebuild make now.  A
   1949 	# possible reason for this being necessary is that the actual
   1950 	# value of TOOLDIR might be different from the value guessed
   1951 	# before the top level obj dir was created.
   1952 	#
   1953 	if ! ${done_rebuildmake} && \
   1954 	    ( [ ! -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] )
   1955 	then
   1956 		rebuildmake
   1957 	fi
   1958 
   1959 	# Install ${toolprefix}make if it was built.
   1960 	#
   1961 	if ${done_rebuildmake}; then
   1962 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
   1963 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
   1964 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
   1965 		make="${TOOLDIR}/bin/${toolprefix}make"
   1966 		statusmsg "Created ${make}"
   1967 	fi
   1968 
   1969 	# Build a ${toolprefix}make wrapper script, usable by hand as
   1970 	# well as by build.sh.
   1971 	#
   1972 	if [ -z "${makewrapper}" ]; then
   1973 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
   1974 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
   1975 	fi
   1976 
   1977 	${runcmd} rm -f "${makewrapper}"
   1978 	if [ "${runcmd}" = "echo" ]; then
   1979 		echo 'cat <<EOF >'${makewrapper}
   1980 		makewrapout=
   1981 	else
   1982 		makewrapout=">>\${makewrapper}"
   1983 	fi
   1984 
   1985 	case "${KSH_VERSION:-${SH_VERSION}}" in
   1986 	*PD\ KSH*|*MIRBSD\ KSH*)
   1987 		set +o braceexpand
   1988 		;;
   1989 	esac
   1990 
   1991 	eval cat <<EOF ${makewrapout}
   1992 #! ${HOST_SH}
   1993 # Set proper variables to allow easy "make" building of a NetBSD subtree.
   1994 # Generated from:  \$NetBSD: build.sh,v 1.362 2022/08/14 08:51:41 lukem Exp $
   1995 # with these arguments: ${_args}
   1996 #
   1997 
   1998 EOF
   1999 	{
   2000 		sorted_vars="$(for var in ${makeenv}; do echo "${var}" ; done \
   2001 			| sort -u )"
   2002 		for var in ${sorted_vars}; do
   2003 			eval val=\"\${${var}}\"
   2004 			eval is_set=\"\${${var}+set}\"
   2005 			if [ -z "${is_set}" ]; then
   2006 				echo "unset ${var}"
   2007 			else
   2008 				qval="$(shell_quote "${val}")"
   2009 				echo "${var}=${qval}; export ${var}"
   2010 			fi
   2011 		done
   2012 
   2013 		cat <<EOF
   2014 
   2015 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
   2016 EOF
   2017 	} | eval cat "${makewrapout}"
   2018 	[ "${runcmd}" = "echo" ] && echo EOF
   2019 	${runcmd} chmod +x "${makewrapper}"
   2020 	statusmsg2 "Updated makewrapper:" "${makewrapper}"
   2021 }
   2022 
   2023 make_in_dir()
   2024 {
   2025 	local dir="$1"
   2026 	local op="$2"
   2027 	${runcmd} cd "${dir}" ||
   2028 	    bomb "Failed to cd to \"${dir}\""
   2029 	${runcmd} "${makewrapper}" ${parallel} ${op} ||
   2030 	    bomb "Failed to make ${op} in \"${dir}\""
   2031 	${runcmd} cd "${TOP}" ||
   2032 	    bomb "Failed to cd back to \"${TOP}\""
   2033 }
   2034 
   2035 buildtools()
   2036 {
   2037 	if [ "${MKOBJDIRS}" != "no" ]; then
   2038 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
   2039 		    bomb "Failed to make obj-tools"
   2040 	fi
   2041 	if [ "${MKUPDATE}" = "no" ]; then
   2042 		make_in_dir tools cleandir
   2043 	fi
   2044 	make_in_dir tools build_install
   2045 	statusmsg "Tools built to ${TOOLDIR}"
   2046 }
   2047 
   2048 buildlibs()
   2049 {
   2050 	if [ "${MKOBJDIRS}" != "no" ]; then
   2051 		${runcmd} "${makewrapper}" ${parallel} obj ||
   2052 		    bomb "Failed to make obj"
   2053 	fi
   2054 	if [ "${MKUPDATE}" = "no" ]; then
   2055 		make_in_dir lib cleandir
   2056 	fi
   2057 	make_in_dir . do-distrib-dirs
   2058 	make_in_dir . includes
   2059 	make_in_dir . do-lib
   2060 	statusmsg "libs built"
   2061 }
   2062 
   2063 getkernelconf()
   2064 {
   2065 	kernelconf="$1"
   2066 	if [ "${MKOBJDIRS}" != "no" ]; then
   2067 		# The correct value of KERNOBJDIR might
   2068 		# depend on a prior "make obj" in
   2069 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
   2070 		#
   2071 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
   2072 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
   2073 		make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
   2074 	fi
   2075 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
   2076 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
   2077 	case "${kernelconf}" in
   2078 	*/*)
   2079 		kernelconfpath="${kernelconf}"
   2080 		kernelconfname="${kernelconf##*/}"
   2081 		;;
   2082 	*)
   2083 		kernelconfpath="${KERNCONFDIR}/${kernelconf}"
   2084 		kernelconfname="${kernelconf}"
   2085 		;;
   2086 	esac
   2087 	kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
   2088 }
   2089 
   2090 diskimage()
   2091 {
   2092 	ARG="$(echo $1 | tr '[:lower:]' '[:upper:]')"
   2093 	[ -f "${DESTDIR}/etc/mtree/set.base" ] ||
   2094 	    bomb "The release binaries must be built first"
   2095 	kerneldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
   2096 	kernel="${kerneldir}/netbsd-${ARG}.gz"
   2097 	[ -f "${kernel}" ] ||
   2098 	    bomb "The kernel ${kernel} must be built first"
   2099 	make_in_dir "${NETBSDSRCDIR}/etc" "smp_${1}"
   2100 }
   2101 
   2102 buildkernel()
   2103 {
   2104 	if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
   2105 		# Building tools every time we build a kernel is clearly
   2106 		# unnecessary.  We could try to figure out whether rebuilding
   2107 		# the tools is necessary this time, but it doesn't seem worth
   2108 		# the trouble.  Instead, we say it's the user's responsibility
   2109 		# to rebuild the tools if necessary.
   2110 		#
   2111 		statusmsg "Building kernel without building new tools"
   2112 		buildkernelwarned=true
   2113 	fi
   2114 	getkernelconf $1
   2115 	statusmsg2 "Building kernel:" "${kernelconf}"
   2116 	statusmsg2 "Build directory:" "${kernelbuildpath}"
   2117 	${runcmd} mkdir -p "${kernelbuildpath}" ||
   2118 	    bomb "Cannot mkdir: ${kernelbuildpath}"
   2119 	if [ "${MKUPDATE}" = "no" ]; then
   2120 		make_in_dir "${kernelbuildpath}" cleandir
   2121 	fi
   2122 	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
   2123 	|| bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
   2124 	CONFIGOPTS=$(getmakevar CONFIGOPTS)
   2125 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" ${CONFIGOPTS} \
   2126 		-b "${kernelbuildpath}" -s "${TOP}/sys" ${configopts} \
   2127 		"${kernelconfpath}" ||
   2128 	    bomb "${toolprefix}config failed for ${kernelconf}"
   2129 	make_in_dir "${kernelbuildpath}" depend
   2130 	make_in_dir "${kernelbuildpath}" all
   2131 
   2132 	if [ "${runcmd}" != "echo" ]; then
   2133 		statusmsg "Kernels built from ${kernelconf}:"
   2134 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   2135 		for kern in ${kernlist:-netbsd}; do
   2136 			[ -f "${kernelbuildpath}/${kern}" ] && \
   2137 			    echo "  ${kernelbuildpath}/${kern}"
   2138 		done | tee -a "${results}"
   2139 	fi
   2140 }
   2141 
   2142 releasekernel()
   2143 {
   2144 	getkernelconf $1
   2145 	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
   2146 	${runcmd} mkdir -p "${kernelreldir}"
   2147 	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   2148 	for kern in ${kernlist:-netbsd}; do
   2149 		builtkern="${kernelbuildpath}/${kern}"
   2150 		[ -f "${builtkern}" ] || continue
   2151 		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
   2152 		statusmsg2 "Kernel copy:" "${releasekern}"
   2153 		if [ "${runcmd}" = "echo" ]; then
   2154 			echo "gzip -c -9 < ${builtkern} > ${releasekern}"
   2155 		else
   2156 			gzip -c -9 < "${builtkern}" > "${releasekern}"
   2157 		fi
   2158 	done
   2159 }
   2160 
   2161 buildkernels()
   2162 {
   2163 	allkernels=$( runcmd= make_in_dir etc '-V ${ALL_KERNELS}' )
   2164 	for k in $allkernels; do
   2165 		buildkernel "${k}"
   2166 	done
   2167 }
   2168 
   2169 buildmodules()
   2170 {
   2171 	setmakeenv MKBINUTILS no
   2172 	if ! ${do_tools} && ! ${buildmoduleswarned:-false}; then
   2173 		# Building tools every time we build modules is clearly
   2174 		# unnecessary as well as a kernel.
   2175 		#
   2176 		statusmsg "Building modules without building new tools"
   2177 		buildmoduleswarned=true
   2178 	fi
   2179 
   2180 	statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
   2181 	if [ "${MKOBJDIRS}" != "no" ]; then
   2182 		make_in_dir sys/modules obj
   2183 	fi
   2184 	if [ "${MKUPDATE}" = "no" ]; then
   2185 		make_in_dir sys/modules cleandir
   2186 	fi
   2187 	make_in_dir sys/modules dependall
   2188 	make_in_dir sys/modules install
   2189 
   2190 	statusmsg "Successful build of kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
   2191 }
   2192 
   2193 builddtb()
   2194 {
   2195 	statusmsg "Building devicetree blobs for NetBSD/${MACHINE} ${DISTRIBVER}"
   2196 	if [ "${MKOBJDIRS}" != "no" ]; then
   2197 		make_in_dir sys/dtb obj
   2198 	fi
   2199 	if [ "${MKUPDATE}" = "no" ]; then
   2200 		make_in_dir sys/dtb cleandir
   2201 	fi
   2202 	make_in_dir sys/dtb dependall
   2203 	make_in_dir sys/dtb install
   2204 
   2205 	statusmsg "Successful build of devicetree blobs for NetBSD/${MACHINE} ${DISTRIBVER}"
   2206 }
   2207 
   2208 installmodules()
   2209 {
   2210 	dir="$1"
   2211 	${runcmd} "${makewrapper}" INSTALLMODULESDIR="${dir}" installmodules ||
   2212 	    bomb "Failed to make installmodules to ${dir}"
   2213 	statusmsg "Successful installmodules to ${dir}"
   2214 }
   2215 
   2216 installworld()
   2217 {
   2218 	dir="$1"
   2219 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
   2220 	    bomb "Failed to make installworld to ${dir}"
   2221 	statusmsg "Successful installworld to ${dir}"
   2222 }
   2223 
   2224 # Run rump build&link tests.
   2225 #
   2226 # To make this feasible for running without having to install includes and
   2227 # libraries into destdir (i.e. quick), we only run ld.  This is possible
   2228 # since the rump kernel is a closed namespace apart from calls to rumpuser.
   2229 # Therefore, if ld complains only about rumpuser symbols, rump kernel
   2230 # linking was successful.
   2231 #
   2232 # We test that rump links with a number of component configurations.
   2233 # These attempt to mimic what is encountered in the full build.
   2234 # See list below.  The list should probably be either autogenerated
   2235 # or managed elsewhere; keep it here until a better idea arises.
   2236 #
   2237 # Above all, note that THIS IS NOT A SUBSTITUTE FOR A FULL BUILD.
   2238 #
   2239 
   2240 RUMP_LIBSETS='
   2241 	-lrumpvfs_nofifofs -lrumpvfs -lrump,
   2242 	-lrumpvfs_nofifofs -lrumpvfs -lrumpdev -lrump,
   2243 	-lrumpvfs_nofifofs -lrumpvfs
   2244 	-lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet -lrump,
   2245 	-lrumpkern_tty -lrumpvfs_nofifofs -lrumpvfs -lrump,
   2246 	-lrumpfs_tmpfs -lrumpvfs_nofifofs -lrumpvfs -lrump,
   2247 	-lrumpfs_ffs -lrumpfs_msdos -lrumpvfs_nofifofs -lrumpvfs -lrumpdev_disk -lrumpdev -lrump,
   2248 	-lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet
   2249 	    -lrumpdev -lrumpvfs_nofifofs -lrumpvfs -lrump,
   2250 	-lrumpnet_sockin -lrumpfs_nfs
   2251 	-lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet
   2252 	-lrumpvfs_nofifofs -lrumpvfs -lrump,
   2253 	-lrumpdev_cgd -lrumpdev_raidframe -lrumpdev_disk -lrumpdev_rnd
   2254 	    -lrumpdev_dm -lrumpdev -lrumpvfs_nofifofs -lrumpvfs -lrumpkern_crypto -lrump'
   2255 dorump()
   2256 {
   2257 	local doclean=""
   2258 	local doobjs=""
   2259 
   2260 	export RUMPKERN_ONLY=1
   2261 	# create obj and distrib dirs
   2262 	if [ "${MKOBJDIRS}" != "no" ]; then
   2263 		make_in_dir "${NETBSDSRCDIR}/etc/mtree" obj
   2264 		make_in_dir "${NETBSDSRCDIR}/sys/rump" obj
   2265 	fi
   2266 	${runcmd} "${makewrapper}" ${parallel} do-distrib-dirs \
   2267 	    || bomb 'could not create distrib-dirs'
   2268 
   2269 	[ "${MKUPDATE}" = "no" ] && doclean="cleandir"
   2270 	targlist="${doclean} ${doobjs} dependall install"
   2271 	# optimize: for test we build only static libs (3x test speedup)
   2272 	if [ "${1}" = "rumptest" ] ; then
   2273 		setmakeenv NOPIC 1
   2274 		setmakeenv NOPROFILE 1
   2275 	fi
   2276 	for cmd in ${targlist} ; do
   2277 		make_in_dir "${NETBSDSRCDIR}/sys/rump" ${cmd}
   2278 	done
   2279 
   2280 	# if we just wanted to build & install rump, we're done
   2281 	[ "${1}" != "rumptest" ] && return
   2282 
   2283 	${runcmd} cd "${NETBSDSRCDIR}/sys/rump/librump/rumpkern" \
   2284 	    || bomb "cd to rumpkern failed"
   2285 	md_quirks=`${runcmd} "${makewrapper}" -V '${_SYMQUIRK}'`
   2286 	# one little, two little, three little backslashes ...
   2287 	md_quirks="$(echo ${md_quirks} | sed 's,\\,\\\\,g'";s/'//g" )"
   2288 	${runcmd} cd "${TOP}" || bomb "cd to ${TOP} failed"
   2289 	tool_ld=`${runcmd} "${makewrapper}" -V '${LD}'`
   2290 
   2291 	local oIFS="${IFS}"
   2292 	IFS=","
   2293 	for set in ${RUMP_LIBSETS} ; do
   2294 		IFS="${oIFS}"
   2295 		${runcmd} ${tool_ld} -nostdlib -L${DESTDIR}/usr/lib	\
   2296 		    -static --whole-archive -lpthread -lc ${set} 2>&1 -o /tmp/rumptest.$$ | \
   2297 		      awk -v quirks="${md_quirks}" '
   2298 			/undefined reference/ &&
   2299 			    !/more undefined references.*follow/{
   2300 				if (match($NF,
   2301 				    "`(rumpuser_|rumpcomp_|__" quirks ")") == 0)
   2302 					fails[NR] = $0
   2303 			}
   2304 			/cannot find -l/{fails[NR] = $0}
   2305 			/cannot open output file/{fails[NR] = $0}
   2306 			END{
   2307 				for (x in fails)
   2308 					print fails[x]
   2309 				exit x!=0
   2310 			}'
   2311 		[ $? -ne 0 ] && bomb "Testlink of rump failed: ${set}"
   2312 	done
   2313 	statusmsg "Rump build&link tests successful"
   2314 }
   2315 
   2316 repro_date() {
   2317 	# try the bsd date fail back the linux one
   2318 	date -u -r "$1" 2> /dev/null || date -u -d "@$1"
   2319 }
   2320 
   2321 setup_mkrepro()
   2322 {
   2323 	local quiet="$1"
   2324 
   2325 	if [ ${MKREPRO-no} != "yes" ]; then
   2326 		return
   2327 	fi
   2328 	if [ ${MKREPRO_TIMESTAMP-0} -ne 0 ]; then
   2329 		return;
   2330 	fi
   2331 
   2332 	local dirs=${NETBSDSRCDIR-/usr/src}/
   2333 	if [ ${MKX11-no} = "yes" ]; then
   2334 		dirs="$dirs ${X11SRCDIR-/usr/xsrc}/"
   2335 	fi
   2336 
   2337 	local cvslatest=$(print_tooldir_program cvslatest)
   2338 	if [ ! -x "${cvslatest}" ]; then
   2339 		buildtools
   2340 	fi
   2341 
   2342 	local cvslatestflags=
   2343 	if ${do_expertmode}; then
   2344 		cvslatestflags=-i
   2345 	fi
   2346 
   2347 	MKREPRO_TIMESTAMP=0
   2348 	local d
   2349 	local t
   2350 	local vcs
   2351 	for d in ${dirs}; do
   2352 		if [ -d "${d}CVS" ]; then
   2353 			t=$("${cvslatest}" ${cvslatestflags} "${d}")
   2354 			vcs=cvs
   2355 		elif [ -d "${d}.git" ]; then
   2356 			t=$(cd "${d}" && git log -1 --format=%ct)
   2357 			vcs=git
   2358 		elif [ -d "${d}.hg" ]; then
   2359 			t=$(hg --repo "$d" log -r . --template '{date.unixtime}\n')
   2360 			vcs=hg
   2361 		elif [ -f "${d}.hg_archival.txt" ]; then
   2362 			local stat=$(print_tooldir_program stat)
   2363 			t=$("${stat}" -t '%s' -f '%m' "${d}.hg_archival.txt")
   2364 			vcs=hg
   2365 		else
   2366 			bomb "Cannot determine VCS for '$d'"
   2367 		fi
   2368 
   2369 		if [ -z "$t" ]; then
   2370 			bomb "Failed to get timestamp for vcs=$vcs in '$d'"
   2371 		fi
   2372 
   2373 		#echo "latest $d $vcs $t"
   2374 		if [ "$t" -gt "$MKREPRO_TIMESTAMP" ]; then
   2375 			MKREPRO_TIMESTAMP="$t"
   2376 		fi
   2377 	done
   2378 
   2379 	[ "${MKREPRO_TIMESTAMP}" != "0" ] || bomb "Failed to compute timestamp"
   2380 	if [ -z "${quiet}" ]; then
   2381 		statusmsg2 "MKREPRO_TIMESTAMP" \
   2382 			"$(repro_date "${MKREPRO_TIMESTAMP}")"
   2383 	fi
   2384 	export MKREPRO MKREPRO_TIMESTAMP
   2385 }
   2386 
   2387 main()
   2388 {
   2389 	initdefaults
   2390 	_args=$@
   2391 	parseoptions "$@"
   2392 
   2393 	sanitycheck
   2394 
   2395 	build_start=$(date)
   2396 	statusmsg2 "${progname} command:" "$0 $*"
   2397 	statusmsg2 "${progname} started:" "${build_start}"
   2398 	statusmsg2 "NetBSD version:"   "${DISTRIBVER}"
   2399 	statusmsg2 "MACHINE:"          "${MACHINE}"
   2400 	statusmsg2 "MACHINE_ARCH:"     "${MACHINE_ARCH}"
   2401 	statusmsg2 "Build platform:"   "${uname_s} ${uname_r} ${uname_m}"
   2402 	statusmsg2 "HOST_SH:"          "${HOST_SH}"
   2403 	if [ -n "${BUILDID}" ]; then
   2404 		statusmsg2 "BUILDID:"  "${BUILDID}"
   2405 	fi
   2406 	if [ -n "${BUILDINFO}" ]; then
   2407 		printf "%b\n" "${BUILDINFO}" | \
   2408 		while read -r line ; do
   2409 			[ -s "${line}" ] && continue
   2410 			statusmsg2 "BUILDINFO:"  "${line}"
   2411 		done
   2412 	fi
   2413 
   2414 	rebuildmake
   2415 	validatemakeparams
   2416 	createmakewrapper
   2417 	setup_mkrepro
   2418 
   2419 	# Perform the operations.
   2420 	#
   2421 	for op in ${operations}; do
   2422 		case "${op}" in
   2423 
   2424 		makewrapper)
   2425 			# no-op
   2426 			;;
   2427 
   2428 		tools)
   2429 			buildtools
   2430 			;;
   2431 		libs)
   2432 			buildlibs
   2433 			;;
   2434 
   2435 		sets)
   2436 			statusmsg "Building sets from pre-populated ${DESTDIR}"
   2437 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   2438 			    bomb "Failed to make ${op}"
   2439 			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
   2440 			statusmsg "Built sets to ${setdir}"
   2441 			;;
   2442 
   2443 		build|distribution|release)
   2444 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   2445 			    bomb "Failed to make ${op}"
   2446 			statusmsg "Successful make ${op}"
   2447 			;;
   2448 
   2449 		cleandir|obj|sourcesets|syspkgs|params)
   2450 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   2451 			    bomb "Failed to make ${op}"
   2452 			statusmsg "Successful make ${op}"
   2453 			;;
   2454 
   2455 		iso-image|iso-image-source)
   2456 			${runcmd} "${makewrapper}" ${parallel} \
   2457 			    CDEXTRA="$CDEXTRA" ${op} ||
   2458 			    bomb "Failed to make ${op}"
   2459 			statusmsg "Successful make ${op}"
   2460 			;;
   2461 
   2462 		live-image|install-image)
   2463 			# install-image and live-image require mtree spec files
   2464 			# built with UNPRIVED.  Assume UNPRIVED build has been
   2465 			# performed if METALOG file is created in DESTDIR.
   2466 			if [ ! -e "${DESTDIR}/METALOG" ] ; then
   2467 				bomb "The release binaries must have been built with -U to create images."
   2468 			fi
   2469 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   2470 			    bomb "Failed to make ${op}"
   2471 			statusmsg "Successful make ${op}"
   2472 			;;
   2473 		kernel=*)
   2474 			arg=${op#*=}
   2475 			buildkernel "${arg}"
   2476 			;;
   2477 		kernel.gdb=*)
   2478 			arg=${op#*=}
   2479 			configopts="-D DEBUG=-g"
   2480 			buildkernel "${arg}"
   2481 			;;
   2482 		releasekernel=*)
   2483 			arg=${op#*=}
   2484 			releasekernel "${arg}"
   2485 			;;
   2486 
   2487 		kernels)
   2488 			buildkernels
   2489 			;;
   2490 
   2491 		disk-image=*)
   2492 			arg=${op#*=}
   2493 			diskimage "${arg}"
   2494 			;;
   2495 
   2496 		dtb)
   2497 			builddtb
   2498 			;;
   2499 
   2500 		modules)
   2501 			buildmodules
   2502 			;;
   2503 
   2504 		installmodules=*)
   2505 			arg=${op#*=}
   2506 			if [ "${arg}" = "/" ] && \
   2507 			    (	[ "${uname_s}" != "NetBSD" ] || \
   2508 				[ "${uname_m}" != "${MACHINE}" ] ); then
   2509 				bomb "'${op}' must != / for cross builds."
   2510 			fi
   2511 			installmodules "${arg}"
   2512 			;;
   2513 
   2514 		install=*)
   2515 			arg=${op#*=}
   2516 			if [ "${arg}" = "/" ] && \
   2517 			    (	[ "${uname_s}" != "NetBSD" ] || \
   2518 				[ "${uname_m}" != "${MACHINE}" ] ); then
   2519 				bomb "'${op}' must != / for cross builds."
   2520 			fi
   2521 			installworld "${arg}"
   2522 			;;
   2523 
   2524 		rump)
   2525 			make_in_dir . do-distrib-dirs
   2526 			make_in_dir . includes
   2527 			make_in_dir lib/csu dependall
   2528 			make_in_dir lib/csu install
   2529 			make_in_dir external/gpl3/gcc/lib/libgcc dependall
   2530 			make_in_dir external/gpl3/gcc/lib/libgcc install
   2531 			dorump "${op}"
   2532 			;;
   2533 
   2534 		rumptest)
   2535 			dorump "${op}"
   2536 			;;
   2537 
   2538 		*)
   2539 			bomb "Unknown operation \`${op}'"
   2540 			;;
   2541 
   2542 		esac
   2543 	done
   2544 
   2545 	statusmsg2 "${progname} ended:" "$(date)"
   2546 	if [ -s "${results}" ]; then
   2547 		echo "===> Summary of results:"
   2548 		sed -e 's/^===>//;s/^/	/' "${results}"
   2549 		echo "===> ."
   2550 	fi
   2551 }
   2552 
   2553 main "$@"
   2554