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