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