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