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