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