Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.100
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.100 2003/05/10 07:12:37 lukem Exp $
      3 #
      4 # Copyright (c) 2001-2003 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.  For systems
     42 # with a strange /bin/sh, "ksh" or "bash" may be an ample alternative.
     43 #
     44 # Note, however, that due to the way the interpreter is invoked above,
     45 # if a POSIX-compliant shell is the first in the PATH, you won't have
     46 # to take any further action.
     47 #
     48 
     49 progname=${0##*/}
     50 toppid=$$
     51 results=/dev/null
     52 trap "exit 1" 1 2 3 15
     53 
     54 bomb()
     55 {
     56 	cat >&2 <<ERRORMESSAGE
     57 
     58 ERROR: $@
     59 *** BUILD ABORTED ***
     60 ERRORMESSAGE
     61 	kill ${toppid}		# in case we were invoked from a subshell
     62 	exit 1
     63 }
     64 
     65 
     66 statusmsg()
     67 {
     68 	${runcmd} echo "===> $@" | tee -a "${results}"
     69 }
     70 
     71 initdefaults()
     72 {
     73 	cd "$(dirname $0)"
     74 	[ -d usr.bin/make ] ||
     75 	    bomb "build.sh must be run from the top source level"
     76 	[ -f share/mk/bsd.own.mk ] ||
     77 	    bomb "src/share/mk is missing; please re-fetch the source tree"
     78 
     79 	uname_s=$(uname -s 2>/dev/null)
     80 	uname_m=$(uname -m 2>/dev/null)
     81 
     82 	# If $PWD is a valid name of the current directory, POSIX mandates
     83 	# that pwd return it by default which causes problems in the
     84 	# presence of symlinks.  Unsetting PWD is simpler than changing
     85 	# every occurrence of pwd to use -P.
     86 	#
     87 	# XXX Except that doesn't work on Solaris.
     88 	#
     89 	unset PWD
     90 	if [ "${uname_s}" = "SunOS" ]; then
     91 		TOP=$(pwd -P)
     92 	else
     93 		TOP=$(pwd)
     94 	fi
     95 
     96 	# Set defaults.
     97 	#
     98 	toolprefix=nb
     99 
    100 	# Some systems have a small ARG_MAX.  -X prevents make(1) from
    101 	# exporting variables in the environment redundantly.
    102 	#
    103 	case "${uname_s}" in
    104 	Darwin | FreeBSD | CYGWIN*)
    105 		MAKEFLAGS=-X
    106 		;;
    107 	*)
    108 		MAKEFLAGS=
    109 		;;
    110 	esac
    111 
    112 	makeenv=
    113 	makewrapper=
    114 	runcmd=
    115 	operations=
    116 	removedirs=
    117 	do_expertmode=false
    118 	do_rebuildmake=false
    119 	do_removedirs=false
    120 
    121 	# do_{operation}=true if given operation is requested.
    122 	#
    123 	do_tools=false
    124 	do_obj=false
    125 	do_build=false
    126 	do_distribution=false
    127 	do_release=false
    128 	do_kernel=false
    129 	do_install=false
    130 	do_sets=false
    131 	do_sourcesets=false
    132 
    133 	# Create scratch directory
    134 	#
    135 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    136 	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
    137 	trap "cd /; rm -r -f \"${tmpdir}\"" 0
    138 	results="${tmpdir}/build.sh.results"
    139 }
    140 
    141 getarch()
    142 {
    143 	# Translate a MACHINE into a default MACHINE_ARCH.
    144 	#
    145 	case "${MACHINE}" in
    146 
    147 	acorn26|acorn32|cats|evbarm|hpcarm|netwinder|shark)
    148 		MACHINE_ARCH=arm
    149 		;;
    150 
    151 	hp700)
    152 		MACHINE_ARCH=hppa
    153 		;;
    154 
    155 	sun2)
    156 		MACHINE_ARCH=m68000
    157 		;;
    158 
    159 	amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
    160 		MACHINE_ARCH=m68k
    161 		;;
    162 
    163 	mipsco|newsmips|sbmips|sgimips)
    164 		MACHINE_ARCH=mipseb
    165 		;;
    166 
    167 	algor|arc|cobalt|evbmips|hpcmips|playstation2|pmax)
    168 		MACHINE_ARCH=mipsel
    169 		;;
    170 
    171 
    172 	pc532)
    173 		MACHINE_ARCH=ns32k
    174 		;;
    175 
    176 	amigappc|bebox|evbppc|macppc|mvmeppc|ofppc|pmppc|prep|sandpoint)
    177 		MACHINE_ARCH=powerpc
    178 		;;
    179 
    180 	evbsh3|mmeye)
    181 		MACHINE_ARCH=sh3eb
    182 		;;
    183 
    184 	dreamcast|hpcsh)
    185 		MACHINE_ARCH=sh3el
    186 		;;
    187 
    188 	evbsh5)
    189 		MACHINE_ARCH=sh5el
    190 		;;
    191 	amd64)
    192 		MACHINE_ARCH=x86_64
    193 		;;
    194 
    195 	alpha|i386|sparc|sparc64|vax)
    196 		MACHINE_ARCH=${MACHINE}
    197 		;;
    198 
    199 	*)
    200 		bomb "Unknown target MACHINE: ${MACHINE}"
    201 		;;
    202 
    203 	esac
    204 }
    205 
    206 validatearch()
    207 {
    208 	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
    209 	#
    210 	case "${MACHINE_ARCH}" in
    211 
    212 	alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|ns32k|powerpc|sh[35]e[bl]|sparc|sparc64|vax|x86_64)
    213 		;;
    214 
    215 	*)
    216 		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
    217 		;;
    218 
    219 	esac
    220 
    221 	# Determine valid MACHINE_ARCHs for MACHINE
    222 	#
    223 	case "${MACHINE}" in
    224 
    225 	evbarm)
    226 		arches="arm armeb"
    227 		;;
    228 
    229 	evbmips|sbmips)
    230 		arches="mipseb mipsel"
    231 		;;
    232 
    233 	evbsh3)
    234 		arches="sh3eb sh3el"
    235 		;;
    236 
    237 	evbsh5)
    238 		arches="sh5eb sh5el"
    239 		;;
    240 
    241 	*)
    242 		oma="${MACHINE_ARCH}"
    243 		getarch
    244 		arches="${MACHINE_ARCH}"
    245 		MACHINE_ARCH="${oma}"
    246 		;;
    247 
    248 	esac
    249 
    250 	# Ensure that MACHINE_ARCH supports MACHINE
    251 	#
    252 	archok=false
    253 	for a in ${arches}; do
    254 		if [ "${a}" = "${MACHINE_ARCH}" ]; then
    255 			archok=true
    256 			break
    257 		fi
    258 	done
    259 	${archok} ||
    260 	    bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
    261 }
    262 
    263 raw_getmakevar()
    264 {
    265 	[ -x "${make}" ] || bomb "raw_getmakevar $1: ${make} is not executable"
    266 	"${make}" -m ${TOP}/share/mk -s -f- _x_ <<EOF
    267 _x_:
    268 	echo \${$1}
    269 .include <bsd.prog.mk>
    270 .include <bsd.kernobj.mk>
    271 EOF
    272 }
    273 
    274 getmakevar()
    275 {
    276 	# raw_getmakevar() doesn't work properly if $make hasn't yet been
    277 	# built, which can happen when running with the "-n" option.
    278 	# getmakevar() deals with this by emitting a literal '$'
    279 	# followed by the variable name, instead of trying to find the
    280 	# variable's value.
    281 	#
    282 	if [ -x "${make}" ]; then
    283 		raw_getmakevar "$1"
    284 	else
    285 		echo "\$$1"
    286 	fi
    287 }
    288 
    289 resolvepath()
    290 {
    291 	case "${OPTARG}" in
    292 	/*)
    293 		;;
    294 	*)
    295 		OPTARG="${TOP}/${OPTARG}"
    296 		;;
    297 	esac
    298 }
    299 
    300 usage()
    301 {
    302 	if [ -n "$*" ]; then
    303 		echo ""
    304 		echo "${progname}: $*"
    305 	fi
    306 	cat <<_usage_
    307 
    308 Usage: ${progname} [-EnorUu] [-a arch] [-B buildid] [-D dest] [-j njob] [-M obj]
    309                 [-m mach] [-O obj] [-R release] [-T tools] [-V var=[value]]
    310                 [-w wrapper]   operation [...]
    311 
    312  Build operations (all imply "obj" and "tools"):
    313     build               Run "make build"
    314     distribution        Run "make distribution" (includes DESTDIR/etc/ files)
    315     release             Run "make release" (includes kernels & distrib media)
    316 
    317  Other operations:
    318     help                Show this message (and exit)
    319     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
    320                         (Always done)
    321     obj                 Run "make obj" (default unless -o is used)
    322     tools               Build and install tools
    323     kernel=conf         Build kernel with config file \`conf'
    324     install=idir        Run "make installworld" to \`idir'
    325                         (useful after 'distribution' or 'release')
    326     sets                Create binary sets in RELEASEDIR/MACHINE/binary/sets
    327     sourcesets          Create source sets in RELEASEDIR/source/sets
    328 
    329  Options:
    330     -a arch     Set MACHINE_ARCH to arch (otherwise deduced from MACHINE)
    331     -B buildId  Set BUILDID to buildId
    332     -D dest     Set DESTDIR to dest
    333     -E          Set "expert" mode; disables various safety checks.
    334                 Should not be used without expert knowledge of the build system
    335     -j njob     Run up to njob jobs in parallel; see make(1)
    336     -M obj      Set obj root directory to obj (sets MAKEOBJDIRPREFIX)
    337     -m mach     Set MACHINE to mach (not required if NetBSD native)
    338     -n          Show commands that would be executed, but do not execute them
    339     -O obj      Set obj root directory to obj (sets a MAKEOBJDIR pattern)
    340     -o          Set MKOBJDIRS=no (do not create objdirs at start of build)
    341     -R release  Set RELEASEDIR to release
    342     -r          Remove contents of TOOLDIR and DESTDIR before building
    343     -T tools    Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
    344                 the environment, ${toolprefix}make will be (re)built unconditionally
    345     -U          Set UNPRIVED (build without requiring root privileges)
    346     -u          Set UPDATE (do not run "make clean" first).
    347 		Without this, everything is rebuilt, including the tools.
    348     -V v=[val]  Set variable \`v' to \`val'
    349     -w wrapper  Create ${toolprefix}make script as wrapper
    350                 (Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE})
    351 
    352 _usage_
    353 	exit 1
    354 }
    355 
    356 parseoptions()
    357 {
    358 	opts='a:B:bD:dEhi:j:k:M:m:nO:oR:rT:tUuV:w:'
    359 	opt_a=no
    360 
    361 	if type getopts >/dev/null 2>&1; then
    362 		# Use POSIX getopts.
    363 		#
    364 		getoptcmd='getopts ${opts} opt && opt=-${opt}'
    365 		optargcmd=':'
    366 		optremcmd='shift $((${OPTIND} -1))'
    367 	else
    368 		type getopt >/dev/null 2>&1 ||
    369 		    bomb "/bin/sh shell is too old; try ksh or bash"
    370 
    371 		# Use old-style getopt(1) (doesn't handle whitespace in args).
    372 		#
    373 		args="$(getopt ${opts} $*)"
    374 		[ $? = 0 ] || usage
    375 		set -- ${args}
    376 
    377 		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    378 		optargcmd='OPTARG="$1"; shift'
    379 		optremcmd=':'
    380 	fi
    381 
    382 	# Parse command line options.
    383 	#
    384 	while eval ${getoptcmd}; do
    385 		case ${opt} in
    386 
    387 		-a)
    388 			eval ${optargcmd}
    389 			MACHINE_ARCH=${OPTARG}
    390 			opt_a=yes
    391 			;;
    392 
    393 		-B)
    394 			eval ${optargcmd}
    395 			BUILDID=${OPTARG}
    396 			;;
    397 
    398 		-b)
    399 			usage "'-b' has been replaced by 'makewrapper'"
    400 			;;
    401 
    402 		-D)
    403 			eval ${optargcmd}; resolvepath
    404 			DESTDIR="${OPTARG}"
    405 			export DESTDIR
    406 			makeenv="${makeenv} DESTDIR"
    407 			;;
    408 
    409 		-d)
    410 			usage "'-d' has been replaced by 'distribution'"
    411 			;;
    412 
    413 		-E)
    414 			do_expertmode=true
    415 			;;
    416 
    417 		-i)
    418 			usage "'-i idir' has been replaced by 'install=idir'"
    419 			;;
    420 
    421 		-j)
    422 			eval ${optargcmd}
    423 			parallel="-j ${OPTARG}"
    424 			;;
    425 
    426 		-k)
    427 			usage "'-k conf' has been replaced by 'kernel=conf'"
    428 			;;
    429 
    430 		-M)
    431 			eval ${optargcmd}; resolvepath
    432 			MAKEOBJDIRPREFIX="${OPTARG}"
    433 			export MAKEOBJDIRPREFIX
    434 			makeobjdir="${OPTARG}"
    435 			makeenv="${makeenv} MAKEOBJDIRPREFIX"
    436 			;;
    437 
    438 			# -m overrides MACHINE_ARCH unless "-a" is specified
    439 		-m)
    440 			eval ${optargcmd}
    441 			MACHINE="${OPTARG}"
    442 			[ "${opt_a}" != "yes" ] && getarch
    443 			;;
    444 
    445 		-n)
    446 			runcmd=echo
    447 			;;
    448 
    449 		-O)
    450 			eval ${optargcmd}; resolvepath
    451 			MAKEOBJDIR="\${.CURDIR:C,^$TOP,$OPTARG,}"
    452 			export MAKEOBJDIR
    453 			makeobjdir="${OPTARG}"
    454 			makeenv="${makeenv} MAKEOBJDIR"
    455 			;;
    456 
    457 		-o)
    458 			MKOBJDIRS=no
    459 			;;
    460 
    461 		-R)
    462 			eval ${optargcmd}; resolvepath
    463 			RELEASEDIR="${OPTARG}"
    464 			export RELEASEDIR
    465 			makeenv="${makeenv} RELEASEDIR"
    466 			;;
    467 
    468 		-r)
    469 			do_removedirs=true
    470 			do_rebuildmake=true
    471 			;;
    472 
    473 		-T)
    474 			eval ${optargcmd}; resolvepath
    475 			TOOLDIR="${OPTARG}"
    476 			export TOOLDIR
    477 			;;
    478 
    479 		-t)
    480 			usage "'-t' has been replaced by 'tools'"
    481 			;;
    482 
    483 		-U)
    484 			UNPRIVED=yes
    485 			export UNPRIVED
    486 			makeenv="${makeenv} UNPRIVED"
    487 			;;
    488 
    489 		-u)
    490 			UPDATE=yes
    491 			export UPDATE
    492 			makeenv="${makeenv} UPDATE"
    493 			;;
    494 
    495 		-V)
    496 			eval ${optargcmd}
    497 			case "${OPTARG}" in
    498 		    # XXX: consider restricting which variables can be changed?
    499 			[a-zA-Z_][a-zA-Z_0-9]*=*)
    500 				var=${OPTARG%%=*}
    501 				value=${OPTARG#*=}
    502 				eval "${var}=\"${value}\"; export ${var}"
    503 				makeenv="${makeenv} ${var}"
    504 				;;
    505 			*)
    506 				usage "-V argument must be of the form 'var=[value]'"
    507 				;;
    508 			esac
    509 			;;
    510 
    511 		-w)
    512 			eval ${optargcmd}; resolvepath
    513 			makewrapper="${OPTARG}"
    514 			;;
    515 
    516 		--)
    517 			break
    518 			;;
    519 
    520 		-'?'|-h)
    521 			usage
    522 			;;
    523 
    524 		esac
    525 	done
    526 
    527 	# Validate operations.
    528 	#
    529 	eval ${optremcmd}
    530 	while [ $# -gt 0 ]; do
    531 		op=$1; shift
    532 		operations="${operations} ${op}"
    533 
    534 		case "${op}" in
    535 
    536 		help)
    537 			usage
    538 			;;
    539 
    540 		makewrapper|obj|tools|build|distribution|release|sets|sourcesets)
    541 			;;
    542 
    543 		kernel=*)
    544 			arg=${op#*=}
    545 			op=${op%%=*}
    546 			[ -n "${arg}" ] ||
    547 			    bomb "Must supply a kernel name with \`kernel=...'"
    548 			;;
    549 
    550 		install=*)
    551 			arg=${op#*=}
    552 			op=${op%%=*}
    553 			[ -n "${arg}" ] ||
    554 			    bomb "Must supply a directory with \`install=...'"
    555 			;;
    556 
    557 		*)
    558 			usage "Unknown operation \`${op}'"
    559 			;;
    560 
    561 		esac
    562 		eval do_${op}=true
    563 	done
    564 	[ -n "${operations}" ] || usage "Missing operation to perform."
    565 
    566 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
    567 	#
    568 	if [ -z "${MACHINE}" ]; then
    569 		[ "${uname_s}" = "NetBSD" ] ||
    570 		    bomb "MACHINE must be set, or -m must be used, for cross builds."
    571 		MACHINE=${uname_m}
    572 	fi
    573 	[ -n "${MACHINE_ARCH}" ] || getarch
    574 	validatearch
    575 
    576 	# Set up default make(1) environment.
    577 	#
    578 	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
    579 	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
    580 	MAKEFLAGS="-m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
    581 	export MAKEFLAGS MACHINE MACHINE_ARCH
    582 }
    583 
    584 rebuildmake()
    585 {
    586 	# Test make source file timestamps against installed ${toolprefix}make
    587 	# binary, if TOOLDIR is pre-set.
    588 	#
    589 	# Note that we do NOT try to grovel "mk.conf" here to find out if
    590 	# TOOLDIR is set there, because it can contain make variable
    591 	# expansions and other stuff only parseable *after* we have a working
    592 	# ${toolprefix}make.  So this logic can only work if the user has
    593 	# pre-set TOOLDIR in the environment or used the -T option to build.sh.
    594 	#
    595 	make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
    596 	if [ -x "${make}" ]; then
    597 		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
    598 			if [ "${f}" -nt "${make}" ]; then
    599 				do_rebuildmake=true
    600 				break
    601 			fi
    602 		done
    603 	else
    604 		do_rebuildmake=true
    605 	fi
    606 
    607 	# Build bootstrap ${toolprefix}make if needed.
    608 	if ${do_rebuildmake}; then
    609 		statusmsg "Bootstrapping ${toolprefix}make"
    610 		${runcmd} cd "${tmpdir}"
    611 		${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
    612 			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
    613 			"${TOP}/tools/make/configure" ||
    614 		    bomb "Configure of ${toolprefix}make failed"
    615 		${runcmd} sh buildmake.sh ||
    616 		    bomb "Build of ${toolprefix}make failed"
    617 		make="${tmpdir}/${toolprefix}make"
    618 		${runcmd} cd "${TOP}"
    619 		${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
    620 	fi
    621 }
    622 
    623 validatemakeparams()
    624 {
    625 	if [ "${runcmd}" = "echo" ]; then
    626 		TOOLCHAIN_MISSING=no
    627 		EXTERNAL_TOOLCHAIN=""
    628 	else
    629 		TOOLCHAIN_MISSING=$(raw_getmakevar TOOLCHAIN_MISSING)
    630 		EXTERNAL_TOOLCHAIN=$(raw_getmakevar EXTERNAL_TOOLCHAIN)
    631 	fi
    632 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
    633 	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
    634 		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
    635 		${runcmd} echo "	MACHINE:      ${MACHINE}"
    636 		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
    637 		${runcmd} echo ""
    638 		${runcmd} echo "All builds for this platform should be done via a traditional make"
    639 		${runcmd} echo "If you wish to use an external cross-toolchain, set"
    640 		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
    641 		${runcmd} echo "in either the environment or mk.conf and rerun"
    642 		${runcmd} echo "	${progname} $*"
    643 		exit 1
    644 	fi
    645 
    646 	# If TOOLDIR isn't already set, make objdirs in "tools" in case the
    647 	# default setting from <bsd.own.mk> is used.
    648 	#
    649 	if [ -z "${TOOLDIR}" ] && [ "${MKOBJDIRS}" != "no" ]; then
    650 		${runcmd} cd tools
    651 		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
    652 		    bomb "Failed to make obj in tools"
    653 		${runcmd} cd "${TOP}"
    654 	fi
    655 
    656 	# If setting -M or -O to root an obj dir make sure the base directory
    657 	# is made before continuing as bsd.own.mk will need this to pick up
    658 	# _SRC_TOP_OBJ_
    659 	#
    660 	if [ "${MKOBJDIRS}" != "no" ] && [ ! -z "${makeobjdir}" ]; then
    661 		${runcmd} mkdir -p "${makeobjdir}"
    662 	fi
    663 
    664 	statusmsg "MACHINE:         ${MACHINE}"
    665 	statusmsg "MACHINE_ARCH:    ${MACHINE_ARCH}"
    666 
    667 	# Find TOOLDIR, DESTDIR, and RELEASEDIR.
    668 	#
    669 	TOOLDIR=$(getmakevar TOOLDIR)
    670 	statusmsg "TOOLDIR path:    ${TOOLDIR}"
    671 	DESTDIR=$(getmakevar DESTDIR)
    672 	RELEASEDIR=$(getmakevar RELEASEDIR)
    673 	if ! $do_expertmode; then
    674 		_SRCTOPOBJ_=$(getmakevar _SRC_TOP_OBJ_)
    675 		: ${DESTDIR:=${_SRCTOPOBJ_}/destdir.${MACHINE}}
    676 		: ${RELEASEDIR:=${_SRCTOPOBJ_}/releasedir}
    677 		makeenv="${makeenv} DESTDIR RELEASEDIR"
    678 	fi
    679 	export TOOLDIR DESTDIR RELEASEDIR
    680 	statusmsg "DESTDIR path:    ${DESTDIR}"
    681 	statusmsg "RELEASEDIR path: ${RELEASEDIR}"
    682 
    683 	# Check validity of TOOLDIR and DESTDIR.
    684 	#
    685 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
    686 		bomb "TOOLDIR '${TOOLDIR}' invalid"
    687 	fi
    688 	removedirs="${TOOLDIR}"
    689 
    690 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
    691 		if ${do_build} || ${do_distribution} || ${do_release}; then
    692 			if ! ${do_build} || \
    693 			   [ "${uname_s}" != "NetBSD" ] || \
    694 			   [ "${uname_m}" != "${MACHINE}" ]; then
    695 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
    696 			fi
    697 			if ! ${do_expertmode}; then
    698 				bomb "DESTDIR must != / for non -E (expert) builds"
    699 			fi
    700 			statusmsg "WARNING: Building to /, in expert mode."
    701 			statusmsg "         This may cause your system to break!  Reasons include:"
    702 			statusmsg "            - your kernel is not up to date"
    703 			statusmsg "            - the libraries or toolchain have changed"
    704 			statusmsg "         YOU HAVE BEEN WARNED!"
    705 		fi
    706 	else
    707 		removedirs="${removedirs} ${DESTDIR}"
    708 	fi
    709 	if ${do_build} || ${do_distribution} || ${do_release}; then
    710 		if ! ${do_expertmode} && \
    711 		    [ $(id -u 2>/dev/null) -ne 0 ] &&\
    712 		    [ -z "${UNPRIVED}" ] ; then
    713 			bomb "-U or -E must be set for build as an unprivileged user."
    714 		fi
    715         fi
    716 }
    717 
    718 
    719 createmakewrapper()
    720 {
    721 	# Remove the target directories.
    722 	#
    723 	if ${do_removedirs}; then
    724 		for f in ${removedirs}; do
    725 			statusmsg "Removing ${f}"
    726 			${runcmd} rm -r -f "${f}"
    727 		done
    728 	fi
    729 
    730 	# Recreate $TOOLDIR.
    731 	#
    732 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
    733 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
    734 
    735 	# Install ${toolprefix}make if it was built.
    736 	#
    737 	if ${do_rebuildmake}; then
    738 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
    739 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
    740 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
    741 		make="${TOOLDIR}/bin/${toolprefix}make"
    742 		statusmsg "Created ${make}"
    743 	fi
    744 
    745 	# Build a ${toolprefix}make wrapper script, usable by hand as
    746 	# well as by build.sh.
    747 	#
    748 	if [ -z "${makewrapper}" ]; then
    749 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${MACHINE}"
    750 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
    751 	fi
    752 
    753 	${runcmd} rm -f "${makewrapper}"
    754 	if [ "${runcmd}" = "echo" ]; then
    755 		echo 'cat <<EOF >'${makewrapper}
    756 		makewrapout=
    757 	else
    758 		makewrapout=">>\${makewrapper}"
    759 	fi
    760 
    761 	eval cat <<EOF ${makewrapout}
    762 #! /bin/sh
    763 # Set proper variables to allow easy "make" building of a NetBSD subtree.
    764 # Generated from:  \$NetBSD: build.sh,v 1.100 2003/05/10 07:12:37 lukem Exp $
    765 #
    766 
    767 EOF
    768 	for f in ${makeenv}; do
    769 		eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}" ${makewrapout}
    770 	done
    771 	eval echo "USETOOLS=yes\; export USETOOLS" ${makewrapout}
    772 
    773 	eval cat <<EOF ${makewrapout}
    774 
    775 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
    776 EOF
    777 	[ "${runcmd}" = "echo" ] && echo EOF
    778 	${runcmd} chmod +x "${makewrapper}"
    779 	statusmsg "Updated ${makewrapper}"
    780 }
    781 
    782 buildtools()
    783 {
    784 	if [ "${MKOBJDIRS}" != "no" ]; then
    785 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
    786 		    bomb "Failed to make obj-tools"
    787 	fi
    788 	${runcmd} cd tools
    789 	if [ -z "${UPDATE}" ]; then
    790 		cleandir=cleandir
    791 	else
    792 		cleandir=
    793 	fi
    794 	${runcmd} "${makewrapper}" ${cleandir} dependall install ||
    795 	    bomb "Failed to make tools"
    796 	statusmsg "Tools built to ${TOOLDIR}"
    797 }
    798 
    799 buildkernel()
    800 {
    801 	kernconf="$1"
    802 	if ! ${do_tools}; then
    803 		# Building tools every time we build a kernel is clearly
    804 		# unnecessary.  We could try to figure out whether rebuilding
    805 		# the tools is necessary this time, but it doesn't seem worth
    806 		# the trouble.  Instead, we say it's the user's responsibility
    807 		# to rebuild the tools if necessary.
    808 		#
    809 		statusmsg "Building kernel without building new tools"
    810 	fi
    811 	statusmsg "Building kernel ${kernconf}"
    812 	if [ "${MKOBJDIRS}" != "no" ] && [ ! -z "${makeobjdir}" ]; then
    813 		# The correct value of KERNOBJDIR might
    814 		# depend on a prior "make obj" in
    815 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
    816 		#
    817 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
    818 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
    819 		${runcmd} cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
    820 		${runcmd} "${makewrapper}" obj ||
    821 		    bomb "Failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
    822 		${runcmd} cd "${TOP}"
    823 	fi
    824 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
    825 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
    826 	case "${kernconf}" in
    827 	*/*)
    828 		kernconfpath="${kernconf}"
    829 		kernconfbase="$(basename "${kernconf}")"
    830 		;;
    831 	*)
    832 		kernconfpath="${KERNCONFDIR}/${kernconf}"
    833 		kernconfbase="${kernconf}"
    834 		;;
    835 	esac
    836 	kernbuilddir="${KERNOBJDIR}/${kernconfbase}"
    837 	statusmsg "Kernel build directory: ${kernbuilddir}"
    838 	${runcmd} mkdir -p "${kernbuilddir}" ||
    839 	    bomb "Cannot mkdir: ${kernbuilddir}"
    840 	if [ -z "${UPDATE}" ]; then
    841 		${runcmd} cd "${kernbuilddir}"
    842 		${runcmd} "${makewrapper}" cleandir ||
    843 		    bomb "Failed to make cleandir in ${kernbuilddir}"
    844 		${runcmd} cd "${TOP}"
    845 	fi
    846 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernbuilddir}" \
    847 		-s "${TOP}/sys" "${kernconfpath}" ||
    848 	    bomb "${toolprefix}config failed for ${kernconf}"
    849 	${runcmd} cd "${kernbuilddir}"
    850 	${runcmd} "${makewrapper}" depend ||
    851 	    bomb "Failed to make depend in ${kernbuilddir}"
    852 	${runcmd} "${makewrapper}" ${parallel} all ||
    853 	    bomb "Failed to make all in ${kernbuilddir}"
    854 	${runcmd} cd "${TOP}"
    855 
    856 	if [ "${runcmd}" != "echo" ]; then
    857 		statusmsg "Kernels built from ${kernconf}:"
    858 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernconfpath})
    859 		for kern in ${kernlist:-netbsd}; do
    860 			[ -f "${kernbuilddir}/${kern}" ] && \
    861 			    echo "  ${kernbuilddir}/${kern}"
    862 		done | tee -a "${results}"
    863 	fi
    864 }
    865 
    866 installworld()
    867 {
    868 	dir="$1"
    869 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
    870 	    bomb "Failed to make installworld to ${dir}"
    871 	statusmsg "Successful installworld to ${dir}"
    872 }
    873 
    874 
    875 main()
    876 {
    877 	initdefaults
    878 	parseoptions "$@"
    879 
    880 	build_start=$(date)
    881 	statusmsg "${progname} command: $0 $@"
    882 	statusmsg "${progname} started: ${build_start}"
    883 
    884 	rebuildmake
    885 	validatemakeparams
    886 	createmakewrapper
    887 
    888 	# Perform the operations.
    889 	#
    890 	for op in ${operations}; do
    891 		case "${op}" in
    892 
    893 		makewrapper)
    894 			# no-op
    895 			;;
    896 
    897 		tools)
    898 			buildtools
    899 			;;
    900 
    901 		obj|build|distribution|release|sets|sourcesets)
    902 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
    903 			    bomb "Failed to make ${op}"
    904 			statusmsg "Successful make ${op}"
    905 			;;
    906 
    907 		kernel=*)
    908 			arg=${op#*=}
    909 			buildkernel "${arg}"
    910 			;;
    911 
    912 		install=*)
    913 			arg=${op#*=}
    914 			if [ "${arg}" = "/" ] && \
    915 			    (	[ "${uname_s}" != "NetBSD" ] || \
    916 				[ "${uname_m}" != "${MACHINE}" ] ); then
    917 				bomb "'${op}' must != / for cross builds."
    918 			fi
    919 			installworld "${arg}"
    920 			;;
    921 
    922 		*)
    923 			bomb "Unknown operation \`${op}'"
    924 			;;
    925 
    926 		esac
    927 	done
    928 
    929 	statusmsg "${progname} ended:   $(date)"
    930 	if [ -s "${results}" ]; then
    931 		echo "===> Summary of results:"
    932 		sed -e 's/^===>//;s/^/	/' "${results}"
    933 		echo "===> ."
    934 	fi
    935 }
    936 
    937 main "$@"
    938