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