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