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