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