build.sh revision 1.246
1#! /usr/bin/env sh
2#	$NetBSD: build.sh,v 1.246 2011/08/15 14:48:00 wiz Exp $
3#
4# Copyright (c) 2001-2009 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#
19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31#
32# Top level build wrapper, for a system containing no tools.
33#
34# This script should run on any POSIX-compliant shell.  If the
35# first "sh" found in the PATH is a POSIX-compliant shell, then
36# you should not need to take any special action.  Otherwise, you
37# should set the environment variable HOST_SH to a POSIX-compliant
38# shell, and invoke build.sh with that shell.  (Depending on your
39# system, one of /bin/ksh, /usr/local/bin/bash, or /usr/xpg4/bin/sh
40# might be a suitable shell.)
41#
42
43progname=${0##*/}
44toppid=$$
45results=/dev/null
46tab='	'
47trap "exit 1" 1 2 3 15
48
49bomb()
50{
51	cat >&2 <<ERRORMESSAGE
52
53ERROR: $@
54*** BUILD ABORTED ***
55ERRORMESSAGE
56	kill ${toppid}		# in case we were invoked from a subshell
57	exit 1
58}
59
60
61statusmsg()
62{
63	${runcmd} echo "===> $@" | tee -a "${results}"
64}
65
66statusmsg2()
67{
68	local msg
69
70	msg="${1}"
71	shift
72	case "${msg}" in
73	????????????????*)	;;
74	??????????*)		msg="${msg}      ";;
75	?????*)			msg="${msg}           ";;
76	*)			msg="${msg}                ";;
77	esac
78	case "${msg}" in
79	?????????????????????*)	;;
80	????????????????????)	msg="${msg} ";;
81	???????????????????)	msg="${msg}  ";;
82	??????????????????)	msg="${msg}   ";;
83	?????????????????)	msg="${msg}    ";;
84	????????????????)	msg="${msg}     ";;
85	esac
86	statusmsg "${msg}$*"
87}
88
89warning()
90{
91	statusmsg "Warning: $@"
92}
93
94# Find a program in the PATH, and print the result.  If not found,
95# print a default.  If $2 is defined (even if it is an empty string),
96# then that is the default; otherwise, $1 is used as the default.
97find_in_PATH()
98{
99	local prog="$1"
100	local result="${2-"$1"}"
101	local oldIFS="${IFS}"
102	local dir
103	IFS=":"
104	for dir in ${PATH}; do
105		if [ -x "${dir}/${prog}" ]; then
106			result="${dir}/${prog}"
107			break
108		fi
109	done
110	IFS="${oldIFS}"
111	echo "${result}"
112}
113
114# Try to find a working POSIX shell, and set HOST_SH to refer to it.
115# Assumes that uname_s, uname_m, and PWD have been set.
116set_HOST_SH()
117{
118	# Even if ${HOST_SH} is already defined, we still do the
119	# sanity checks at the end.
120
121	# Solaris has /usr/xpg4/bin/sh.
122	#
123	[ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
124		[ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
125
126	# Try to get the name of the shell that's running this script,
127	# by parsing the output from "ps".  We assume that, if the host
128	# system's ps command supports -o comm at all, it will do so
129	# in the usual way: a one-line header followed by a one-line
130	# result, possibly including trailing white space.  And if the
131	# host system's ps command doesn't support -o comm, we assume
132	# that we'll get an error message on stderr and nothing on
133	# stdout.  (We don't try to use ps -o 'comm=' to suppress the
134	# header line, because that is less widely supported.)
135	#
136	# If we get the wrong result here, the user can override it by
137	# specifying HOST_SH in the environment.
138	#
139	[ -z "${HOST_SH}" ] && HOST_SH="$(
140		(ps -p $$ -o comm | sed -ne "2s/[ ${tab}]*\$//p") 2>/dev/null )"
141
142	# If nothing above worked, use "sh".  We will later find the
143	# first directory in the PATH that has a "sh" program.
144	#
145	[ -z "${HOST_SH}" ] && HOST_SH="sh"
146
147	# If the result so far is not an absolute path, try to prepend
148	# PWD or search the PATH.
149	#
150	case "${HOST_SH}" in
151	/*)	:
152		;;
153	*/*)	HOST_SH="${PWD}/${HOST_SH}"
154		;;
155	*)	HOST_SH="$(find_in_PATH "${HOST_SH}")"
156		;;
157	esac
158
159	# If we don't have an absolute path by now, bomb.
160	#
161	case "${HOST_SH}" in
162	/*)	:
163		;;
164	*)	bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
165		;;
166	esac
167
168	# If HOST_SH is not executable, bomb.
169	#
170	[ -x "${HOST_SH}" ] ||
171	    bomb "HOST_SH=\"${HOST_SH}\" is not executable."
172}
173
174initdefaults()
175{
176	makeenv=
177	makewrapper=
178	makewrappermachine=
179	runcmd=
180	operations=
181	removedirs=
182
183	[ -d usr.bin/make ] || cd "$(dirname $0)"
184	[ -d usr.bin/make ] ||
185	    bomb "build.sh must be run from the top source level"
186	[ -f share/mk/bsd.own.mk ] ||
187	    bomb "src/share/mk is missing; please re-fetch the source tree"
188
189	# Set LC_ALL=C before we try to parse the output from any command
190	setmakeenv LC_ALL C
191
192	# Find information about the build platform.  This should be
193	# kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
194	# variables in share/mk/bsd.sys.mk.
195	#
196	# Note that "uname -p" is not part of POSIX, but we want uname_p
197	# to be set to the host MACHINE_ARCH, if possible.  On systems
198	# where "uname -p" fails, prints "unknown", or prints a string
199	# that does not look like an identifier, fall back to using the
200	# output from "uname -m" instead.
201	#
202	uname_s=$(uname -s 2>/dev/null)
203	uname_r=$(uname -r 2>/dev/null)
204	uname_m=$(uname -m 2>/dev/null)
205	uname_p=$(uname -p 2>/dev/null || echo "unknown")
206	case "${uname_p}" in
207	''|unknown|*[^-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
208	esac
209
210	id_u=$(id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null)
211
212	# If $PWD is a valid name of the current directory, POSIX mandates
213	# that pwd return it by default which causes problems in the
214	# presence of symlinks.  Unsetting PWD is simpler than changing
215	# every occurrence of pwd to use -P.
216	#
217	# XXX Except that doesn't work on Solaris. Or many Linuces.
218	#
219	unset PWD
220	TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
221
222	# The user can set HOST_SH in the environment, or we try to
223	# guess an appropriate value.  Then we set several other
224	# variables from HOST_SH.
225	#
226	set_HOST_SH
227	setmakeenv HOST_SH "${HOST_SH}"
228	setmakeenv BSHELL "${HOST_SH}"
229	setmakeenv CONFIG_SHELL "${HOST_SH}"
230
231	# Set defaults.
232	#
233	toolprefix=nb
234
235	# Some systems have a small ARG_MAX.  -X prevents make(1) from
236	# exporting variables in the environment redundantly.
237	#
238	case "${uname_s}" in
239	Darwin | FreeBSD | CYGWIN*)
240		MAKEFLAGS=-X
241		;;
242	*)
243		MAKEFLAGS=
244		;;
245	esac
246
247	# do_{operation}=true if given operation is requested.
248	#
249	do_expertmode=false
250	do_rebuildmake=false
251	do_removedirs=false
252	do_tools=false
253	do_cleandir=false
254	do_obj=false
255	do_build=false
256	do_distribution=false
257	do_release=false
258	do_kernel=false
259	do_releasekernel=false
260	do_modules=false
261	do_installmodules=false
262	do_install=false
263	do_sets=false
264	do_sourcesets=false
265	do_syspkgs=false
266	do_iso_image=false
267	do_iso_image_source=false
268	do_params=false
269	do_rump=false
270
271	# done_{operation}=true if given operation has been done.
272	#
273	done_rebuildmake=false
274
275	# Create scratch directory
276	#
277	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
278	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
279	trap "cd /; rm -r -f \"${tmpdir}\"" 0
280	results="${tmpdir}/build.sh.results"
281
282	# Set source directories
283	#
284	setmakeenv NETBSDSRCDIR "${TOP}"
285
286	# Make sure KERNOBJDIR is an absolute path if defined
287	#
288	case "${KERNOBJDIR}" in
289	''|/*)	;;
290	*)	KERNOBJDIR="${TOP}/${KERNOBJDIR}"
291		setmakeenv KERNOBJDIR "${KERNOBJDIR}"
292		;;
293	esac
294
295	# Find the version of NetBSD
296	#
297	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
298
299	# Set the BUILDSEED to NetBSD-"N"
300	#
301	setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
302
303	# Set MKARZERO to "yes"
304	#
305	setmakeenv MKARZERO "yes"
306
307	# Set various environment variables to known defaults,
308	# to minimize (cross-)build problems observed "in the field".
309	#
310	unsetmakeenv INFODIR
311	unsetmakeenv LESSCHARSET
312}
313
314getarch()
315{
316	# Translate some MACHINE name aliases (known only to build.sh)
317	# into proper MACHINE and MACHINE_ARCH names.  Save the alias
318	# name in makewrappermachine.
319	#
320	case "${MACHINE}" in
321
322	evbarm-e[bl])
323		makewrappermachine=${MACHINE}
324		# MACHINE_ARCH is "arm" or "armeb", not "armel"
325		MACHINE_ARCH=arm${MACHINE##*-}
326		MACHINE_ARCH=${MACHINE_ARCH%el}
327		MACHINE=${MACHINE%-e[bl]}
328		;;
329
330	evbmips-e[bl]|sbmips-e[bl])
331		makewrappermachine=${MACHINE}
332		MACHINE_ARCH=mips${MACHINE##*-}
333		MACHINE=${MACHINE%-e[bl]}
334		;;
335
336	evbmips64-e[bl]|sbmips64-e[bl])
337		makewrappermachine=${MACHINE}
338		MACHINE_ARCH=mips64${MACHINE##*-}
339		MACHINE=${MACHINE%64-e[bl]}
340		;;
341
342	evbsh3-e[bl])
343		makewrappermachine=${MACHINE}
344		MACHINE_ARCH=sh3${MACHINE##*-}
345		MACHINE=${MACHINE%-e[bl]}
346		;;
347
348	esac
349
350	# Translate a MACHINE into a default MACHINE_ARCH.
351	#
352	case "${MACHINE}" in
353
354	acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
355		MACHINE_ARCH=arm
356		;;
357
358	evbarm)		# unspecified MACHINE_ARCH gets LE
359		MACHINE_ARCH=${MACHINE_ARCH:=arm}
360		;;
361
362	hp700)
363		MACHINE_ARCH=hppa
364		;;
365
366	sun2)
367		MACHINE_ARCH=m68000
368		;;
369
370	amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
371		MACHINE_ARCH=m68k
372		;;
373
374	evbmips|sbmips)		# no default MACHINE_ARCH
375		;;
376
377	sgimips64)
378		makewrappermachine=${MACHINE}
379		MACHINE=${MACHINE%64}
380		MACHINE_ARCH=mips64eb
381		;;
382
383	ews4800mips|mipsco|newsmips|sgimips|emips)
384		MACHINE_ARCH=mipseb
385		;;
386
387	algor64|arc64|cobalt64|pmax64)
388		makewrappermachine=${MACHINE}
389		MACHINE=${MACHINE%64}
390		MACHINE_ARCH=mips64el
391		;;
392
393	algor|arc|cobalt|hpcmips|pmax)
394		MACHINE_ARCH=mipsel
395		;;
396
397	evbppc64|macppc64|ofppc64)
398		makewrappermachine=${MACHINE}
399		MACHINE=${MACHINE%64}
400		MACHINE_ARCH=powerpc64
401		;;
402
403	amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
404		MACHINE_ARCH=powerpc
405		;;
406
407	evbsh3)			# no default MACHINE_ARCH
408		;;
409
410	mmeye)
411		MACHINE_ARCH=sh3eb
412		;;
413
414	dreamcast|hpcsh|landisk)
415		MACHINE_ARCH=sh3el
416		;;
417
418	amd64)
419		MACHINE_ARCH=x86_64
420		;;
421
422	alpha|i386|sparc|sparc64|vax|ia64)
423		MACHINE_ARCH=${MACHINE}
424		;;
425
426	*)
427		bomb "Unknown target MACHINE: ${MACHINE}"
428		;;
429
430	esac
431}
432
433validatearch()
434{
435	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
436	#
437	case "${MACHINE_ARCH}" in
438
439	alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
440		;;
441
442	"")
443		bomb "No MACHINE_ARCH provided"
444		;;
445
446	*)
447		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
448		;;
449
450	esac
451
452	# Determine valid MACHINE_ARCHs for MACHINE
453	#
454	case "${MACHINE}" in
455
456	evbarm)
457		arches="arm armeb"
458		;;
459
460	algor|arc|cobalt|pmax)
461		arches="mipsel mips64el"
462		;;
463
464	evbmips|sbmips)
465		arches="mipseb mipsel mips64eb mips64el"
466		;;
467
468	sgimips)
469		arches="mipseb mips64eb"
470		;;
471
472	evbsh3)
473		arches="sh3eb sh3el"
474		;;
475
476	macppc|evbppc|ofppc)
477		arches="powerpc powerpc64"
478		;;
479	*)
480		oma="${MACHINE_ARCH}"
481		getarch
482		arches="${MACHINE_ARCH}"
483		MACHINE_ARCH="${oma}"
484		;;
485
486	esac
487
488	# Ensure that MACHINE_ARCH supports MACHINE
489	#
490	archok=false
491	for a in ${arches}; do
492		if [ "${a}" = "${MACHINE_ARCH}" ]; then
493			archok=true
494			break
495		fi
496	done
497	${archok} ||
498	    bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
499}
500
501# nobomb_getmakevar --
502# Given the name of a make variable in $1, print make's idea of the
503# value of that variable, or return 1 if there's an error.
504#
505nobomb_getmakevar()
506{
507	[ -x "${make}" ] || return 1
508	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
509_x_:
510	echo \${$1}
511.include <bsd.prog.mk>
512.include <bsd.kernobj.mk>
513EOF
514}
515
516# nobomb_getmakevar --
517# Given the name of a make variable in $1, print make's idea of the
518# value of that variable, or bomb if there's an error.
519#
520bomb_getmakevar()
521{
522	[ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
523	nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
524}
525
526# nobomb_getmakevar --
527# Given the name of a make variable in $1, print make's idea of the
528# value of that variable, or print a literal '$' followed by the
529# variable name if ${make} is not executable.  This is intended for use in
530# messages that need to be readable even if $make hasn't been built,
531# such as when build.sh is run with the "-n" option.
532#
533getmakevar()
534{
535	if [ -x "${make}" ]; then
536		bomb_getmakevar "$1"
537	else
538		echo "\$$1"
539	fi
540}
541
542setmakeenv()
543{
544	eval "$1='$2'; export $1"
545	makeenv="${makeenv} $1"
546}
547
548unsetmakeenv()
549{
550	eval "unset $1"
551	makeenv="${makeenv} $1"
552}
553
554# Given a variable name in $1, modify the variable in place as follows:
555# For each space-separated word in the variable, call resolvepath.
556resolvepaths()
557{
558	local var="$1"
559	local val
560	eval val=\"\${${var}}\"
561	local newval=''
562	local word
563	for word in ${val}; do
564		resolvepath word
565		newval="${newval}${newval:+ }${word}"
566	done
567	eval ${var}=\"\${newval}\"
568}
569
570# Given a variable name in $1, modify the variable in place as follows:
571# Convert possibly-relative path to absolute path by prepending
572# ${TOP} if necessary.  Also delete trailing "/", if any.
573resolvepath()
574{
575	local var="$1"
576	local val
577	eval val=\"\${${var}}\"
578	case "${val}" in
579	/)
580		;;
581	/*)
582		val="${val%/}"
583		;;
584	*)
585		val="${TOP}/${val%/}"
586		;;
587	esac
588	eval ${var}=\"\${val}\"
589}
590
591usage()
592{
593	if [ -n "$*" ]; then
594		echo ""
595		echo "${progname}: $*"
596	fi
597	cat <<_usage_
598
599Usage: ${progname} [-EhnorUuxy] [-a arch] [-B buildid] [-C cdextras]
600                [-D dest] [-j njob] [-M obj] [-m mach] [-N noisy]
601                [-O obj] [-R release] [-S seed] [-T tools]
602                [-V var=[value]] [-w wrapper] [-X x11src] [-Y extsrcsrc]
603                [-Z var]
604                operation [...]
605
606 Build operations (all imply "obj" and "tools"):
607    build               Run "make build".
608    distribution        Run "make distribution" (includes DESTDIR/etc/ files).
609    release             Run "make release" (includes kernels & distrib media).
610
611 Other operations:
612    help                Show this message and exit.
613    makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
614                        Always performed.
615    cleandir            Run "make cleandir".  [Default unless -u is used]
616    obj                 Run "make obj".  [Default unless -o is used]
617    tools               Build and install tools.
618    install=idir        Run "make installworld" to \`idir' to install all sets
619                        except \`etc'.  Useful after "distribution" or "release"
620    kernel=conf         Build kernel with config file \`conf'
621    releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
622    installmodules=idir Run "make installmodules" to \`idir' to install all
623                        kernel modules.
624    modules             Build kernel modules.
625    rumptest            Do a linktest for rump (for developers).
626    sets                Create binary sets in
627                        RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
628                        DESTDIR should be populated beforehand.
629    sourcesets          Create source sets in RELEASEDIR/source/sets.
630    syspkgs             Create syspkgs in
631                        RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
632    iso-image           Create CD-ROM image in RELEASEDIR/iso.
633    iso-image-source    Create CD-ROM image with source in RELEASEDIR/iso.
634    params              Display various make(1) parameters.
635
636 Options:
637    -a arch        Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
638    -B buildid     Set BUILDID to buildid.
639    -C cdextras    Append cdextras to CDEXTRA variable for inclusion on CD-ROM.
640    -D dest        Set DESTDIR to dest.  [Default: destdir.MACHINE]
641    -E             Set "expert" mode; disables various safety checks.
642                   Should not be used without expert knowledge of the build system.
643    -h             Print this help message.
644    -j njob        Run up to njob jobs in parallel; see make(1) -j.
645    -M obj         Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
646                   Unsets MAKEOBJDIR.
647    -m mach        Set MACHINE to mach; not required if NetBSD native.
648    -N noisy       Set the noisyness (MAKEVERBOSE) level of the build:
649                       0   Minimal output ("quiet")
650                       1   Describe what is occurring
651                       2   Describe what is occurring and echo the actual command
652                       3   Ignore the effect of the "@" prefix in make commands
653                       4   Trace shell commands using the shell's -x flag
654                   [Default: 2]
655    -n             Show commands that would be executed, but do not execute them.
656    -O obj         Set obj root directory to obj; sets a MAKEOBJDIR pattern.
657                   Unsets MAKEOBJDIRPREFIX.
658    -o             Set MKOBJDIRS=no; do not create objdirs at start of build.
659    -R release     Set RELEASEDIR to release.  [Default: releasedir]
660    -r             Remove contents of TOOLDIR and DESTDIR before building.
661    -S seed        Set BUILDSEED to seed.  [Default: NetBSD-majorversion]
662    -T tools       Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
663                   the environment, ${toolprefix}make will be (re)built
664                   unconditionally.
665    -U             Set MKUNPRIVED=yes; build without requiring root privileges,
666                   install from an UNPRIVED build with proper file permissions.
667    -u             Set MKUPDATE=yes; do not run "make cleandir" first.
668                   Without this, everything is rebuilt, including the tools.
669    -V var=[value] Set variable \`var' to \`value'.
670    -w wrapper     Create ${toolprefix}make script as wrapper.
671                   [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
672    -X x11src      Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
673    -x             Set MKX11=yes; build X11 from X11SRCDIR
674    -Y extsrcsrc   Set EXTSRCSRCDIR to extsrcsrc.  [Default: /usr/extsrc]
675    -y             Set MKEXTSRC=yes; build extsrc from EXTSRCSRCDIR
676    -Z var         Unset ("zap") variable \`var'.
677
678_usage_
679	exit 1
680}
681
682parseoptions()
683{
684	opts='a:B:C:D:Ehj:M:m:N:nO:oR:rS:T:UuV:w:X:xY:yZ:'
685	opt_a=no
686
687	if type getopts >/dev/null 2>&1; then
688		# Use POSIX getopts.
689		#
690		getoptcmd='getopts ${opts} opt && opt=-${opt}'
691		optargcmd=':'
692		optremcmd='shift $((${OPTIND} -1))'
693	else
694		type getopt >/dev/null 2>&1 ||
695		    bomb "/bin/sh shell is too old; try ksh or bash"
696
697		# Use old-style getopt(1) (doesn't handle whitespace in args).
698		#
699		args="$(getopt ${opts} $*)"
700		[ $? = 0 ] || usage
701		set -- ${args}
702
703		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
704		optargcmd='OPTARG="$1"; shift'
705		optremcmd=':'
706	fi
707
708	# Parse command line options.
709	#
710	while eval ${getoptcmd}; do
711		case ${opt} in
712
713		-a)
714			eval ${optargcmd}
715			MACHINE_ARCH=${OPTARG}
716			opt_a=yes
717			;;
718
719		-B)
720			eval ${optargcmd}
721			BUILDID=${OPTARG}
722			;;
723
724		-C)
725			eval ${optargcmd}; resolvepaths OPTARG
726			CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
727			;;
728
729		-D)
730			eval ${optargcmd}; resolvepath OPTARG
731			setmakeenv DESTDIR "${OPTARG}"
732			;;
733
734		-E)
735			do_expertmode=true
736			;;
737
738		-j)
739			eval ${optargcmd}
740			parallel="-j ${OPTARG}"
741			;;
742
743		-M)
744			eval ${optargcmd}; resolvepath OPTARG
745			case "${OPTARG}" in
746			\$*)	usage "-M argument must not begin with '$'"
747				;;
748			*\$*)	# can use resolvepath, but can't set TOP_objdir
749				resolvepath OPTARG
750				;;
751			*)	resolvepath OPTARG
752				TOP_objdir="${OPTARG}${TOP}"
753				;;
754			esac
755			unsetmakeenv MAKEOBJDIR
756			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
757			;;
758
759			# -m overrides MACHINE_ARCH unless "-a" is specified
760		-m)
761			eval ${optargcmd}
762			MACHINE="${OPTARG}"
763			[ "${opt_a}" != "yes" ] && getarch
764			;;
765
766		-N)
767			eval ${optargcmd}
768			case "${OPTARG}" in
769			0|1|2|3|4)
770				setmakeenv MAKEVERBOSE "${OPTARG}"
771				;;
772			*)
773				usage "'${OPTARG}' is not a valid value for -N"
774				;;
775			esac
776			;;
777
778		-n)
779			runcmd=echo
780			;;
781
782		-O)
783			eval ${optargcmd}
784			case "${OPTARG}" in
785			*\$*)	usage "-O argument must not contain '$'"
786				;;
787			*)	resolvepath OPTARG
788				TOP_objdir="${OPTARG}"
789				;;
790			esac
791			unsetmakeenv MAKEOBJDIRPREFIX
792			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
793			;;
794
795		-o)
796			MKOBJDIRS=no
797			;;
798
799		-R)
800			eval ${optargcmd}; resolvepath OPTARG
801			setmakeenv RELEASEDIR "${OPTARG}"
802			;;
803
804		-r)
805			do_removedirs=true
806			do_rebuildmake=true
807			;;
808
809		-S)
810			eval ${optargcmd}
811			setmakeenv BUILDSEED "${OPTARG}"
812			;;
813
814		-T)
815			eval ${optargcmd}; resolvepath OPTARG
816			TOOLDIR="${OPTARG}"
817			export TOOLDIR
818			;;
819
820		-U)
821			setmakeenv MKUNPRIVED yes
822			;;
823
824		-u)
825			setmakeenv MKUPDATE yes
826			;;
827
828		-V)
829			eval ${optargcmd}
830			case "${OPTARG}" in
831		    # XXX: consider restricting which variables can be changed?
832			[a-zA-Z_][a-zA-Z_0-9]*=*)
833				setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
834				;;
835			*)
836				usage "-V argument must be of the form 'var=[value]'"
837				;;
838			esac
839			;;
840
841		-w)
842			eval ${optargcmd}; resolvepath OPTARG
843			makewrapper="${OPTARG}"
844			;;
845
846		-X)
847			eval ${optargcmd}; resolvepath OPTARG
848			setmakeenv X11SRCDIR "${OPTARG}"
849			;;
850
851		-x)
852			setmakeenv MKX11 yes
853			;;
854
855		-Y)
856			eval ${optargcmd}; resolvepath OPTARG
857			setmakeenv EXTSRCSRCDIR "${OPTARG}"
858			;;
859
860		-y)
861			setmakeenv MKEXTSRC yes
862			;;
863
864		-Z)
865			eval ${optargcmd}
866		    # XXX: consider restricting which variables can be unset?
867			unsetmakeenv "${OPTARG}"
868			;;
869
870		--)
871			break
872			;;
873
874		-'?'|-h)
875			usage
876			;;
877
878		esac
879	done
880
881	# Validate operations.
882	#
883	eval ${optremcmd}
884	while [ $# -gt 0 ]; do
885		op=$1; shift
886		operations="${operations} ${op}"
887
888		case "${op}" in
889
890		help)
891			usage
892			;;
893
894		makewrapper|cleandir|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
895			;;
896
897		iso-image)
898			op=iso_image	# used as part of a variable name
899			;;
900
901		iso-image-source)
902			op=iso_image_source   # used as part of a variable name
903			;;
904
905		kernel=*|releasekernel=*)
906			arg=${op#*=}
907			op=${op%%=*}
908			[ -n "${arg}" ] ||
909			    bomb "Must supply a kernel name with \`${op}=...'"
910			;;
911
912		modules)
913			op=modules
914			;;
915
916		install=*|installmodules=*)
917			arg=${op#*=}
918			op=${op%%=*}
919			[ -n "${arg}" ] ||
920			    bomb "Must supply a directory with \`install=...'"
921			;;
922
923		rump|rumptest)
924			op=${op}
925			;;
926
927		*)
928			usage "Unknown operation \`${op}'"
929			;;
930
931		esac
932		eval do_${op}=true
933	done
934	[ -n "${operations}" ] || usage "Missing operation to perform."
935
936	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
937	#
938	if [ -z "${MACHINE}" ]; then
939		[ "${uname_s}" = "NetBSD" ] ||
940		    bomb "MACHINE must be set, or -m must be used, for cross builds."
941		MACHINE=${uname_m}
942	fi
943	[ -n "${MACHINE_ARCH}" ] || getarch
944	validatearch
945
946	# Set up default make(1) environment.
947	#
948	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
949	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
950	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
951	export MAKEFLAGS MACHINE MACHINE_ARCH
952}
953
954sanitycheck()
955{
956	# If the PATH contains any non-absolute components (including,
957	# but not limited to, "." or ""), then complain.  As an exception,
958	# allow "" or "." as the last component of the PATH.  This is fatal
959	# if expert mode is not in effect.
960	#
961	local path="${PATH}"
962	path="${path%:}"	# delete trailing ":"
963	path="${path%:.}"	# delete trailing ":."
964	case ":${path}:/" in
965	*:[!/]*)
966		if ${do_expertmode}; then
967			warning "PATH contains non-absolute components"
968		else
969			bomb "PATH environment variable must not" \
970			     "contain non-absolute components"
971		fi
972		;;
973	esac
974}
975
976# print_tooldir_make --
977# Try to find and print a path to an existing
978# ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
979# new version of ${toolprefix}make has been built.
980#
981# * If TOOLDIR was set in the environment or on the command line, use
982#   that value.
983# * Otherwise try to guess what TOOLDIR would be if not overridden by
984#   /etc/mk.conf, and check whether the resulting directory contains
985#   a copy of ${toolprefix}make (this should work for everybody who
986#   doesn't override TOOLDIR via /etc/mk.conf);
987# * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
988#   in the PATH (this might accidentally find a non-NetBSD version of
989#   make, which will lead to failure in the next step);
990# * If a copy of make was found above, try to use it with
991#   nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
992#   result only if it's a directory that already exists;
993# * If a value of TOOLDIR was found above, and if
994#   ${TOOLDIR}/bin/${toolprefix}make exists, print that value.
995#
996print_tooldir_make()
997{
998	local possible_TOP_OBJ
999	local possible_TOOLDIR
1000	local possible_make
1001	local tooldir_make
1002
1003	if [ -n "${TOOLDIR}" ]; then
1004		echo "${TOOLDIR}/bin/${toolprefix}make"
1005		return 0
1006	fi
1007
1008	# Set host_ostype to something like "NetBSD-4.5.6-i386".  This
1009	# is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
1010	#
1011	local host_ostype="${uname_s}-$(
1012		echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1013		)-$(
1014		echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1015		)"
1016
1017	# Look in a few potential locations for
1018	# ${possible_TOOLDIR}/bin/${toolprefix}make.
1019	# If we find it, then set possible_make.
1020	#
1021	# In the usual case (without interference from environment
1022	# variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
1023	# "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
1024	#
1025	# In practice it's difficult to figure out the correct value
1026	# for _SRC_TOP_OBJ_.  In the easiest case, when the -M or -O
1027	# options were passed to build.sh, then ${TOP_objdir} will be
1028	# the correct value.  We also try a few other possibilities, but
1029	# we do not replicate all the logic of <bsd.obj.mk>.
1030	#
1031	for possible_TOP_OBJ in \
1032		"${TOP_objdir}" \
1033		"${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
1034		"${TOP}" \
1035		"${TOP}/obj" \
1036		"${TOP}/obj.${MACHINE}"
1037	do
1038		[ -n "${possible_TOP_OBJ}" ] || continue
1039		possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
1040		possible_make="${possible_TOOLDIR}/bin/${toolprefix}make"
1041		if [ -x "${possible_make}" ]; then
1042			break
1043		else
1044			unset possible_make
1045		fi
1046	done
1047
1048	# If the above didn't work, search the PATH for a suitable
1049	# ${toolprefix}make, nbmake, bmake, or make.
1050	#
1051	: ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
1052	: ${possible_make:=$(find_in_PATH nbmake '')}
1053	: ${possible_make:=$(find_in_PATH bmake '')}
1054	: ${possible_make:=$(find_in_PATH make '')}
1055
1056	# At this point, we don't care whether possible_make is in the
1057	# correct TOOLDIR or not; we simply want it to be usable by
1058	# getmakevar to help us find the correct TOOLDIR.
1059	#
1060	# Use ${possible_make} with nobomb_getmakevar to try to find
1061	# the value of TOOLDIR.  Believe the result only if it's
1062	# a directory that already exists and contains bin/${toolprefix}make.
1063	#
1064	if [ -x "${possible_make}" ]; then
1065		possible_TOOLDIR="$(
1066			make="${possible_make}" nobomb_getmakevar TOOLDIR
1067			)"
1068		if [ $? = 0 ] && [ -n "${possible_TOOLDIR}" ] \
1069		    && [ -d "${possible_TOOLDIR}" ];
1070		then
1071			tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
1072			if [ -x "${tooldir_make}" ]; then
1073				echo "${tooldir_make}"
1074				return 0
1075			fi
1076		fi
1077	fi
1078	return 1
1079}
1080
1081# rebuildmake --
1082# Rebuild nbmake in a temporary directory if necessary.  Sets $make
1083# to a path to the nbmake executable.  Sets done_rebuildmake=true
1084# if nbmake was rebuilt.
1085#
1086# There is a cyclic dependency between building nbmake and choosing
1087# TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
1088# would like to use getmakevar to get the value of TOOLDIR; but we can't
1089# use getmakevar before we have an up to date version of nbmake; we
1090# might already have an up to date version of nbmake in TOOLDIR, but we
1091# don't yet know where TOOLDIR is.
1092#
1093# The default value of TOOLDIR also depends on the location of the top
1094# level object directory, so $(getmakevar TOOLDIR) invoked before or
1095# after making the top level object directory may produce different
1096# results.
1097#
1098# Strictly speaking, we should do the following:
1099#
1100#    1. build a new version of nbmake in a temporary directory;
1101#    2. use the temporary nbmake to create the top level obj directory;
1102#    3. use $(getmakevar TOOLDIR) with the temporary nbmake to
1103#       get the corect value of TOOLDIR;
1104#    4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
1105#
1106# However, people don't like building nbmake unnecessarily if their
1107# TOOLDIR has not changed since an earlier build.  We try to avoid
1108# rebuilding a temporary version of nbmake by taking some shortcuts to
1109# guess a value for TOOLDIR, looking for an existing version of nbmake
1110# in that TOOLDIR, and checking whether that nbmake is newer than the
1111# sources used to build it.
1112#
1113rebuildmake()
1114{
1115	make="$(print_tooldir_make)"
1116	if [ -n "${make}" ] && [ -x "${make}" ]; then
1117		for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
1118			if [ "${f}" -nt "${make}" ]; then
1119				statusmsg "${make} outdated" \
1120					"(older than ${f}), needs building."
1121				do_rebuildmake=true
1122				break
1123			fi
1124		done
1125	else
1126		statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
1127		do_rebuildmake=true
1128	fi
1129
1130	# Build bootstrap ${toolprefix}make if needed.
1131	if ${do_rebuildmake}; then
1132		statusmsg "Bootstrapping ${toolprefix}make"
1133		${runcmd} cd "${tmpdir}"
1134		${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
1135			CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
1136			${HOST_SH} "${TOP}/tools/make/configure" ||
1137		    bomb "Configure of ${toolprefix}make failed"
1138		${runcmd} ${HOST_SH} buildmake.sh ||
1139		    bomb "Build of ${toolprefix}make failed"
1140		make="${tmpdir}/${toolprefix}make"
1141		${runcmd} cd "${TOP}"
1142		${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
1143		done_rebuildmake=true
1144	fi
1145}
1146
1147validatemakeparams()
1148{
1149	if [ "${runcmd}" = "echo" ]; then
1150		TOOLCHAIN_MISSING=no
1151		EXTERNAL_TOOLCHAIN=""
1152	else
1153		TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
1154		EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
1155	fi
1156	if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
1157	   [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
1158		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
1159		${runcmd} echo "	MACHINE:      ${MACHINE}"
1160		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
1161		${runcmd} echo ""
1162		${runcmd} echo "All builds for this platform should be done via a traditional make"
1163		${runcmd} echo "If you wish to use an external cross-toolchain, set"
1164		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to toolchain root>"
1165		${runcmd} echo "in either the environment or mk.conf and rerun"
1166		${runcmd} echo "	${progname} $*"
1167		exit 1
1168	fi
1169
1170	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
1171	# These may be set as build.sh options or in "mk.conf".
1172	# Don't export them as they're only used for tests in build.sh.
1173	#
1174	MKOBJDIRS=$(getmakevar MKOBJDIRS)
1175	MKUNPRIVED=$(getmakevar MKUNPRIVED)
1176	MKUPDATE=$(getmakevar MKUPDATE)
1177
1178	if [ "${MKOBJDIRS}" != "no" ]; then
1179		# Create the top-level object directory.
1180		#
1181		# "make obj NOSUBDIR=" can handle most cases, but it
1182		# can't handle the case where MAKEOBJDIRPREFIX is set
1183		# while the corresponding directory does not exist
1184		# (rules in <bsd.obj.mk> would abort the build).  We
1185		# therefore have to handle the MAKEOBJDIRPREFIX case
1186		# without invoking "make obj".  The MAKEOBJDIR case
1187		# could be handled either way, but we choose to handle
1188		# it similarly to MAKEOBJDIRPREFIX.
1189		#
1190		if [ -n "${TOP_obj}" ]; then
1191			# It must have been set by the "-M" or "-O"
1192			# command line options, so there's no need to
1193			# use getmakevar
1194			:
1195		elif [ -n "$MAKEOBJDIRPREFIX" ]; then
1196			TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
1197		elif [ -n "$MAKEOBJDIR" ]; then
1198			TOP_obj="$(getmakevar MAKEOBJDIR)"
1199		fi
1200		if [ -n "$TOP_obj" ]; then
1201			${runcmd} mkdir -p "${TOP_obj}" ||
1202			    bomb "Can't create top level object directory" \
1203					"${TOP_obj}"
1204		else
1205			${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
1206			    bomb "Can't create top level object directory" \
1207					"using make obj"
1208		fi
1209
1210		# make obj in tools to ensure that the objdir for "tools"
1211		# is available.
1212		#
1213		${runcmd} cd tools
1214		${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
1215		    bomb "Failed to make obj in tools"
1216		${runcmd} cd "${TOP}"
1217	fi
1218
1219	# Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
1220	# and bomb if they have changed from the values we had from the
1221	# command line or environment.
1222	#
1223	# This must be done after creating the top-level object directory.
1224	#
1225	for var in TOOLDIR DESTDIR RELEASEDIR
1226	do
1227		eval oldval=\"\$${var}\"
1228		newval="$(getmakevar $var)"
1229		if ! $do_expertmode; then
1230			: ${_SRC_TOP_OBJ_:=$(getmakevar _SRC_TOP_OBJ_)}
1231			case "$var" in
1232			DESTDIR)
1233				: ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
1234				makeenv="${makeenv} DESTDIR"
1235				;;
1236			RELEASEDIR)
1237				: ${newval:=${_SRC_TOP_OBJ_}/releasedir}
1238				makeenv="${makeenv} RELEASEDIR"
1239				;;
1240			esac
1241		fi
1242		if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]; then
1243			bomb "Value of ${var} has changed" \
1244				"(was \"${oldval}\", now \"${newval}\")"
1245		fi
1246		eval ${var}=\"\${newval}\"
1247		eval export ${var}
1248		statusmsg2 "${var} path:" "${newval}"
1249	done
1250
1251	# RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
1252	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
1253
1254	# Check validity of TOOLDIR and DESTDIR.
1255	#
1256	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
1257		bomb "TOOLDIR '${TOOLDIR}' invalid"
1258	fi
1259	removedirs="${TOOLDIR}"
1260
1261	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
1262		if ${do_build} || ${do_distribution} || ${do_release}; then
1263			if ! ${do_build} || \
1264			   [ "${uname_s}" != "NetBSD" ] || \
1265			   [ "${uname_m}" != "${MACHINE}" ]; then
1266				bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
1267			fi
1268			if ! ${do_expertmode}; then
1269				bomb "DESTDIR must != / for non -E (expert) builds"
1270			fi
1271			statusmsg "WARNING: Building to /, in expert mode."
1272			statusmsg "         This may cause your system to break!  Reasons include:"
1273			statusmsg "            - your kernel is not up to date"
1274			statusmsg "            - the libraries or toolchain have changed"
1275			statusmsg "         YOU HAVE BEEN WARNED!"
1276		fi
1277	else
1278		removedirs="${removedirs} ${DESTDIR}"
1279	fi
1280	if ${do_build} || ${do_distribution} || ${do_release}; then
1281		if ! ${do_expertmode} && \
1282		    [ "$id_u" -ne 0 ] && \
1283		    [ "${MKUNPRIVED}" = "no" ] ; then
1284			bomb "-U or -E must be set for build as an unprivileged user."
1285		fi
1286	fi
1287	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
1288		bomb "Must set RELEASEDIR with \`releasekernel=...'"
1289	fi
1290
1291	# Install as non-root is a bad idea.
1292	#
1293	if ${do_install} && [ "$id_u" -ne 0 ] ; then
1294		if ${do_expertmode}; then
1295			warning "Will install as an unprivileged user."
1296		else
1297			bomb "-E must be set for install as an unprivileged user."
1298		fi
1299	fi
1300
1301	# If a previous build.sh run used -U (and therefore created a
1302	# METALOG file), then most subsequent build.sh runs must also
1303	# use -U.  If DESTDIR is about to be removed, then don't perform
1304	# this check.
1305	#
1306	case "${do_removedirs} ${removedirs} " in
1307	true*" ${DESTDIR} "*)
1308		# DESTDIR is about to be removed
1309		;;
1310	*)
1311		if ( ${do_build} || ${do_distribution} || ${do_release} || \
1312		    ${do_install} ) && \
1313		    [ -e "${DESTDIR}/METALOG" ] && \
1314		    [ "${MKUNPRIVED}" = "no" ] ; then
1315			if $do_expertmode; then
1316				warning "A previous build.sh run specified -U."
1317			else
1318				bomb "A previous build.sh run specified -U; you must specify it again now."
1319			fi
1320		fi
1321		;;
1322	esac
1323}
1324
1325
1326createmakewrapper()
1327{
1328	# Remove the target directories.
1329	#
1330	if ${do_removedirs}; then
1331		for f in ${removedirs}; do
1332			statusmsg "Removing ${f}"
1333			${runcmd} rm -r -f "${f}"
1334		done
1335	fi
1336
1337	# Recreate $TOOLDIR.
1338	#
1339	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
1340	    bomb "mkdir of '${TOOLDIR}/bin' failed"
1341
1342	# If we did not previously rebuild ${toolprefix}make, then
1343	# check whether $make is still valid and the same as the output
1344	# from print_tooldir_make.  If not, then rebuild make now.  A
1345	# possible reason for this being necessary is that the actual
1346	# value of TOOLDIR might be different from the value guessed
1347	# before the top level obj dir was created.
1348	#
1349	if ! ${done_rebuildmake} && \
1350	    ( [ ! -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] )
1351	then
1352		rebuildmake
1353	fi
1354
1355	# Install ${toolprefix}make if it was built.
1356	#
1357	if ${done_rebuildmake}; then
1358		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
1359		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
1360		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
1361		make="${TOOLDIR}/bin/${toolprefix}make"
1362		statusmsg "Created ${make}"
1363	fi
1364
1365	# Build a ${toolprefix}make wrapper script, usable by hand as
1366	# well as by build.sh.
1367	#
1368	if [ -z "${makewrapper}" ]; then
1369		makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
1370		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
1371	fi
1372
1373	${runcmd} rm -f "${makewrapper}"
1374	if [ "${runcmd}" = "echo" ]; then
1375		echo 'cat <<EOF >'${makewrapper}
1376		makewrapout=
1377	else
1378		makewrapout=">>\${makewrapper}"
1379	fi
1380
1381	case "${KSH_VERSION:-${SH_VERSION}}" in
1382	*PD\ KSH*|*MIRBSD\ KSH*)
1383		set +o braceexpand
1384		;;
1385	esac
1386
1387	eval cat <<EOF ${makewrapout}
1388#! ${HOST_SH}
1389# Set proper variables to allow easy "make" building of a NetBSD subtree.
1390# Generated from:  \$NetBSD: build.sh,v 1.246 2011/08/15 14:48:00 wiz Exp $
1391# with these arguments: ${_args}
1392#
1393
1394EOF
1395	{
1396		for f in ${makeenv}; do
1397			if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
1398				eval echo "unset ${f}"
1399			else
1400				eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
1401			fi
1402		done
1403
1404		eval cat <<EOF
1405MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
1406USETOOLS=yes; export USETOOLS
1407EOF
1408	} | eval sort -u "${makewrapout}"
1409	eval cat <<EOF "${makewrapout}"
1410
1411exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
1412EOF
1413	[ "${runcmd}" = "echo" ] && echo EOF
1414	${runcmd} chmod +x "${makewrapper}"
1415	statusmsg2 "Updated makewrapper:" "${makewrapper}"
1416}
1417
1418make_in_dir()
1419{
1420	dir="$1"
1421	op="$2"
1422	${runcmd} cd "${dir}" ||
1423	    bomb "Failed to cd to \"${dir}\""
1424	${runcmd} "${makewrapper}" ${parallel} ${op} ||
1425	    bomb "Failed to make ${op} in \"${dir}\""
1426	${runcmd} cd "${TOP}" ||
1427	    bomb "Failed to cd back to \"${TOP}\""
1428}
1429
1430buildtools()
1431{
1432	if [ "${MKOBJDIRS}" != "no" ]; then
1433		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
1434		    bomb "Failed to make obj-tools"
1435	fi
1436	if [ "${MKUPDATE}" = "no" ]; then
1437		make_in_dir tools cleandir
1438	fi
1439	make_in_dir tools dependall
1440	make_in_dir tools install
1441	statusmsg "Tools built to ${TOOLDIR}"
1442}
1443
1444getkernelconf()
1445{
1446	kernelconf="$1"
1447	if [ "${MKOBJDIRS}" != "no" ]; then
1448		# The correct value of KERNOBJDIR might
1449		# depend on a prior "make obj" in
1450		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
1451		#
1452		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
1453		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
1454		make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
1455	fi
1456	KERNCONFDIR="$(getmakevar KERNCONFDIR)"
1457	KERNOBJDIR="$(getmakevar KERNOBJDIR)"
1458	case "${kernelconf}" in
1459	*/*)
1460		kernelconfpath="${kernelconf}"
1461		kernelconfname="${kernelconf##*/}"
1462		;;
1463	*)
1464		kernelconfpath="${KERNCONFDIR}/${kernelconf}"
1465		kernelconfname="${kernelconf}"
1466		;;
1467	esac
1468	kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
1469}
1470
1471buildkernel()
1472{
1473	if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
1474		# Building tools every time we build a kernel is clearly
1475		# unnecessary.  We could try to figure out whether rebuilding
1476		# the tools is necessary this time, but it doesn't seem worth
1477		# the trouble.  Instead, we say it's the user's responsibility
1478		# to rebuild the tools if necessary.
1479		#
1480		statusmsg "Building kernel without building new tools"
1481		buildkernelwarned=true
1482	fi
1483	getkernelconf $1
1484	statusmsg2 "Building kernel:" "${kernelconf}"
1485	statusmsg2 "Build directory:" "${kernelbuildpath}"
1486	${runcmd} mkdir -p "${kernelbuildpath}" ||
1487	    bomb "Cannot mkdir: ${kernelbuildpath}"
1488	if [ "${MKUPDATE}" = "no" ]; then
1489		make_in_dir "${kernelbuildpath}" cleandir
1490	fi
1491	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
1492	|| bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
1493	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
1494		-s "${TOP}/sys" "${kernelconfpath}" ||
1495	    bomb "${toolprefix}config failed for ${kernelconf}"
1496	make_in_dir "${kernelbuildpath}" depend
1497	make_in_dir "${kernelbuildpath}" all
1498
1499	if [ "${runcmd}" != "echo" ]; then
1500		statusmsg "Kernels built from ${kernelconf}:"
1501		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1502		for kern in ${kernlist:-netbsd}; do
1503			[ -f "${kernelbuildpath}/${kern}" ] && \
1504			    echo "  ${kernelbuildpath}/${kern}"
1505		done | tee -a "${results}"
1506	fi
1507}
1508
1509releasekernel()
1510{
1511	getkernelconf $1
1512	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
1513	${runcmd} mkdir -p "${kernelreldir}"
1514	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1515	for kern in ${kernlist:-netbsd}; do
1516		builtkern="${kernelbuildpath}/${kern}"
1517		[ -f "${builtkern}" ] || continue
1518		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
1519		statusmsg2 "Kernel copy:" "${releasekern}"
1520		if [ "${runcmd}" = "echo" ]; then
1521			echo "gzip -c -9 < ${builtkern} > ${releasekern}"
1522		else
1523			gzip -c -9 < "${builtkern}" > "${releasekern}"
1524		fi
1525	done
1526}
1527
1528buildmodules()
1529{
1530	setmakeenv MKBINUTILS no
1531	if ! ${do_tools} && ! ${buildmoduleswarned:-false}; then
1532		# Building tools every time we build modules is clearly
1533		# unnecessary as well as a kernel.
1534		#
1535		statusmsg "Building modules without building new tools"
1536		buildmoduleswarned=true
1537	fi
1538
1539	statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
1540	if [ "${MKOBJDIRS}" != "no" ]; then
1541		make_in_dir sys/modules obj ||
1542		    bomb "Failed to make obj in sys/modules"
1543	fi
1544	if [ "${MKUPDATE}" = "no" ]; then
1545		make_in_dir sys/modules cleandir
1546	fi
1547	${runcmd} "${makewrapper}" ${parallel} do-sys-modules ||
1548	    bomb "Failed to make do-sys-modules"
1549
1550	statusmsg "Successful build of kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
1551}
1552
1553installmodules()
1554{
1555	dir="$1"
1556	${runcmd} "${makewrapper}" INSTALLMODULESDIR="${dir}" installmodules ||
1557	    bomb "Failed to make installmodules to ${dir}"
1558	statusmsg "Successful installmodules to ${dir}"
1559}
1560
1561installworld()
1562{
1563	dir="$1"
1564	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
1565	    bomb "Failed to make installworld to ${dir}"
1566	statusmsg "Successful installworld to ${dir}"
1567}
1568
1569# Run rump build&link tests.
1570#
1571# To make this feasible for running without having to install includes and
1572# libraries into destdir (i.e. quick), we only run ld.  This is possible
1573# since the rump kernel is a closed namespace apart from calls to rumpuser.
1574# Therefore, if ld complains only about rumpuser symbols, rump kernel
1575# linking was successful.
1576#
1577# We test that rump links with a number of component configurations.
1578# These attempt to mimic what is encountered in the full build.
1579# See list below.  The list should probably be either autogenerated
1580# or managed elsewhere; keep it here until a better idea arises.
1581#
1582# Above all, note that THIS IS NOT A SUBSTITUTE FOR A FULL BUILD.
1583#
1584
1585RUMP_LIBSETS='
1586	-lrump,
1587	-lrumpvfs -lrump,
1588	-lrumpdev -lrump,
1589	-lrumpnet -lrump,
1590	-lrumpkern_tty -lrumpvfs -lrump,
1591	-lrumpfs_tmpfs -lrumpvfs -lrump,
1592	-lrumpfs_ffs -lrumpfs_msdos -lrumpvfs -lrumpdev_disk -lrumpdev -lrump,
1593	-lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet -lrump,
1594	-lrumpnet_sockin -lrumpfs_smbfs -lrumpdev_netsmb
1595	    -lrumpkern_crypto -lrumpdev -lrumpnet -lrumpvfs -lrump,
1596	-lrumpnet_sockin -lrumpfs_nfs -lrumpnet -lrumpvfs -lrump,
1597	-lrumpdev_cgd -lrumpdev_raidframe -lrumpdev_disk -lrumpdev_rnd
1598	    -lrumpdev_dm -lrumpdev -lrumpvfs -lrumpkern_crypto -lrump'
1599dorump()
1600{
1601	local doclean=""
1602	local doobjs=""
1603
1604	# we cannot link libs without building csu, and that leads to lossage
1605	[ "${1}" != "rumptest" ] && bomb 'build.sh rump not yet functional. ' \
1606	    'did you mean "rumptest"?'
1607
1608	# create obj and distrib dirs
1609	if [ "${MKOBJDIRS}" != "no" ]; then
1610		make_in_dir "${NETBSDSRCDIR}/etc/mtree" obj
1611		make_in_dir "${NETBSDSRCDIR}/sys/rump" obj
1612	fi
1613	${runcmd} "${makewrapper}" ${parallel} do-distrib-dirs \
1614	    || bomb 'could not create distrib-dirs'
1615
1616	[ "${MKUPDATE}" = "no" ] && doclean="cleandir"
1617	targlist="${doclean} ${doobjs} dependall install"
1618	# optimize: for test we build only static libs (3x test speedup)
1619	if [ "${1}" = "rumptest" ] ; then
1620		setmakeenv NOPIC 1
1621		setmakeenv NOPROFILE 1
1622	fi
1623	for cmd in ${targlist} ; do
1624		make_in_dir "${NETBSDSRCDIR}/sys/rump" ${cmd}
1625	done
1626
1627	# if we just wanted to build & install rump, we're done
1628	[ "${1}" != "rumptest" ] && return
1629
1630	${runcmd} cd "${NETBSDSRCDIR}/sys/rump/librump/rumpkern" \
1631	    || bomb "cd to rumpkern failed"
1632	md_quirks=`${runcmd} "${makewrapper}" -V '${_SYMQUIRK}'`
1633	# one little, two little, three little backslashes ...
1634	md_quirks="$(echo ${md_quirks} | sed 's,\\,\\\\,g'";s/'//g" )"
1635	${runcmd} cd "${TOP}" || bomb "cd to ${TOP} failed"
1636	tool_ld=`${runcmd} "${makewrapper}" -V '${LD}'`
1637
1638	local oIFS="${IFS}"
1639	IFS=","
1640	for set in ${RUMP_LIBSETS} ; do
1641		IFS="${oIFS}"
1642		${runcmd} ${tool_ld} -nostdlib -L${DESTDIR}/usr/lib	\
1643		    -static --whole-archive ${set} 2>&1 -o /tmp/rumptest.$$ | \
1644		      awk -v quirks="${md_quirks}" '
1645			/undefined reference/ &&
1646			    !/more undefined references.*follow/{
1647				if (match($NF,
1648				    "`(rumpuser_|__" quirks ")") == 0)
1649					fails[NR] = $0
1650			}
1651			/cannot find -l/{fails[NR] = $0}
1652			/cannot open output file/{fails[NR] = $0}
1653			END{
1654				for (x in fails)
1655					print fails[x]
1656				exit x!=0
1657			}'
1658		[ $? -ne 0 ] && bomb "Testlink of rump failed: ${set}"
1659	done
1660	statusmsg "Rump build&link tests successful"
1661}
1662
1663main()
1664{
1665	initdefaults
1666	_args=$@
1667	parseoptions "$@"
1668
1669	sanitycheck
1670
1671	build_start=$(date)
1672	statusmsg2 "${progname} command:" "$0 $*"
1673	statusmsg2 "${progname} started:" "${build_start}"
1674	statusmsg2 "NetBSD version:"   "${DISTRIBVER}"
1675	statusmsg2 "MACHINE:"          "${MACHINE}"
1676	statusmsg2 "MACHINE_ARCH:"     "${MACHINE_ARCH}"
1677	statusmsg2 "Build platform:"   "${uname_s} ${uname_r} ${uname_m}"
1678	statusmsg2 "HOST_SH:"          "${HOST_SH}"
1679
1680	rebuildmake
1681	validatemakeparams
1682	createmakewrapper
1683
1684	# Perform the operations.
1685	#
1686	for op in ${operations}; do
1687		case "${op}" in
1688
1689		makewrapper)
1690			# no-op
1691			;;
1692
1693		tools)
1694			buildtools
1695			;;
1696
1697		sets)
1698			statusmsg "Building sets from pre-populated ${DESTDIR}"
1699			${runcmd} "${makewrapper}" ${parallel} ${op} ||
1700			    bomb "Failed to make ${op}"
1701			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
1702			statusmsg "Built sets to ${setdir}"
1703			;;
1704
1705		cleandir|obj|build|distribution|release|sourcesets|syspkgs|params)
1706			${runcmd} "${makewrapper}" ${parallel} ${op} ||
1707			    bomb "Failed to make ${op}"
1708			statusmsg "Successful make ${op}"
1709			;;
1710
1711		iso-image|iso-image-source)
1712			${runcmd} "${makewrapper}" ${parallel} \
1713			    CDEXTRA="$CDEXTRA" ${op} ||
1714			    bomb "Failed to make ${op}"
1715			statusmsg "Successful make ${op}"
1716			;;
1717
1718		kernel=*)
1719			arg=${op#*=}
1720			buildkernel "${arg}"
1721			;;
1722
1723		releasekernel=*)
1724			arg=${op#*=}
1725			releasekernel "${arg}"
1726			;;
1727
1728		modules)
1729			buildmodules
1730			;;
1731
1732		installmodules=*)
1733			arg=${op#*=}
1734			if [ "${arg}" = "/" ] && \
1735			    (	[ "${uname_s}" != "NetBSD" ] || \
1736				[ "${uname_m}" != "${MACHINE}" ] ); then
1737				bomb "'${op}' must != / for cross builds."
1738			fi
1739			installmodules "${arg}"
1740			;;
1741
1742		install=*)
1743			arg=${op#*=}
1744			if [ "${arg}" = "/" ] && \
1745			    (	[ "${uname_s}" != "NetBSD" ] || \
1746				[ "${uname_m}" != "${MACHINE}" ] ); then
1747				bomb "'${op}' must != / for cross builds."
1748			fi
1749			installworld "${arg}"
1750			;;
1751
1752		rump|rumptest)
1753			dorump "${op}"
1754			;;
1755
1756		*)
1757			bomb "Unknown operation \`${op}'"
1758			;;
1759
1760		esac
1761	done
1762
1763	statusmsg2 "${progname} ended:" "$(date)"
1764	if [ -s "${results}" ]; then
1765		echo "===> Summary of results:"
1766		sed -e 's/^===>//;s/^/	/' "${results}"
1767		echo "===> ."
1768	fi
1769}
1770
1771main "$@"
1772