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