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