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