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