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