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