xref: /netbsd-src/build.sh (revision 16dd98ceb38c1ca26b86ecfbd036b6e293c7d8a7)
1#! /usr/bin/env sh
2#	$NetBSD: build.sh,v 1.388 2024/12/28 00:39:56 gutteridge Exp $
3#
4# Copyright (c) 2001-2023 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, to build or cross-build NetBSD.
33#
34
35#
36# {{{ Begin shell feature tests.
37#
38# We try to determine whether or not this script is being run under
39# a shell that supports the features that we use.  If not, we try to
40# re-exec the script under another shell.  If we can't find another
41# suitable shell, then we show a message and exit.
42#
43
44errmsg=			# error message, if not empty
45shelltest=false		# if true, exit after testing the shell
46re_exec_allowed=true	# if true, we may exec under another shell
47
48# Parse special command line options in $1.  These special options are
49# for internal use only, are not documented, and are not valid anywhere
50# other than $1.
51case "$1" in
52--shelltest)
53    shelltest=true
54    re_exec_allowed=false
55    shift
56    ;;
57--no-re-exec)
58    re_exec_allowed=false
59    shift
60    ;;
61esac
62
63# Solaris /bin/sh, and other SVR4 shells, do not support "!".
64# This is the first feature that we test, because subsequent
65# tests use "!".
66#
67# Unfortunately, if the shell doesn't support ! most of the tests
68# following which use '!' are likely to simply abort with a syntax error.
69# Not executing the code containing ! does not avoid compiling it.
70#
71if test -z "$errmsg"
72then
73    if ( eval '! false' ) >/dev/null 2>&1
74    then
75	:
76    else
77	errmsg='Shell does not support "!".'
78    fi
79fi
80
81# Does the shell support functions?
82#
83if test -z "$errmsg"
84then
85    if ! (
86	eval 'somefunction() { : ; }'
87	) >/dev/null 2>&1
88    then
89	errmsg='Shell does not support functions.'
90    fi
91fi
92
93# Does the shell support the "local" keyword for variables in functions?
94#
95# Local variables are not required by SUSv3, but some scripts run during
96# the NetBSD build use them.
97#
98# ksh93 fails this test; it uses an incompatible syntax involving the
99# keywords 'function' and 'typeset'.
100#
101if test -z "$errmsg"
102then
103    if ! (
104	eval 'f() { local v=2; }; v=1; f && test x"$v" = x1'
105	) >/dev/null 2>&1
106    then
107	errmsg='Shell does not support the "local" keyword in functions.'
108    fi
109fi
110
111# Does the shell support ${var%suffix}, ${var#prefix}, and their variants?
112#
113# We don't bother testing for ${var+value}, ${var-value}, or their variants,
114# since shells without those (unlikely) are sure to fail other tests too.
115#
116if test -z "$errmsg"
117then
118    if ! (
119	eval 'var=a/b/c ;
120	      test x"${var#*/};${var##*/};${var%/*};${var%%/*}" = \
121		   x"b/c;c;a/b;a" ;'
122	) >/dev/null 2>&1
123    then
124	errmsg='Shell does not support "${var%suffix}" or "${var#prefix}".'
125    fi
126fi
127
128# Does the shell support IFS?
129#
130# zsh in normal mode (as opposed to "emulate sh" mode) fails this test.
131#
132if test -z "$errmsg"
133then
134    if ! (
135	eval 'IFS=: ; v=":a b::c" ; set -- $v ; IFS=+ ;
136		test x"$#;$1,$2,$3,$4;$*" = x"4;,a b,,c;+a b++c"'
137	) >/dev/null 2>&1
138    then
139	errmsg='Shell does not support IFS word splitting.'
140    fi
141fi
142
143# Does the shell support ${1+"$@"}?
144#
145# Some versions of zsh fail this test, even in "emulate sh" mode.
146#
147if test -z "$errmsg"
148then
149    if ! (
150	eval 'set -- "a a a" "b b b"
151	      set -- ${1+"$@"}
152	      test x"$#;$1;$2" = x"2;a a a;b b b" '
153	) >/dev/null 2>&1
154    then
155	errmsg='Shell does not support ${1+"$@"}.'
156    fi
157fi
158
159# Does the shell support $(...) command substitution?
160#
161if test -z "$errmsg"
162then
163    if ! (
164	eval 'var=$(echo abc); test x"$var" = xabc'
165	) >/dev/null 2>&1
166    then
167	errmsg='Shell does not support "$(...)" command substitution.'
168    fi
169fi
170
171# Does the shell support $(...) command substitution with
172# unbalanced parentheses?
173#
174# Some shells known to fail this test are:  NetBSD /bin/ksh (as of 2009-12),
175# bash-3.1, pdksh-5.2.14, zsh-4.2.7 in "emulate sh" mode.
176#
177if test -z "$errmsg"
178then
179    if ! (
180	eval 'var=$(case x in x) echo abc;; esac); test x"$var" = x"abc"'
181	) >/dev/null 2>&1
182    then
183	# XXX: This test is ignored because so many shells fail it; instead,
184	#      the NetBSD build avoids using the problematic construct.
185	: ignore 'Shell does not support "$(...)" with unbalanced ")".'
186    fi
187fi
188
189# Does the shell support getopts or getopt?
190#  (XXX: Q: why the need for the eval here, looks unncessary)
191#
192if test -z "$errmsg"
193then
194    if ! (
195	eval 'type getopts || type getopt'
196	) >/dev/null 2>&1
197    then
198	errmsg='Shell does not support getopts or getopt.'
199    fi
200fi
201
202#
203# If shelltest is true, exit now, reporting whether or not the shell is good.
204#
205if "$shelltest"
206then
207    if test -n "$errmsg"
208    then
209	echo >&2 "$0: $errmsg"
210	exit 1
211    else
212	exit 0
213    fi
214fi
215
216#
217# If the shell was bad, try to exec a better shell, or report an error.
218#
219# Loops are broken by passing an extra "--no-re-exec" flag to the new
220# instance of this script.
221#
222if test -n "$errmsg"
223then
224    if "$re_exec_allowed"
225    then
226	for othershell in \
227	    "${HOST_SH}" /usr/xpg4/bin/sh ksh ksh88 mksh pdksh dash bash
228	    # NOTE: some shells known not to work are:
229	    # any shell using csh syntax;
230	    # Solaris /bin/sh (missing many modern features);
231	    # ksh93 (incompatible syntax for local variables);
232	    # zsh (many differences, unless run in compatibility mode).
233	do
234	    test -n "$othershell" || continue
235	    if eval 'type "$othershell"' >/dev/null 2>&1 \
236		&& $othershell "$0" --shelltest >/dev/null 2>&1
237	    then
238		cat <<EOF
239$0: $errmsg
240$0: Retrying under $othershell
241EOF
242		HOST_SH="$othershell"
243		export HOST_SH
244		exec $othershell "$0" --no-re-exec "$@" # avoid ${1+"$@"}
245	    fi
246	    # If HOST_SH was set, but failed the test above,
247	    # then give up without trying any other shells.
248	    test x"${othershell}" = x"${HOST_SH}" && break
249	done
250    fi
251
252    #
253    # If we get here, then the shell is bad, and we either could not
254    # find a replacement, or were not allowed to try a replacement.
255    #
256    cat <<EOF
257$0: $errmsg
258
259The NetBSD build system requires a shell that supports modern POSIX
260features, as well as the "local" keyword in functions (which is a
261widely-implemented but non-standardised feature).
262
263Please re-run this script under a suitable shell.  For example:
264
265	/path/to/suitable/shell $0 ...
266
267The above command will usually enable build.sh to automatically set
268HOST_SH=/path/to/suitable/shell, but if that fails, then you may also
269need to explicitly set the HOST_SH environment variable, as follows:
270
271	HOST_SH=/path/to/suitable/shell
272	export HOST_SH
273	\${HOST_SH} $0 ...
274EOF
275    exit 1
276fi
277
278#
279# }}} End shell feature tests.
280#
281
282progname=${0##*/}
283toppid=$$
284results=/dev/null
285tab='	'
286nl='
287'
288trap "exit 1" 1 2 3 15
289
290bomb()
291{
292	cat >&2 <<ERRORMESSAGE
293
294ERROR: $*
295
296*** BUILD ABORTED ***
297ERRORMESSAGE
298	kill ${toppid}		# in case we were invoked from a subshell
299	exit 1
300}
301
302# Quote args to make them safe in the shell.
303# Usage: quotedlist="$(shell_quote args...)"
304#
305# After building up a quoted list, use it by evaling it inside
306# double quotes, like this:
307#    eval "set -- $quotedlist"
308# or like this:
309#    eval "\$command $quotedlist \$filename"
310#
311shell_quote()
312(
313	local result=
314	local arg qarg
315	LC_COLLATE=C ; export LC_COLLATE # so [a-zA-Z0-9] works in ASCII
316	for arg in "$@"
317	do
318		case "${arg}" in
319		'')
320			qarg="''"
321			;;
322		*[!-./a-zA-Z0-9]*)
323			# Convert each embedded ' to '\'',
324			# then insert ' at the beginning of the first line,
325			# and append ' at the end of the last line.
326			# Finally, elide unnecessary '' pairs at the
327			# beginning and end of the result and as part of
328			# '\'''\'' sequences that result from multiple
329			# adjacent quotes in he input.
330			qarg=$(printf "%s\n" "$arg" |
331			    ${SED:-sed} -e "s/'/'\\\\''/g" \
332				-e "1s/^/'/" -e "\$s/\$/'/" \
333				-e "1s/^''//" -e "\$s/''\$//" \
334				-e "s/'''/'/g"
335			    )
336			;;
337		*)
338			# Arg is not the empty string, and does not contain
339			# any unsafe characters.  Leave it unchanged for
340			# readability.
341			qarg="${arg}"
342			;;
343		esac
344		result="${result}${result:+ }${qarg}"
345	done
346	printf "%s\n" "$result"
347)
348
349statusmsg()
350{
351	${runcmd} echo "===> $*" | tee -a "${results}"
352}
353
354statusmsg2()
355{
356	local msg
357
358	msg=${1}
359	shift
360
361	case "${msg}" in
362	????????????????*)	;;
363	??????????*)		msg="${msg}      ";;
364	?????*)			msg="${msg}           ";;
365	*)			msg="${msg}                ";;
366	esac
367	case "${msg}" in
368	?????????????????????*)	;;
369	????????????????????)	msg="${msg} ";;
370	???????????????????)	msg="${msg}  ";;
371	??????????????????)	msg="${msg}   ";;
372	?????????????????)	msg="${msg}    ";;
373	????????????????)	msg="${msg}     ";;
374	esac
375	statusmsg "${msg}$*"
376}
377
378warning()
379{
380	statusmsg "Warning: $*"
381}
382
383# Find a program in the PATH, and show the result.  If not found,
384# show a default.  If $2 is defined (even if it is an empty string),
385# then that is the default; otherwise, $1 is used as the default.
386#
387find_in_PATH()
388{
389	local prog="$1"
390	local result="${2-$1}"
391	local dir
392	local IFS=:
393
394	for dir in ${PATH}
395	do
396		if [ -x "${dir}/${prog}" ]
397		then
398			result=${dir}/${prog}
399			break
400		fi
401	done
402	echo "${result}"
403}
404
405# Try to find a working POSIX shell, and set HOST_SH to refer to it.
406# Assumes that uname_s, uname_m, and PWD have been set.
407#
408set_HOST_SH()
409{
410	# Even if ${HOST_SH} is already defined, we still do the
411	# sanity checks at the end.
412
413	# Solaris has /usr/xpg4/bin/sh.
414	#
415	[ -z "${HOST_SH}" ] &&
416		[ "${uname_s}" = SunOS ] &&
417		[ -x /usr/xpg4/bin/sh ] &&
418			HOST_SH=/usr/xpg4/bin/sh
419
420	# Try to get the name of the shell that's running this script,
421	# by parsing the output from "ps".  We assume that, if the host
422	# system's ps command supports -o comm at all, it will do so
423	# in the usual way: a one-line header followed by a one-line
424	# result, possibly including trailing white space.  And if the
425	# host system's ps command doesn't support -o comm, we assume
426	# that we'll get an error message on stderr and nothing on
427	# stdout.  (We don't try to use ps -o 'comm=' to suppress the
428	# header line, because that is less widely supported.)
429	#
430	# If we get the wrong result here, the user can override it by
431	# specifying HOST_SH in the environment.
432	#
433	[ -z "${HOST_SH}" ] && HOST_SH=$(
434		(
435			ps -p $$ -o comm |
436			    sed -ne "2s/[ ${tab}]*\$//p"
437		) 2>/dev/null
438	)
439
440	# If nothing above worked, use "sh".  We will later find the
441	# first directory in the PATH that has a "sh" program.
442	#
443	[ -z "${HOST_SH}" ] && HOST_SH="sh"
444
445	# If the result so far is not an absolute path, try to prepend
446	# PWD or search the PATH.
447	#
448	case "${HOST_SH}" in
449	/*)	:
450		;;
451	*/*)	HOST_SH="${PWD}/${HOST_SH}"
452		;;
453	*)	HOST_SH="$(find_in_PATH "${HOST_SH}")"
454		;;
455	esac
456
457	# If we don't have an absolute path by now, bomb.
458	#
459	case "${HOST_SH}" in
460	/*)	:
461		;;
462	*)	bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path"
463		;;
464	esac
465
466	# If HOST_SH is not executable, bomb.
467	#
468	[ -x "${HOST_SH}" ] ||
469	    bomb "HOST_SH=\"${HOST_SH}\" is not executable"
470
471	# If HOST_SH fails tests, bomb.
472	# ("$0" may be a path that is no longer valid, because we have
473	# performed "cd $(dirname $0)", so don't use $0 here.)
474	#
475	"${HOST_SH}" build.sh --shelltest ||
476	    bomb "HOST_SH=\"${HOST_SH}\" failed functionality tests"
477}
478
479# initdefaults --
480# Set defaults before parsing command line options.
481#
482initdefaults()
483{
484	makeenv=
485	makewrapper=
486	makewrappermachine=
487	runcmd=
488	operations=
489	removedirs=
490
491	[ -d usr.bin/make ] || cd "$(dirname $0)"
492	[ -d usr.bin/make ] ||
493	    bomb "usr.bin/make not found; build.sh must be run from" \
494	         "the top level of source directory"
495	[ -f share/mk/bsd.own.mk ] ||
496	    bomb "src/share/mk is missing; please re-fetch the source tree"
497
498	# Set various environment variables to known defaults,
499	# to minimize (cross-)build problems observed "in the field".
500	#
501	# LC_ALL=C must be set before we try to parse the output from
502	# any command.  Other variables are set (or unset) here, before
503	# we parse command line arguments.
504	#
505	# These variables can be overridden via "-V var=value" if
506	# you know what you are doing.
507	#
508	unsetmakeenv C_INCLUDE_PATH
509	unsetmakeenv CPLUS_INCLUDE_PATH
510	unsetmakeenv INFODIR
511	unsetmakeenv LESSCHARSET
512	unsetmakeenv MAKEFLAGS
513	unsetmakeenv TERMINFO
514	setmakeenv LC_ALL C
515
516	# Find information about the build platform.  This should be
517	# kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
518	# variables in share/mk/bsd.sys.mk.
519	#
520	# Note that "uname -p" is not part of POSIX, but we want uname_p
521	# to be set to the host MACHINE_ARCH, if possible.  On systems
522	# where "uname -p" fails, shows "unknown", or shows a string
523	# that does not look like an identifier, fall back to using the
524	# output from "uname -m" instead.
525	#
526	uname_s=$(uname -s 2>/dev/null)
527	uname_r=$(uname -r 2>/dev/null)
528	uname_m=$(uname -m 2>/dev/null)
529	uname_p=$(uname -p 2>/dev/null || echo "unknown")
530	case "${uname_p}" in
531	''|unknown|*[!-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
532	esac
533
534	id_u=$(id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null)
535
536	# If $PWD is a valid name of the current directory, POSIX mandates
537	# that pwd return it by default which causes problems in the
538	# presence of symlinks.  Unsetting PWD is simpler than changing
539	# every occurrence of pwd to use -P.
540	#
541	# XXX Except that doesn't work on Solaris. Or many Linuces.
542	#     And the standard says altering PWD produces unspecified results
543	# So instead just cd -P to $PWD which should make PWD be symlink free
544	#
545	cd -P "${PWD}" || bomb "Cannot cd to \$PWD ($PWD)"
546	# At this point TOP=$PWD should just work, but let's be ultra safe.
547	TOP=$( (exec pwd -P 2>/dev/null) || (exec pwd 2>/dev/null) )
548
549	# The user can set HOST_SH in the environment, or we try to
550	# guess an appropriate value.  Then we set several other
551	# variables from HOST_SH.
552	#
553	set_HOST_SH
554	setmakeenv HOST_SH "${HOST_SH}"
555	setmakeenv BSHELL "${HOST_SH}"
556	setmakeenv CONFIG_SHELL "${HOST_SH}"
557
558	# Set defaults.
559	#
560	toolprefix=nb
561
562	# Some systems have a small ARG_MAX.  -X prevents make(1) from
563	# exporting variables in the environment redundantly.
564	#
565	case "${uname_s}" in
566	Darwin | FreeBSD | CYGWIN*)
567		MAKEFLAGS="-X ${MAKEFLAGS}"
568		;;
569	esac
570
571	# do_{operation}=true if given operation is requested.
572	#
573	do_expertmode=false
574	do_rebuildmake=false
575	do_removedirs=false
576	do_tools=false
577	do_libs=false
578	do_cleandir=false
579	do_obj=false
580	do_build=false
581	do_distribution=false
582	do_release=false
583	do_kernel=false
584	do_releasekernel=false
585	do_kernels=false
586	do_modules=false
587	do_installmodules=false
588	do_install=false
589	do_sets=false
590	do_sourcesets=false
591	do_syspkgs=false
592	do_pkg=false
593	do_iso_image=false
594	do_iso_image_source=false
595	do_live_image=false
596	do_install_image=false
597	do_disk_image=false
598	do_params=false
599	do_show_params=false
600	do_rump=false
601	do_dtb=false
602
603	# done_{operation}=true if given operation has been done.
604	#
605	done_rebuildmake=false
606
607	# Create scratch directory
608	#
609	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
610	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
611	trap "cd /; rm -r -f \"${tmpdir}\"" 0
612	results="${tmpdir}/build.sh.results"
613
614	# Set source directories
615	#
616	setmakeenv NETBSDSRCDIR "${TOP}"
617
618	# Make sure KERNOBJDIR is an absolute path if defined
619	#
620	case "${KERNOBJDIR}" in
621	''|/*)	;;
622	*)	KERNOBJDIR="${TOP}/${KERNOBJDIR}"
623		setmakeenv KERNOBJDIR "${KERNOBJDIR}"
624		;;
625	esac
626
627	# Find the version of NetBSD
628	#
629	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
630
631	# Set the BUILDSEED to NetBSD-"N"
632	#
633	setmakeenv BUILDSEED \
634		"NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
635
636	# Set MKARZERO to "yes"
637	#
638	setmakeenv MKARZERO yes
639
640}
641
642# valid_MACHINE_ARCH -- A multi-line string, listing all valid
643# MACHINE/MACHINE_ARCH pairs.
644#
645# Each line contains a MACHINE and MACHINE_ARCH value, an optional ALIAS
646# which may be used to refer to the MACHINE/MACHINE_ARCH pair, and an
647# optional DEFAULT or NO_DEFAULT keyword.
648#
649# When a MACHINE corresponds to multiple possible values of
650# MACHINE_ARCH, then this table should list all allowed combinations.
651# If the MACHINE is associated with a default MACHINE_ARCH (to be
652# used when the user specifies the MACHINE but fails to specify the
653# MACHINE_ARCH), then one of the lines should have the "DEFAULT"
654# keyword.  If there is no default MACHINE_ARCH for a particular
655# MACHINE, then there should be a line with the "NO_DEFAULT" keyword,
656# and with a blank MACHINE_ARCH.
657#
658valid_MACHINE_ARCH='
659MACHINE=acorn32		MACHINE_ARCH=earmv4	ALIAS=eacorn32 DEFAULT
660MACHINE=algor		MACHINE_ARCH=mips64el	ALIAS=algor64
661MACHINE=algor		MACHINE_ARCH=mipsel	DEFAULT
662MACHINE=alpha		MACHINE_ARCH=alpha
663MACHINE=amd64		MACHINE_ARCH=x86_64
664MACHINE=amiga		MACHINE_ARCH=m68k
665MACHINE=amigappc	MACHINE_ARCH=powerpc
666MACHINE=arc		MACHINE_ARCH=mips64el	ALIAS=arc64
667MACHINE=arc		MACHINE_ARCH=mipsel	DEFAULT
668MACHINE=atari		MACHINE_ARCH=m68k
669MACHINE=bebox		MACHINE_ARCH=powerpc
670MACHINE=cats		MACHINE_ARCH=earmv4	ALIAS=ecats DEFAULT
671MACHINE=cesfic		MACHINE_ARCH=m68k
672MACHINE=cobalt		MACHINE_ARCH=mips64el	ALIAS=cobalt64
673MACHINE=cobalt		MACHINE_ARCH=mipsel	DEFAULT
674MACHINE=dreamcast	MACHINE_ARCH=sh3el
675MACHINE=emips		MACHINE_ARCH=mipseb
676MACHINE=epoc32		MACHINE_ARCH=earmv4	ALIAS=eepoc32 DEFAULT
677MACHINE=evbarm		MACHINE_ARCH=		NO_DEFAULT
678MACHINE=evbarm		MACHINE_ARCH=earmv4	ALIAS=evbearmv4-el	ALIAS=evbarmv4-el
679MACHINE=evbarm		MACHINE_ARCH=earmv4eb	ALIAS=evbearmv4-eb	ALIAS=evbarmv4-eb
680MACHINE=evbarm		MACHINE_ARCH=earmv5	ALIAS=evbearmv5-el	ALIAS=evbarmv5-el
681MACHINE=evbarm		MACHINE_ARCH=earmv5hf	ALIAS=evbearmv5hf-el	ALIAS=evbarmv5hf-el
682MACHINE=evbarm		MACHINE_ARCH=earmv5eb	ALIAS=evbearmv5-eb	ALIAS=evbarmv5-eb
683MACHINE=evbarm		MACHINE_ARCH=earmv5hfeb	ALIAS=evbearmv5hf-eb	ALIAS=evbarmv5hf-eb
684MACHINE=evbarm		MACHINE_ARCH=earmv6	ALIAS=evbearmv6-el	ALIAS=evbarmv6-el
685MACHINE=evbarm		MACHINE_ARCH=earmv6hf	ALIAS=evbearmv6hf-el	ALIAS=evbarmv6hf-el
686MACHINE=evbarm		MACHINE_ARCH=earmv6eb	ALIAS=evbearmv6-eb	ALIAS=evbarmv6-eb
687MACHINE=evbarm		MACHINE_ARCH=earmv6hfeb	ALIAS=evbearmv6hf-eb	ALIAS=evbarmv6hf-eb
688MACHINE=evbarm		MACHINE_ARCH=earmv7	ALIAS=evbearmv7-el	ALIAS=evbarmv7-el
689MACHINE=evbarm		MACHINE_ARCH=earmv7eb	ALIAS=evbearmv7-eb	ALIAS=evbarmv7-eb
690MACHINE=evbarm		MACHINE_ARCH=earmv7hf	ALIAS=evbearmv7hf-el	ALIAS=evbarmv7hf-el
691MACHINE=evbarm		MACHINE_ARCH=earmv7hfeb	ALIAS=evbearmv7hf-eb	ALIAS=evbarmv7hf-eb
692MACHINE=evbarm		MACHINE_ARCH=aarch64	ALIAS=evbarm64-el	ALIAS=evbarm64
693MACHINE=evbarm		MACHINE_ARCH=aarch64eb	ALIAS=evbarm64-eb
694MACHINE=evbcf		MACHINE_ARCH=coldfire
695MACHINE=evbmips		MACHINE_ARCH=		NO_DEFAULT
696MACHINE=evbmips		MACHINE_ARCH=mips64eb	ALIAS=evbmips64-eb
697MACHINE=evbmips		MACHINE_ARCH=mips64el	ALIAS=evbmips64-el
698MACHINE=evbmips		MACHINE_ARCH=mipseb	ALIAS=evbmips-eb
699MACHINE=evbmips		MACHINE_ARCH=mipsel	ALIAS=evbmips-el
700MACHINE=evbmips		MACHINE_ARCH=mipsn64eb	ALIAS=evbmipsn64-eb
701MACHINE=evbmips		MACHINE_ARCH=mipsn64el	ALIAS=evbmipsn64-el
702MACHINE=evbppc		MACHINE_ARCH=powerpc	DEFAULT
703MACHINE=evbppc		MACHINE_ARCH=powerpc64	ALIAS=evbppc64
704MACHINE=evbsh3		MACHINE_ARCH=		NO_DEFAULT
705MACHINE=evbsh3		MACHINE_ARCH=sh3eb	ALIAS=evbsh3-eb
706MACHINE=evbsh3		MACHINE_ARCH=sh3el	ALIAS=evbsh3-el
707MACHINE=ews4800mips	MACHINE_ARCH=mipseb
708MACHINE=hp300		MACHINE_ARCH=m68k
709MACHINE=hppa		MACHINE_ARCH=hppa
710MACHINE=hpcarm		MACHINE_ARCH=earmv4	ALIAS=hpcearm DEFAULT
711MACHINE=hpcmips		MACHINE_ARCH=mipsel
712MACHINE=hpcsh		MACHINE_ARCH=sh3el
713MACHINE=i386		MACHINE_ARCH=i386
714MACHINE=ia64		MACHINE_ARCH=ia64
715MACHINE=ibmnws		MACHINE_ARCH=powerpc
716MACHINE=iyonix		MACHINE_ARCH=earm	ALIAS=eiyonix DEFAULT
717MACHINE=landisk		MACHINE_ARCH=sh3el
718MACHINE=luna68k		MACHINE_ARCH=m68k
719MACHINE=mac68k		MACHINE_ARCH=m68k
720MACHINE=macppc		MACHINE_ARCH=powerpc	DEFAULT
721MACHINE=macppc		MACHINE_ARCH=powerpc64	ALIAS=macppc64
722MACHINE=mipsco		MACHINE_ARCH=mipseb
723MACHINE=mmeye		MACHINE_ARCH=sh3eb
724MACHINE=mvme68k		MACHINE_ARCH=m68k
725MACHINE=mvmeppc		MACHINE_ARCH=powerpc
726MACHINE=netwinder	MACHINE_ARCH=earmv4	ALIAS=enetwinder DEFAULT
727MACHINE=news68k		MACHINE_ARCH=m68k
728MACHINE=newsmips	MACHINE_ARCH=mipseb
729MACHINE=next68k		MACHINE_ARCH=m68k
730MACHINE=ofppc		MACHINE_ARCH=powerpc	DEFAULT
731MACHINE=ofppc		MACHINE_ARCH=powerpc64	ALIAS=ofppc64
732MACHINE=or1k		MACHINE_ARCH=or1k
733MACHINE=playstation2	MACHINE_ARCH=mipsel
734MACHINE=pmax		MACHINE_ARCH=mips64el	ALIAS=pmax64
735MACHINE=pmax		MACHINE_ARCH=mipsel	DEFAULT
736MACHINE=prep		MACHINE_ARCH=powerpc
737MACHINE=riscv		MACHINE_ARCH=riscv64	ALIAS=riscv64 DEFAULT
738MACHINE=riscv		MACHINE_ARCH=riscv32	ALIAS=riscv32
739MACHINE=rs6000		MACHINE_ARCH=powerpc
740MACHINE=sandpoint	MACHINE_ARCH=powerpc
741MACHINE=sbmips		MACHINE_ARCH=		NO_DEFAULT
742MACHINE=sbmips		MACHINE_ARCH=mips64eb	ALIAS=sbmips64-eb
743MACHINE=sbmips		MACHINE_ARCH=mips64el	ALIAS=sbmips64-el
744MACHINE=sbmips		MACHINE_ARCH=mipseb	ALIAS=sbmips-eb
745MACHINE=sbmips		MACHINE_ARCH=mipsel	ALIAS=sbmips-el
746MACHINE=sgimips		MACHINE_ARCH=mips64eb	ALIAS=sgimips64
747MACHINE=sgimips		MACHINE_ARCH=mipseb	DEFAULT
748MACHINE=shark		MACHINE_ARCH=earmv4	ALIAS=eshark DEFAULT
749MACHINE=sparc		MACHINE_ARCH=sparc
750MACHINE=sparc64		MACHINE_ARCH=sparc64
751MACHINE=sun2		MACHINE_ARCH=m68000
752MACHINE=sun3		MACHINE_ARCH=m68k
753MACHINE=vax		MACHINE_ARCH=vax
754MACHINE=virt68k		MACHINE_ARCH=m68k
755MACHINE=x68k		MACHINE_ARCH=m68k
756MACHINE=zaurus		MACHINE_ARCH=earm	ALIAS=ezaurus DEFAULT
757'
758
759# getarch -- find the default MACHINE_ARCH for a MACHINE,
760# or convert an alias to a MACHINE/MACHINE_ARCH pair.
761#
762# Saves the original value of MACHINE in makewrappermachine before
763# alias processing.
764#
765# Sets MACHINE and MACHINE_ARCH if the input MACHINE value is
766# recognised as an alias, or recognised as a machine that has a default
767# MACHINE_ARCH (or that has only one possible MACHINE_ARCH).
768#
769# Leaves MACHINE and MACHINE_ARCH unchanged if MACHINE is recognised
770# as being associated with multiple MACHINE_ARCH values with no default.
771#
772# Bombs if MACHINE is not recognised.
773#
774getarch()
775{
776	local IFS
777	local found=
778	local line
779
780	IFS="${nl}"
781	makewrappermachine="${MACHINE}"
782	for line in ${valid_MACHINE_ARCH}
783	do
784		line="${line%%#*}" # ignore comments
785		line="$( IFS=" ${tab}" ; echo $line )" # normalise white space
786		case "${line} " in
787		' ')
788			# skip blank lines or comment lines
789			continue
790			;;
791		*" ALIAS=${MACHINE} "*)
792			# Found a line with a matching ALIAS=<alias>.
793			found="$line"
794			break
795			;;
796		"MACHINE=${MACHINE} "*" NO_DEFAULT"*)
797			# Found an explicit "NO_DEFAULT" for this MACHINE.
798			found="$line"
799			break
800			;;
801		"MACHINE=${MACHINE} "*" DEFAULT"*)
802			# Found an explicit "DEFAULT" for this MACHINE.
803			found="$line"
804			break
805			;;
806		"MACHINE=${MACHINE} "*)
807			# Found a line for this MACHINE.  If it's the
808			# first such line, then tentatively accept it.
809			# If it's not the first matching line, then
810			# remember that there was more than one match.
811			case "$found" in
812			'')	found="$line" ;;
813			*)	found="MULTIPLE_MATCHES" ;;
814			esac
815			;;
816		esac
817	done
818
819	case "$found" in
820	*NO_DEFAULT*|*MULTIPLE_MATCHES*)
821		# MACHINE is OK, but MACHINE_ARCH is still unknown
822		return
823		;;
824	"MACHINE="*" MACHINE_ARCH="*)
825		# Obey the MACHINE= and MACHINE_ARCH= parts of the line.
826		IFS=' '
827		for frag in ${found}
828		do
829			case "$frag" in
830			MACHINE=*|MACHINE_ARCH=*)
831				eval "$frag"
832				;;
833			esac
834		done
835		;;
836	*)
837		bomb "Unknown target MACHINE: ${MACHINE}"
838		;;
839	esac
840}
841
842# validatearch -- check that the MACHINE/MACHINE_ARCH pair is supported.
843#
844# Bombs if the pair is not supported.
845#
846validatearch()
847{
848	local IFS
849	local line
850	local foundpair=false foundmachine=false foundarch=false
851
852	case "${MACHINE_ARCH}" in
853	"")
854		bomb "No MACHINE_ARCH provided." \
855		     "Use 'build.sh -m ${MACHINE} list-arch' to show options"
856		;;
857	esac
858
859	IFS="${nl}"
860	for line in ${valid_MACHINE_ARCH}
861	do
862		line="${line%%#*}" # ignore comments
863		line="$( IFS=" ${tab}" ; echo $line )" # normalise white space
864		case "${line} " in
865		' ')
866			# skip blank lines or comment lines
867			continue
868			;;
869		"MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH} "*)
870			foundpair=true
871			;;
872		"MACHINE=${MACHINE} "*)
873			foundmachine=true
874			;;
875		*"MACHINE_ARCH=${MACHINE_ARCH} "*)
876			foundarch=true
877			;;
878		esac
879	done
880
881	case "${foundpair}:${foundmachine}:${foundarch}" in
882	true:*)
883		: OK
884		;;
885	*:false:*)
886		bomb "Unknown target MACHINE: ${MACHINE}"
887		;;
888	*:*:false)
889		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
890		;;
891	*)
892		bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
893		;;
894	esac
895}
896
897# listarch -- list valid MACHINE/MACHINE_ARCH/ALIAS values,
898# optionally restricted to those where the MACHINE and/or MACHINE_ARCH
899# match specified glob patterns.
900#
901listarch()
902{
903	local machglob="$1" archglob="$2"
904	local IFS
905	local wildcard='*'
906	local line xline frag
907	local line_matches_machine line_matches_arch
908	local found=false
909
910	# Empty machglob or archglob should match anything
911	: "${machglob:=${wildcard}}"
912	: "${archglob:=${wildcard}}"
913
914	IFS="${nl}"
915	for line in ${valid_MACHINE_ARCH}
916	do
917		line="${line%%#*}" # ignore comments
918		xline="$( IFS=" ${tab}" ; echo $line )" # normalise white space
919		[ -z "${xline}" ] && continue # skip blank or comment lines
920
921		line_matches_machine=false
922		line_matches_arch=false
923
924		IFS=' '
925		for frag in ${xline}
926		do
927			case "${frag}" in
928			MACHINE=${machglob})
929				line_matches_machine=true ;;
930			ALIAS=${machglob})
931				line_matches_machine=true ;;
932			MACHINE_ARCH=${archglob})
933				line_matches_arch=true ;;
934			esac
935		done
936
937		if $line_matches_machine && $line_matches_arch
938		then
939			found=true
940			echo "$line"
941		fi
942	done
943	if ! $found
944	then
945		echo >&2 "No match for" \
946		    "MACHINE=${machglob} MACHINE_ARCH=${archglob}"
947		return 1
948	fi
949	return 0
950}
951
952# nobomb_getmakevar --
953# Given the name of a make variable in $1, show make's idea of the
954# value of that variable, or return 1 if there's an error.
955#
956nobomb_getmakevar()
957{
958	[ -x "${make}" ] || return 1
959	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
960_x_:
961	echo \${$1}
962.include <bsd.prog.mk>
963.include <bsd.kernobj.mk>
964EOF
965}
966
967# bomb_getmakevar --
968# Given the name of a make variable in $1, show make's idea of the
969# value of that variable, or bomb if there's an error.
970#
971bomb_getmakevar()
972{
973	[ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
974	nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
975}
976
977# getmakevar --
978# Given the name of a make variable in $1, show make's idea of the
979# value of that variable, or show a literal '$' followed by the
980# variable name if ${make} is not executable.  This is intended for use in
981# messages that need to be readable even if $make hasn't been built,
982# such as when build.sh is run with the "-n" option.
983#
984getmakevar()
985{
986	if [ -x "${make}" ]
987	then
988		bomb_getmakevar "$1"
989	else
990		echo "\$$1"
991	fi
992}
993
994setmakeenv()
995{
996	eval "$1='$2'; export $1"
997	makeenv="${makeenv} $1"
998}
999
1000safe_setmakeenv()
1001{
1002	case "$1" in
1003
1004	#	Look for any vars we want to prohibit here, like:
1005	# Bad | Dangerous)	usage "Cannot override $1 with -V";;
1006
1007	# That first char is OK has already been verified.
1008	*[!A-Za-z0-9_]*)	usage "Bad variable name (-V): '$1'";;
1009	esac
1010	setmakeenv "$@"
1011}
1012
1013unsetmakeenv()
1014{
1015	eval "unset $1"
1016	makeenv="${makeenv} $1"
1017}
1018
1019safe_unsetmakeenv()
1020{
1021	case "$1" in
1022
1023	#	Look for any vars user should not be able to unset
1024	# Needed | Must_Have)	usage "Variable $1 cannot be unset";;
1025
1026	[!A-Za-z_]* | *[!A-Za-z0-9_]*)	usage "Bad variable name (-Z): '$1'";;
1027	esac
1028	unsetmakeenv "$1"
1029}
1030
1031# Clear all variables defined in makeenv.  Used to run a subprocess
1032# outside the usual NetBSD build's make environment.
1033#
1034clearmakeenv()
1035{
1036	local var
1037
1038	for var in ${makeenv}
1039	do
1040		unset ${var}
1041	done
1042}
1043
1044# Given a variable name in $1, modify the variable in place as follows:
1045# For each space-separated word in the variable, call resolvepath.
1046#
1047resolvepaths()
1048{
1049	local var="$1"
1050	local val
1051	eval val=\"\${${var}}\"
1052	local newval=
1053	local word
1054
1055	for word in ${val}
1056	do
1057		resolvepath word
1058		newval="${newval}${newval:+ }${word}"
1059	done
1060	eval ${var}=\"\${newval}\"
1061}
1062
1063# Given a variable name in $1, modify the variable in place as follows:
1064# Convert possibly-relative path to absolute path by prepending
1065# ${TOP} if necessary.  Also delete trailing "/", if any.
1066#
1067resolvepath()
1068{
1069	local var="$1"
1070	local val
1071	eval val=\"\${${var}}\"
1072	case "${val}" in
1073	/)
1074		;;
1075	/*)
1076		val="${val%/}"
1077		;;
1078	*)
1079		val="${TOP}/${val%/}"
1080		;;
1081	esac
1082	eval ${var}=\"\${val}\"
1083}
1084
1085# Show synopsis to stdout.
1086#
1087synopsis()
1088{
1089	cat <<_usage_
1090
1091Usage: ${progname} [-EnoPRrUux] [-a ARCH] [-B BID] [-C EXTRAS]
1092                [-c COMPILER] [-D DEST] [-j NJOB] [-M MOBJ] [-m MACH]
1093                [-N NOISY] [-O OOBJ] [-R RELEASE] [-S SEED] [-T TOOLS]
1094                [-V VAR=[VALUE]] [-w WRAPPER] [-X X11SRC]
1095                [-Z VAR]
1096                OPERATION ...
1097       ${progname} ( -h | -? )
1098
1099_usage_
1100}
1101
1102# Show help to stdout.
1103#
1104help()
1105{
1106	synopsis
1107	cat <<_usage_
1108 Build OPERATIONs (all imply "obj" and "tools"):
1109    build               Run "make build".
1110    distribution        Run "make distribution" (includes DESTDIR/etc/ files).
1111    release             Run "make release" (includes kernels & distrib media).
1112
1113 Other OPERATIONs:
1114    help                Show this help message, and exit.
1115    makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper
1116                        and ${toolprefix}make.
1117                        Always performed.
1118    cleandir            Run "make cleandir".  [Default unless -u is used]
1119    dtb                 Build devicetree blobs.
1120    obj                 Run "make obj".  [Default unless -o is used]
1121    tools               Build and install tools.
1122    install=IDIR        Run "make installworld" to IDIR to install all sets
1123                        except 'etc'.  Useful after "distribution" or "release".
1124    kernel=CONF         Build kernel with config file CONF.
1125    kernel.gdb=CONF     Build kernel (including netbsd.gdb) with config
1126                        file CONF.
1127    releasekernel=CONF  Install kernel built by kernel=CONF to RELEASEDIR.
1128    kernels             Build all kernels.
1129    installmodules=IDIR Run "make installmodules" to IDIR to install all
1130                        kernel modules.
1131    modules             Build kernel modules.
1132    rumptest            Do a linktest for rump (for developers).
1133    sets                Create binary sets in
1134                        RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
1135                        DESTDIR should be populated beforehand.
1136    distsets            Same as "distribution sets".
1137    sourcesets          Create source sets in RELEASEDIR/source/sets.
1138    syspkgs             Create syspkgs in
1139                        RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
1140    pkg=CATEGORY/PKG    (EXPERIMENT) Build a package CATEGORY/PKG from pkgsrc.
1141    iso-image           Create CD-ROM image in RELEASEDIR/images.
1142    iso-image-source    Create CD-ROM image with source in RELEASEDIR/images.
1143    live-image          Create bootable live image in
1144                        RELEASEDIR/RELEASEMACHINEDIR/installation/liveimage.
1145    install-image       Create bootable installation image in
1146                        RELEASEDIR/RELEASEMACHINEDIR/installation/installimage.
1147    disk-image=TARGET   Create bootable disk image in
1148                        RELEASEDIR/RELEASEMACHINEDIR/binary/gzimg/TARGET.img.gz.
1149    params              Create params file with various make(1) parameters.
1150    show-params         Show various make(1) parameters.
1151    list-arch           Show a list of valid MACHINE/MACHINE_ARCH values,
1152                        and exit.  The list may be narrowed by passing glob
1153                        patterns or exact values in MACHINE or MACHINE_ARCH.
1154    mkrepro-timestamp   Show the latest source timestamp used for reproducible
1155                        builds and exit.  Requires -P or -V MKREPRO=yes.
1156    show-revisionid	Show the revision ID of the current directory
1157			(in SCM-dependent format) and exit.
1158			Requires -P or -V MKREPRO=yes.
1159
1160 Options:
1161    -a ARCH        Set MACHINE_ARCH=ARCH.  [Default: deduced from MACHINE]
1162    -B BID         Set BUILDID=BID.
1163    -C EXTRAS      Append EXTRAS to CDEXTRA for inclusion on CD-ROM.
1164    -c COMPILER    Select compiler from COMPILER:
1165                       clang
1166                       gcc
1167                   [Default: gcc]
1168    -D DEST        Set DESTDIR=DEST.  [Default: destdir.\${MACHINE}]
1169    -E             Set "expert" mode; disables various safety checks.
1170                   Should not be used without expert knowledge of the build
1171                   system.
1172    -h             Show this help message, and exit.
1173    -j NJOB        Run up to NJOB jobs in parallel; see make(1) -j.
1174    -M MOBJ        Set obj root directory to MOBJ; sets MAKEOBJDIRPREFIX=MOBJ,
1175                   unsets MAKEOBJDIR.
1176    -m MACH        Set MACHINE=MACH.  Some MACH values are actually
1177                   aliases that set MACHINE/MACHINE_ARCH pairs.
1178                   [Default: deduced from the host system if the host
1179                   OS is NetBSD]
1180    -N NOISY       Set the noisiness (MAKEVERBOSE) level of the build to NOISY:
1181                       0   Minimal output ("quiet").
1182                       1   Describe what is occurring.
1183                       2   Describe what is occurring and echo the actual
1184                           command.
1185                       3   Ignore the effect of the "@" prefix in make
1186                           commands.
1187                       4   Trace shell commands using the shell's -x flag.
1188                   [Default: 2]
1189    -n             Show commands that would be executed, but do not execute
1190                   them.
1191    -O OOBJ        Set obj root directory to OOBJ; sets a MAKEOBJDIR pattern
1192                   using OOBJ, unsets MAKEOBJDIRPREFIX.
1193    -o             Set MKOBJDIRS=no; do not create objdirs at start of build.
1194    -P             Set MKREPRO and MKREPRO_TIMESTAMP to the latest source
1195                   CVS timestamp for reproducible builds.
1196    -R RELEASE     Set RELEASEDIR=RELEASE.  [Default: releasedir]
1197    -r             Remove contents of TOOLDIR and DESTDIR before building.
1198    -S SEED        Set BUILDSEED=SEED.  [Default: NetBSD-majorversion]
1199    -T TOOLS       Set TOOLDIR=TOOLS.  If unset, and TOOLDIR is not set
1200                   in the environment, ${toolprefix}make will be (re)built
1201                   unconditionally.
1202    -U             Set MKUNPRIVED=yes; build without requiring root privileges,
1203                   install from an unprivileged build with proper file
1204                   permissions.
1205    -u             Set MKUPDATE=yes; do not run "make cleandir" first.
1206                   Without this, everything is rebuilt, including the tools.
1207    -V VAR=[VALUE] Set variable VAR=VALUE.
1208    -w WRAPPER     Create ${toolprefix}make script as WRAPPER.
1209                   [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
1210    -X X11SRC      Set X11SRCDIR=X11SRC.  [Default: /usr/xsrc]
1211    -x             Set MKX11=yes; build X11 from X11SRCDIR.
1212    -Z VAR         Unset ("zap") variable VAR.
1213    -?             Show this help message, and exit.
1214
1215_usage_
1216}
1217
1218# Show optional error message, help to stderr, and exit 1.
1219#
1220usage()
1221{
1222	if [ -n "$*" ]
1223	then
1224		echo 1>&2 ""
1225		echo 1>&2 "${progname}: $*"
1226	fi
1227	synopsis 1>&2
1228	exit 1
1229}
1230
1231parseoptions()
1232{
1233	opts=a:B:C:c:D:Ehj:M:m:N:nO:oPR:rS:T:UuV:w:X:xZ:
1234	opt_a=false
1235	opt_m=false
1236	local did_show_info=false
1237
1238	if type getopts >/dev/null 2>&1
1239	then
1240		# Use POSIX getopts.
1241		#
1242		getoptcmd='getopts :${opts} opt && opt=-${opt}'
1243		optargcmd=':'
1244		optremcmd='shift $((${OPTIND} -1))'
1245	else
1246		type getopt >/dev/null 2>&1 ||
1247		    bomb "Shell does not support getopts or getopt"
1248
1249		# Use old-style getopt(1) (doesn't handle whitespace in args).
1250		#
1251		args="$(getopt ${opts} $*)"
1252		[ $? = 0 ] || usage
1253		set -- ${args}
1254
1255		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
1256		optargcmd='OPTARG="$1"; shift'
1257		optremcmd=':'
1258	fi
1259
1260	# Parse command line options.
1261	#
1262	while eval ${getoptcmd}
1263	do
1264		case ${opt} in
1265
1266		-a)
1267			eval ${optargcmd}
1268			MACHINE_ARCH=${OPTARG}
1269			opt_a=true
1270			;;
1271
1272		-B)
1273			eval ${optargcmd}
1274			BUILDID=${OPTARG}
1275			;;
1276
1277		-C)
1278			eval ${optargcmd}
1279			resolvepaths OPTARG
1280			CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
1281			;;
1282
1283		-c)
1284			eval ${optargcmd}
1285			case "${OPTARG}" in
1286			gcc)	# default, no variables needed
1287				;;
1288			clang)	setmakeenv HAVE_LLVM yes
1289				setmakeenv MKLLVM yes
1290				setmakeenv MKGCC no
1291				;;
1292			#pcc)	...
1293			#	;;
1294			*)	bomb "Unknown compiler: ${OPTARG}"
1295			esac
1296			;;
1297
1298		-D)
1299			eval ${optargcmd}
1300			resolvepath OPTARG
1301			setmakeenv DESTDIR "${OPTARG}"
1302			;;
1303
1304		-E)
1305			do_expertmode=true
1306			;;
1307
1308		-j)
1309			eval ${optargcmd}
1310			parallel="-j ${OPTARG}"
1311			;;
1312
1313		-M)
1314			eval ${optargcmd}
1315			resolvepath OPTARG
1316			case "${OPTARG}" in
1317			\$*)	usage "-M argument must not begin with '\$'"
1318				;;
1319			*\$*)	# can use resolvepath, but can't set TOP_objdir
1320				resolvepath OPTARG
1321				;;
1322			*)	resolvepath OPTARG
1323				TOP_objdir="${OPTARG}${TOP}"
1324				;;
1325			esac
1326			unsetmakeenv MAKEOBJDIR
1327			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
1328			;;
1329
1330			# -m overrides MACHINE_ARCH unless "-a" is specified
1331		-m)
1332			eval ${optargcmd}
1333			MACHINE="${OPTARG}"
1334			opt_m=true
1335			;;
1336
1337		-N)
1338			eval ${optargcmd}
1339			case "${OPTARG}" in
1340			0|1|2|3|4)
1341				setmakeenv MAKEVERBOSE "${OPTARG}"
1342				;;
1343			*)
1344				usage "'${OPTARG}' is not a valid value for -N"
1345				;;
1346			esac
1347			;;
1348
1349		-n)
1350			runcmd=echo
1351			;;
1352
1353		-O)
1354			eval ${optargcmd}
1355			case "${OPTARG}" in
1356			*\$*)	usage "-O argument must not contain '\$'"
1357				;;
1358			*)	resolvepath OPTARG
1359				TOP_objdir="${OPTARG}"
1360				;;
1361			esac
1362			unsetmakeenv MAKEOBJDIRPREFIX
1363			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
1364			;;
1365
1366		-o)
1367			MKOBJDIRS=no
1368			;;
1369
1370		-P)
1371			MKREPRO=yes
1372			;;
1373
1374		-R)
1375			eval ${optargcmd}
1376			resolvepath OPTARG
1377			setmakeenv RELEASEDIR "${OPTARG}"
1378			;;
1379
1380		-r)
1381			do_removedirs=true
1382			do_rebuildmake=true
1383			;;
1384
1385		-S)
1386			eval ${optargcmd}
1387			setmakeenv BUILDSEED "${OPTARG}"
1388			;;
1389
1390		-T)
1391			eval ${optargcmd}
1392			resolvepath OPTARG
1393			TOOLDIR="${OPTARG}"
1394			export TOOLDIR
1395			;;
1396
1397		-U)
1398			setmakeenv MKUNPRIVED yes
1399			;;
1400
1401		-u)
1402			setmakeenv MKUPDATE yes
1403			;;
1404
1405		-V)
1406			eval ${optargcmd}
1407			case "${OPTARG}" in
1408		    # XXX: consider restricting which variables can be changed?
1409			[a-zA-Z_]*=*)
1410				safe_setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
1411				;;
1412			[a-zA-Z_]*)
1413				safe_setmakeenv "${OPTARG}" ""
1414				;;
1415			*)
1416				usage "-V argument must be of the form 'VAR[=VALUE]'"
1417				;;
1418			esac
1419			;;
1420
1421		-w)
1422			eval ${optargcmd}
1423			resolvepath OPTARG
1424			makewrapper="${OPTARG}"
1425			;;
1426
1427		-X)
1428			eval ${optargcmd}
1429			resolvepath OPTARG
1430			setmakeenv X11SRCDIR "${OPTARG}"
1431			;;
1432
1433		-x)
1434			setmakeenv MKX11 yes
1435			;;
1436
1437		-Z)
1438			eval ${optargcmd}
1439		    # XXX: consider restricting which variables can be unset?
1440			safe_unsetmakeenv "${OPTARG}"
1441			;;
1442
1443		--)
1444			break
1445			;;
1446
1447		-h)
1448			help
1449			exit 0
1450			;;
1451
1452		'-?')
1453			if [ "${OPTARG}" = '?' ]
1454			then
1455				help
1456				exit 0
1457			fi
1458			usage "Unknown option -${OPTARG}"
1459			;;
1460
1461		-:)
1462			usage "Missing argument for option -${OPTARG}"
1463			;;
1464
1465		*)
1466			usage "Unimplemented option ${opt}"
1467			;;
1468
1469		esac
1470	done
1471
1472	# Validate operations.
1473	#
1474	eval ${optremcmd}
1475	while [ $# -gt 0 ]
1476	do
1477		op=$1; shift
1478		operations="${operations} ${op}"
1479
1480		case "${op}" in
1481
1482		help)
1483			help
1484			exit 0
1485			;;
1486
1487		list-arch)
1488			listarch "${MACHINE}" "${MACHINE_ARCH}"
1489			exit
1490			;;
1491		mkrepro-timestamp)
1492			setup_mkrepro quiet
1493			echo "${MKREPRO_TIMESTAMP:-0}"
1494			did_show_info=true
1495			;;
1496
1497		show-revisionid)
1498			setup_mkrepro quiet
1499			echo "${NETBSD_REVISIONID}"
1500			did_show_info=true
1501			;;
1502
1503		kernel=*|releasekernel=*|kernel.gdb=*)
1504			arg=${op#*=}
1505			op=${op%%=*}
1506			[ -n "${arg}" ] ||
1507			    bomb "Must supply a kernel name with '${op}=...'"
1508			;;
1509
1510		disk-image=*)
1511			arg=${op#*=}
1512			op=disk_image
1513			[ -n "${arg}" ] ||
1514			    bomb "Must supply a target name with '${op}=...'"
1515
1516			;;
1517
1518		pkg=*)
1519			arg=${op#*=}
1520			op=${op%%=*}
1521			[ -n "${arg}" ] ||
1522			    bomb "Must supply category/package with 'pkg=...'"
1523			;;
1524
1525		install=*|installmodules=*)
1526			arg=${op#*=}
1527			op=${op%%=*}
1528			[ -n "${arg}" ] ||
1529			    bomb "Must supply a directory with 'install=...'"
1530			;;
1531
1532		distsets)
1533			operations="$(echo "$operations" | sed 's/distsets/distribution sets/')"
1534			do_sets=true
1535			op=distribution
1536			;;
1537
1538		build|\
1539		cleandir|\
1540		distribution|\
1541		dtb|\
1542		install-image|\
1543		iso-image-source|\
1544		iso-image|\
1545		kernels|\
1546		libs|\
1547		live-image|\
1548		makewrapper|\
1549		modules|\
1550		obj|\
1551		params|\
1552		release|\
1553		rump|\
1554		rumptest|\
1555		sets|\
1556		show-params|\
1557		sourcesets|\
1558		syspkgs|\
1559		tools)
1560			;;
1561
1562		*)
1563			usage "Unknown OPERATION '${op}'"
1564			;;
1565
1566		esac
1567		# ${op} may contain chars that are not allowed in variable
1568		# names.  Replace them with '_' before setting do_${op}.
1569		op="$( echo "$op" | tr -s '.-' '__')"
1570		eval do_${op}=true
1571	done
1572
1573	"$did_show_info" && [ "${MKREPRO_TIMESTAMP:-0}" -ne 0 ] && exit
1574
1575	[ -n "${operations}" ] || usage "Missing OPERATION to perform"
1576
1577	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
1578	#
1579	if [ -z "${MACHINE}" ]
1580	then
1581		[ "${uname_s}" = NetBSD ] || {
1582		    bomb "MACHINE must be set, or -m must be used," \
1583		         "for cross builds"
1584		}
1585		MACHINE=${uname_m}
1586		MACHINE_ARCH=${uname_p}
1587	fi
1588	if $opt_m && ! $opt_a
1589	then
1590		# Settings implied by the command line -m option
1591		# override MACHINE_ARCH from the environment (if any).
1592		getarch
1593	fi
1594	[ -n "${MACHINE_ARCH}" ] || getarch
1595	validatearch
1596
1597	# Set up default make(1) environment.
1598	#
1599	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
1600	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
1601	[ -z "${BUILDINFO}" ] || makeenv="${makeenv} BUILDINFO"
1602	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS}"
1603	MAKEFLAGS="${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
1604	export MAKEFLAGS MACHINE MACHINE_ARCH
1605	setmakeenv USETOOLS yes
1606	setmakeenv MAKEWRAPPERMACHINE "${makewrappermachine:-${MACHINE}}"
1607	setmakeenv MAKE_OBJDIR_CHECK_WRITABLE no
1608}
1609
1610# sanitycheck --
1611# Sanity check after parsing command line options, before rebuildmake.
1612#
1613sanitycheck()
1614{
1615	# Install as non-root is a bad idea.
1616	#
1617	if ${do_install} && [ "$id_u" -ne 0 ]
1618	then
1619		if ${do_expertmode}
1620		then
1621		    warning "Will install as an unprivileged user"
1622		else
1623		    bomb "-E must be set for install as an unprivileged user"
1624		fi
1625	fi
1626
1627	# If the PATH contains any non-absolute components (including,
1628	# but not limited to, "." or ""), then complain.  As an exception,
1629	# allow "" or "." as the last component of the PATH.  This is fatal
1630	# if expert mode is not in effect.
1631	#
1632	local path="${PATH}"
1633	path="${path%:}"	# delete trailing ":"
1634	path="${path%:.}"	# delete trailing ":."
1635	case ":${path}:/" in
1636	*:[!/~]*)
1637		if ${do_expertmode}
1638		then
1639			warning "PATH contains non-absolute components"
1640		else
1641			bomb "PATH environment variable must not" \
1642			     "contain non-absolute components"
1643		fi
1644		;;
1645	esac
1646
1647	while [ "${MKX11-no}" = yes ]		# not really a loop
1648	do
1649		test -n "${X11SRCDIR}" && {
1650		    test -d "${X11SRCDIR}/external" ||
1651			bomb "X11SRCDIR (${X11SRCDIR}) does not exist (with -x)"
1652		    break
1653		}
1654		for _xd in \
1655		    "${NETBSDSRCDIR%/*}/xsrc" \
1656		    "${NETBSDSRCDIR}/xsrc" \
1657		    /usr/xsrc
1658		do
1659		    test -f "${_xd}/Makefile" &&
1660			setmakeenv X11SRCDIR "${_xd}" &&
1661			    break 2
1662		done
1663		bomb "Asked to build X11 but no xsrc"
1664	done
1665
1666	while $do_pkg				# not really a loop
1667	do
1668		test -n "${PKGSRCDIR}" && {
1669		    test -f "${PKGSRCDIR}/mk/bsd.pkg.mk" ||
1670			bomb "PKGSRCDIR (${PKGSRCDIR}) does not exist"
1671		    break
1672		}
1673		for _pd in \
1674		    "${NETBSDSRCDIR%/*}/pkgsrc" \
1675		    "${NETBSDSRCDIR}/pkgsrc" \
1676		    /usr/pkgsrc
1677		do
1678		    test -f "${_pd}/mk/bsd.pkg.mk" &&
1679			setmakeenv PKGSRCDIR "${_pd}" &&
1680			    break 2
1681		done
1682		bomb "Asked to build package but no pkgsrc"
1683	done
1684	if $do_pkg && [ "${MKX11-no}" = yes ]
1685	then
1686		# See comment below about X11_TYPE in pkgsrc mk.conf.
1687		# (Feel free to remove this, and set X11_TYPE to
1688		# native/modular according to MKX11=yes/no, if you want
1689		# to do the work to make X11_TYPE=native cross-builds
1690		# work.)
1691		bomb "Experimental \`build.sh pkg=...'" \
1692		     "does not support -x/MKX11=yes"
1693	fi
1694}
1695
1696# print_tooldir_program --
1697# Try to find and show a path to an existing
1698# ${TOOLDIR}/bin/${toolprefix}program
1699#
1700print_tooldir_program()
1701{
1702	local possible_TOP_OBJ
1703	local possible_TOOLDIR
1704	local possible_program
1705	local tooldir_program
1706	local program="${1}"
1707
1708	if [ -n "${TOOLDIR}" ]
1709	then
1710		echo "${TOOLDIR}/bin/${toolprefix}${program}"
1711		return
1712	fi
1713
1714	# Set host_ostype to something like "NetBSD-4.5.6-i386".  This
1715	# is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
1716	#
1717	local host_ostype="${uname_s}-$(
1718			echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1719		)-$(
1720			echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1721		)"
1722
1723	# Look in a few potential locations for
1724	# ${possible_TOOLDIR}/bin/${toolprefix}${program}.
1725	# If we find it, then set possible_program.
1726	#
1727	# In the usual case (without interference from environment
1728	# variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
1729	# "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
1730	#
1731	# In practice it's difficult to figure out the correct value
1732	# for _SRC_TOP_OBJ_.  In the easiest case, when the -M or -O
1733	# options were passed to build.sh, then ${TOP_objdir} will be
1734	# the correct value.  We also try a few other possibilities, but
1735	# we do not replicate all the logic of <bsd.obj.mk>.
1736	#
1737	for possible_TOP_OBJ in \
1738		"${TOP_objdir}" \
1739		"${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
1740		"${TOP}" \
1741		"${TOP}/obj" \
1742		"${TOP}/obj.${MACHINE}"
1743	do
1744	    [ -n "${possible_TOP_OBJ}" ] || continue
1745	    possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
1746	    possible_program="${possible_TOOLDIR}/bin/${toolprefix}${program}"
1747	    if [ -x "${possible_program}" ]
1748	    then
1749		echo ${possible_program}
1750		return
1751	    fi
1752	done
1753	echo ''
1754}
1755
1756# print_tooldir_make --
1757# Try to find and show a path to an existing
1758# ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
1759# new version of ${toolprefix}make has been built.
1760#
1761# * If TOOLDIR was set in the environment or on the command line, use
1762#   that value.
1763# * Otherwise try to guess what TOOLDIR would be if not overridden by
1764#   /etc/mk.conf, and check whether the resulting directory contains
1765#   a copy of ${toolprefix}make (this should work for everybody who
1766#   doesn't override TOOLDIR via /etc/mk.conf);
1767# * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
1768#   in the PATH (this might accidentally find a version of make that
1769#   does not understand the syntax used by NetBSD make, and that will
1770#   lead to failure in the next step);
1771# * If a copy of make was found above, try to use it with
1772#   nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
1773#   result only if it's a directory that already exists;
1774# * If a value of TOOLDIR was found above, and if
1775#   ${TOOLDIR}/bin/${toolprefix}make exists, show that value.
1776#
1777print_tooldir_make()
1778{
1779	local possible_make
1780	local possible_TOOLDIR
1781	local tooldir_make
1782
1783	possible_make=$(print_tooldir_program make)
1784	# If the above didn't work, search the PATH for a suitable
1785	# ${toolprefix}make, nbmake, bmake, or make.
1786	#
1787	: ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
1788	: ${possible_make:=$(find_in_PATH nbmake '')}
1789	: ${possible_make:=$(find_in_PATH bmake '')}
1790	: ${possible_make:=$(find_in_PATH make '')}
1791
1792	# At this point, we don't care whether possible_make is in the
1793	# correct TOOLDIR or not; we simply want it to be usable by
1794	# getmakevar to help us find the correct TOOLDIR.
1795	#
1796	# Use ${possible_make} with nobomb_getmakevar to try to find
1797	# the value of TOOLDIR.  Believe the result only if it's
1798	# a directory that already exists and contains bin/${toolprefix}make.
1799	#
1800	if [ -x "${possible_make}" ]
1801	then
1802		possible_TOOLDIR=$(
1803			make="${possible_make}" \
1804			    nobomb_getmakevar TOOLDIR 2>/dev/null
1805			)
1806		if [ $? = 0 ] &&
1807		   [ -n "${possible_TOOLDIR}" ] &&
1808		   [ -d "${possible_TOOLDIR}" ]
1809		then
1810			tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
1811			if [ -x "${tooldir_make}" ]
1812			then
1813				echo "${tooldir_make}"
1814				return 0
1815			fi
1816		fi
1817	fi
1818	return 1
1819}
1820
1821# rebuildmake --
1822# Rebuild nbmake in a temporary directory if necessary.  Sets $make
1823# to a path to the nbmake executable.  Sets done_rebuildmake=true
1824# if nbmake was rebuilt.
1825#
1826# There is a cyclic dependency between building nbmake and choosing
1827# TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
1828# would like to use getmakevar to get the value of TOOLDIR; but we can't
1829# use getmakevar before we have an up to date version of nbmake; we
1830# might already have an up to date version of nbmake in TOOLDIR, but we
1831# don't yet know where TOOLDIR is.
1832#
1833# The default value of TOOLDIR also depends on the location of the top
1834# level object directory, so $(getmakevar TOOLDIR) invoked before or
1835# after making the top level object directory may produce different
1836# results.
1837#
1838# Strictly speaking, we should do the following:
1839#
1840#    1. build a new version of nbmake in a temporary directory;
1841#    2. use the temporary nbmake to create the top level obj directory;
1842#    3. use $(getmakevar TOOLDIR) with the temporary nbmake to
1843#       get the correct value of TOOLDIR;
1844#    4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
1845#
1846# However, people don't like building nbmake unnecessarily if their
1847# TOOLDIR has not changed since an earlier build.  We try to avoid
1848# rebuilding a temporary version of nbmake by taking some shortcuts to
1849# guess a value for TOOLDIR, looking for an existing version of nbmake
1850# in that TOOLDIR, and checking whether that nbmake is newer than the
1851# sources used to build it.
1852#
1853rebuildmake()
1854{
1855	make="$(print_tooldir_make)"
1856	if [ -n "${make}" ] && [ -x "${make}" ]
1857	then
1858		for f in usr.bin/make/*.[ch]
1859		do
1860			if [ "${f}" -nt "${make}" ]
1861			then
1862				statusmsg "${make} outdated" \
1863					"(older than ${f}), needs building."
1864				do_rebuildmake=true
1865				break
1866			fi
1867		done
1868	else
1869		statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
1870		do_rebuildmake=true
1871	fi
1872
1873	# Build bootstrap ${toolprefix}make if needed.
1874	if ! ${do_rebuildmake}
1875	then
1876		return
1877	fi
1878
1879	# Silent configure with MAKEVERBOSE==0
1880	if [ ${MAKEVERBOSE:-2} -eq 0 ]
1881	then
1882		configure_args=--silent
1883	fi
1884
1885	statusmsg "Bootstrapping ${toolprefix}make"
1886	${runcmd} cd "${tmpdir}"
1887	${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
1888		CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
1889	    ${HOST_SH} "${TOP}/tools/make/configure" ${configure_args} ||
1890	( cp ${tmpdir}/config.log ${tmpdir}-config.log
1891	      bomb "Configure of ${toolprefix}make failed," \
1892		   "see ${tmpdir}-config.log for details" )
1893	${runcmd} ${HOST_SH} buildmake.sh ||
1894	    bomb "Build of ${toolprefix}make failed"
1895	make="${tmpdir}/${toolprefix}make"
1896	${runcmd} cd "${TOP}"
1897	${runcmd} rm -f usr.bin/make/*.o
1898	done_rebuildmake=true
1899}
1900
1901# validatemakeparams --
1902# Perform some late sanity checks, after rebuildmake,
1903# but before createmakewrapper or any real work.
1904#
1905# Creates the top-level obj directory, because that
1906# is needed by some of the sanity checks.
1907#
1908# Shows status messages reporting the values of several variables.
1909#
1910validatemakeparams()
1911{
1912	# Determine MAKECONF first, and set in the makewrapper.
1913	# If set in the environment, then use that.
1914	# else if ./mk.conf exists, then set MAKECONF to that,
1915	# else use the default from share/mk/bsd.own.mk (/etc/mk.conf).
1916	#
1917	if [ -n "${MAKECONF+1}" ]
1918	then
1919		setmakeenv MAKECONF "${MAKECONF}"
1920		statusmsg2 "getenv MAKECONF:" "${MAKECONF}"
1921	elif [ -f "${TOP}/mk.conf" ]
1922	then
1923		setmakeenv MAKECONF "${TOP}/mk.conf"
1924		statusmsg2 "mk.conf MAKECONF:" "${MAKECONF}"
1925	else
1926		MAKECONF=$(getmakevar MAKECONF)
1927		setmakeenv MAKECONF "${MAKECONF}"
1928		statusmsg2 "share/mk MAKECONF:" "${MAKECONF}"
1929	fi
1930	if [ -z "${MAKECONF}" ]
1931	then
1932		bomb "MAKECONF must not be empty"
1933	elif [ -e "${MAKECONF}" ]
1934	then
1935		statusmsg2 "MAKECONF file:" "${MAKECONF}"
1936	else
1937		statusmsg2 "MAKECONF file:" "${MAKECONF} (File not found)"
1938	fi
1939
1940	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE.
1941	# These may be set as build.sh options or in "mk.conf".
1942	# Don't export them as they're only used for tests in build.sh.
1943	#
1944	MKOBJDIRS=$(getmakevar MKOBJDIRS)
1945	MKUNPRIVED=$(getmakevar MKUNPRIVED)
1946	MKUPDATE=$(getmakevar MKUPDATE)
1947
1948	# Non-root should always use either the -U or -E flag.
1949	#
1950	if ! ${do_expertmode} && [ "$id_u" -ne 0 ] && [ "${MKUNPRIVED}" = no ]
1951	then
1952		bomb "-U or -E must be set for build as an unprivileged user"
1953	fi
1954
1955	if [ "${runcmd}" = echo ]
1956	then
1957		TOOLCHAIN_MISSING=no
1958		EXTERNAL_TOOLCHAIN=
1959	else
1960		TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
1961		EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
1962	fi
1963	if [ "${TOOLCHAIN_MISSING}" = yes ] && [ -z "${EXTERNAL_TOOLCHAIN}" ]
1964	then
1965		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain)" \
1966						"is not yet available for"
1967		${runcmd} echo "	MACHINE:      ${MACHINE}"
1968		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
1969		${runcmd} echo ""
1970		${runcmd} echo "All builds for this platform should be done" \
1971						"via a traditional make"
1972		${runcmd} echo "If you wish to use an external" \
1973						"cross-toolchain, set"
1974		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to" \
1975						"toolchain root>"
1976		${runcmd} echo "in either the environment or mk.conf and rerun"
1977		${runcmd} echo "	${progname} $*"
1978		exit 1
1979	fi
1980
1981	if [ "${MKOBJDIRS}" != no ]
1982	then
1983		# Create the top-level object directory.
1984		#
1985		# "make obj NOSUBDIR=" can handle most cases, but it
1986		# can't handle the case where MAKEOBJDIRPREFIX is set
1987		# while the corresponding directory does not exist
1988		# (rules in <bsd.obj.mk> would abort the build).  We
1989		# therefore have to handle the MAKEOBJDIRPREFIX case
1990		# without invoking "make obj".  The MAKEOBJDIR case
1991		# could be handled either way, but we choose to handle
1992		# it similarly to MAKEOBJDIRPREFIX.
1993		#
1994		if [ -n "${TOP_obj}" ]
1995		then
1996			# It must have been set by the "-M" or "-O"
1997			# command line options, so there's no need to
1998			# use getmakevar
1999			:
2000		elif [ -n "$MAKEOBJDIRPREFIX" ]
2001		then
2002			TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
2003		elif [ -n "$MAKEOBJDIR" ]
2004		then
2005			TOP_obj="$(getmakevar MAKEOBJDIR)"
2006		fi
2007		if [ -n "$TOP_obj" ]
2008		then
2009		    ${runcmd} mkdir -p "${TOP_obj}" ||
2010			bomb "Can't create top level object directory" \
2011			     "${TOP_obj}"
2012		else
2013		    ${runcmd} "${make}" -m "${TOP}/share/mk" obj NOSUBDIR= ||
2014			    bomb "Can't create top level object directory" \
2015				 "using make obj"
2016		fi
2017
2018		# make obj in tools to ensure that the objdir for "tools"
2019		# is available.
2020		#
2021		${runcmd} cd tools
2022		${runcmd} "${make}" -m "${TOP}/share/mk" obj NOSUBDIR= ||
2023		    bomb "Failed to make obj in tools"
2024		${runcmd} cd "${TOP}"
2025	fi
2026
2027	# Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
2028	# and bomb if they have changed from the values we had from the
2029	# command line or environment.
2030	#
2031	# This must be done after creating the top-level object directory.
2032	#
2033	for var in TOOLDIR DESTDIR RELEASEDIR
2034	do
2035		eval oldval=\"\$${var}\"
2036		newval="$(getmakevar $var)"
2037		if ! $do_expertmode
2038		then
2039			: ${_SRC_TOP_OBJ_:=$(getmakevar _SRC_TOP_OBJ_)}
2040			case "$var" in
2041			DESTDIR)
2042				: ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
2043				makeenv="${makeenv} DESTDIR"
2044				;;
2045			RELEASEDIR)
2046				: ${newval:=${_SRC_TOP_OBJ_}/releasedir}
2047				makeenv="${makeenv} RELEASEDIR"
2048				;;
2049			esac
2050		fi
2051		if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]
2052		then
2053			bomb "Value of ${var} has changed" \
2054			     "(was \"${oldval}\", now \"${newval}\")"
2055		fi
2056		eval ${var}=\"\${newval}\"
2057		eval export ${var}
2058		statusmsg2 "${var} path:" "${newval}"
2059	done
2060
2061	# RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
2062	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
2063
2064	# Check validity of TOOLDIR and DESTDIR.
2065	#
2066	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = / ]
2067	then
2068		bomb "TOOLDIR '${TOOLDIR}' invalid"
2069	fi
2070	removedirs="${TOOLDIR}"
2071
2072	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = / ]
2073	then
2074		if ${do_distribution} || ${do_release} ||
2075		   [ "${uname_s}" != NetBSD ] ||
2076		   [ "${uname_m}" != "${MACHINE}" ]
2077		then
2078			bomb "DESTDIR must != / for cross builds," \
2079			     "or ${progname} 'distribution' or 'release'"
2080		fi
2081		if ! ${do_expertmode}
2082		then
2083			bomb "DESTDIR must != / for non -E (expert) builds"
2084		fi
2085		statusmsg "WARNING: Building to /, in expert mode."
2086		statusmsg "         This may cause your system to break!"
2087		statusmsg "         Reasons include:"
2088		statusmsg "           - your kernel is not up to date"
2089		statusmsg "           - the libraries or toolchain have changed"
2090		statusmsg "         YOU HAVE BEEN WARNED!"
2091	else
2092		removedirs="${removedirs} ${DESTDIR}"
2093	fi
2094	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]
2095	then
2096		bomb "Must set RELEASEDIR with 'releasekernel=...'"
2097	fi
2098
2099	# If a previous build.sh run used -U (and therefore created a
2100	# METALOG file), then most subsequent build.sh runs must also
2101	# use -U.  If DESTDIR is about to be removed, then don't perform
2102	# this check.
2103	#
2104	case "${do_removedirs} ${removedirs} " in
2105	true*" ${DESTDIR} "*)
2106		# DESTDIR is about to be removed
2107		;;
2108	*)
2109		if [ -e "${DESTDIR}/METALOG" ] &&
2110		   [ "${MKUNPRIVED}" = no ]
2111		then
2112			if $do_expertmode
2113			then
2114				warning "A previous build.sh run specified -U"
2115			else
2116				bomb "A previous build.sh run specified -U;" \
2117				     "you must specify it again now"
2118			fi
2119		fi
2120		;;
2121	esac
2122
2123	# live-image and install-image targets require binary sets
2124	# (actually DESTDIR/etc/mtree/set.* files) built with MKUNPRIVED.
2125	# If release operation is specified with live-image or install-image,
2126	# the release op should be performed with -U for later image ops.
2127	#
2128	if ${do_release} &&
2129	    { ${do_live_image} || ${do_install_image} ; } &&
2130	    [ "${MKUNPRIVED}" = no ]
2131	then
2132		bomb "-U must be specified on building release" \
2133		     "to create images later"
2134	fi
2135}
2136
2137
2138createmakewrapper()
2139{
2140	# Remove the target directories.
2141	#
2142	if ${do_removedirs}
2143	then
2144		for f in ${removedirs}
2145		do
2146			statusmsg "Removing ${f}"
2147			${runcmd} rm -r -f "${f}"
2148		done
2149	fi
2150
2151	# Recreate $TOOLDIR.
2152	#
2153	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
2154	    bomb "mkdir of '${TOOLDIR}/bin' failed"
2155
2156	# If we did not previously rebuild ${toolprefix}make, then
2157	# check whether $make is still valid and the same as the output
2158	# from print_tooldir_make.  If not, then rebuild make now.  A
2159	# possible reason for this being necessary is that the actual
2160	# value of TOOLDIR might be different from the value guessed
2161	# before the top level obj dir was created.
2162	#
2163	if ! ${done_rebuildmake} &&
2164	   { ! [ -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] ; }
2165	then
2166		rebuildmake
2167	fi
2168
2169	# Install ${toolprefix}make if it was built.
2170	#
2171	if ${done_rebuildmake}
2172	then
2173		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
2174		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
2175		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
2176		make="${TOOLDIR}/bin/${toolprefix}make"
2177		statusmsg "Created ${make}"
2178	fi
2179
2180	# Build a ${toolprefix}make wrapper script, usable by hand as
2181	# well as by build.sh.
2182	#
2183	if [ -z "${makewrapper}" ]
2184	then
2185		makewrapper="${TOOLDIR}/bin/${toolprefix}make"
2186		makewrapper="${makewrapper}-${makewrappermachine:-${MACHINE}}"
2187		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
2188	fi
2189
2190	${runcmd} rm -f "${makewrapper}"
2191	if [ "${runcmd}" = echo ]
2192	then
2193		echo 'cat <<EOF >'${makewrapper}
2194		makewrapout=
2195	else
2196		makewrapout=">>\${makewrapper}"
2197	fi
2198
2199	case "${KSH_VERSION:-${SH_VERSION}}" in
2200	*PD\ KSH*|*MIRBSD\ KSH*)
2201		set +o braceexpand
2202		;;
2203	esac
2204
2205	eval cat <<EOF ${makewrapout}
2206#! ${HOST_SH}
2207# Set proper variables to allow easy "make" building of a NetBSD subtree.
2208# Generated from:  \$NetBSD: build.sh,v 1.388 2024/12/28 00:39:56 gutteridge Exp $
2209# with these arguments: ${_args}
2210#
2211
2212EOF
2213	{
2214		sorted_vars=$(
2215			for var in ${makeenv}
2216			do
2217				echo "${var}"
2218			done |
2219				sort -u
2220		)
2221		for var in ${sorted_vars}
2222		do
2223			eval val=\"\${${var}}\"
2224			eval is_set=\"\${${var}+set}\"
2225			if [ -z "${is_set}" ]
2226			then
2227				echo "unset ${var}"
2228			else
2229				qval="$(shell_quote "${val}")"
2230				echo "${var}=${qval}; export ${var}"
2231			fi
2232		done
2233
2234		cat <<-EOF
2235
2236			exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
2237		EOF
2238	} | eval cat "${makewrapout}"
2239	[ "${runcmd}" = echo ] && echo EOF
2240	${runcmd} chmod +x "${makewrapper}"
2241	statusmsg2 "Updated makewrapper:" "${makewrapper}"
2242}
2243
2244make_in_dir()
2245{
2246	local dir="$1"
2247	local op="$2"
2248	${runcmd} cd "${dir}" ||
2249	    bomb "Failed to cd to \"${dir}\""
2250	${runcmd} "${makewrapper}" ${parallel} ${op} ||
2251	    bomb "Failed to make ${op} in \"${dir}\""
2252	${runcmd} cd "${TOP}" ||
2253	    bomb "Failed to cd back to \"${TOP}\""
2254}
2255
2256buildtools()
2257{
2258	if [ "${MKOBJDIRS}" != no ]
2259	then
2260		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
2261		    bomb "Failed to make obj-tools"
2262	fi
2263	if [ "${MKUPDATE}" = no ]
2264	then
2265		make_in_dir tools cleandir
2266	fi
2267	make_in_dir tools build_install
2268	statusmsg "Tools built to ${TOOLDIR}"
2269}
2270
2271buildlibs()
2272{
2273	if [ "${MKOBJDIRS}" != no ]
2274	then
2275		${runcmd} "${makewrapper}" ${parallel} obj ||
2276		    bomb "Failed to make obj"
2277	fi
2278	if [ "${MKUPDATE}" = no ]
2279	then
2280		make_in_dir lib cleandir
2281	fi
2282	make_in_dir . do-distrib-dirs
2283	make_in_dir . includes
2284	make_in_dir . do-lib
2285	statusmsg "libs built"
2286}
2287
2288getkernelconf()
2289{
2290	kernelconf=$1
2291	if [ "${MKOBJDIRS}" != no ]
2292	then
2293		# The correct value of KERNOBJDIR might
2294		# depend on a prior "make obj" in
2295		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
2296		#
2297		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
2298		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
2299		make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
2300	fi
2301	KERNCONFDIR=$(getmakevar KERNCONFDIR)
2302	KERNOBJDIR=$(getmakevar KERNOBJDIR)
2303	case "${kernelconf}" in
2304	*/*)
2305		kernelconfpath=${kernelconf}
2306		kernelconfname=${kernelconf##*/}
2307		;;
2308	*)
2309		kernelconfpath=${KERNCONFDIR}/${kernelconf}
2310		kernelconfname=${kernelconf}
2311		;;
2312	esac
2313	kernelbuildpath=${KERNOBJDIR}/${kernelconfname}
2314}
2315
2316diskimage()
2317{
2318	ARG="$(echo "$1" | tr '[:lower:]' '[:upper:]')"
2319	[ -f "${DESTDIR}/etc/mtree/set.base" ] ||
2320	    bomb "The release binaries must be built first"
2321	kerneldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
2322	kernel="${kerneldir}/netbsd-${ARG}.gz"
2323	[ -f "${kernel}" ] ||
2324	    bomb "The kernel ${kernel} must be built first"
2325	make_in_dir "${NETBSDSRCDIR}/etc" "smp_${1}"
2326}
2327
2328buildkernel()
2329{
2330	if ! ${do_tools} && ! ${buildkernelwarned:-false}
2331	then
2332		# Building tools every time we build a kernel is clearly
2333		# unnecessary.  We could try to figure out whether rebuilding
2334		# the tools is necessary this time, but it doesn't seem worth
2335		# the trouble.  Instead, we say it's the user's responsibility
2336		# to rebuild the tools if necessary.
2337		#
2338		statusmsg "Building kernel without building new tools"
2339		buildkernelwarned=true
2340	fi
2341	getkernelconf $1
2342	statusmsg2 "Building kernel:" "${kernelconf}"
2343	statusmsg2 "Build directory:" "${kernelbuildpath}"
2344	${runcmd} mkdir -p "${kernelbuildpath}" ||
2345	    bomb "Cannot mkdir: ${kernelbuildpath}"
2346	if [ "${MKUPDATE}" = no ]
2347	then
2348		make_in_dir "${kernelbuildpath}" cleandir
2349	fi
2350	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] ||
2351		bomb "${TOOLDIR}/bin/${toolprefix}config does not exist." \
2352		     "You need to \"$0 tools\" first"
2353	CONFIGOPTS=$(getmakevar CONFIGOPTS)
2354	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" ${CONFIGOPTS} \
2355		-b "${kernelbuildpath}" -s "${TOP}/sys" ${configopts} \
2356		"${kernelconfpath}" ||
2357	    bomb "${toolprefix}config failed for ${kernelconf}"
2358	make_in_dir "${kernelbuildpath}" depend
2359	make_in_dir "${kernelbuildpath}" all
2360
2361	if [ "${runcmd}" != echo ]
2362	then
2363		statusmsg "Kernels built from ${kernelconf}:"
2364		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
2365		for kern in ${kernlist:-netbsd}
2366		do
2367			[ -f "${kernelbuildpath}/${kern}" ] &&
2368			    echo "  ${kernelbuildpath}/${kern}"
2369		done | tee -a "${results}"
2370	fi
2371}
2372
2373releasekernel()
2374{
2375	getkernelconf $1
2376	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
2377	${runcmd} mkdir -p "${kernelreldir}"
2378	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
2379	for kern in ${kernlist:-netbsd}
2380	do
2381		builtkern="${kernelbuildpath}/${kern}"
2382		[ -f "${builtkern}" ] || continue
2383		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
2384		statusmsg2 "Kernel copy:" "${releasekern}"
2385		if [ "${runcmd}" = echo ]
2386		then
2387			echo "gzip -c -9 < ${builtkern} > ${releasekern}"
2388		else
2389			gzip -c -9 < "${builtkern}" > "${releasekern}"
2390		fi
2391	done
2392}
2393
2394buildkernels()
2395{
2396	allkernels=$( runcmd= make_in_dir etc '-V ${ALL_KERNELS}' )
2397	for k in $allkernels
2398	do
2399		buildkernel "${k}"
2400	done
2401}
2402
2403buildmodules()
2404{
2405	setmakeenv MKBINUTILS no
2406	if ! ${do_tools} && ! ${buildmoduleswarned:-false}
2407	then
2408		# Building tools every time we build modules is clearly
2409		# unnecessary as well as a kernel.
2410		#
2411		statusmsg "Building modules without building new tools"
2412		buildmoduleswarned=true
2413	fi
2414
2415	statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
2416	if [ "${MKOBJDIRS}" != no ]
2417	then
2418		make_in_dir sys/modules obj
2419	fi
2420	if [ "${MKUPDATE}" = no ]
2421	then
2422		make_in_dir sys/modules cleandir
2423	fi
2424	make_in_dir sys/modules dependall
2425	make_in_dir sys/modules install
2426
2427	statusmsg "Successful build of kernel modules for" \
2428		  "NetBSD/${MACHINE} ${DISTRIBVER}"
2429}
2430
2431builddtb()
2432{
2433	statusmsg "Building devicetree blobs for" \
2434		  "NetBSD/${MACHINE} ${DISTRIBVER}"
2435	if [ "${MKOBJDIRS}" != no ]
2436	then
2437		make_in_dir sys/dtb obj
2438	fi
2439	if [ "${MKUPDATE}" = no ]
2440	then
2441		make_in_dir sys/dtb cleandir
2442	fi
2443	make_in_dir sys/dtb dependall
2444	make_in_dir sys/dtb install
2445
2446	statusmsg "Successful build of devicetree blobs for" \
2447		  "NetBSD/${MACHINE} ${DISTRIBVER}"
2448}
2449
2450buildpkg()
2451{
2452	local catpkg
2453	local pkgroot
2454	local makejobsarg
2455	local makejobsvar
2456	local quiet
2457	local opsys_version
2458
2459	catpkg="$1"
2460
2461	pkgroot="${TOP_objdir:-${TOP}}/pkgroot"
2462	${runcmd} mkdir -p "${pkgroot}" ||
2463	    bomb "Can't create package root" "${pkgroot}"
2464
2465	# Get a symlink-free absolute path to pkg -- pkgsrc wants this.
2466	#
2467	# XXX See TOP= above regarding pwd -P.
2468	pkgroot=$(unset PWD; cd "${pkgroot}" &&
2469		((exec pwd -P 2>/dev/null) || (exec pwd 2>/dev/null)))
2470
2471	case $parallel in
2472	"-j "*)
2473		makejobsarg="--make-jobs ${parallel#-j }"
2474		makejobsvar="MAKE_JOBS=${parallel#-j }"
2475		;;
2476	*)	makejobsarg=
2477		makejobsvar=
2478		;;
2479	esac
2480
2481	if [ "${MAKEVERBOSE}" -eq 0 ]
2482	then
2483		quiet="--quiet"
2484	else
2485		quiet=
2486	fi
2487
2488	# Derived from pkgsrc/mk/bsd.prefs.mk rev. 1.451.
2489	opsys_version=$(echo "${DISTRIBVER}" |
2490		awk -F. '{
2491				major=int($1)
2492				minor=int($2)
2493				if (minor>=100) minor=99
2494				patch=int($3)
2495				if (patch>=100) patch=99
2496				printf "%02d%02d%02d", major, minor, patch
2497			}'
2498	)
2499
2500	# Bootstrap pkgsrc if needed.
2501	#
2502	# XXX Redo this if it's out-of-date, not just if it's missing.
2503	if ! [ -x "${pkgroot}/pkg/bin/bmake" ]
2504	then
2505		statusmsg "Bootstrapping pkgsrc"
2506
2507		cat >"${pkgroot}/mk.conf-fragment" <<EOF
2508USE_CROSS_COMPILE?=	no
2509TOOLDIR=		${TOOLDIR}
2510CROSS_DESTDIR=		${DESTDIR}
2511CROSS_MACHINE_ARCH=	${MACHINE_ARCH}
2512CROSS_OPSYS=		NetBSD
2513CROSS_OS_VERSION=	${DISTRIBVER}
2514CROSS_OPSYS_VERSION=	${opsys_version}
2515CROSS_LOWER_OPSYS=	netbsd
2516CROSS_LOWER_OPSYS_VERSUFFIX=	# empty
2517CROSS_LOWER_OS_VARIANT=		# empty
2518CROSS_LOWER_VARIANT_VERSION=	# empty
2519CROSS_LOWER_VENDOR=		# empty
2520CROSS_OBJECT_FMT=	ELF
2521
2522ALLOW_VULNERABLE_PACKAGES=	yes
2523BINPKG_SITES=			# empty
2524FAILOVER_FETCH=			yes
2525FETCH_TIMEOUT=			1800
2526PASSIVE_FETCH=			yes
2527
2528DISTDIR=		${pkgroot}/distfiles
2529PACKAGES=		${pkgroot}/packages
2530WRKOBJDIR=		${pkgroot}/work
2531
2532# pkgsrc cross-builds are not set up to support native X, but also part
2533# of the point of pkgsrc cross-build infrastructure is to not need
2534# native X any more.
2535#
2536# (If you fix this, remove the bomb in build.sh pkg=... on MKX11=yes.)
2537X11_TYPE=		modular
2538
2539.-include "${MAKECONF}"
2540
2541MKDEBUG=		no	# interferes with pkgsrc builds
2542EOF
2543
2544		# XXX Set --abi for mips and whatever else needs it?
2545		# XXX Unprivileged native tools, privileged cross.
2546		(
2547			cd "${PKGSRCDIR}" &&
2548			clearmakeenv &&
2549			./bootstrap/bootstrap \
2550				${makejobsarg} \
2551				--mk-fragment "${pkgroot}/mk.conf-fragment" \
2552				--prefix "${pkgroot}/pkg" \
2553				${quiet} \
2554				--unprivileged \
2555				--workdir "${pkgroot}/bootwork"
2556		) ||
2557			 bomb "Failed to bootstrap pkgsrc"
2558	fi
2559
2560	# Build the package.
2561	(
2562		cd "${PKGSRCDIR}/${catpkg}" &&
2563		clearmakeenv &&
2564		"${pkgroot}/pkg/bin/bmake" package \
2565			USE_CROSS_COMPILE=yes \
2566			${makejobsvar}
2567	) ||
2568		bomb "Failed to build ${catpkg}"
2569}
2570
2571installmodules()
2572{
2573	dir="$1"
2574	${runcmd} "${makewrapper}" INSTALLMODULESDIR="${dir}" installmodules ||
2575	    bomb "Failed to make installmodules to ${dir}"
2576	statusmsg "Successful installmodules to ${dir}"
2577}
2578
2579installworld()
2580{
2581	dir="$1"
2582	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
2583	    bomb "Failed to make installworld to ${dir}"
2584	statusmsg "Successful installworld to ${dir}"
2585}
2586
2587# Run rump build&link tests.
2588#
2589# To make this feasible for running without having to install includes and
2590# libraries into destdir (i.e. quick), we only run ld.  This is possible
2591# since the rump kernel is a closed namespace apart from calls to rumpuser.
2592# Therefore, if ld complains only about rumpuser symbols, rump kernel
2593# linking was successful.
2594#
2595# We test that rump links with a number of component configurations.
2596# These attempt to mimic what is encountered in the full build.
2597# See list below.  The list should probably be either autogenerated
2598# or managed elsewhere; keep it here until a better idea arises.
2599#
2600# Above all, note that THIS IS NOT A SUBSTITUTE FOR A FULL BUILD.
2601#
2602
2603# XXX: uwe: kern/56599 - while riastradh addressed librump problems,
2604# there are still unwanted dependencies:
2605#    net -> net_net
2606#    vfs -> fifo
2607
2608# -lrumpvfs -> $LRUMPVFS for now
2609LRUMPVFS="-lrumpvfs -lrumpvfs_nofifofs"
2610
2611RUMP_LIBSETS="
2612	-lrump,
2613        -lrumpvfs
2614            --no-whole-archive -lrumpvfs_nofifofs -lrump,
2615	-lrumpkern_tty
2616            --no-whole-archive $LRUMPVFS -lrump,
2617	-lrumpfs_tmpfs
2618            --no-whole-archive $LRUMPVFS -lrump,
2619	-lrumpfs_ffs -lrumpfs_msdos
2620            --no-whole-archive $LRUMPVFS -lrumpdev_disk -lrumpdev -lrump,
2621	-lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet
2622	    --no-whole-archive -lrump,
2623	-lrumpfs_nfs
2624	    --no-whole-archive $LRUMPVFS
2625	    -lrumpnet_sockin -lrumpnet_virtif -lrumpnet_netinet
2626            --start-group -lrumpnet_net -lrumpnet --end-group -lrump,
2627	-lrumpdev_cgd -lrumpdev_raidframe -lrumpdev_rnd -lrumpdev_dm
2628            --no-whole-archive $LRUMPVFS -lrumpdev_disk -lrumpdev -lrumpkern_crypto -lrump
2629"
2630
2631dorump()
2632{
2633	local doclean=
2634	local doobjs=
2635
2636	export RUMPKERN_ONLY=1
2637	# create obj and distrib dirs
2638	if [ "${MKOBJDIRS}" != no ]
2639	then
2640		make_in_dir "${NETBSDSRCDIR}/etc/mtree" obj
2641		make_in_dir "${NETBSDSRCDIR}/sys/rump" obj
2642	fi
2643	${runcmd} "${makewrapper}" ${parallel} do-distrib-dirs \
2644	    || bomb "Could not create distrib-dirs"
2645
2646	[ "${MKUPDATE}" = no ] && doclean="cleandir"
2647	targlist="${doclean} ${doobjs} dependall install"
2648
2649	# optimize: for test we build only static libs (3x test speedup)
2650	if [ "${1}" = rumptest ]
2651	then
2652		setmakeenv NOPIC 1
2653		setmakeenv NOPROFILE 1
2654	fi
2655
2656	for cmd in ${targlist}
2657	do
2658		make_in_dir "${NETBSDSRCDIR}/sys/rump" ${cmd}
2659	done
2660
2661	# if we just wanted to build & install rump, we're done
2662	[ "${1}" != rumptest ] && return
2663
2664	${runcmd} cd "${NETBSDSRCDIR}/sys/rump/librump/rumpkern" ||
2665		bomb "cd to rumpkern failed"
2666
2667	md_quirks=`${runcmd} "${makewrapper}" -V '${_SYMQUIRK}'`
2668	# one little, two little, three little backslashes ...
2669	md_quirks="$(echo ${md_quirks} | sed 's,\\,\\\\,g'";s/'//g" )"
2670	${runcmd} cd "${TOP}" || bomb "cd to ${TOP} failed"
2671	tool_ld=`${runcmd} "${makewrapper}" -V '${LD}'`
2672
2673	local oIFS="${IFS-UnSeTT}"
2674	IFS=","
2675	for set in ${RUMP_LIBSETS}
2676	do
2677		case "${oIFS}" in
2678		UnSeTT)	unset IFS;;
2679		*)	IFS="${oIFS}";;
2680		esac
2681		${runcmd} ${tool_ld} -nostdlib -L${DESTDIR}/usr/lib	\
2682		    -static --whole-archive ${set} --no-whole-archive   \
2683		    -lpthread -lc 2>&1 -o /tmp/rumptest.$$ |
2684		      awk -v quirks="${md_quirks}" '
2685			/undefined reference/ &&
2686			    !/more undefined references.*follow/{
2687				if (match($NF,
2688				    "`(rumpuser_|rumpcomp_|__" quirks ")") == 0)
2689					fails[NR] = $0
2690			}
2691			/cannot find -l/{fails[NR] = $0}
2692			/cannot open output file/{fails[NR] = $0}
2693			END{
2694				for (x in fails)
2695					print fails[x]
2696				exit x!=0
2697			}'
2698		[ $? -ne 0 ] && bomb "Testlink of rump failed: ${set}"
2699	done
2700	statusmsg "Rump build&link tests successful"
2701}
2702
2703repro_date() {
2704	# try the bsd date fail back the linux one
2705	date -u -r "$1" 2> /dev/null || date -u -d "@$1"
2706}
2707
2708setup_mkrepro()
2709{
2710	local quiet="$1"
2711
2712	if [ "${MKREPRO-no}" != yes ]
2713	then
2714		return
2715	fi
2716	if [ "${MKREPRO_TIMESTAMP-0}" -ne 0 ]
2717	then
2718		return
2719	fi
2720
2721	local dirs="${NETBSDSRCDIR-/usr/src}/"
2722	if [ "${MKX11-no}" = yes ]
2723	then
2724		dirs="$dirs ${X11SRCDIR-/usr/xsrc}/"
2725	fi
2726
2727	MKREPRO_TIMESTAMP=0
2728	NETBSD_REVISIONID=
2729	local d
2730	local t
2731	local tag
2732	local vcs
2733	for d in ${dirs}
2734	do
2735		if [ -d "${d}CVS" ]
2736		then
2737			local cvslatest="$(print_tooldir_program cvslatest)"
2738			if ! [ -x "${cvslatest}" ]
2739			then
2740				buildtools
2741			fi
2742			local nbdate="$(print_tooldir_program date)"
2743
2744			local cvslatestflags=
2745			if "${do_expertmode}"
2746			then
2747				cvslatestflags=-i
2748			fi
2749
2750			t=$("${cvslatest}" ${cvslatestflags} "${d}") ||
2751				bomb "${cvslatest} failed"
2752			if [ -f "${d}CVS/Tag" ]
2753			then
2754				tag=$( sed 's/^T//' < "${d}CVS/Tag" )
2755			else
2756				tag=HEAD
2757			fi
2758			NETBSD_REVISIONID="${tag}-$(
2759				${nbdate} -u -r ${t} '+%Y%m%d%H%M%S')"
2760			vcs=cvs
2761		elif [ -d "${d}.git" ] || [ -f "${d}.git" ]
2762		then
2763			t=$(cd "${d}" && git log -1 --format=%ct) ||
2764				bomb "git log %ct failed"
2765			NETBSD_REVISIONID=$(
2766			   cd "${d}" && git log -1 --format=%H) ||
2767				bomb "git log %H failed"
2768			vcs=git
2769		elif [ -d "${d}.hg" ]
2770		then
2771			t=$(hg --repo "$d" \
2772			    log -r . --template '{date.unixtime}\n') ||
2773				bomb "hg log failed"
2774			NETBSD_REVISIONID=$(hg --repo "$d" \
2775			    identify --template '{id}\n') ||
2776				bomb "hg identify failed"
2777			vcs=hg
2778		elif [ -f "${d}.hg_archival.txt" ]
2779		then
2780			local stat
2781			stat=$(print_tooldir_program stat) ||
2782				bomb "print_tooldir_program stat failed"
2783			if ! [ -x "${stat}" ]
2784			then
2785				buildtools
2786			fi
2787
2788			t=$("${stat}" -t '%s' -f '%m' "${d}.hg_archival.txt") ||
2789				bomb "stat failed on ${d}.hg_archival.txt"
2790			NETBSD_REVISIONID=$(
2791			    awk '/^node:/ { print $2 }' <"${d}.hg_archival.txt"
2792			  ) || bomb \
2793			      "awk failed to find node: in ${d}.hg_archival.txt"
2794			vcs=hg
2795		else
2796			bomb "Cannot determine VCS for '$d'"
2797		fi
2798
2799		if [ -z "$t" ]
2800		then
2801			bomb "Failed to get timestamp for vcs=$vcs in '$d'"
2802		fi
2803
2804		#echo "latest $d $vcs $t"
2805		if [ "$t" -gt "$MKREPRO_TIMESTAMP" ]
2806		then
2807			MKREPRO_TIMESTAMP="$t"
2808		fi
2809	done
2810
2811	[ "${MKREPRO_TIMESTAMP}" -ne 0 ] || bomb "Failed to compute timestamp"
2812	if [ -z "${quiet}" ]
2813	then
2814		statusmsg2 "MKREPRO_TIMESTAMP" \
2815			"$(repro_date "${MKREPRO_TIMESTAMP}")"
2816	fi
2817	export MKREPRO MKREPRO_TIMESTAMP NETBSD_REVISIONID
2818}
2819
2820main()
2821{
2822	initdefaults
2823	_args=$*
2824	parseoptions "$@"
2825
2826	sanitycheck
2827
2828	build_start=$(date)
2829	statusmsg2 "${progname} command:" "$0 $*"
2830	statusmsg2 "${progname} started:" "${build_start}"
2831	statusmsg2 "NetBSD version:"   "${DISTRIBVER}"
2832	statusmsg2 "MACHINE:"          "${MACHINE}"
2833	statusmsg2 "MACHINE_ARCH:"     "${MACHINE_ARCH}"
2834	statusmsg2 "Build platform:"   "${uname_s} ${uname_r} ${uname_m}"
2835	statusmsg2 "HOST_SH:"          "${HOST_SH}"
2836	if [ -n "${BUILDID}" ]
2837	then
2838		statusmsg2 BUILDID: "${BUILDID}"
2839	fi
2840	if [ -n "${BUILDINFO}" ]
2841	then
2842		printf "%b\n" "${BUILDINFO}" |
2843		while read -r line
2844		do
2845			[ -s "${line}" ] && continue
2846			statusmsg2 BUILDINFO: "${line}"
2847		done
2848	fi
2849
2850	if [ -n "${MAKECONF+1}" ] && [ -z "${MAKECONF}" ]
2851	then
2852		bomb "MAKECONF must not be empty"
2853	fi
2854
2855	rebuildmake
2856	validatemakeparams
2857	createmakewrapper
2858	setup_mkrepro
2859
2860	# Perform the operations.
2861	#
2862	for op in ${operations}
2863	do
2864		case "${op}" in
2865
2866		makewrapper)
2867			# no-op
2868			;;
2869
2870		tools)
2871			buildtools
2872			;;
2873		libs)
2874			buildlibs
2875			;;
2876
2877		sets)
2878			statusmsg "Building sets from pre-populated ${DESTDIR}"
2879			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2880			    bomb "Failed to make ${op}"
2881			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
2882			statusmsg "Built sets to ${setdir}"
2883			;;
2884
2885		build|distribution|release)
2886			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2887			    bomb "Failed to make ${op}"
2888			statusmsg "Successful make ${op}"
2889			;;
2890
2891		cleandir|obj|sourcesets|syspkgs|params|show-params)
2892			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2893			    bomb "Failed to make ${op}"
2894			statusmsg "Successful make ${op}"
2895			;;
2896
2897		iso-image|iso-image-source)
2898			${runcmd} "${makewrapper}" ${parallel} \
2899			    CDEXTRA="$CDEXTRA" ${op} ||
2900				bomb "Failed to make ${op}"
2901			statusmsg "Successful make ${op}"
2902			;;
2903
2904		live-image|install-image)
2905			# install-image and live-image require mtree spec files
2906			# built with MKUNPRIVED.  Assume MKUNPRIVED build has
2907			# been performed if METALOG file is created in DESTDIR.
2908			if [ ! -e "${DESTDIR}/METALOG" ]
2909			then
2910				bomb "The release binaries must have been" \
2911				     "built with -U to create images"
2912			fi
2913			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2914			    bomb "Failed to make ${op}"
2915			statusmsg "Successful make ${op}"
2916			;;
2917		kernel=*)
2918			arg=${op#*=}
2919			buildkernel "${arg}"
2920			;;
2921		kernel.gdb=*)
2922			arg=${op#*=}
2923			configopts="-D DEBUG=-g"
2924			buildkernel "${arg}"
2925			;;
2926		releasekernel=*)
2927			arg=${op#*=}
2928			releasekernel "${arg}"
2929			;;
2930
2931		kernels)
2932			buildkernels
2933			;;
2934
2935		disk-image=*)
2936			arg=${op#*=}
2937			diskimage "${arg}"
2938			;;
2939
2940		dtb)
2941			builddtb
2942			;;
2943
2944		modules)
2945			buildmodules
2946			;;
2947
2948		pkg=*)
2949			arg=${op#*=}
2950			if ! [ -d "${PKGSRCDIR}/${arg}" ]
2951			then
2952				bomb "no such package ${arg}"
2953			fi
2954			buildpkg "${arg}"
2955			;;
2956
2957		installmodules=*)
2958			arg=${op#*=}
2959			if [ "${arg}" = / ] && {
2960				[ "${uname_s}" != NetBSD ] ||
2961				[ "${uname_m}" != "${MACHINE}" ]
2962			    }
2963			then
2964				bomb "'${op}' must != / for cross builds"
2965			fi
2966			installmodules "${arg}"
2967			;;
2968
2969		install=*)
2970			arg=${op#*=}
2971			if [ "${arg}" = / ] && {
2972				[ "${uname_s}" != NetBSD ] ||
2973				[ "${uname_m}" != "${MACHINE}" ]
2974			   }
2975			then
2976				bomb "'${op}' must != / for cross builds"
2977			fi
2978			installworld "${arg}"
2979			;;
2980
2981		rump)
2982			make_in_dir . do-distrib-dirs
2983			make_in_dir . includes
2984			make_in_dir lib/csu dependall
2985			make_in_dir lib/csu install
2986			make_in_dir external/gpl3/gcc/lib/libgcc dependall
2987			make_in_dir external/gpl3/gcc/lib/libgcc install
2988			dorump "${op}"
2989			;;
2990
2991		rumptest)
2992			dorump "${op}"
2993			;;
2994
2995		*)
2996			bomb "Unknown OPERATION '${op}'"
2997			;;
2998
2999		esac
3000	done
3001
3002	statusmsg2 "${progname} ended:" "$(date)"
3003	if [ -s "${results}" ]
3004	then
3005		echo "===> Summary of results:"
3006		sed -e 's/^===>//;s/^/	/' "${results}"
3007		echo "===> ."
3008	fi
3009}
3010
3011main "$@"
3012