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