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