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