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