Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.198.2.3
      1 #! /usr/bin/env sh
      2 #	$NetBSD: build.sh,v 1.198.2.3 2009/03/18 05:39:06 snj Exp $
      3 #
      4 # Copyright (c) 2001-2008 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 #
     19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29 # POSSIBILITY OF SUCH DAMAGE.
     30 #
     31 #
     32 # Top level build wrapper, for a system containing no tools.
     33 #
     34 # This script should run on any POSIX-compliant shell.  If the
     35 # first "sh" found in the PATH is a POSIX-compliant shell, then
     36 # you should not need to take any special action.  Otherwise, you
     37 # should set the environment variable HOST_SH to a POSIX-compliant
     38 # shell, and invoke build.sh with that shell.  (Depending on your
     39 # system, one of /bin/ksh, /usr/local/bin/bash, or /usr/xpg4/bin/sh
     40 # might be a suitable shell.)
     41 #
     42 
     43 progname=${0##*/}
     44 toppid=$$
     45 results=/dev/null
     46 tab='	'
     47 trap "exit 1" 1 2 3 15
     48 
     49 bomb()
     50 {
     51 	cat >&2 <<ERRORMESSAGE
     52 
     53 ERROR: $@
     54 *** BUILD ABORTED ***
     55 ERRORMESSAGE
     56 	kill ${toppid}		# in case we were invoked from a subshell
     57 	exit 1
     58 }
     59 
     60 
     61 statusmsg()
     62 {
     63 	${runcmd} echo "===> $@" | tee -a "${results}"
     64 }
     65 
     66 warning()
     67 {
     68 	statusmsg "Warning: $@"
     69 }
     70 
     71 # Find a program in the PATH, and print the result.  If not found,
     72 # print a default.  If $2 is defined (even if it is an empty string),
     73 # then that is the default; otherwise, $1 is used as the default.
     74 find_in_PATH()
     75 {
     76 	local prog="$1"
     77 	local result="${2-"$1"}"
     78 	local oldIFS="${IFS}"
     79 	local dir
     80 	IFS=":"
     81 	for dir in ${PATH}; do
     82 		if [ -x "${dir}/${prog}" ]; then
     83 			result="${dir}/${prog}"
     84 			break
     85 		fi
     86 	done
     87 	IFS="${oldIFS}"
     88 	echo "${result}"
     89 }
     90 
     91 # Try to find a working POSIX shell, and set HOST_SH to refer to it.
     92 # Assumes that uname_s, uname_m, and PWD have been set.
     93 set_HOST_SH()
     94 {
     95 	# Even if ${HOST_SH} is already defined, we still do the
     96 	# sanity checks at the end.
     97 
     98 	# Solaris has /usr/xpg4/bin/sh.
     99 	#
    100 	[ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
    101 		[ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
    102 
    103 	# Try to get the name of the shell that's running this script,
    104 	# by parsing the output from "ps".  We assume that, if the host
    105 	# system's ps command supports -o comm at all, it will do so
    106 	# in the usual way: a one-line header followed by a one-line
    107 	# result, possibly including trailing white space.  And if the
    108 	# host system's ps command doesn't support -o comm, we assume
    109 	# that we'll get an error message on stderr and nothing on
    110 	# stdout.  (We don't try to use ps -o 'comm=' to suppress the
    111 	# header line, because that is less widely supported.)
    112 	#
    113 	# If we get the wrong result here, the user can override it by
    114 	# specifying HOST_SH in the environment.
    115 	#
    116 	[ -z "${HOST_SH}" ] && HOST_SH="$(
    117 		(ps -p $$ -o comm | sed -ne "2s/[ ${tab}]*\$//p") 2>/dev/null )"
    118 
    119 	# If nothing above worked, use "sh".  We will later find the
    120 	# first directory in the PATH that has a "sh" program.
    121 	#
    122 	[ -z "${HOST_SH}" ] && HOST_SH="sh"
    123 
    124 	# If the result so far is not an absolute path, try to prepend
    125 	# PWD or search the PATH.
    126 	#
    127 	case "${HOST_SH}" in
    128 	/*)	:
    129 		;;
    130 	*/*)	HOST_SH="${PWD}/${HOST_SH}"
    131 		;;
    132 	*)	HOST_SH="$(find_in_PATH "${HOST_SH}")"
    133 		;;
    134 	esac
    135 
    136 	# If we don't have an absolute path by now, bomb.
    137 	#
    138 	case "${HOST_SH}" in
    139 	/*)	:
    140 		;;
    141 	*)	bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
    142 		;;
    143 	esac
    144 
    145 	# If HOST_SH is not executable, bomb.
    146 	#
    147 	[ -x "${HOST_SH}" ] ||
    148 	    bomb "HOST_SH=\"${HOST_SH}\" is not executable."
    149 }
    150 
    151 initdefaults()
    152 {
    153 	makeenv=
    154 	makewrapper=
    155 	makewrappermachine=
    156 	runcmd=
    157 	operations=
    158 	removedirs=
    159 
    160 	[ -d usr.bin/make ] || cd "$(dirname $0)"
    161 	[ -d usr.bin/make ] ||
    162 	    bomb "build.sh must be run from the top source level"
    163 	[ -f share/mk/bsd.own.mk ] ||
    164 	    bomb "src/share/mk is missing; please re-fetch the source tree"
    165 
    166 	# Find information about the build platform.  This should be
    167 	# kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
    168 	# variables in share/mk/bsd.sys.mk.
    169 	#
    170 	# Note that "uname -p" is not part of POSIX, but we want uname_p
    171 	# to be set to the host MACHINE_ARCH, if possible.  On systems
    172 	# where "uname -p" fails, prints "unknown", or prints a string
    173 	# that does not look like an identifier, fall back to using the
    174 	# output from "uname -m" instead.
    175 	#
    176 	uname_s=$(uname -s 2>/dev/null)
    177 	uname_r=$(uname -r 2>/dev/null)
    178 	uname_m=$(uname -m 2>/dev/null)
    179 	uname_p=$(uname -p 2>/dev/null || echo "unknown")
    180 	case "${uname_p}" in
    181 	''|unknown|*[^-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
    182 	esac
    183 
    184 	# If $PWD is a valid name of the current directory, POSIX mandates
    185 	# that pwd return it by default which causes problems in the
    186 	# presence of symlinks.  Unsetting PWD is simpler than changing
    187 	# every occurrence of pwd to use -P.
    188 	#
    189 	# XXX Except that doesn't work on Solaris. Or many Linuces.
    190 	#
    191 	unset PWD
    192 	TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
    193 
    194 	# The user can set HOST_SH in the environment, or we try to
    195 	# guess an appropriate value.  Then we set several other
    196 	# variables from HOST_SH.
    197 	#
    198 	set_HOST_SH
    199 	setmakeenv HOST_SH "${HOST_SH}"
    200 	setmakeenv BSHELL "${HOST_SH}"
    201 	setmakeenv CONFIG_SHELL "${HOST_SH}"
    202 
    203 	# Set defaults.
    204 	#
    205 	toolprefix=nb
    206 
    207 	# Some systems have a small ARG_MAX.  -X prevents make(1) from
    208 	# exporting variables in the environment redundantly.
    209 	#
    210 	case "${uname_s}" in
    211 	Darwin | FreeBSD | CYGWIN*)
    212 		MAKEFLAGS=-X
    213 		;;
    214 	*)
    215 		MAKEFLAGS=
    216 		;;
    217 	esac
    218 
    219 	# do_{operation}=true if given operation is requested.
    220 	#
    221 	do_expertmode=false
    222 	do_rebuildmake=false
    223 	do_removedirs=false
    224 	do_tools=false
    225 	do_cleandir=false
    226 	do_obj=false
    227 	do_build=false
    228 	do_distribution=false
    229 	do_release=false
    230 	do_kernel=false
    231 	do_releasekernel=false
    232 	do_install=false
    233 	do_sets=false
    234 	do_sourcesets=false
    235 	do_syspkgs=false
    236 	do_iso_image=false
    237 	do_iso_image_source=false
    238 	do_params=false
    239 
    240 	# Create scratch directory
    241 	#
    242 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    243 	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
    244 	trap "cd /; rm -r -f \"${tmpdir}\"" 0
    245 	results="${tmpdir}/build.sh.results"
    246 
    247 	# Set source directories
    248 	#
    249 	setmakeenv NETBSDSRCDIR "${TOP}"
    250 
    251 	# Determine top-level obj directory.
    252 	# Defaults to the top-level source directory.
    253 	# If $MAKEOBJDIRPREFIX is set in the environment, use it.
    254 	# We can't check $MAKEOBJDIR since that may be a make(1)
    255 	# expression that we can't evaluate at this time.
    256 	#
    257 	TOP_objdir="${TOP}"
    258 	if [ -n "${MAKEOBJDIRPREFIX}" ]; then
    259 		TOP_objdir="${MAKEOBJDIRPREFIX}${TOP}"
    260 	elif [ -n "${MAKEOBJDIR}" ]; then
    261 		warning "Can't parse \$(MAKEOBJDIR) \"$MAKEOBJDIR\" to determine top objdir"
    262 	fi
    263 
    264 	# Find the version of NetBSD
    265 	#
    266 	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
    267 
    268 	# Set the BUILDSEED to NetBSD-"N"
    269 	#
    270 	setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
    271 
    272 	# Set various environment variables to known defaults,
    273 	# to minimize (cross-)build problems observed "in the field".
    274 	#
    275 	unsetmakeenv INFODIR
    276 	unsetmakeenv LESSCHARSET
    277 	setmakeenv LC_ALL C
    278 }
    279 
    280 getarch()
    281 {
    282 	# Translate some MACHINE name aliases (known only to build.sh)
    283 	# into proper MACHINE and MACHINE_ARCH names.  Save the alias
    284 	# name in makewrappermachine.
    285 	#
    286 	case "${MACHINE}" in
    287 
    288 	evbarm-e[bl])
    289 		makewrappermachine=${MACHINE}
    290 		# MACHINE_ARCH is "arm" or "armeb", not "armel"
    291 		MACHINE_ARCH=arm${MACHINE##*-}
    292 		MACHINE_ARCH=${MACHINE_ARCH%el}
    293 		MACHINE=${MACHINE%-e[bl]}
    294 		;;
    295 
    296 	evbmips-e[bl]|sbmips-e[bl])
    297 		makewrappermachine=${MACHINE}
    298 		MACHINE_ARCH=mips${MACHINE##*-}
    299 		MACHINE=${MACHINE%-e[bl]}
    300 		;;
    301 
    302 	evbmips64-e[bl]|sbmips64-e[bl])
    303 		makewrappermachine=${MACHINE}
    304 		MACHINE_ARCH=mips64${MACHINE##*-}
    305 		MACHINE=${MACHINE%64-e[bl]}
    306 		;;
    307 
    308 	evbsh3-e[bl])
    309 		makewrappermachine=${MACHINE}
    310 		MACHINE_ARCH=sh3${MACHINE##*-}
    311 		MACHINE=${MACHINE%-e[bl]}
    312 		;;
    313 
    314 	esac
    315 
    316 	# Translate a MACHINE into a default MACHINE_ARCH.
    317 	#
    318 	case "${MACHINE}" in
    319 
    320 	acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
    321 		MACHINE_ARCH=arm
    322 		;;
    323 
    324 	evbarm)		# unspecified MACHINE_ARCH gets LE
    325 		MACHINE_ARCH=${MACHINE_ARCH:=arm}
    326 		;;
    327 
    328 	hp700)
    329 		MACHINE_ARCH=hppa
    330 		;;
    331 
    332 	sun2)
    333 		MACHINE_ARCH=m68000
    334 		;;
    335 
    336 	amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
    337 		MACHINE_ARCH=m68k
    338 		;;
    339 
    340 	evbmips|sbmips)		# no default MACHINE_ARCH
    341 		;;
    342 
    343 	ews4800mips|mipsco|newsmips|sgimips)
    344 		MACHINE_ARCH=mipseb
    345 		;;
    346 
    347 	algor|arc|cobalt|hpcmips|playstation2|pmax)
    348 		MACHINE_ARCH=mipsel
    349 		;;
    350 
    351 	evbppc64|macppc64|ofppc64)
    352 		makewrappermachine=${MACHINE}
    353 		MACHINE=${MACHINE%64}
    354 		MACHINE_ARCH=powerpc64
    355 		;;
    356 
    357 	amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
    358 		MACHINE_ARCH=powerpc
    359 		;;
    360 
    361 	evbsh3)			# no default MACHINE_ARCH
    362 		;;
    363 
    364 	mmeye)
    365 		MACHINE_ARCH=sh3eb
    366 		;;
    367 
    368 	dreamcast|hpcsh|landisk)
    369 		MACHINE_ARCH=sh3el
    370 		;;
    371 
    372 	amd64)
    373 		MACHINE_ARCH=x86_64
    374 		;;
    375 
    376 	alpha|i386|sparc|sparc64|vax|ia64)
    377 		MACHINE_ARCH=${MACHINE}
    378 		;;
    379 
    380 	*)
    381 		bomb "Unknown target MACHINE: ${MACHINE}"
    382 		;;
    383 
    384 	esac
    385 }
    386 
    387 validatearch()
    388 {
    389 	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
    390 	#
    391 	case "${MACHINE_ARCH}" in
    392 
    393 	alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
    394 		;;
    395 
    396 	"")
    397 		bomb "No MACHINE_ARCH provided"
    398 		;;
    399 
    400 	*)
    401 		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
    402 		;;
    403 
    404 	esac
    405 
    406 	# Determine valid MACHINE_ARCHs for MACHINE
    407 	#
    408 	case "${MACHINE}" in
    409 
    410 	evbarm)
    411 		arches="arm armeb"
    412 		;;
    413 
    414 	evbmips|sbmips)
    415 		arches="mipseb mipsel mips64eb mips64el"
    416 		;;
    417 
    418 	sgimips)
    419 		arches="mipseb mips64eb"
    420 		;;
    421 
    422 	evbsh3)
    423 		arches="sh3eb sh3el"
    424 		;;
    425 
    426 	macppc|evbppc|ofppc)
    427 		arches="powerpc powerpc64"
    428 		;;
    429 	*)
    430 		oma="${MACHINE_ARCH}"
    431 		getarch
    432 		arches="${MACHINE_ARCH}"
    433 		MACHINE_ARCH="${oma}"
    434 		;;
    435 
    436 	esac
    437 
    438 	# Ensure that MACHINE_ARCH supports MACHINE
    439 	#
    440 	archok=false
    441 	for a in ${arches}; do
    442 		if [ "${a}" = "${MACHINE_ARCH}" ]; then
    443 			archok=true
    444 			break
    445 		fi
    446 	done
    447 	${archok} ||
    448 	    bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
    449 }
    450 
    451 nobomb_getmakevar()
    452 {
    453 	[ -x "${make}" ] || return 1
    454 	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
    455 _x_:
    456 	echo \${$1}
    457 .include <bsd.prog.mk>
    458 .include <bsd.kernobj.mk>
    459 EOF
    460 }
    461 
    462 raw_getmakevar()
    463 {
    464 	[ -x "${make}" ] || bomb "raw_getmakevar $1: ${make} is not executable"
    465 	nobomb_getmakevar "$1" || bomb "raw_getmakevar $1: ${make} failed"
    466 }
    467 
    468 getmakevar()
    469 {
    470 	# raw_getmakevar() doesn't work properly if $make hasn't yet been
    471 	# built, which can happen when running with the "-n" option.
    472 	# getmakevar() deals with this by emitting a literal '$'
    473 	# followed by the variable name, instead of trying to find the
    474 	# variable's value.
    475 	#
    476 	if [ -x "${make}" ]; then
    477 		raw_getmakevar "$1"
    478 	else
    479 		echo "\$$1"
    480 	fi
    481 }
    482 
    483 setmakeenv()
    484 {
    485 	eval "$1='$2'; export $1"
    486 	makeenv="${makeenv} $1"
    487 }
    488 
    489 unsetmakeenv()
    490 {
    491 	eval "unset $1"
    492 	makeenv="${makeenv} $1"
    493 }
    494 
    495 # Convert possibly-relative paths to absolute paths by prepending
    496 # ${TOP} if necessary.  Also delete trailing "/", if any.
    497 resolvepaths()
    498 {
    499 	_OPTARG=
    500 	for oa in ${OPTARG}; do
    501 		case "${oa}" in
    502 		/)
    503 			;;
    504 		/*)
    505 			oa="${oa%/}"
    506 			;;
    507 		*)
    508 			oa="${TOP}/${oa%/}"
    509 			;;
    510 		esac
    511 		_OPTARG="${_OPTARG} ${oa}"
    512 	done
    513 	OPTARG="${_OPTARG}"
    514 }
    515 
    516 # Convert possibly-relative path to absolute path by prepending
    517 # ${TOP} if necessary.  Also delete trailing "/", if any.
    518 resolvepath()
    519 {
    520 	case "${OPTARG}" in
    521 	/)
    522 		;;
    523 	/*)
    524 		OPTARG="${OPTARG%/}"
    525 		;;
    526 	*)
    527 		OPTARG="${TOP}/${OPTARG%/}"
    528 		;;
    529 	esac
    530 }
    531 
    532 usage()
    533 {
    534 	if [ -n "$*" ]; then
    535 		echo ""
    536 		echo "${progname}: $*"
    537 	fi
    538 	cat <<_usage_
    539 
    540 Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-C cdextras]
    541                 [-D dest] [-j njob] [-M obj] [-m mach] [-N noisy]
    542                 [-O obj] [-R release] [-S seed] [-T tools]
    543                 [-V var=[value]] [-w wrapper] [-X x11src] [-Z var]
    544                 operation [...]
    545 
    546  Build operations (all imply "obj" and "tools"):
    547     build               Run "make build".
    548     distribution        Run "make distribution" (includes DESTDIR/etc/ files).
    549     release             Run "make release" (includes kernels & distrib media).
    550 
    551  Other operations:
    552     help                Show this message and exit.
    553     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
    554                         Always performed.
    555     cleandir            Run "make cleandir".  [Default unless -u is used]
    556     obj                 Run "make obj".  [Default unless -o is used]
    557     tools               Build and install tools.
    558     install=idir        Run "make installworld" to \`idir' to install all sets
    559                         except \`etc'.  Useful after "distribution" or "release"
    560     kernel=conf         Build kernel with config file \`conf'
    561     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
    562     sets                Create binary sets in
    563                         RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
    564                         DESTDIR should be populated beforehand.
    565     sourcesets          Create source sets in RELEASEDIR/source/sets.
    566     syspkgs             Create syspkgs in
    567                         RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
    568     iso-image           Create CD-ROM image in RELEASEDIR/iso.
    569     iso-image-source    Create CD-ROM image with source in RELEASEDIR/iso.
    570     params              Display various make(1) parameters.
    571 
    572  Options:
    573     -a arch     Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
    574     -B buildId  Set BUILDID to buildId.
    575     -C cdextras Set CDEXTRA to cdextras
    576     -D dest     Set DESTDIR to dest.  [Default: destdir.MACHINE]
    577     -E          Set "expert" mode; disables various safety checks.
    578                 Should not be used without expert knowledge of the build system.
    579     -h          Print this help message.
    580     -j njob     Run up to njob jobs in parallel; see make(1) -j.
    581     -M obj      Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
    582                 Unsets MAKEOBJDIR.
    583     -m mach     Set MACHINE to mach; not required if NetBSD native.
    584     -N noisy    Set the noisyness (MAKEVERBOSE) level of the build:
    585                     0   Quiet
    586                     1   Operations are described, commands are suppressed
    587                     2   Full output
    588                 [Default: 2]
    589     -n          Show commands that would be executed, but do not execute them.
    590     -O obj      Set obj root directory to obj; sets a MAKEOBJDIR pattern.
    591                 Unsets MAKEOBJDIRPREFIX.
    592     -o          Set MKOBJDIRS=no; do not create objdirs at start of build.
    593     -R release  Set RELEASEDIR to release.  [Default: releasedir]
    594     -r          Remove contents of TOOLDIR and DESTDIR before building.
    595     -S seed     Set BUILDSEED to seed.  [Default: NetBSD-majorversion]
    596     -T tools    Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
    597                 the environment, ${toolprefix}make will be (re)built unconditionally.
    598     -U          Set MKUNPRIVED=yes; build without requiring root privileges,
    599                 install from an UNPRIVED build with proper file permissions.
    600     -u          Set MKUPDATE=yes; do not run "make cleandir" first.
    601                 Without this, everything is rebuilt, including the tools.
    602     -V v=[val]  Set variable \`v' to \`val'.
    603     -w wrapper  Create ${toolprefix}make script as wrapper.
    604                 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
    605     -X x11src   Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
    606     -x          Set MKX11=yes; build X11R6 from X11SRCDIR
    607     -Z v        Unset ("zap") variable \`v'.
    608 
    609 _usage_
    610 	exit 1
    611 }
    612 
    613 parseoptions()
    614 {
    615 	opts='a:B:C:D:Ehj:M:m:N:nO:oR:rS:T:UuV:w:xX:Z:'
    616 	opt_a=no
    617 
    618 	if type getopts >/dev/null 2>&1; then
    619 		# Use POSIX getopts.
    620 		#
    621 		getoptcmd='getopts ${opts} opt && opt=-${opt}'
    622 		optargcmd=':'
    623 		optremcmd='shift $((${OPTIND} -1))'
    624 	else
    625 		type getopt >/dev/null 2>&1 ||
    626 		    bomb "/bin/sh shell is too old; try ksh or bash"
    627 
    628 		# Use old-style getopt(1) (doesn't handle whitespace in args).
    629 		#
    630 		args="$(getopt ${opts} $*)"
    631 		[ $? = 0 ] || usage
    632 		set -- ${args}
    633 
    634 		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    635 		optargcmd='OPTARG="$1"; shift'
    636 		optremcmd=':'
    637 	fi
    638 
    639 	# Parse command line options.
    640 	#
    641 	while eval ${getoptcmd}; do
    642 		case ${opt} in
    643 
    644 		-a)
    645 			eval ${optargcmd}
    646 			MACHINE_ARCH=${OPTARG}
    647 			opt_a=yes
    648 			;;
    649 
    650 		-B)
    651 			eval ${optargcmd}
    652 			BUILDID=${OPTARG}
    653 			;;
    654 
    655 		-C)
    656 			eval ${optargcmd}; resolvepaths
    657 			iso_dir=${OPTARG}
    658 			;;
    659 
    660 		-D)
    661 			eval ${optargcmd}; resolvepath
    662 			setmakeenv DESTDIR "${OPTARG}"
    663 			;;
    664 
    665 		-E)
    666 			do_expertmode=true
    667 			;;
    668 
    669 		-j)
    670 			eval ${optargcmd}
    671 			parallel="-j ${OPTARG}"
    672 			;;
    673 
    674 		-M)
    675 			eval ${optargcmd}; resolvepath
    676 			TOP_objdir="${OPTARG}${TOP}"
    677 			unsetmakeenv MAKEOBJDIR
    678 			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
    679 			;;
    680 
    681 			# -m overrides MACHINE_ARCH unless "-a" is specified
    682 		-m)
    683 			eval ${optargcmd}
    684 			MACHINE="${OPTARG}"
    685 			[ "${opt_a}" != "yes" ] && getarch
    686 			;;
    687 
    688 		-N)
    689 			eval ${optargcmd}
    690 			case "${OPTARG}" in
    691 			0|1|2)
    692 				setmakeenv MAKEVERBOSE "${OPTARG}"
    693 				;;
    694 			*)
    695 				usage "'${OPTARG}' is not a valid value for -N"
    696 				;;
    697 			esac
    698 			;;
    699 
    700 		-n)
    701 			runcmd=echo
    702 			;;
    703 
    704 		-O)
    705 			eval ${optargcmd}; resolvepath
    706 			TOP_objdir="${OPTARG}"
    707 			unsetmakeenv MAKEOBJDIRPREFIX
    708 			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
    709 			;;
    710 
    711 		-o)
    712 			MKOBJDIRS=no
    713 			;;
    714 
    715 		-R)
    716 			eval ${optargcmd}; resolvepath
    717 			setmakeenv RELEASEDIR "${OPTARG}"
    718 			;;
    719 
    720 		-r)
    721 			do_removedirs=true
    722 			do_rebuildmake=true
    723 			;;
    724 
    725 		-S)
    726 			eval ${optargcmd}
    727 			setmakeenv BUILDSEED "${OPTARG}"
    728 			;;
    729 
    730 		-T)
    731 			eval ${optargcmd}; resolvepath
    732 			TOOLDIR="${OPTARG}"
    733 			export TOOLDIR
    734 			;;
    735 
    736 		-U)
    737 			setmakeenv MKUNPRIVED yes
    738 			;;
    739 
    740 		-u)
    741 			setmakeenv MKUPDATE yes
    742 			;;
    743 
    744 		-V)
    745 			eval ${optargcmd}
    746 			case "${OPTARG}" in
    747 		    # XXX: consider restricting which variables can be changed?
    748 			[a-zA-Z_][a-zA-Z_0-9]*=*)
    749 				setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
    750 				;;
    751 			*)
    752 				usage "-V argument must be of the form 'var=[value]'"
    753 				;;
    754 			esac
    755 			;;
    756 
    757 		-w)
    758 			eval ${optargcmd}; resolvepath
    759 			makewrapper="${OPTARG}"
    760 			;;
    761 
    762 		-X)
    763 			eval ${optargcmd}; resolvepath
    764 			setmakeenv X11SRCDIR "${OPTARG}"
    765 			;;
    766 
    767 		-x)
    768 			setmakeenv MKX11 yes
    769 			;;
    770 
    771 		-Z)
    772 			eval ${optargcmd}
    773 		    # XXX: consider restricting which variables can be unset?
    774 			unsetmakeenv "${OPTARG}"
    775 			;;
    776 
    777 		--)
    778 			break
    779 			;;
    780 
    781 		-'?'|-h)
    782 			usage
    783 			;;
    784 
    785 		esac
    786 	done
    787 
    788 	# Validate operations.
    789 	#
    790 	eval ${optremcmd}
    791 	while [ $# -gt 0 ]; do
    792 		op=$1; shift
    793 		operations="${operations} ${op}"
    794 
    795 		case "${op}" in
    796 
    797 		help)
    798 			usage
    799 			;;
    800 
    801 		makewrapper|cleandir|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
    802 			;;
    803 
    804 		iso-image)
    805 			op=iso_image	# used as part of a variable name
    806 			;;
    807 
    808 		iso-image-source)
    809 			op=iso_image_source   # used as part of a variable name
    810 			;;
    811 
    812 		kernel=*|releasekernel=*)
    813 			arg=${op#*=}
    814 			op=${op%%=*}
    815 			[ -n "${arg}" ] ||
    816 			    bomb "Must supply a kernel name with \`${op}=...'"
    817 			;;
    818 
    819 		install=*)
    820 			arg=${op#*=}
    821 			op=${op%%=*}
    822 			[ -n "${arg}" ] ||
    823 			    bomb "Must supply a directory with \`install=...'"
    824 			;;
    825 
    826 		*)
    827 			usage "Unknown operation \`${op}'"
    828 			;;
    829 
    830 		esac
    831 		eval do_${op}=true
    832 	done
    833 	[ -n "${operations}" ] || usage "Missing operation to perform."
    834 
    835 	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
    836 	#
    837 	if [ -z "${MACHINE}" ]; then
    838 		[ "${uname_s}" = "NetBSD" ] ||
    839 		    bomb "MACHINE must be set, or -m must be used, for cross builds."
    840 		MACHINE=${uname_m}
    841 	fi
    842 	[ -n "${MACHINE_ARCH}" ] || getarch
    843 	validatearch
    844 
    845 	# Set up default make(1) environment.
    846 	#
    847 	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
    848 	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
    849 	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
    850 	export MAKEFLAGS MACHINE MACHINE_ARCH
    851 }
    852 
    853 sanitycheck()
    854 {
    855 	# If the PATH contains any non-absolute components (including,
    856 	# but not limited to, "." or ""), then complain.  As an exception,
    857 	# allow "" or "." as the last component of the PATH.  This is fatal
    858 	# if expert mode is not in effect.
    859 	#
    860 	local path="${PATH}"
    861 	path="${path%:}"	# delete trailing ":"
    862 	path="${path%:.}"	# delete trailing ":."
    863 	case ":${path}:/" in
    864 	*:[!/]*)
    865 		if ${do_expertmode}; then
    866 			warning "PATH contains non-absolute components"
    867 		else
    868 			bomb "PATH environment variable must not" \
    869 			     "contain non-absolute components"
    870 		fi
    871 		;;
    872 	esac
    873 }
    874 
    875 # Try to set a value for TOOLDIR.  This is difficult because of a cyclic
    876 # dependency: TOOLDIR may be affected by settings in /etc/mk.conf, so
    877 # we would like to use getmakevar to get the value of TOOLDIR, but we
    878 # can't use getmakevar before we have an up to date version of nbmake;
    879 # we might already have an up to date version of nbmake in TOOLDIR, but
    880 # we don't yet know where TOOLDIR is.
    881 #
    882 # In principle, we could break the cycle by building a copy of nbmake
    883 # in a temporary directory.  However, people who use the default value
    884 # of TOOLDIR do not like to have nbmake rebuilt every time they run
    885 # build.sh.
    886 #
    887 # We try to please everybody as follows:
    888 #
    889 # * If TOOLDIR was set in the environment or on the command line, use
    890 #   that value.
    891 # * Otherwise try to guess what TOOLDIR would be if not overridden by
    892 #   /etc/mk.conf, and check whether the resulting directory contains
    893 #   a copy of ${toolprefix}make (this should work for everybody who
    894 #   doesn't override TOOLDIR via /etc/mk.conf);
    895 # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
    896 #   in the PATH (this might accidentally find a non-NetBSD version of
    897 #   make, which will lead to failure in the next step);
    898 # * If a copy of make was found above, try to use it with
    899 #   nobomb_getmakevar to find the correct value for TOOLDIR;
    900 # * If all else fails, leave TOOLDIR unset.  Our caller is expected to
    901 #   be able to cope with this.
    902 #
    903 try_set_TOOLDIR()
    904 {
    905 	[ -n "${TOOLDIR}" ] && return
    906 
    907 	# Set host_ostype to something like "NetBSD-4.5.6-i386".  This
    908 	# is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
    909 	#
    910 	local host_ostype="${uname_s}-$(
    911 		echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
    912 		)-$(
    913 		echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
    914 		)"
    915 
    916 	# Look in a few potential locations for
    917 	# ${possible_TOOLDIR}/bin/${toolprefix}make.
    918 	# If we find it, then set guess_make.
    919 	#
    920 	# In the usual case (without interference from environment
    921 	# variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
    922 	# "${TOP_objdir}/tooldir.${host_ostype}".  However, in practice
    923 	# we might have the wrong value of TOP_objdir, so we also try
    924 	# some other possibilities.
    925 	#
    926 	local possible_TOP_OBJ
    927 	local possible_TOOLDIR
    928 	for possible_TOP_OBJ in "${TOP_objdir}" "${TOP}" "${TOP}/obj" \
    929 		"${TOP}/obj.${MACHINE}"
    930 	do
    931 		possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
    932 		guess_make="${possible_TOOLDIR}/bin/${toolprefix}make"
    933 		if [ -x "${guess_make}" ]; then
    934 			break;
    935 		else
    936 			unset guess_make
    937 		fi
    938 	done
    939 
    940 	# If the above didn't work, search the PATH for a suitable
    941 	# ${toolprefix}make, nbmake, bmake, or make.
    942 	#
    943 	: ${guess_make:=$(find_in_PATH ${toolprefix}make '')}
    944 	: ${guess_make:=$(find_in_PATH nbmake '')}
    945 	: ${guess_make:=$(find_in_PATH bmake '')}
    946 	: ${guess_make:=$(find_in_PATH make '')}
    947 
    948 	# Use ${guess_make} with nobomb_getmakevar to try to find
    949 	# the value of TOOLDIR.  If this fails, unset TOOLDIR.
    950 	#
    951 	unset TOOLDIR
    952 	if [ -x "${guess_make}" ]; then
    953 		TOOLDIR=$(make="${guess_make}" nobomb_getmakevar TOOLDIR)
    954 		[ $? -eq 0 -a -n "${TOOLDIR}" ] || unset TOOLDIR
    955 	fi
    956 }
    957 
    958 rebuildmake()
    959 {
    960 	# Test make source file timestamps against installed ${toolprefix}make
    961 	# binary, if TOOLDIR is pre-set or if try_set_TOOLDIR can set it.
    962 	#
    963 	try_set_TOOLDIR
    964 	make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
    965 	if [ -x "${make}" ]; then
    966 		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
    967 			if [ "${f}" -nt "${make}" ]; then
    968 				statusmsg "${make} outdated (older than ${f}), needs building."
    969 				do_rebuildmake=true
    970 				break
    971 			fi
    972 		done
    973 	else
    974 		statusmsg "No ${make}, needs building."
    975 		do_rebuildmake=true
    976 	fi
    977 
    978 	# Build bootstrap ${toolprefix}make if needed.
    979 	if ${do_rebuildmake}; then
    980 		statusmsg "Bootstrapping ${toolprefix}make"
    981 		${runcmd} cd "${tmpdir}"
    982 		${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
    983 			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
    984 			${HOST_SH} "${TOP}/tools/make/configure" ||
    985 		    bomb "Configure of ${toolprefix}make failed"
    986 		${runcmd} ${HOST_SH} buildmake.sh ||
    987 		    bomb "Build of ${toolprefix}make failed"
    988 		make="${tmpdir}/${toolprefix}make"
    989 		${runcmd} cd "${TOP}"
    990 		${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
    991 	fi
    992 }
    993 
    994 validatemakeparams()
    995 {
    996 	if [ "${runcmd}" = "echo" ]; then
    997 		TOOLCHAIN_MISSING=no
    998 		EXTERNAL_TOOLCHAIN=""
    999 	else
   1000 		TOOLCHAIN_MISSING=$(raw_getmakevar TOOLCHAIN_MISSING)
   1001 		EXTERNAL_TOOLCHAIN=$(raw_getmakevar EXTERNAL_TOOLCHAIN)
   1002 	fi
   1003 	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
   1004 	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
   1005 		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
   1006 		${runcmd} echo "	MACHINE:      ${MACHINE}"
   1007 		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
   1008 		${runcmd} echo ""
   1009 		${runcmd} echo "All builds for this platform should be done via a traditional make"
   1010 		${runcmd} echo "If you wish to use an external cross-toolchain, set"
   1011 		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
   1012 		${runcmd} echo "in either the environment or mk.conf and rerun"
   1013 		${runcmd} echo "	${progname} $*"
   1014 		exit 1
   1015 	fi
   1016 
   1017 	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
   1018 	# These may be set as build.sh options or in "mk.conf".
   1019 	# Don't export them as they're only used for tests in build.sh.
   1020 	#
   1021 	MKOBJDIRS=$(getmakevar MKOBJDIRS)
   1022 	MKUNPRIVED=$(getmakevar MKUNPRIVED)
   1023 	MKUPDATE=$(getmakevar MKUPDATE)
   1024 
   1025 	if [ "${MKOBJDIRS}" != "no" ]; then
   1026 		# Try to create the top level object directory before
   1027 		# running "make obj", otherwise <bsd.own.mk> will not
   1028 		# set the correct value for _SRC_TOP_OBJ_.
   1029 		#
   1030 		# If either -M or -O was specified, then we have the
   1031 		# directory name already.
   1032 		#
   1033 		# If neither -M nor -O was specified, then try to get
   1034 		# the directory name from bsd.obj.mk's __usrobjdir
   1035 		# variable, which is set using complex rules.  This
   1036 		# works only if TOP = /usr/src.
   1037 		#
   1038 		top_obj_dir="${TOP_objdir}"
   1039 		if [ -z "${top_obj_dir}" ]; then
   1040 			if [ "$TOP" = "/usr/src" ]; then
   1041 				top_obj_dir="$(getmakevar __usrobjdir)"
   1042 			# else __usrobjdir is not actually used
   1043 			fi
   1044 
   1045 		fi
   1046 		case "$top_obj_dir" in
   1047 		*/*)
   1048 			${runcmd} mkdir -p "${top_obj_dir}" \
   1049 			|| bomb "Can't create object" \
   1050 				"directory ${top_obj_dir}"
   1051 			;;
   1052 		*)
   1053 			# We don't know what the top level object
   1054 			# directory should be, so we can't create it.
   1055 			# A nonexistant directory might cause an error
   1056 			# when we "make obj" later, but we ignore it for
   1057 			# now.
   1058 			;;
   1059 		esac
   1060 
   1061 		# make obj in tools to ensure that the objdir for the top-level
   1062 		# of the source tree and for "tools" is available, in case the
   1063 		# default TOOLDIR setting from <bsd.own.mk> is used, or the
   1064 		# build.sh default DESTDIR and RELEASEDIR is to be used.
   1065 		#
   1066 		${runcmd} cd tools
   1067 		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
   1068 		    bomb "Failed to make obj in tools"
   1069 		${runcmd} cd "${TOP}"
   1070 	fi
   1071 
   1072 	# Find TOOLDIR, DESTDIR, RELEASEDIR, and RELEASEMACHINEDIR.
   1073 	#
   1074 	TOOLDIR=$(getmakevar TOOLDIR)
   1075 	statusmsg "TOOLDIR path:     ${TOOLDIR}"
   1076 	DESTDIR=$(getmakevar DESTDIR)
   1077 	RELEASEDIR=$(getmakevar RELEASEDIR)
   1078 	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
   1079 	if ! $do_expertmode; then
   1080 		_SRC_TOP_OBJ_=$(getmakevar _SRC_TOP_OBJ_)
   1081 		: ${DESTDIR:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
   1082 		: ${RELEASEDIR:=${_SRC_TOP_OBJ_}/releasedir}
   1083 		makeenv="${makeenv} DESTDIR RELEASEDIR"
   1084 	fi
   1085 	export TOOLDIR DESTDIR RELEASEDIR
   1086 	statusmsg "DESTDIR path:     ${DESTDIR}"
   1087 	statusmsg "RELEASEDIR path:  ${RELEASEDIR}"
   1088 
   1089 	# Check validity of TOOLDIR and DESTDIR.
   1090 	#
   1091 	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
   1092 		bomb "TOOLDIR '${TOOLDIR}' invalid"
   1093 	fi
   1094 	removedirs="${TOOLDIR}"
   1095 
   1096 	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
   1097 		if ${do_build} || ${do_distribution} || ${do_release}; then
   1098 			if ! ${do_build} || \
   1099 			   [ "${uname_s}" != "NetBSD" ] || \
   1100 			   [ "${uname_m}" != "${MACHINE}" ]; then
   1101 				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
   1102 			fi
   1103 			if ! ${do_expertmode}; then
   1104 				bomb "DESTDIR must != / for non -E (expert) builds"
   1105 			fi
   1106 			statusmsg "WARNING: Building to /, in expert mode."
   1107 			statusmsg "         This may cause your system to break!  Reasons include:"
   1108 			statusmsg "            - your kernel is not up to date"
   1109 			statusmsg "            - the libraries or toolchain have changed"
   1110 			statusmsg "         YOU HAVE BEEN WARNED!"
   1111 		fi
   1112 	else
   1113 		removedirs="${removedirs} ${DESTDIR}"
   1114 	fi
   1115 	if ${do_build} || ${do_distribution} || ${do_release}; then
   1116 		if ! ${do_expertmode} && \
   1117 		    [ "$(id -u 2>/dev/null)" -ne 0 ] && \
   1118 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1119 			bomb "-U or -E must be set for build as an unprivileged user."
   1120 		fi
   1121 	fi
   1122 	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
   1123 		bomb "Must set RELEASEDIR with \`releasekernel=...'"
   1124 	fi
   1125 
   1126 	# Install as non-root is a bad idea.
   1127 	#
   1128 	if ${do_install} && [ "$(id -u 2>/dev/null)" -ne 0 ] ; then
   1129 		if ${do_expertmode}; then
   1130 			warning "Will install as an unprivileged user."
   1131 		else
   1132 			bomb "-E must be set for install as an unprivileged user."
   1133 		fi
   1134 	fi
   1135 
   1136 	# If a previous build.sh run used -U (and therefore created a
   1137 	# METALOG file), then most subsequent build.sh runs must also
   1138 	# use -U.  If DESTDIR is about to be removed, then don't perform
   1139 	# this check.
   1140 	#
   1141 	case "${do_removedirs} ${removedirs} " in
   1142 	true*" ${DESTDIR} "*)
   1143 		# DESTDIR is about to be removed
   1144 		;;
   1145 	*)
   1146 		if ( ${do_build} || ${do_distribution} || ${do_release} || \
   1147 		    ${do_install} ) && \
   1148 		    [ -e "${DESTDIR}/METALOG" ] && \
   1149 		    [ "${MKUNPRIVED}" = "no" ] ; then
   1150 			if $do_expertmode; then
   1151 				warning "A previous build.sh run specified -U."
   1152 			else
   1153 				bomb "A previous build.sh run specified -U; you must specify it again now."
   1154 			fi
   1155 		fi
   1156 		;;
   1157 	esac
   1158 }
   1159 
   1160 
   1161 createmakewrapper()
   1162 {
   1163 	# Remove the target directories.
   1164 	#
   1165 	if ${do_removedirs}; then
   1166 		for f in ${removedirs}; do
   1167 			statusmsg "Removing ${f}"
   1168 			${runcmd} rm -r -f "${f}"
   1169 		done
   1170 	fi
   1171 
   1172 	# Recreate $TOOLDIR.
   1173 	#
   1174 	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
   1175 	    bomb "mkdir of '${TOOLDIR}/bin' failed"
   1176 
   1177 	# Install ${toolprefix}make if it was built.
   1178 	#
   1179 	if ${do_rebuildmake}; then
   1180 		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
   1181 		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
   1182 		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
   1183 		make="${TOOLDIR}/bin/${toolprefix}make"
   1184 		statusmsg "Created ${make}"
   1185 	fi
   1186 
   1187 	# Build a ${toolprefix}make wrapper script, usable by hand as
   1188 	# well as by build.sh.
   1189 	#
   1190 	if [ -z "${makewrapper}" ]; then
   1191 		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
   1192 		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
   1193 	fi
   1194 
   1195 	${runcmd} rm -f "${makewrapper}"
   1196 	if [ "${runcmd}" = "echo" ]; then
   1197 		echo 'cat <<EOF >'${makewrapper}
   1198 		makewrapout=
   1199 	else
   1200 		makewrapout=">>\${makewrapper}"
   1201 	fi
   1202 
   1203 	case "${KSH_VERSION:-${SH_VERSION}}" in
   1204 	*PD\ KSH*|*MIRBSD\ KSH*)
   1205 		set +o braceexpand
   1206 		;;
   1207 	esac
   1208 
   1209 	eval cat <<EOF ${makewrapout}
   1210 #! ${HOST_SH}
   1211 # Set proper variables to allow easy "make" building of a NetBSD subtree.
   1212 # Generated from:  \$NetBSD: build.sh,v 1.198.2.3 2009/03/18 05:39:06 snj Exp $
   1213 # with these arguments: ${_args}
   1214 #
   1215 
   1216 EOF
   1217 	{
   1218 		for f in ${makeenv}; do
   1219 			if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
   1220 				eval echo "unset ${f}"
   1221 			else
   1222 				eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
   1223 			fi
   1224 		done
   1225 
   1226 		eval cat <<EOF
   1227 MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
   1228 USETOOLS=yes; export USETOOLS
   1229 EOF
   1230 	} | eval sort -u "${makewrapout}"
   1231 	eval cat <<EOF "${makewrapout}"
   1232 
   1233 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
   1234 EOF
   1235 	[ "${runcmd}" = "echo" ] && echo EOF
   1236 	${runcmd} chmod +x "${makewrapper}"
   1237 	statusmsg "makewrapper:      ${makewrapper}"
   1238 	statusmsg "Updated ${makewrapper}"
   1239 }
   1240 
   1241 buildtools()
   1242 {
   1243 	if [ "${MKOBJDIRS}" != "no" ]; then
   1244 		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
   1245 		    bomb "Failed to make obj-tools"
   1246 	fi
   1247 	${runcmd} cd tools
   1248 	if [ "${MKUPDATE}" = "no" ]; then
   1249 		${runcmd} "${makewrapper}" ${parallel} cleandir ||
   1250 		    bomb "Failed to make cleandir tools"
   1251 	fi
   1252 	${runcmd} "${makewrapper}" ${parallel} dependall ||
   1253 	    bomb "Failed to make dependall tools"
   1254 	${runcmd} "${makewrapper}" ${parallel} install ||
   1255 	    bomb "Failed to make install tools"
   1256 	statusmsg "Tools built to ${TOOLDIR}"
   1257 	${runcmd} cd "${TOP}"
   1258 }
   1259 
   1260 getkernelconf()
   1261 {
   1262 	kernelconf="$1"
   1263 	if [ "${MKOBJDIRS}" != "no" ]; then
   1264 		# The correct value of KERNOBJDIR might
   1265 		# depend on a prior "make obj" in
   1266 		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
   1267 		#
   1268 		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
   1269 		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
   1270 		${runcmd} cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
   1271 		${runcmd} "${makewrapper}" ${parallel} obj ||
   1272 		    bomb "Failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
   1273 		${runcmd} cd "${TOP}"
   1274 	fi
   1275 	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
   1276 	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
   1277 	case "${kernelconf}" in
   1278 	*/*)
   1279 		kernelconfpath="${kernelconf}"
   1280 		kernelconfname="${kernelconf##*/}"
   1281 		;;
   1282 	*)
   1283 		kernelconfpath="${KERNCONFDIR}/${kernelconf}"
   1284 		kernelconfname="${kernelconf}"
   1285 		;;
   1286 	esac
   1287 	kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
   1288 }
   1289 
   1290 buildkernel()
   1291 {
   1292 	if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
   1293 		# Building tools every time we build a kernel is clearly
   1294 		# unnecessary.  We could try to figure out whether rebuilding
   1295 		# the tools is necessary this time, but it doesn't seem worth
   1296 		# the trouble.  Instead, we say it's the user's responsibility
   1297 		# to rebuild the tools if necessary.
   1298 		#
   1299 		statusmsg "Building kernel without building new tools"
   1300 		buildkernelwarned=true
   1301 	fi
   1302 	getkernelconf $1
   1303 	statusmsg "Building kernel:  ${kernelconf}"
   1304 	statusmsg "Build directory:  ${kernelbuildpath}"
   1305 	${runcmd} mkdir -p "${kernelbuildpath}" ||
   1306 	    bomb "Cannot mkdir: ${kernelbuildpath}"
   1307 	if [ "${MKUPDATE}" = "no" ]; then
   1308 		${runcmd} cd "${kernelbuildpath}"
   1309 		${runcmd} "${makewrapper}" ${parallel} cleandir ||
   1310 		    bomb "Failed to make cleandir in ${kernelbuildpath}"
   1311 		${runcmd} cd "${TOP}"
   1312 	fi
   1313 	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
   1314 	|| bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
   1315 	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
   1316 		-s "${TOP}/sys" "${kernelconfpath}" ||
   1317 	    bomb "${toolprefix}config failed for ${kernelconf}"
   1318 	${runcmd} cd "${kernelbuildpath}"
   1319 	${runcmd} "${makewrapper}" ${parallel} depend ||
   1320 	    bomb "Failed to make depend in ${kernelbuildpath}"
   1321 	${runcmd} "${makewrapper}" ${parallel} all ||
   1322 	    bomb "Failed to make all in ${kernelbuildpath}"
   1323 	${runcmd} cd "${TOP}"
   1324 
   1325 	if [ "${runcmd}" != "echo" ]; then
   1326 		statusmsg "Kernels built from ${kernelconf}:"
   1327 		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1328 		for kern in ${kernlist:-netbsd}; do
   1329 			[ -f "${kernelbuildpath}/${kern}" ] && \
   1330 			    echo "  ${kernelbuildpath}/${kern}"
   1331 		done | tee -a "${results}"
   1332 	fi
   1333 }
   1334 
   1335 releasekernel()
   1336 {
   1337 	getkernelconf $1
   1338 	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
   1339 	${runcmd} mkdir -p "${kernelreldir}"
   1340 	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
   1341 	for kern in ${kernlist:-netbsd}; do
   1342 		builtkern="${kernelbuildpath}/${kern}"
   1343 		[ -f "${builtkern}" ] || continue
   1344 		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
   1345 		statusmsg "Kernel copy:      ${releasekern}"
   1346 		if [ "${runcmd}" = "echo" ]; then
   1347 			echo "gzip -c -9 < ${builtkern} > ${releasekern}"
   1348 		else
   1349 			gzip -c -9 < "${builtkern}" > "${releasekern}"
   1350 		fi
   1351 	done
   1352 }
   1353 
   1354 installworld()
   1355 {
   1356 	dir="$1"
   1357 	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
   1358 	    bomb "Failed to make installworld to ${dir}"
   1359 	statusmsg "Successful installworld to ${dir}"
   1360 }
   1361 
   1362 
   1363 main()
   1364 {
   1365 	initdefaults
   1366 	_args=$@
   1367 	parseoptions "$@"
   1368 
   1369 	sanitycheck
   1370 
   1371 	build_start=$(date)
   1372 	statusmsg "${progname} command: $0 $@"
   1373 	statusmsg "${progname} started: ${build_start}"
   1374 	statusmsg "NetBSD version:   ${DISTRIBVER}"
   1375 	statusmsg "MACHINE:          ${MACHINE}"
   1376 	statusmsg "MACHINE_ARCH:     ${MACHINE_ARCH}"
   1377 	statusmsg "Build platform:   ${uname_s} ${uname_r} ${uname_m}"
   1378 	statusmsg "HOST_SH:          ${HOST_SH}"
   1379 
   1380 	rebuildmake
   1381 	validatemakeparams
   1382 	createmakewrapper
   1383 
   1384 	# Perform the operations.
   1385 	#
   1386 	for op in ${operations}; do
   1387 		case "${op}" in
   1388 
   1389 		makewrapper)
   1390 			# no-op
   1391 			;;
   1392 
   1393 		tools)
   1394 			buildtools
   1395 			;;
   1396 
   1397 		sets)
   1398 			statusmsg "Building sets from pre-populated ${DESTDIR}"
   1399 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1400 			    bomb "Failed to make ${op}"
   1401 			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
   1402 			statusmsg "Built sets to ${setdir}"
   1403 			;;
   1404 
   1405 		cleandir|obj|build|distribution|release|sourcesets|syspkgs|params)
   1406 			${runcmd} "${makewrapper}" ${parallel} ${op} ||
   1407 			    bomb "Failed to make ${op}"
   1408 			statusmsg "Successful make ${op}"
   1409 			;;
   1410 
   1411 		iso-image|iso-image-source)
   1412 			${runcmd} "${makewrapper}" ${parallel} \
   1413 			    CDEXTRA="$iso_dir" ${op} ||
   1414 			    bomb "Failed to make ${op}"
   1415 			statusmsg "Successful make ${op}"
   1416 			;;
   1417 
   1418 		kernel=*)
   1419 			arg=${op#*=}
   1420 			buildkernel "${arg}"
   1421 			;;
   1422 
   1423 		releasekernel=*)
   1424 			arg=${op#*=}
   1425 			releasekernel "${arg}"
   1426 			;;
   1427 
   1428 		install=*)
   1429 			arg=${op#*=}
   1430 			if [ "${arg}" = "/" ] && \
   1431 			    (	[ "${uname_s}" != "NetBSD" ] || \
   1432 				[ "${uname_m}" != "${MACHINE}" ] ); then
   1433 				bomb "'${op}' must != / for cross builds."
   1434 			fi
   1435 			installworld "${arg}"
   1436 			;;
   1437 
   1438 		*)
   1439 			bomb "Unknown operation \`${op}'"
   1440 			;;
   1441 
   1442 		esac
   1443 	done
   1444 
   1445 	statusmsg "${progname} ended:   $(date)"
   1446 	if [ -s "${results}" ]; then
   1447 		echo "===> Summary of results:"
   1448 		sed -e 's/^===>//;s/^/	/' "${results}"
   1449 		echo "===> ."
   1450 	fi
   1451 }
   1452 
   1453 main "$@"
   1454