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