Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.98
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.98 2003/05/08 14:19:39 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 	export TOOLDIR DESTDIR RELEASEDIR
    672 	if ! $do_expertmode; then
    673 		_SRCTOPOBJ_=$(getmakevar _SRC_TOP_OBJ_)
    674 		: ${DESTDIR:=${_SRCTOPOBJ_}/destdir.${MACHINE}}
    675 		: ${RELEASEDIR:=${_SRCTOPOBJ_}/releasedir}
    676 	fi
    677 	statusmsg "DESTDIR path:    ${DESTDIR}"
    678 	statusmsg "RELEASEDIR path: ${RELEASEDIR}"
    679 
    680 	# Check validity of TOOLDIR and DESTDIR.
    681 	#
    682 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
    683 		bomb "TOOLDIR '${TOOLDIR}' invalid"
    684 	fi
    685 	removedirs="${TOOLDIR}"
    686 
    687 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
    688 		if ${do_build} || ${do_distribution} || ${do_release}; then
    689 			if ! ${do_build} || \
    690 			   [ "${uname_s}" != "NetBSD" ] || \
    691 			   [ "${uname_m}" != "${MACHINE}" ]; then
    692 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
    693 			fi
    694 			if ! ${do_expertmode}; then
    695 				bomb "DESTDIR must != / for non -E (expert) builds"
    696 			fi
    697 			statusmsg "WARNING: Building to /, in expert mode."
    698 			statusmsg "         This may cause your system to break!  Reasons include:"
    699 			statusmsg "            - your kernel is not up to date"
    700 			statusmsg "            - the libraries or toolchain have changed"
    701 			statusmsg "         YOU HAVE BEEN WARNED!"
    702 		fi
    703 	else
    704 		removedirs="${removedirs} ${DESTDIR}"
    705 	fi
    706 	if ${do_build} || ${do_distribution} || ${do_release}; then
    707 		if ! ${do_expertmode} && \
    708 		    [ $(id -u 2>/dev/null) -ne 0 ] &&\
    709 		    [ -z "${UNPRIVED}" ] ; then
    710 			bomb "-U or -E must be set for build as an unprivileged user."
    711 		fi
    712         fi
    713 }
    714 
    715 
    716 createmakewrapper()
    717 {
    718 	# Remove the target directories.
    719 	#
    720 	if ${do_removedirs}; then
    721 		for f in ${removedirs}; do
    722 			statusmsg "Removing ${f}"
    723 			${runcmd} rm -r -f "${f}"
    724 		done
    725 	fi
    726 
    727 	# Recreate $TOOLDIR.
    728 	#
    729 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
    730 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
    731 
    732 	# Install ${toolprefix}make if it was built.
    733 	#
    734 	if ${do_rebuildmake}; then
    735 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
    736 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
    737 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
    738 		make="${TOOLDIR}/bin/${toolprefix}make"
    739 		statusmsg "Created ${make}"
    740 	fi
    741 
    742 	# Build a ${toolprefix}make wrapper script, usable by hand as
    743 	# well as by build.sh.
    744 	#
    745 	if [ -z "${makewrapper}" ]; then
    746 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${MACHINE}"
    747 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
    748 	fi
    749 
    750 	${runcmd} rm -f "${makewrapper}"
    751 	if [ "${runcmd}" = "echo" ]; then
    752 		echo 'cat <<EOF >'${makewrapper}
    753 		makewrapout=
    754 	else
    755 		makewrapout=">>\${makewrapper}"
    756 	fi
    757 
    758 	eval cat <<EOF ${makewrapout}
    759 #! /bin/sh
    760 # Set proper variables to allow easy "make" building of a NetBSD subtree.
    761 # Generated from:  \$NetBSD: build.sh,v 1.98 2003/05/08 14:19:39 lukem Exp $
    762 #
    763 
    764 EOF
    765 	for f in ${makeenv}; do
    766 		eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}" ${makewrapout}
    767 	done
    768 	eval echo "USETOOLS=yes\; export USETOOLS" ${makewrapout}
    769 
    770 	eval cat <<EOF ${makewrapout}
    771 
    772 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
    773 EOF
    774 	[ "${runcmd}" = "echo" ] && echo EOF
    775 	${runcmd} chmod +x "${makewrapper}"
    776 	statusmsg "Updated ${makewrapper}"
    777 }
    778 
    779 buildtools()
    780 {
    781 	if [ "${MKOBJDIRS}" != "no" ]; then
    782 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
    783 		    bomb "Failed to make obj-tools"
    784 	fi
    785 	${runcmd} cd tools
    786 	if [ -z "${UPDATE}" ]; then
    787 		cleandir=cleandir
    788 	else
    789 		cleandir=
    790 	fi
    791 	${runcmd} "${makewrapper}" ${cleandir} dependall install ||
    792 	    bomb "Failed to make tools"
    793 	statusmsg "Tools built to ${TOOLDIR}"
    794 }
    795 
    796 buildkernel()
    797 {
    798 	kernconf="$1"
    799 	if ! ${do_tools}; then
    800 		# Building tools every time we build a kernel is clearly
    801 		# unnecessary.  We could try to figure out whether rebuilding
    802 		# the tools is necessary this time, but it doesn't seem worth
    803 		# the trouble.  Instead, we say it's the user's responsibility
    804 		# to rebuild the tools if necessary.
    805 		#
    806 		statusmsg "Building kernel without building new tools"
    807 	fi
    808 	statusmsg "Building kernel ${kernconf}"
    809 	if [ "${MKOBJDIRS}" != "no" ] && [ ! -z "${makeobjdir}" ]; then
    810 		# The correct value of KERNOBJDIR might
    811 		# depend on a prior "make obj" in
    812 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
    813 		#
    814 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
    815 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
    816 		${runcmd} cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
    817 		${runcmd} "${makewrapper}" obj ||
    818 		    bomb "Failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
    819 		${runcmd} cd "${TOP}"
    820 	fi
    821 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
    822 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
    823 	case "${kernconf}" in
    824 	*/*)
    825 		kernconfpath="${kernconf}"
    826 		kernconfbase="$(basename "${kernconf}")"
    827 		;;
    828 	*)
    829 		kernconfpath="${KERNCONFDIR}/${kernconf}"
    830 		kernconfbase="${kernconf}"
    831 		;;
    832 	esac
    833 	kernbuilddir="${KERNOBJDIR}/${kernconfbase}"
    834 	statusmsg "Kernel build directory: ${kernbuilddir}"
    835 	${runcmd} mkdir -p "${kernbuilddir}" ||
    836 	    bomb "Cannot mkdir: ${kernbuilddir}"
    837 	if [ -z "${UPDATE}" ]; then
    838 		${runcmd} cd "${kernbuilddir}"
    839 		${runcmd} "${makewrapper}" cleandir ||
    840 		    bomb "Failed to make cleandir in ${kernbuilddir}"
    841 		${runcmd} cd "${TOP}"
    842 	fi
    843 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernbuilddir}" \
    844 		-s "${TOP}/sys" "${kernconfpath}" ||
    845 	    bomb "${toolprefix}config failed for ${kernconf}"
    846 	${runcmd} cd "${kernbuilddir}"
    847 	${runcmd} "${makewrapper}" depend ||
    848 	    bomb "Failed to make depend in ${kernbuilddir}"
    849 	${runcmd} "${makewrapper}" ${parallel} all ||
    850 	    bomb "Failed to make all in ${kernbuilddir}"
    851 	${runcmd} cd "${TOP}"
    852 
    853 	if [ "${runcmd}" != "echo" ]; then
    854 		statusmsg "Kernels built from ${kernconf}:"
    855 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernconfpath})
    856 		for kern in ${kernlist:-netbsd}; do
    857 			[ -f "${kernbuilddir}/${kern}" ] && \
    858 			    echo "  ${kernbuilddir}/${kern}"
    859 		done | tee -a "${results}"
    860 	fi
    861 }
    862 
    863 installworld()
    864 {
    865 	dir="$1"
    866 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
    867 	    bomb "Failed to make installworld to ${dir}"
    868 	statusmsg "Successful installworld to ${dir}"
    869 }
    870 
    871 
    872 main()
    873 {
    874 	initdefaults
    875 	parseoptions "$@"
    876 
    877 	build_start=$(date)
    878 	statusmsg "${progname} command: $0 $@"
    879 	statusmsg "${progname} started: ${build_start}"
    880 
    881 	rebuildmake
    882 	validatemakeparams
    883 	createmakewrapper
    884 
    885 	# Perform the operations.
    886 	#
    887 	for op in ${operations}; do
    888 		case "${op}" in
    889 
    890 		makewrapper)
    891 			# no-op
    892 			;;
    893 
    894 		tools)
    895 			buildtools
    896 			;;
    897 
    898 		obj|build|distribution|release|sets)
    899 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
    900 			    bomb "Failed to make ${op}"
    901 			statusmsg "Successful make ${op}"
    902 			;;
    903 
    904 		kernel=*)
    905 			arg=${op#*=}
    906 			buildkernel "${arg}"
    907 			;;
    908 
    909 		install=*)
    910 			arg=${op#*=}
    911 			if [ "${arg}" = "/" ] && \
    912 			    (	[ "${uname_s}" != "NetBSD" ] || \
    913 				[ "${uname_m}" != "${MACHINE}" ] ); then
    914 				bomb "'${op}' must != / for cross builds."
    915 			fi
    916 			installworld "${arg}"
    917 			;;
    918 
    919 		*)
    920 			bomb "Unknown operation \`${op}'"
    921 			;;
    922 
    923 		esac
    924 	done
    925 
    926 	statusmsg "${progname} ended:   $(date)"
    927 	if [ -s "${results}" ]; then
    928 		echo "===> Summary of results:"
    929 		sed -e 's/^===>//;s/^/	/' "${results}"
    930 		echo "===> ."
    931 	fi
    932 }
    933 
    934 main "$@"
    935