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