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