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