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