Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.109
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.109 2003/07/20 09:26:49 lukem Exp $
      3 #
      4 # Copyright (c) 2001-2003 The NetBSD Foundation, Inc.
      5 # All rights reserved.
      6 #
      7 # This code is derived from software contributed to The NetBSD Foundation
      8 # by Todd Vierling and Luke Mewburn.
      9 #
     10 # Redistribution and use in source and binary forms, with or without
     11 # modification, are permitted provided that the following conditions
     12 # are met:
     13 # 1. Redistributions of source code must retain the above copyright
     14 #    notice, this list of conditions and the following disclaimer.
     15 # 2. Redistributions in binary form must reproduce the above copyright
     16 #    notice, this list of conditions and the following disclaimer in the
     17 #    documentation and/or other materials provided with the distribution.
     18 # 3. All advertising materials mentioning features or use of this software
     19 #    must display the following acknowledgement:
     20 #        This product includes software developed by the NetBSD
     21 #        Foundation, Inc. and its contributors.
     22 # 4. Neither the name of The NetBSD Foundation nor the names of its
     23 #    contributors may be used to endorse or promote products derived
     24 #    from this software without specific prior written permission.
     25 #
     26 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36 # POSSIBILITY OF SUCH DAMAGE.
     37 #
     38 #
     39 # Top level build wrapper, for a system containing no tools.
     40 #
     41 # This script should run on any POSIX-compliant shell.  For systems
     42 # with a strange /bin/sh, "ksh" or "bash" may be an ample alternative.
     43 #
     44 # Note, however, that due to the way the interpreter is invoked above,
     45 # if a POSIX-compliant shell is the first in the PATH, you won't have
     46 # to take any further action.
     47 #
     48 
     49 progname=${0##*/}
     50 toppid=$$
     51 results=/dev/null
     52 trap "exit 1" 1 2 3 15
     53 
     54 bomb()
     55 {
     56 	cat >&2 <<ERRORMESSAGE
     57 
     58 ERROR: $@
     59 *** BUILD ABORTED ***
     60 ERRORMESSAGE
     61 	kill ${toppid}		# in case we were invoked from a subshell
     62 	exit 1
     63 }
     64 
     65 
     66 statusmsg()
     67 {
     68 	${runcmd} echo "===> $@" | tee -a "${results}"
     69 }
     70 
     71 initdefaults()
     72 {
     73 	cd "$(dirname $0)"
     74 	[ -d usr.bin/make ] ||
     75 	    bomb "build.sh must be run from the top source level"
     76 	[ -f share/mk/bsd.own.mk ] ||
     77 	    bomb "src/share/mk is missing; please re-fetch the source tree"
     78 
     79 	uname_s=$(uname -s 2>/dev/null)
     80 	uname_m=$(uname -m 2>/dev/null)
     81 
     82 	# If $PWD is a valid name of the current directory, POSIX mandates
     83 	# that pwd return it by default which causes problems in the
     84 	# presence of symlinks.  Unsetting PWD is simpler than changing
     85 	# every occurrence of pwd to use -P.
     86 	#
     87 	# XXX Except that doesn't work on Solaris.
     88 	#
     89 	unset PWD
     90 	if [ "${uname_s}" = "SunOS" ]; then
     91 		TOP=$(pwd -P)
     92 	else
     93 		TOP=$(pwd)
     94 	fi
     95 
     96 	# Set defaults.
     97 	#
     98 	toolprefix=nb
     99 
    100 	# Some systems have a small ARG_MAX.  -X prevents make(1) from
    101 	# exporting variables in the environment redundantly.
    102 	#
    103 	case "${uname_s}" in
    104 	Darwin | FreeBSD | CYGWIN*)
    105 		MAKEFLAGS=-X
    106 		;;
    107 	*)
    108 		MAKEFLAGS=
    109 		;;
    110 	esac
    111 
    112 	makeenv=
    113 	makewrapper=
    114 	makewrappermachine=
    115 	runcmd=
    116 	operations=
    117 	removedirs=
    118 	do_expertmode=false
    119 	do_rebuildmake=false
    120 	do_removedirs=false
    121 
    122 	# do_{operation}=true if given operation is requested.
    123 	#
    124 	do_tools=false
    125 	do_obj=false
    126 	do_build=false
    127 	do_distribution=false
    128 	do_release=false
    129 	do_kernel=false
    130 	do_releasekernel=false
    131 	do_install=false
    132 	do_sets=false
    133 	do_sourcesets=false
    134 	do_params=false
    135 
    136 	# Create scratch directory
    137 	#
    138 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    139 	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
    140 	trap "cd /; rm -r -f \"${tmpdir}\"" 0
    141 	results="${tmpdir}/build.sh.results"
    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|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 resolvepath()
    320 {
    321 	case "${OPTARG}" in
    322 	/*)
    323 		;;
    324 	*)
    325 		OPTARG="${TOP}/${OPTARG}"
    326 		;;
    327 	esac
    328 }
    329 
    330 usage()
    331 {
    332 	if [ -n "$*" ]; then
    333 		echo ""
    334 		echo "${progname}: $*"
    335 	fi
    336 	cat <<_usage_
    337 
    338 Usage: ${progname} [-EnorUu] [-a arch] [-B buildid] [-D dest] [-j njob] [-M obj]
    339                 [-m mach] [-O obj] [-R release] [-T tools] [-V var=[value]]
    340                 [-w wrapper]   operation [...]
    341 
    342  Build operations (all imply "obj" and "tools"):
    343     build               Run "make build"
    344     distribution        Run "make distribution" (includes DESTDIR/etc/ files)
    345     release             Run "make release" (includes kernels & distrib media)
    346 
    347  Other operations:
    348     help                Show this message (and exit)
    349     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
    350                         (Always done)
    351     obj                 Run "make obj" (default unless -o is used)
    352     tools               Build and install tools
    353     install=idir        Run "make installworld" to \`idir'
    354                         (useful after 'distribution' or 'release')
    355     kernel=conf         Build kernel with config file \`conf'
    356     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR
    357     sets                Create binary sets in RELEASEDIR/MACHINE/binary/sets
    358     sourcesets          Create source sets in RELEASEDIR/source/sets
    359     params              Display various make(1) parameters
    360 
    361  Options:
    362     -a arch     Set MACHINE_ARCH to arch (otherwise deduced from MACHINE)
    363     -B buildId  Set BUILDID to buildId
    364     -D dest     Set DESTDIR to dest.  (Default: destdir.MACHINE)
    365     -E          Set "expert" mode; disables various safety checks.
    366                 Should not be used without expert knowledge of the build system
    367     -j njob     Run up to njob jobs in parallel; see make(1)
    368     -M obj      Set obj root directory to obj (sets MAKEOBJDIRPREFIX)
    369     -m mach     Set MACHINE to mach (not required if NetBSD native)
    370     -n          Show commands that would be executed, but do not execute them
    371     -O obj      Set obj root directory to obj (sets a MAKEOBJDIR pattern)
    372     -o          Set MKOBJDIRS=no (do not create objdirs at start of build)
    373     -R release  Set RELEASEDIR to release.  (Default: releasedir)
    374     -r          Remove contents of TOOLDIR and DESTDIR before building
    375     -T tools    Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
    376                 the environment, ${toolprefix}make will be (re)built unconditionally
    377     -U          Set MKUNPRIVED=yes (build without requiring root privileges)
    378     -u          Set MKUPDATE=yes (do not run "make clean" first).
    379 		Without this, everything is rebuilt, including the tools.
    380     -V v=[val]  Set variable \`v' to \`val'
    381     -w wrapper  Create ${toolprefix}make script as wrapper
    382                 (Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE})
    383 
    384 _usage_
    385 	exit 1
    386 }
    387 
    388 parseoptions()
    389 {
    390 	opts='a:B:bD:dEhi:j:k:M:m:nO:oR:rT:tUuV:w:'
    391 	opt_a=no
    392 
    393 	if type getopts >/dev/null 2>&1; then
    394 		# Use POSIX getopts.
    395 		#
    396 		getoptcmd='getopts ${opts} opt && opt=-${opt}'
    397 		optargcmd=':'
    398 		optremcmd='shift $((${OPTIND} -1))'
    399 	else
    400 		type getopt >/dev/null 2>&1 ||
    401 		    bomb "/bin/sh shell is too old; try ksh or bash"
    402 
    403 		# Use old-style getopt(1) (doesn't handle whitespace in args).
    404 		#
    405 		args="$(getopt ${opts} $*)"
    406 		[ $? = 0 ] || usage
    407 		set -- ${args}
    408 
    409 		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    410 		optargcmd='OPTARG="$1"; shift'
    411 		optremcmd=':'
    412 	fi
    413 
    414 	# Parse command line options.
    415 	#
    416 	while eval ${getoptcmd}; do
    417 		case ${opt} in
    418 
    419 		-a)
    420 			eval ${optargcmd}
    421 			MACHINE_ARCH=${OPTARG}
    422 			opt_a=yes
    423 			;;
    424 
    425 		-B)
    426 			eval ${optargcmd}
    427 			BUILDID=${OPTARG}
    428 			;;
    429 
    430 		-b)
    431 			usage "'-b' has been replaced by 'makewrapper'"
    432 			;;
    433 
    434 		-D)
    435 			eval ${optargcmd}; resolvepath
    436 			setmakeenv DESTDIR "${OPTARG}"
    437 			;;
    438 
    439 		-d)
    440 			usage "'-d' has been replaced by 'distribution'"
    441 			;;
    442 
    443 		-E)
    444 			do_expertmode=true
    445 			;;
    446 
    447 		-i)
    448 			usage "'-i idir' has been replaced by 'install=idir'"
    449 			;;
    450 
    451 		-j)
    452 			eval ${optargcmd}
    453 			parallel="-j ${OPTARG}"
    454 			;;
    455 
    456 		-k)
    457 			usage "'-k conf' has been replaced by 'kernel=conf'"
    458 			;;
    459 
    460 		-M)
    461 			eval ${optargcmd}; resolvepath
    462 			makeobjdir="${OPTARG}"
    463 			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
    464 			;;
    465 
    466 			# -m overrides MACHINE_ARCH unless "-a" is specified
    467 		-m)
    468 			eval ${optargcmd}
    469 			MACHINE="${OPTARG}"
    470 			[ "${opt_a}" != "yes" ] && getarch
    471 			;;
    472 
    473 		-n)
    474 			runcmd=echo
    475 			;;
    476 
    477 		-O)
    478 			eval ${optargcmd}; resolvepath
    479 			makeobjdir="${OPTARG}"
    480 			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
    481 			;;
    482 
    483 		-o)
    484 			MKOBJDIRS=no
    485 			;;
    486 
    487 		-R)
    488 			eval ${optargcmd}; resolvepath
    489 			setmakeenv RELEASEDIR "${OPTARG}"
    490 			;;
    491 
    492 		-r)
    493 			do_removedirs=true
    494 			do_rebuildmake=true
    495 			;;
    496 
    497 		-T)
    498 			eval ${optargcmd}; resolvepath
    499 			TOOLDIR="${OPTARG}"
    500 			export TOOLDIR
    501 			;;
    502 
    503 		-t)
    504 			usage "'-t' has been replaced by 'tools'"
    505 			;;
    506 
    507 		-U)
    508 			setmakeenv MKUNPRIVED yes
    509 			;;
    510 
    511 		-u)
    512 			setmakeenv MKUPDATE yes
    513 			;;
    514 
    515 		-V)
    516 			eval ${optargcmd}
    517 			case "${OPTARG}" in
    518 		    # XXX: consider restricting which variables can be changed?
    519 			[a-zA-Z_][a-zA-Z_0-9]*=*)
    520 				setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
    521 				;;
    522 			*)
    523 				usage "-V argument must be of the form 'var=[value]'"
    524 				;;
    525 			esac
    526 			;;
    527 
    528 		-w)
    529 			eval ${optargcmd}; resolvepath
    530 			makewrapper="${OPTARG}"
    531 			;;
    532 
    533 		--)
    534 			break
    535 			;;
    536 
    537 		-'?'|-h)
    538 			usage
    539 			;;
    540 
    541 		esac
    542 	done
    543 
    544 	# Validate operations.
    545 	#
    546 	eval ${optremcmd}
    547 	while [ $# -gt 0 ]; do
    548 		op=$1; shift
    549 		operations="${operations} ${op}"
    550 
    551 		case "${op}" in
    552 
    553 		help)
    554 			usage
    555 			;;
    556 
    557 		makewrapper|obj|tools|build|distribution|release|sets|sourcesets|params)
    558 			;;
    559 
    560 		kernel=*|releasekernel=*)
    561 			arg=${op#*=}
    562 			op=${op%%=*}
    563 			[ -n "${arg}" ] ||
    564 			    bomb "Must supply a kernel name with \`${op}=...'"
    565 			;;
    566 
    567 		install=*)
    568 			arg=${op#*=}
    569 			op=${op%%=*}
    570 			[ -n "${arg}" ] ||
    571 			    bomb "Must supply a directory with \`install=...'"
    572 			;;
    573 
    574 		*)
    575 			usage "Unknown operation \`${op}'"
    576 			;;
    577 
    578 		esac
    579 		eval do_${op}=true
    580 	done
    581 	[ -n "${operations}" ] || usage "Missing operation to perform."
    582 
    583 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
    584 	#
    585 	if [ -z "${MACHINE}" ]; then
    586 		[ "${uname_s}" = "NetBSD" ] ||
    587 		    bomb "MACHINE must be set, or -m must be used, for cross builds."
    588 		MACHINE=${uname_m}
    589 	fi
    590 	[ -n "${MACHINE_ARCH}" ] || getarch
    591 	validatearch
    592 
    593 	# Set up default make(1) environment.
    594 	#
    595 	setmakeenv LC_ALL C
    596 	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
    597 	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
    598 	MAKEFLAGS="-m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
    599 	export MAKEFLAGS MACHINE MACHINE_ARCH
    600 }
    601 
    602 rebuildmake()
    603 {
    604 	# Test make source file timestamps against installed ${toolprefix}make
    605 	# binary, if TOOLDIR is pre-set.
    606 	#
    607 	# Note that we do NOT try to grovel "mk.conf" here to find out if
    608 	# TOOLDIR is set there, because it can contain make variable
    609 	# expansions and other stuff only parseable *after* we have a working
    610 	# ${toolprefix}make.  So this logic can only work if the user has
    611 	# pre-set TOOLDIR in the environment or used the -T option to build.sh.
    612 	#
    613 	make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
    614 	if [ -x "${make}" ]; then
    615 		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
    616 			if [ "${f}" -nt "${make}" ]; then
    617 				do_rebuildmake=true
    618 				break
    619 			fi
    620 		done
    621 	else
    622 		do_rebuildmake=true
    623 	fi
    624 
    625 	# Build bootstrap ${toolprefix}make if needed.
    626 	if ${do_rebuildmake}; then
    627 		statusmsg "Bootstrapping ${toolprefix}make"
    628 		${runcmd} cd "${tmpdir}"
    629 		${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
    630 			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
    631 			"${TOP}/tools/make/configure" ||
    632 		    bomb "Configure of ${toolprefix}make failed"
    633 		${runcmd} sh buildmake.sh ||
    634 		    bomb "Build of ${toolprefix}make failed"
    635 		make="${tmpdir}/${toolprefix}make"
    636 		${runcmd} cd "${TOP}"
    637 		${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
    638 	fi
    639 }
    640 
    641 validatemakeparams()
    642 {
    643 	if [ "${runcmd}" = "echo" ]; then
    644 		TOOLCHAIN_MISSING=no
    645 		EXTERNAL_TOOLCHAIN=""
    646 	else
    647 		TOOLCHAIN_MISSING=$(raw_getmakevar TOOLCHAIN_MISSING)
    648 		EXTERNAL_TOOLCHAIN=$(raw_getmakevar EXTERNAL_TOOLCHAIN)
    649 	fi
    650 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
    651 	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
    652 		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
    653 		${runcmd} echo "	MACHINE:      ${MACHINE}"
    654 		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
    655 		${runcmd} echo ""
    656 		${runcmd} echo "All builds for this platform should be done via a traditional make"
    657 		${runcmd} echo "If you wish to use an external cross-toolchain, set"
    658 		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
    659 		${runcmd} echo "in either the environment or mk.conf and rerun"
    660 		${runcmd} echo "	${progname} $*"
    661 		exit 1
    662 	fi
    663 
    664 	if [ "${MKOBJDIRS}" != "no" ]; then
    665 		# If setting -M or -O to the root of an obj dir, make sure
    666 		# the base directory is made before continuing as <bsd.own.mk>
    667 		# will need this to pick up _SRC_TOP_OBJ_
    668 		#
    669 		if [ ! -z "${makeobjdir}" ]; then
    670 			${runcmd} mkdir -p "${makeobjdir}"
    671 		fi
    672 
    673 		# make obj in tools to ensure that the objdir for the top-level
    674 		# of the source tree and for "tools" is available, in case the
    675 		# default TOOLDIR setting from <bsd.own.mk> is used, or the
    676 		# build.sh default DESTDIR and RELEASEDIR is to be used.
    677 		#
    678 		${runcmd} cd tools
    679 		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
    680 		    bomb "Failed to make obj in tools"
    681 		${runcmd} cd "${TOP}"
    682 	fi
    683 
    684 	statusmsg "MACHINE:          ${MACHINE}"
    685 	statusmsg "MACHINE_ARCH:     ${MACHINE_ARCH}"
    686 
    687 	# Find TOOLDIR, DESTDIR, and RELEASEDIR.
    688 	#
    689 	TOOLDIR=$(getmakevar TOOLDIR)
    690 	statusmsg "TOOLDIR path:     ${TOOLDIR}"
    691 	DESTDIR=$(getmakevar DESTDIR)
    692 	RELEASEDIR=$(getmakevar RELEASEDIR)
    693 	if ! $do_expertmode; then
    694 		_SRC_TOP_OBJ_=$(getmakevar _SRC_TOP_OBJ_)
    695 		: ${DESTDIR:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
    696 		: ${RELEASEDIR:=${_SRC_TOP_OBJ_}/releasedir}
    697 		makeenv="${makeenv} DESTDIR RELEASEDIR"
    698 	fi
    699 	export TOOLDIR DESTDIR RELEASEDIR
    700 	statusmsg "DESTDIR path:     ${DESTDIR}"
    701 	statusmsg "RELEASEDIR path:  ${RELEASEDIR}"
    702 
    703 	# Check validity of TOOLDIR and DESTDIR.
    704 	#
    705 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
    706 		bomb "TOOLDIR '${TOOLDIR}' invalid"
    707 	fi
    708 	removedirs="${TOOLDIR}"
    709 
    710 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
    711 		if ${do_build} || ${do_distribution} || ${do_release}; then
    712 			if ! ${do_build} || \
    713 			   [ "${uname_s}" != "NetBSD" ] || \
    714 			   [ "${uname_m}" != "${MACHINE}" ]; then
    715 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
    716 			fi
    717 			if ! ${do_expertmode}; then
    718 				bomb "DESTDIR must != / for non -E (expert) builds"
    719 			fi
    720 			statusmsg "WARNING: Building to /, in expert mode."
    721 			statusmsg "         This may cause your system to break!  Reasons include:"
    722 			statusmsg "            - your kernel is not up to date"
    723 			statusmsg "            - the libraries or toolchain have changed"
    724 			statusmsg "         YOU HAVE BEEN WARNED!"
    725 		fi
    726 	else
    727 		removedirs="${removedirs} ${DESTDIR}"
    728 	fi
    729 	if ${do_build} || ${do_distribution} || ${do_release}; then
    730 		if ! ${do_expertmode} && \
    731 		    [ $(id -u 2>/dev/null) -ne 0 ] && \
    732 		    [ -z "${MKUNPRIVED}" ] ; then
    733 			bomb "-U or -E must be set for build as an unprivileged user."
    734 		fi
    735         fi
    736 	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
    737 		bomb "Must set RELEASEDIR with \`releasekernel=...'"
    738 	fi
    739 }
    740 
    741 
    742 createmakewrapper()
    743 {
    744 	# Remove the target directories.
    745 	#
    746 	if ${do_removedirs}; then
    747 		for f in ${removedirs}; do
    748 			statusmsg "Removing ${f}"
    749 			${runcmd} rm -r -f "${f}"
    750 		done
    751 	fi
    752 
    753 	# Recreate $TOOLDIR.
    754 	#
    755 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
    756 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
    757 
    758 	# Install ${toolprefix}make if it was built.
    759 	#
    760 	if ${do_rebuildmake}; then
    761 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
    762 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
    763 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
    764 		make="${TOOLDIR}/bin/${toolprefix}make"
    765 		statusmsg "Created ${make}"
    766 	fi
    767 
    768 	# Build a ${toolprefix}make wrapper script, usable by hand as
    769 	# well as by build.sh.
    770 	#
    771 	if [ -z "${makewrapper}" ]; then
    772 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
    773 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
    774 	fi
    775 
    776 	${runcmd} rm -f "${makewrapper}"
    777 	if [ "${runcmd}" = "echo" ]; then
    778 		echo 'cat <<EOF >'${makewrapper}
    779 		makewrapout=
    780 	else
    781 		makewrapout=">>\${makewrapper}"
    782 	fi
    783 
    784 	eval cat <<EOF ${makewrapout}
    785 #! /bin/sh
    786 # Set proper variables to allow easy "make" building of a NetBSD subtree.
    787 # Generated from:  \$NetBSD: build.sh,v 1.109 2003/07/20 09:26:49 lukem Exp $
    788 #
    789 
    790 EOF
    791 	for f in ${makeenv}; do
    792 		eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}" ${makewrapout}
    793 	done
    794 	eval echo "USETOOLS=yes\; export USETOOLS" ${makewrapout}
    795 
    796 	eval cat <<EOF ${makewrapout}
    797 
    798 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
    799 EOF
    800 	[ "${runcmd}" = "echo" ] && echo EOF
    801 	${runcmd} chmod +x "${makewrapper}"
    802 	statusmsg "makewrapper:      ${makewrapper}"
    803 	statusmsg "Updated ${makewrapper}"
    804 }
    805 
    806 buildtools()
    807 {
    808 	if [ "${MKOBJDIRS}" != "no" ]; then
    809 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
    810 		    bomb "Failed to make obj-tools"
    811 	fi
    812 	${runcmd} cd tools
    813 	if [ -z "${MKUPDATE}" ]; then
    814 		cleandir=cleandir
    815 	else
    816 		cleandir=
    817 	fi
    818 	${runcmd} "${makewrapper}" ${cleandir} dependall install ||
    819 	    bomb "Failed to make tools"
    820 	statusmsg "Tools built to ${TOOLDIR}"
    821 }
    822 
    823 getkernelconf()
    824 {
    825 	kernelconf="$1"
    826 	if [ "${MKOBJDIRS}" != "no" ] && [ ! -z "${makeobjdir}" ]; then
    827 		# The correct value of KERNOBJDIR might
    828 		# depend on a prior "make obj" in
    829 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
    830 		#
    831 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
    832 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
    833 		${runcmd} cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
    834 		${runcmd} "${makewrapper}" obj ||
    835 		    bomb "Failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
    836 		${runcmd} cd "${TOP}"
    837 	fi
    838 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
    839 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
    840 	case "${kernelconf}" in
    841 	*/*)
    842 		kernelconfpath="${kernelconf}"
    843 		kernelconfname="${kernelconf##*/}"
    844 		;;
    845 	*)
    846 		kernelconfpath="${KERNCONFDIR}/${kernelconf}"
    847 		kernelconfname="${kernelconf}"
    848 		;;
    849 	esac
    850 	kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
    851 }
    852 
    853 buildkernel()
    854 {
    855 	if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
    856 		# Building tools every time we build a kernel is clearly
    857 		# unnecessary.  We could try to figure out whether rebuilding
    858 		# the tools is necessary this time, but it doesn't seem worth
    859 		# the trouble.  Instead, we say it's the user's responsibility
    860 		# to rebuild the tools if necessary.
    861 		#
    862 		statusmsg "Building kernel without building new tools"
    863 		buildkernelwarned=true
    864 	fi
    865 	getkernelconf $1
    866 	statusmsg "Building kernel:  ${kernelconf}"
    867 	statusmsg "Build directory:  ${kernelbuildpath}"
    868 	${runcmd} mkdir -p "${kernelbuildpath}" ||
    869 	    bomb "Cannot mkdir: ${kernelbuildpath}"
    870 	if [ -z "${MKUPDATE}" ]; then
    871 		${runcmd} cd "${kernelbuildpath}"
    872 		${runcmd} "${makewrapper}" cleandir ||
    873 		    bomb "Failed to make cleandir in ${kernelbuildpath}"
    874 		${runcmd} cd "${TOP}"
    875 	fi
    876 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
    877 		-s "${TOP}/sys" "${kernelconfpath}" ||
    878 	    bomb "${toolprefix}config failed for ${kernelconf}"
    879 	${runcmd} cd "${kernelbuildpath}"
    880 	${runcmd} "${makewrapper}" depend ||
    881 	    bomb "Failed to make depend in ${kernelbuildpath}"
    882 	${runcmd} "${makewrapper}" ${parallel} all ||
    883 	    bomb "Failed to make all in ${kernelbuildpath}"
    884 	${runcmd} cd "${TOP}"
    885 
    886 	if [ "${runcmd}" != "echo" ]; then
    887 		statusmsg "Kernels built from ${kernelconf}:"
    888 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
    889 		for kern in ${kernlist:-netbsd}; do
    890 			[ -f "${kernelbuildpath}/${kern}" ] && \
    891 			    echo "  ${kernelbuildpath}/${kern}"
    892 		done | tee -a "${results}"
    893 	fi
    894 }
    895 
    896 releasekernel()
    897 {
    898 	getkernelconf $1
    899 	kernelreldir="${RELEASEDIR}/${MACHINE}/binary/kernel"
    900 	${runcmd} mkdir -p "${kernelreldir}"
    901 	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
    902 echo "releasekernel: conf ${kernelconf}, name ${kernelconfname}, build ${kernelbuildpath}"
    903 	for kern in ${kernlist:-netbsd}; do
    904 echo "  checking ${kern} in ${kernelbuildpath}"
    905 		builtkern="${kernelbuildpath}/${kern}"
    906 		[ -f "${builtkern}" ] || continue
    907 		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
    908 		statusmsg "Kernel copy:      ${releasekern}"
    909 		${runcmd} gzip -c -9 < "${builtkern}" > "${releasekern}"
    910 	done
    911 }
    912 
    913 installworld()
    914 {
    915 	dir="$1"
    916 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
    917 	    bomb "Failed to make installworld to ${dir}"
    918 	statusmsg "Successful installworld to ${dir}"
    919 }
    920 
    921 
    922 main()
    923 {
    924 	initdefaults
    925 	parseoptions "$@"
    926 
    927 	build_start=$(date)
    928 	statusmsg "${progname} command: $0 $@"
    929 	statusmsg "${progname} started: ${build_start}"
    930 
    931 	rebuildmake
    932 	validatemakeparams
    933 	createmakewrapper
    934 
    935 	# Perform the operations.
    936 	#
    937 	for op in ${operations}; do
    938 		case "${op}" in
    939 
    940 		makewrapper)
    941 			# no-op
    942 			;;
    943 
    944 		tools)
    945 			buildtools
    946 			;;
    947 
    948 		obj|build|distribution|release|sets|sourcesets|params)
    949 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
    950 			    bomb "Failed to make ${op}"
    951 			statusmsg "Successful make ${op}"
    952 			;;
    953 
    954 		kernel=*)
    955 			arg=${op#*=}
    956 			buildkernel "${arg}"
    957 			;;
    958 
    959 		releasekernel=*)
    960 			arg=${op#*=}
    961 			releasekernel "${arg}"
    962 			;;
    963 
    964 		install=*)
    965 			arg=${op#*=}
    966 			if [ "${arg}" = "/" ] && \
    967 			    (	[ "${uname_s}" != "NetBSD" ] || \
    968 				[ "${uname_m}" != "${MACHINE}" ] ); then
    969 				bomb "'${op}' must != / for cross builds."
    970 			fi
    971 			installworld "${arg}"
    972 			;;
    973 
    974 		*)
    975 			bomb "Unknown operation \`${op}'"
    976 			;;
    977 
    978 		esac
    979 	done
    980 
    981 	statusmsg "${progname} started: ${build_start}"
    982 	statusmsg "${progname} ended:   $(date)"
    983 	if [ -s "${results}" ]; then
    984 		echo "===> Summary of results:"
    985 		sed -e 's/^===>//;s/^/	/' "${results}"
    986 		echo "===> ."
    987 	fi
    988 }
    989 
    990 main "$@"
    991