xref: /netbsd-src/build.sh (revision 16dd98ceb38c1ca26b86ecfbd036b6e293c7d8a7)
1f1de59e0Spgoyette#! /usr/bin/env sh
2*16dd98ceSgutteridge#	$NetBSD: build.sh,v 1.388 2024/12/28 00:39:56 gutteridge Exp $
3f1de59e0Spgoyette#
49d6c4a26Slukem# Copyright (c) 2001-2023 The NetBSD Foundation, Inc.
5f1de59e0Spgoyette# All rights reserved.
6f1de59e0Spgoyette#
7f1de59e0Spgoyette# This code is derived from software contributed to The NetBSD Foundation
8f1de59e0Spgoyette# by Todd Vierling and Luke Mewburn.
9f1de59e0Spgoyette#
10f1de59e0Spgoyette# Redistribution and use in source and binary forms, with or without
11f1de59e0Spgoyette# modification, are permitted provided that the following conditions
12f1de59e0Spgoyette# are met:
13f1de59e0Spgoyette# 1. Redistributions of source code must retain the above copyright
14f1de59e0Spgoyette#    notice, this list of conditions and the following disclaimer.
15f1de59e0Spgoyette# 2. Redistributions in binary form must reproduce the above copyright
16f1de59e0Spgoyette#    notice, this list of conditions and the following disclaimer in the
17f1de59e0Spgoyette#    documentation and/or other materials provided with the distribution.
18f1de59e0Spgoyette#
19f1de59e0Spgoyette# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f1de59e0Spgoyette# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f1de59e0Spgoyette# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f1de59e0Spgoyette# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f1de59e0Spgoyette# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f1de59e0Spgoyette# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f1de59e0Spgoyette# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f1de59e0Spgoyette# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f1de59e0Spgoyette# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f1de59e0Spgoyette# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f1de59e0Spgoyette# POSSIBILITY OF SUCH DAMAGE.
30f1de59e0Spgoyette#
31f1de59e0Spgoyette#
32f1de59e0Spgoyette# Top level build wrapper, to build or cross-build NetBSD.
33f1de59e0Spgoyette#
34f1de59e0Spgoyette
35f1de59e0Spgoyette#
36f1de59e0Spgoyette# {{{ Begin shell feature tests.
37f1de59e0Spgoyette#
38f1de59e0Spgoyette# We try to determine whether or not this script is being run under
39f1de59e0Spgoyette# a shell that supports the features that we use.  If not, we try to
40f1de59e0Spgoyette# re-exec the script under another shell.  If we can't find another
419d6c4a26Slukem# suitable shell, then we show a message and exit.
42f1de59e0Spgoyette#
43f1de59e0Spgoyette
449e6ac23fSkreerrmsg=			# error message, if not empty
45f1de59e0Spgoyetteshelltest=false		# if true, exit after testing the shell
46f1de59e0Spgoyettere_exec_allowed=true	# if true, we may exec under another shell
47f1de59e0Spgoyette
48f1de59e0Spgoyette# Parse special command line options in $1.  These special options are
49f1de59e0Spgoyette# for internal use only, are not documented, and are not valid anywhere
50f1de59e0Spgoyette# other than $1.
51f1de59e0Spgoyettecase "$1" in
529e6ac23fSkre--shelltest)
53f1de59e0Spgoyette    shelltest=true
54f1de59e0Spgoyette    re_exec_allowed=false
55f1de59e0Spgoyette    shift
56f1de59e0Spgoyette    ;;
579e6ac23fSkre--no-re-exec)
58f1de59e0Spgoyette    re_exec_allowed=false
59f1de59e0Spgoyette    shift
60f1de59e0Spgoyette    ;;
61f1de59e0Spgoyetteesac
62f1de59e0Spgoyette
63f1de59e0Spgoyette# Solaris /bin/sh, and other SVR4 shells, do not support "!".
64f1de59e0Spgoyette# This is the first feature that we test, because subsequent
65f1de59e0Spgoyette# tests use "!".
66f1de59e0Spgoyette#
679e6ac23fSkre# Unfortunately, if the shell doesn't support ! most of the tests
689e6ac23fSkre# following which use '!' are likely to simply abort with a syntax error.
699e6ac23fSkre# Not executing the code containing ! does not avoid compiling it.
709e6ac23fSkre#
719e6ac23fSkreif test -z "$errmsg"
729e6ac23fSkrethen
739e6ac23fSkre    if ( eval '! false' ) >/dev/null 2>&1
749e6ac23fSkre    then
75f1de59e0Spgoyette	:
76f1de59e0Spgoyette    else
77f1de59e0Spgoyette	errmsg='Shell does not support "!".'
78f1de59e0Spgoyette    fi
79f1de59e0Spgoyettefi
80f1de59e0Spgoyette
81f1de59e0Spgoyette# Does the shell support functions?
82f1de59e0Spgoyette#
839e6ac23fSkreif test -z "$errmsg"
849e6ac23fSkrethen
85f1de59e0Spgoyette    if ! (
86f1de59e0Spgoyette	eval 'somefunction() { : ; }'
87f1de59e0Spgoyette	) >/dev/null 2>&1
88f1de59e0Spgoyette    then
89f1de59e0Spgoyette	errmsg='Shell does not support functions.'
90f1de59e0Spgoyette    fi
91f1de59e0Spgoyettefi
92f1de59e0Spgoyette
93f1de59e0Spgoyette# Does the shell support the "local" keyword for variables in functions?
94f1de59e0Spgoyette#
95f1de59e0Spgoyette# Local variables are not required by SUSv3, but some scripts run during
96f1de59e0Spgoyette# the NetBSD build use them.
97f1de59e0Spgoyette#
98f1de59e0Spgoyette# ksh93 fails this test; it uses an incompatible syntax involving the
99f1de59e0Spgoyette# keywords 'function' and 'typeset'.
100f1de59e0Spgoyette#
1019e6ac23fSkreif test -z "$errmsg"
1029e6ac23fSkrethen
103f1de59e0Spgoyette    if ! (
1049e6ac23fSkre	eval 'f() { local v=2; }; v=1; f && test x"$v" = x1'
105f1de59e0Spgoyette	) >/dev/null 2>&1
106f1de59e0Spgoyette    then
107f1de59e0Spgoyette	errmsg='Shell does not support the "local" keyword in functions.'
108f1de59e0Spgoyette    fi
109f1de59e0Spgoyettefi
110f1de59e0Spgoyette
111f1de59e0Spgoyette# Does the shell support ${var%suffix}, ${var#prefix}, and their variants?
112f1de59e0Spgoyette#
113f1de59e0Spgoyette# We don't bother testing for ${var+value}, ${var-value}, or their variants,
1149e6ac23fSkre# since shells without those (unlikely) are sure to fail other tests too.
115f1de59e0Spgoyette#
1169e6ac23fSkreif test -z "$errmsg"
1179e6ac23fSkrethen
118f1de59e0Spgoyette    if ! (
119f1de59e0Spgoyette	eval 'var=a/b/c ;
120f1de59e0Spgoyette	      test x"${var#*/};${var##*/};${var%/*};${var%%/*}" = \
121f1de59e0Spgoyette		   x"b/c;c;a/b;a" ;'
122f1de59e0Spgoyette	) >/dev/null 2>&1
123f1de59e0Spgoyette    then
124f1de59e0Spgoyette	errmsg='Shell does not support "${var%suffix}" or "${var#prefix}".'
125f1de59e0Spgoyette    fi
126f1de59e0Spgoyettefi
127f1de59e0Spgoyette
128f1de59e0Spgoyette# Does the shell support IFS?
129f1de59e0Spgoyette#
130f1de59e0Spgoyette# zsh in normal mode (as opposed to "emulate sh" mode) fails this test.
131f1de59e0Spgoyette#
1329e6ac23fSkreif test -z "$errmsg"
1339e6ac23fSkrethen
134f1de59e0Spgoyette    if ! (
135f1de59e0Spgoyette	eval 'IFS=: ; v=":a b::c" ; set -- $v ; IFS=+ ;
136f1de59e0Spgoyette		test x"$#;$1,$2,$3,$4;$*" = x"4;,a b,,c;+a b++c"'
137f1de59e0Spgoyette	) >/dev/null 2>&1
138f1de59e0Spgoyette    then
139f1de59e0Spgoyette	errmsg='Shell does not support IFS word splitting.'
140f1de59e0Spgoyette    fi
141f1de59e0Spgoyettefi
142f1de59e0Spgoyette
143f1de59e0Spgoyette# Does the shell support ${1+"$@"}?
144f1de59e0Spgoyette#
145f1de59e0Spgoyette# Some versions of zsh fail this test, even in "emulate sh" mode.
146f1de59e0Spgoyette#
1479e6ac23fSkreif test -z "$errmsg"
1489e6ac23fSkrethen
149f1de59e0Spgoyette    if ! (
1509e6ac23fSkre	eval 'set -- "a a a" "b b b"
1519e6ac23fSkre	      set -- ${1+"$@"}
1529e6ac23fSkre	      test x"$#;$1;$2" = x"2;a a a;b b b" '
153f1de59e0Spgoyette	) >/dev/null 2>&1
154f1de59e0Spgoyette    then
155f1de59e0Spgoyette	errmsg='Shell does not support ${1+"$@"}.'
156f1de59e0Spgoyette    fi
157f1de59e0Spgoyettefi
158f1de59e0Spgoyette
159f1de59e0Spgoyette# Does the shell support $(...) command substitution?
160f1de59e0Spgoyette#
1619e6ac23fSkreif test -z "$errmsg"
1629e6ac23fSkrethen
163f1de59e0Spgoyette    if ! (
1649e6ac23fSkre	eval 'var=$(echo abc); test x"$var" = xabc'
165f1de59e0Spgoyette	) >/dev/null 2>&1
166f1de59e0Spgoyette    then
167f1de59e0Spgoyette	errmsg='Shell does not support "$(...)" command substitution.'
168f1de59e0Spgoyette    fi
169f1de59e0Spgoyettefi
170f1de59e0Spgoyette
171f1de59e0Spgoyette# Does the shell support $(...) command substitution with
172f1de59e0Spgoyette# unbalanced parentheses?
173f1de59e0Spgoyette#
174f1de59e0Spgoyette# Some shells known to fail this test are:  NetBSD /bin/ksh (as of 2009-12),
175f1de59e0Spgoyette# bash-3.1, pdksh-5.2.14, zsh-4.2.7 in "emulate sh" mode.
176f1de59e0Spgoyette#
1779e6ac23fSkreif test -z "$errmsg"
1789e6ac23fSkrethen
179f1de59e0Spgoyette    if ! (
180f1de59e0Spgoyette	eval 'var=$(case x in x) echo abc;; esac); test x"$var" = x"abc"'
181f1de59e0Spgoyette	) >/dev/null 2>&1
182f1de59e0Spgoyette    then
183f1de59e0Spgoyette	# XXX: This test is ignored because so many shells fail it; instead,
184f1de59e0Spgoyette	#      the NetBSD build avoids using the problematic construct.
185f1de59e0Spgoyette	: ignore 'Shell does not support "$(...)" with unbalanced ")".'
186f1de59e0Spgoyette    fi
187f1de59e0Spgoyettefi
188f1de59e0Spgoyette
189f1de59e0Spgoyette# Does the shell support getopts or getopt?
1909e6ac23fSkre#  (XXX: Q: why the need for the eval here, looks unncessary)
191f1de59e0Spgoyette#
1929e6ac23fSkreif test -z "$errmsg"
1939e6ac23fSkrethen
194f1de59e0Spgoyette    if ! (
195f1de59e0Spgoyette	eval 'type getopts || type getopt'
196f1de59e0Spgoyette	) >/dev/null 2>&1
197f1de59e0Spgoyette    then
198f1de59e0Spgoyette	errmsg='Shell does not support getopts or getopt.'
199f1de59e0Spgoyette    fi
200f1de59e0Spgoyettefi
201f1de59e0Spgoyette
202f1de59e0Spgoyette#
203f1de59e0Spgoyette# If shelltest is true, exit now, reporting whether or not the shell is good.
204f1de59e0Spgoyette#
2059e6ac23fSkreif "$shelltest"
2069e6ac23fSkrethen
2079e6ac23fSkre    if test -n "$errmsg"
2089e6ac23fSkre    then
209f1de59e0Spgoyette	echo >&2 "$0: $errmsg"
210f1de59e0Spgoyette	exit 1
211f1de59e0Spgoyette    else
212f1de59e0Spgoyette	exit 0
213f1de59e0Spgoyette    fi
214f1de59e0Spgoyettefi
215f1de59e0Spgoyette
216f1de59e0Spgoyette#
217f1de59e0Spgoyette# If the shell was bad, try to exec a better shell, or report an error.
218f1de59e0Spgoyette#
219f1de59e0Spgoyette# Loops are broken by passing an extra "--no-re-exec" flag to the new
220f1de59e0Spgoyette# instance of this script.
221f1de59e0Spgoyette#
2229e6ac23fSkreif test -n "$errmsg"
2239e6ac23fSkrethen
2249e6ac23fSkre    if "$re_exec_allowed"
2259e6ac23fSkre    then
226f1de59e0Spgoyette	for othershell in \
227f1de59e0Spgoyette	    "${HOST_SH}" /usr/xpg4/bin/sh ksh ksh88 mksh pdksh dash bash
228f1de59e0Spgoyette	    # NOTE: some shells known not to work are:
229f1de59e0Spgoyette	    # any shell using csh syntax;
230f1de59e0Spgoyette	    # Solaris /bin/sh (missing many modern features);
231f1de59e0Spgoyette	    # ksh93 (incompatible syntax for local variables);
232f1de59e0Spgoyette	    # zsh (many differences, unless run in compatibility mode).
233f1de59e0Spgoyette	do
234f1de59e0Spgoyette	    test -n "$othershell" || continue
235f1de59e0Spgoyette	    if eval 'type "$othershell"' >/dev/null 2>&1 \
2369e6ac23fSkre		&& $othershell "$0" --shelltest >/dev/null 2>&1
237f1de59e0Spgoyette	    then
238f1de59e0Spgoyette		cat <<EOF
239f1de59e0Spgoyette$0: $errmsg
240f1de59e0Spgoyette$0: Retrying under $othershell
241f1de59e0SpgoyetteEOF
242f1de59e0Spgoyette		HOST_SH="$othershell"
243f1de59e0Spgoyette		export HOST_SH
244f1de59e0Spgoyette		exec $othershell "$0" --no-re-exec "$@" # avoid ${1+"$@"}
245f1de59e0Spgoyette	    fi
246f1de59e0Spgoyette	    # If HOST_SH was set, but failed the test above,
247f1de59e0Spgoyette	    # then give up without trying any other shells.
248f1de59e0Spgoyette	    test x"${othershell}" = x"${HOST_SH}" && break
249f1de59e0Spgoyette	done
250f1de59e0Spgoyette    fi
251f1de59e0Spgoyette
252f1de59e0Spgoyette    #
253f1de59e0Spgoyette    # If we get here, then the shell is bad, and we either could not
254f1de59e0Spgoyette    # find a replacement, or were not allowed to try a replacement.
255f1de59e0Spgoyette    #
256f1de59e0Spgoyette    cat <<EOF
257f1de59e0Spgoyette$0: $errmsg
258f1de59e0Spgoyette
259f1de59e0SpgoyetteThe NetBSD build system requires a shell that supports modern POSIX
260f1de59e0Spgoyettefeatures, as well as the "local" keyword in functions (which is a
261f1de59e0Spgoyettewidely-implemented but non-standardised feature).
262f1de59e0Spgoyette
263f1de59e0SpgoyettePlease re-run this script under a suitable shell.  For example:
264f1de59e0Spgoyette
265f1de59e0Spgoyette	/path/to/suitable/shell $0 ...
266f1de59e0Spgoyette
267f1de59e0SpgoyetteThe above command will usually enable build.sh to automatically set
268f1de59e0SpgoyetteHOST_SH=/path/to/suitable/shell, but if that fails, then you may also
269f1de59e0Spgoyetteneed to explicitly set the HOST_SH environment variable, as follows:
270f1de59e0Spgoyette
271f1de59e0Spgoyette	HOST_SH=/path/to/suitable/shell
272f1de59e0Spgoyette	export HOST_SH
273f1de59e0Spgoyette	\${HOST_SH} $0 ...
274f1de59e0SpgoyetteEOF
275f1de59e0Spgoyette    exit 1
276f1de59e0Spgoyettefi
277f1de59e0Spgoyette
278f1de59e0Spgoyette#
279f1de59e0Spgoyette# }}} End shell feature tests.
280f1de59e0Spgoyette#
281f1de59e0Spgoyette
282f1de59e0Spgoyetteprogname=${0##*/}
283f1de59e0Spgoyettetoppid=$$
284f1de59e0Spgoyetteresults=/dev/null
285f1de59e0Spgoyettetab='	'
286f1de59e0Spgoyettenl='
287f1de59e0Spgoyette'
288f1de59e0Spgoyettetrap "exit 1" 1 2 3 15
289f1de59e0Spgoyette
290f1de59e0Spgoyettebomb()
291f1de59e0Spgoyette{
292f1de59e0Spgoyette	cat >&2 <<ERRORMESSAGE
293f1de59e0Spgoyette
2949e6ac23fSkreERROR: $*
295262db514Slukem
296f1de59e0Spgoyette*** BUILD ABORTED ***
297f1de59e0SpgoyetteERRORMESSAGE
298f1de59e0Spgoyette	kill ${toppid}		# in case we were invoked from a subshell
299f1de59e0Spgoyette	exit 1
300f1de59e0Spgoyette}
301f1de59e0Spgoyette
302f1de59e0Spgoyette# Quote args to make them safe in the shell.
303f1de59e0Spgoyette# Usage: quotedlist="$(shell_quote args...)"
304f1de59e0Spgoyette#
305f1de59e0Spgoyette# After building up a quoted list, use it by evaling it inside
306f1de59e0Spgoyette# double quotes, like this:
307f1de59e0Spgoyette#    eval "set -- $quotedlist"
308f1de59e0Spgoyette# or like this:
309f1de59e0Spgoyette#    eval "\$command $quotedlist \$filename"
310f1de59e0Spgoyette#
311f1de59e0Spgoyetteshell_quote()
3129e6ac23fSkre(
3139e6ac23fSkre	local result=
314f1de59e0Spgoyette	local arg qarg
315f1de59e0Spgoyette	LC_COLLATE=C ; export LC_COLLATE # so [a-zA-Z0-9] works in ASCII
3169e6ac23fSkre	for arg in "$@"
3179e6ac23fSkre	do
318f1de59e0Spgoyette		case "${arg}" in
319f1de59e0Spgoyette		'')
320f1de59e0Spgoyette			qarg="''"
321f1de59e0Spgoyette			;;
322f1de59e0Spgoyette		*[!-./a-zA-Z0-9]*)
323f1de59e0Spgoyette			# Convert each embedded ' to '\'',
324f1de59e0Spgoyette			# then insert ' at the beginning of the first line,
325f1de59e0Spgoyette			# and append ' at the end of the last line.
326f1de59e0Spgoyette			# Finally, elide unnecessary '' pairs at the
327f1de59e0Spgoyette			# beginning and end of the result and as part of
328f1de59e0Spgoyette			# '\'''\'' sequences that result from multiple
329f1de59e0Spgoyette			# adjacent quotes in he input.
3309e6ac23fSkre			qarg=$(printf "%s\n" "$arg" |
331f1de59e0Spgoyette			    ${SED:-sed} -e "s/'/'\\\\''/g" \
332f1de59e0Spgoyette				-e "1s/^/'/" -e "\$s/\$/'/" \
333f1de59e0Spgoyette				-e "1s/^''//" -e "\$s/''\$//" \
334f1de59e0Spgoyette				-e "s/'''/'/g"
3359e6ac23fSkre			    )
336f1de59e0Spgoyette			;;
337f1de59e0Spgoyette		*)
338f1de59e0Spgoyette			# Arg is not the empty string, and does not contain
339f1de59e0Spgoyette			# any unsafe characters.  Leave it unchanged for
340f1de59e0Spgoyette			# readability.
341f1de59e0Spgoyette			qarg="${arg}"
342f1de59e0Spgoyette			;;
343f1de59e0Spgoyette		esac
344f1de59e0Spgoyette		result="${result}${result:+ }${qarg}"
345f1de59e0Spgoyette	done
346f1de59e0Spgoyette	printf "%s\n" "$result"
3479e6ac23fSkre)
348f1de59e0Spgoyette
349f1de59e0Spgoyettestatusmsg()
350f1de59e0Spgoyette{
3519e6ac23fSkre	${runcmd} echo "===> $*" | tee -a "${results}"
352f1de59e0Spgoyette}
353f1de59e0Spgoyette
354f1de59e0Spgoyettestatusmsg2()
355f1de59e0Spgoyette{
356f1de59e0Spgoyette	local msg
357f1de59e0Spgoyette
3589e6ac23fSkre	msg=${1}
359f1de59e0Spgoyette	shift
3609e6ac23fSkre
361f1de59e0Spgoyette	case "${msg}" in
362f1de59e0Spgoyette	????????????????*)	;;
363f1de59e0Spgoyette	??????????*)		msg="${msg}      ";;
364f1de59e0Spgoyette	?????*)			msg="${msg}           ";;
365f1de59e0Spgoyette	*)			msg="${msg}                ";;
366f1de59e0Spgoyette	esac
367f1de59e0Spgoyette	case "${msg}" in
368f1de59e0Spgoyette	?????????????????????*)	;;
369f1de59e0Spgoyette	????????????????????)	msg="${msg} ";;
370f1de59e0Spgoyette	???????????????????)	msg="${msg}  ";;
371f1de59e0Spgoyette	??????????????????)	msg="${msg}   ";;
372f1de59e0Spgoyette	?????????????????)	msg="${msg}    ";;
373f1de59e0Spgoyette	????????????????)	msg="${msg}     ";;
374f1de59e0Spgoyette	esac
375f1de59e0Spgoyette	statusmsg "${msg}$*"
376f1de59e0Spgoyette}
377f1de59e0Spgoyette
378f1de59e0Spgoyettewarning()
379f1de59e0Spgoyette{
3809e6ac23fSkre	statusmsg "Warning: $*"
381f1de59e0Spgoyette}
382f1de59e0Spgoyette
3839d6c4a26Slukem# Find a program in the PATH, and show the result.  If not found,
3849d6c4a26Slukem# show a default.  If $2 is defined (even if it is an empty string),
385f1de59e0Spgoyette# then that is the default; otherwise, $1 is used as the default.
386546bd46bSlukem#
387f1de59e0Spgoyettefind_in_PATH()
388f1de59e0Spgoyette{
389e6351014Skre	local prog="$1"
390e6351014Skre	local result="${2-$1}"
391f1de59e0Spgoyette	local dir
3929e6ac23fSkre	local IFS=:
3939e6ac23fSkre
3949e6ac23fSkre	for dir in ${PATH}
3959e6ac23fSkre	do
3969e6ac23fSkre		if [ -x "${dir}/${prog}" ]
3979e6ac23fSkre		then
3989e6ac23fSkre			result=${dir}/${prog}
399f1de59e0Spgoyette			break
400f1de59e0Spgoyette		fi
401f1de59e0Spgoyette	done
402f1de59e0Spgoyette	echo "${result}"
403f1de59e0Spgoyette}
404f1de59e0Spgoyette
405f1de59e0Spgoyette# Try to find a working POSIX shell, and set HOST_SH to refer to it.
406f1de59e0Spgoyette# Assumes that uname_s, uname_m, and PWD have been set.
407546bd46bSlukem#
408f1de59e0Spgoyetteset_HOST_SH()
409f1de59e0Spgoyette{
410f1de59e0Spgoyette	# Even if ${HOST_SH} is already defined, we still do the
411f1de59e0Spgoyette	# sanity checks at the end.
412f1de59e0Spgoyette
413f1de59e0Spgoyette	# Solaris has /usr/xpg4/bin/sh.
414f1de59e0Spgoyette	#
4159e6ac23fSkre	[ -z "${HOST_SH}" ] &&
4169e6ac23fSkre		[ "${uname_s}" = SunOS ] &&
4179e6ac23fSkre		[ -x /usr/xpg4/bin/sh ] &&
4189e6ac23fSkre			HOST_SH=/usr/xpg4/bin/sh
419f1de59e0Spgoyette
420f1de59e0Spgoyette	# Try to get the name of the shell that's running this script,
421f1de59e0Spgoyette	# by parsing the output from "ps".  We assume that, if the host
422f1de59e0Spgoyette	# system's ps command supports -o comm at all, it will do so
423f1de59e0Spgoyette	# in the usual way: a one-line header followed by a one-line
424f1de59e0Spgoyette	# result, possibly including trailing white space.  And if the
425f1de59e0Spgoyette	# host system's ps command doesn't support -o comm, we assume
426f1de59e0Spgoyette	# that we'll get an error message on stderr and nothing on
427f1de59e0Spgoyette	# stdout.  (We don't try to use ps -o 'comm=' to suppress the
428f1de59e0Spgoyette	# header line, because that is less widely supported.)
429f1de59e0Spgoyette	#
430f1de59e0Spgoyette	# If we get the wrong result here, the user can override it by
431f1de59e0Spgoyette	# specifying HOST_SH in the environment.
432f1de59e0Spgoyette	#
4339e6ac23fSkre	[ -z "${HOST_SH}" ] && HOST_SH=$(
4349e6ac23fSkre		(
4359e6ac23fSkre			ps -p $$ -o comm |
4369e6ac23fSkre			    sed -ne "2s/[ ${tab}]*\$//p"
4379e6ac23fSkre		) 2>/dev/null
4389e6ac23fSkre	)
439f1de59e0Spgoyette
440f1de59e0Spgoyette	# If nothing above worked, use "sh".  We will later find the
441f1de59e0Spgoyette	# first directory in the PATH that has a "sh" program.
442f1de59e0Spgoyette	#
443f1de59e0Spgoyette	[ -z "${HOST_SH}" ] && HOST_SH="sh"
444f1de59e0Spgoyette
445f1de59e0Spgoyette	# If the result so far is not an absolute path, try to prepend
446f1de59e0Spgoyette	# PWD or search the PATH.
447f1de59e0Spgoyette	#
448f1de59e0Spgoyette	case "${HOST_SH}" in
449f1de59e0Spgoyette	/*)	:
450f1de59e0Spgoyette		;;
451f1de59e0Spgoyette	*/*)	HOST_SH="${PWD}/${HOST_SH}"
452f1de59e0Spgoyette		;;
453f1de59e0Spgoyette	*)	HOST_SH="$(find_in_PATH "${HOST_SH}")"
454f1de59e0Spgoyette		;;
455f1de59e0Spgoyette	esac
456f1de59e0Spgoyette
457f1de59e0Spgoyette	# If we don't have an absolute path by now, bomb.
458f1de59e0Spgoyette	#
459f1de59e0Spgoyette	case "${HOST_SH}" in
460f1de59e0Spgoyette	/*)	:
461f1de59e0Spgoyette		;;
462262db514Slukem	*)	bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path"
463f1de59e0Spgoyette		;;
464f1de59e0Spgoyette	esac
465f1de59e0Spgoyette
466f1de59e0Spgoyette	# If HOST_SH is not executable, bomb.
467f1de59e0Spgoyette	#
468f1de59e0Spgoyette	[ -x "${HOST_SH}" ] ||
469262db514Slukem	    bomb "HOST_SH=\"${HOST_SH}\" is not executable"
470f1de59e0Spgoyette
471f1de59e0Spgoyette	# If HOST_SH fails tests, bomb.
472f1de59e0Spgoyette	# ("$0" may be a path that is no longer valid, because we have
473f1de59e0Spgoyette	# performed "cd $(dirname $0)", so don't use $0 here.)
474f1de59e0Spgoyette	#
475f1de59e0Spgoyette	"${HOST_SH}" build.sh --shelltest ||
476262db514Slukem	    bomb "HOST_SH=\"${HOST_SH}\" failed functionality tests"
477f1de59e0Spgoyette}
478f1de59e0Spgoyette
479f1de59e0Spgoyette# initdefaults --
480f1de59e0Spgoyette# Set defaults before parsing command line options.
481f1de59e0Spgoyette#
482f1de59e0Spgoyetteinitdefaults()
483f1de59e0Spgoyette{
484f1de59e0Spgoyette	makeenv=
485f1de59e0Spgoyette	makewrapper=
486f1de59e0Spgoyette	makewrappermachine=
487f1de59e0Spgoyette	runcmd=
488f1de59e0Spgoyette	operations=
489f1de59e0Spgoyette	removedirs=
490f1de59e0Spgoyette
491f1de59e0Spgoyette	[ -d usr.bin/make ] || cd "$(dirname $0)"
492f1de59e0Spgoyette	[ -d usr.bin/make ] ||
4939e6ac23fSkre	    bomb "usr.bin/make not found; build.sh must be run from" \
4949e6ac23fSkre	         "the top level of source directory"
495f1de59e0Spgoyette	[ -f share/mk/bsd.own.mk ] ||
496f1de59e0Spgoyette	    bomb "src/share/mk is missing; please re-fetch the source tree"
497f1de59e0Spgoyette
498f1de59e0Spgoyette	# Set various environment variables to known defaults,
499f1de59e0Spgoyette	# to minimize (cross-)build problems observed "in the field".
500f1de59e0Spgoyette	#
501f1de59e0Spgoyette	# LC_ALL=C must be set before we try to parse the output from
502f1de59e0Spgoyette	# any command.  Other variables are set (or unset) here, before
503f1de59e0Spgoyette	# we parse command line arguments.
504f1de59e0Spgoyette	#
505f1de59e0Spgoyette	# These variables can be overridden via "-V var=value" if
506f1de59e0Spgoyette	# you know what you are doing.
507f1de59e0Spgoyette	#
508a0439553Schristos	unsetmakeenv C_INCLUDE_PATH
509a0439553Schristos	unsetmakeenv CPLUS_INCLUDE_PATH
510f1de59e0Spgoyette	unsetmakeenv INFODIR
511f1de59e0Spgoyette	unsetmakeenv LESSCHARSET
512f1de59e0Spgoyette	unsetmakeenv MAKEFLAGS
513f1de59e0Spgoyette	unsetmakeenv TERMINFO
514f1de59e0Spgoyette	setmakeenv LC_ALL C
515f1de59e0Spgoyette
516f1de59e0Spgoyette	# Find information about the build platform.  This should be
517f1de59e0Spgoyette	# kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
518f1de59e0Spgoyette	# variables in share/mk/bsd.sys.mk.
519f1de59e0Spgoyette	#
520f1de59e0Spgoyette	# Note that "uname -p" is not part of POSIX, but we want uname_p
521f1de59e0Spgoyette	# to be set to the host MACHINE_ARCH, if possible.  On systems
5229d6c4a26Slukem	# where "uname -p" fails, shows "unknown", or shows a string
523f1de59e0Spgoyette	# that does not look like an identifier, fall back to using the
524f1de59e0Spgoyette	# output from "uname -m" instead.
525f1de59e0Spgoyette	#
526f1de59e0Spgoyette	uname_s=$(uname -s 2>/dev/null)
527f1de59e0Spgoyette	uname_r=$(uname -r 2>/dev/null)
528f1de59e0Spgoyette	uname_m=$(uname -m 2>/dev/null)
529f1de59e0Spgoyette	uname_p=$(uname -p 2>/dev/null || echo "unknown")
530f1de59e0Spgoyette	case "${uname_p}" in
53106c7dd3eSsborrill	''|unknown|*[!-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
532f1de59e0Spgoyette	esac
533f1de59e0Spgoyette
534f1de59e0Spgoyette	id_u=$(id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null)
535f1de59e0Spgoyette
536f1de59e0Spgoyette	# If $PWD is a valid name of the current directory, POSIX mandates
537f1de59e0Spgoyette	# that pwd return it by default which causes problems in the
538f1de59e0Spgoyette	# presence of symlinks.  Unsetting PWD is simpler than changing
539f1de59e0Spgoyette	# every occurrence of pwd to use -P.
540f1de59e0Spgoyette	#
541f1de59e0Spgoyette	# XXX Except that doesn't work on Solaris. Or many Linuces.
5429e6ac23fSkre	#     And the standard says altering PWD produces unspecified results
5439e6ac23fSkre	# So instead just cd -P to $PWD which should make PWD be symlink free
544f1de59e0Spgoyette	#
5459e6ac23fSkre	cd -P "${PWD}" || bomb "Cannot cd to \$PWD ($PWD)"
5469e6ac23fSkre	# At this point TOP=$PWD should just work, but let's be ultra safe.
547f1de59e0Spgoyette	TOP=$( (exec pwd -P 2>/dev/null) || (exec pwd 2>/dev/null) )
548f1de59e0Spgoyette
549f1de59e0Spgoyette	# The user can set HOST_SH in the environment, or we try to
550f1de59e0Spgoyette	# guess an appropriate value.  Then we set several other
551f1de59e0Spgoyette	# variables from HOST_SH.
552f1de59e0Spgoyette	#
553f1de59e0Spgoyette	set_HOST_SH
554f1de59e0Spgoyette	setmakeenv HOST_SH "${HOST_SH}"
555f1de59e0Spgoyette	setmakeenv BSHELL "${HOST_SH}"
556f1de59e0Spgoyette	setmakeenv CONFIG_SHELL "${HOST_SH}"
557f1de59e0Spgoyette
558f1de59e0Spgoyette	# Set defaults.
559f1de59e0Spgoyette	#
560f1de59e0Spgoyette	toolprefix=nb
561f1de59e0Spgoyette
562f1de59e0Spgoyette	# Some systems have a small ARG_MAX.  -X prevents make(1) from
563f1de59e0Spgoyette	# exporting variables in the environment redundantly.
564f1de59e0Spgoyette	#
565f1de59e0Spgoyette	case "${uname_s}" in
566f1de59e0Spgoyette	Darwin | FreeBSD | CYGWIN*)
567f1de59e0Spgoyette		MAKEFLAGS="-X ${MAKEFLAGS}"
568f1de59e0Spgoyette		;;
569f1de59e0Spgoyette	esac
570f1de59e0Spgoyette
571f1de59e0Spgoyette	# do_{operation}=true if given operation is requested.
572f1de59e0Spgoyette	#
573f1de59e0Spgoyette	do_expertmode=false
574f1de59e0Spgoyette	do_rebuildmake=false
575f1de59e0Spgoyette	do_removedirs=false
576f1de59e0Spgoyette	do_tools=false
5770a37cd5eSchristos	do_libs=false
578f1de59e0Spgoyette	do_cleandir=false
579f1de59e0Spgoyette	do_obj=false
580f1de59e0Spgoyette	do_build=false
581f1de59e0Spgoyette	do_distribution=false
582f1de59e0Spgoyette	do_release=false
583f1de59e0Spgoyette	do_kernel=false
584f1de59e0Spgoyette	do_releasekernel=false
585f1de59e0Spgoyette	do_kernels=false
586f1de59e0Spgoyette	do_modules=false
587f1de59e0Spgoyette	do_installmodules=false
588f1de59e0Spgoyette	do_install=false
589f1de59e0Spgoyette	do_sets=false
590f1de59e0Spgoyette	do_sourcesets=false
591f1de59e0Spgoyette	do_syspkgs=false
59259040e53Sriastradh	do_pkg=false
593f1de59e0Spgoyette	do_iso_image=false
594f1de59e0Spgoyette	do_iso_image_source=false
595f1de59e0Spgoyette	do_live_image=false
596f1de59e0Spgoyette	do_install_image=false
597f1de59e0Spgoyette	do_disk_image=false
598f1de59e0Spgoyette	do_params=false
599439f74efSlukem	do_show_params=false
600f1de59e0Spgoyette	do_rump=false
601043d88a7Sjmcneill	do_dtb=false
602f1de59e0Spgoyette
603f1de59e0Spgoyette	# done_{operation}=true if given operation has been done.
604f1de59e0Spgoyette	#
605f1de59e0Spgoyette	done_rebuildmake=false
606f1de59e0Spgoyette
607f1de59e0Spgoyette	# Create scratch directory
608f1de59e0Spgoyette	#
609f1de59e0Spgoyette	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
610f1de59e0Spgoyette	mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
611f1de59e0Spgoyette	trap "cd /; rm -r -f \"${tmpdir}\"" 0
612f1de59e0Spgoyette	results="${tmpdir}/build.sh.results"
613f1de59e0Spgoyette
614f1de59e0Spgoyette	# Set source directories
615f1de59e0Spgoyette	#
616f1de59e0Spgoyette	setmakeenv NETBSDSRCDIR "${TOP}"
617f1de59e0Spgoyette
618f1de59e0Spgoyette	# Make sure KERNOBJDIR is an absolute path if defined
619f1de59e0Spgoyette	#
620f1de59e0Spgoyette	case "${KERNOBJDIR}" in
621f1de59e0Spgoyette	''|/*)	;;
622f1de59e0Spgoyette	*)	KERNOBJDIR="${TOP}/${KERNOBJDIR}"
623f1de59e0Spgoyette		setmakeenv KERNOBJDIR "${KERNOBJDIR}"
624f1de59e0Spgoyette		;;
625f1de59e0Spgoyette	esac
626f1de59e0Spgoyette
627f1de59e0Spgoyette	# Find the version of NetBSD
628f1de59e0Spgoyette	#
629f1de59e0Spgoyette	DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
630f1de59e0Spgoyette
631f1de59e0Spgoyette	# Set the BUILDSEED to NetBSD-"N"
632f1de59e0Spgoyette	#
6339e6ac23fSkre	setmakeenv BUILDSEED \
6349e6ac23fSkre		"NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
635f1de59e0Spgoyette
636f1de59e0Spgoyette	# Set MKARZERO to "yes"
637f1de59e0Spgoyette	#
6389e6ac23fSkre	setmakeenv MKARZERO yes
639f1de59e0Spgoyette
640f1de59e0Spgoyette}
641f1de59e0Spgoyette
642f1de59e0Spgoyette# valid_MACHINE_ARCH -- A multi-line string, listing all valid
643f1de59e0Spgoyette# MACHINE/MACHINE_ARCH pairs.
644f1de59e0Spgoyette#
645f1de59e0Spgoyette# Each line contains a MACHINE and MACHINE_ARCH value, an optional ALIAS
646f1de59e0Spgoyette# which may be used to refer to the MACHINE/MACHINE_ARCH pair, and an
647f1de59e0Spgoyette# optional DEFAULT or NO_DEFAULT keyword.
648f1de59e0Spgoyette#
649f1de59e0Spgoyette# When a MACHINE corresponds to multiple possible values of
650f1de59e0Spgoyette# MACHINE_ARCH, then this table should list all allowed combinations.
651f1de59e0Spgoyette# If the MACHINE is associated with a default MACHINE_ARCH (to be
652f1de59e0Spgoyette# used when the user specifies the MACHINE but fails to specify the
653f1de59e0Spgoyette# MACHINE_ARCH), then one of the lines should have the "DEFAULT"
654f1de59e0Spgoyette# keyword.  If there is no default MACHINE_ARCH for a particular
655f1de59e0Spgoyette# MACHINE, then there should be a line with the "NO_DEFAULT" keyword,
656f1de59e0Spgoyette# and with a blank MACHINE_ARCH.
657f1de59e0Spgoyette#
658f1de59e0Spgoyettevalid_MACHINE_ARCH='
65953e13300SchristosMACHINE=acorn32		MACHINE_ARCH=earmv4	ALIAS=eacorn32 DEFAULT
660f1de59e0SpgoyetteMACHINE=algor		MACHINE_ARCH=mips64el	ALIAS=algor64
661f1de59e0SpgoyetteMACHINE=algor		MACHINE_ARCH=mipsel	DEFAULT
662f1de59e0SpgoyetteMACHINE=alpha		MACHINE_ARCH=alpha
663f1de59e0SpgoyetteMACHINE=amd64		MACHINE_ARCH=x86_64
664f1de59e0SpgoyetteMACHINE=amiga		MACHINE_ARCH=m68k
665f1de59e0SpgoyetteMACHINE=amigappc	MACHINE_ARCH=powerpc
666f1de59e0SpgoyetteMACHINE=arc		MACHINE_ARCH=mips64el	ALIAS=arc64
667f1de59e0SpgoyetteMACHINE=arc		MACHINE_ARCH=mipsel	DEFAULT
668f1de59e0SpgoyetteMACHINE=atari		MACHINE_ARCH=m68k
669f1de59e0SpgoyetteMACHINE=bebox		MACHINE_ARCH=powerpc
670f1de59e0SpgoyetteMACHINE=cats		MACHINE_ARCH=earmv4	ALIAS=ecats DEFAULT
671f1de59e0SpgoyetteMACHINE=cesfic		MACHINE_ARCH=m68k
672f1de59e0SpgoyetteMACHINE=cobalt		MACHINE_ARCH=mips64el	ALIAS=cobalt64
673f1de59e0SpgoyetteMACHINE=cobalt		MACHINE_ARCH=mipsel	DEFAULT
674f1de59e0SpgoyetteMACHINE=dreamcast	MACHINE_ARCH=sh3el
675f1de59e0SpgoyetteMACHINE=emips		MACHINE_ARCH=mipseb
67653e13300SchristosMACHINE=epoc32		MACHINE_ARCH=earmv4	ALIAS=eepoc32 DEFAULT
6776437518aSjmcneillMACHINE=evbarm		MACHINE_ARCH=		NO_DEFAULT
67856608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv4	ALIAS=evbearmv4-el	ALIAS=evbarmv4-el
67956608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv4eb	ALIAS=evbearmv4-eb	ALIAS=evbarmv4-eb
68056608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv5	ALIAS=evbearmv5-el	ALIAS=evbarmv5-el
681db045f7dSrinMACHINE=evbarm		MACHINE_ARCH=earmv5hf	ALIAS=evbearmv5hf-el	ALIAS=evbarmv5hf-el
68256608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv5eb	ALIAS=evbearmv5-eb	ALIAS=evbarmv5-eb
683db045f7dSrinMACHINE=evbarm		MACHINE_ARCH=earmv5hfeb	ALIAS=evbearmv5hf-eb	ALIAS=evbarmv5hf-eb
68456608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv6	ALIAS=evbearmv6-el	ALIAS=evbarmv6-el
68556608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv6hf	ALIAS=evbearmv6hf-el	ALIAS=evbarmv6hf-el
68656608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv6eb	ALIAS=evbearmv6-eb	ALIAS=evbarmv6-eb
68756608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv6hfeb	ALIAS=evbearmv6hf-eb	ALIAS=evbarmv6hf-eb
68856608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv7	ALIAS=evbearmv7-el	ALIAS=evbarmv7-el
68956608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv7eb	ALIAS=evbearmv7-eb	ALIAS=evbarmv7-eb
69056608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv7hf	ALIAS=evbearmv7hf-el	ALIAS=evbarmv7hf-el
69156608fa6SmrgMACHINE=evbarm		MACHINE_ARCH=earmv7hfeb	ALIAS=evbearmv7hf-eb	ALIAS=evbarmv7hf-eb
6926437518aSjmcneillMACHINE=evbarm		MACHINE_ARCH=aarch64	ALIAS=evbarm64-el	ALIAS=evbarm64
693f1de59e0SpgoyetteMACHINE=evbarm		MACHINE_ARCH=aarch64eb	ALIAS=evbarm64-eb
694f1de59e0SpgoyetteMACHINE=evbcf		MACHINE_ARCH=coldfire
695f1de59e0SpgoyetteMACHINE=evbmips		MACHINE_ARCH=		NO_DEFAULT
696f1de59e0SpgoyetteMACHINE=evbmips		MACHINE_ARCH=mips64eb	ALIAS=evbmips64-eb
697f1de59e0SpgoyetteMACHINE=evbmips		MACHINE_ARCH=mips64el	ALIAS=evbmips64-el
698f1de59e0SpgoyetteMACHINE=evbmips		MACHINE_ARCH=mipseb	ALIAS=evbmips-eb
699f1de59e0SpgoyetteMACHINE=evbmips		MACHINE_ARCH=mipsel	ALIAS=evbmips-el
700609e7268SchristosMACHINE=evbmips		MACHINE_ARCH=mipsn64eb	ALIAS=evbmipsn64-eb
701609e7268SchristosMACHINE=evbmips		MACHINE_ARCH=mipsn64el	ALIAS=evbmipsn64-el
702f1de59e0SpgoyetteMACHINE=evbppc		MACHINE_ARCH=powerpc	DEFAULT
703f1de59e0SpgoyetteMACHINE=evbppc		MACHINE_ARCH=powerpc64	ALIAS=evbppc64
704f1de59e0SpgoyetteMACHINE=evbsh3		MACHINE_ARCH=		NO_DEFAULT
705f1de59e0SpgoyetteMACHINE=evbsh3		MACHINE_ARCH=sh3eb	ALIAS=evbsh3-eb
706f1de59e0SpgoyetteMACHINE=evbsh3		MACHINE_ARCH=sh3el	ALIAS=evbsh3-el
707f1de59e0SpgoyetteMACHINE=ews4800mips	MACHINE_ARCH=mipseb
708f1de59e0SpgoyetteMACHINE=hp300		MACHINE_ARCH=m68k
709f1de59e0SpgoyetteMACHINE=hppa		MACHINE_ARCH=hppa
710f1de59e0SpgoyetteMACHINE=hpcarm		MACHINE_ARCH=earmv4	ALIAS=hpcearm DEFAULT
711f1de59e0SpgoyetteMACHINE=hpcmips		MACHINE_ARCH=mipsel
712f1de59e0SpgoyetteMACHINE=hpcsh		MACHINE_ARCH=sh3el
713f1de59e0SpgoyetteMACHINE=i386		MACHINE_ARCH=i386
714f1de59e0SpgoyetteMACHINE=ia64		MACHINE_ARCH=ia64
715f1de59e0SpgoyetteMACHINE=ibmnws		MACHINE_ARCH=powerpc
716f1de59e0SpgoyetteMACHINE=iyonix		MACHINE_ARCH=earm	ALIAS=eiyonix DEFAULT
717f1de59e0SpgoyetteMACHINE=landisk		MACHINE_ARCH=sh3el
718f1de59e0SpgoyetteMACHINE=luna68k		MACHINE_ARCH=m68k
719f1de59e0SpgoyetteMACHINE=mac68k		MACHINE_ARCH=m68k
720f1de59e0SpgoyetteMACHINE=macppc		MACHINE_ARCH=powerpc	DEFAULT
721f1de59e0SpgoyetteMACHINE=macppc		MACHINE_ARCH=powerpc64	ALIAS=macppc64
722f1de59e0SpgoyetteMACHINE=mipsco		MACHINE_ARCH=mipseb
723f1de59e0SpgoyetteMACHINE=mmeye		MACHINE_ARCH=sh3eb
724f1de59e0SpgoyetteMACHINE=mvme68k		MACHINE_ARCH=m68k
725f1de59e0SpgoyetteMACHINE=mvmeppc		MACHINE_ARCH=powerpc
726f1de59e0SpgoyetteMACHINE=netwinder	MACHINE_ARCH=earmv4	ALIAS=enetwinder DEFAULT
727f1de59e0SpgoyetteMACHINE=news68k		MACHINE_ARCH=m68k
728f1de59e0SpgoyetteMACHINE=newsmips	MACHINE_ARCH=mipseb
729f1de59e0SpgoyetteMACHINE=next68k		MACHINE_ARCH=m68k
730f1de59e0SpgoyetteMACHINE=ofppc		MACHINE_ARCH=powerpc	DEFAULT
731f1de59e0SpgoyetteMACHINE=ofppc		MACHINE_ARCH=powerpc64	ALIAS=ofppc64
732f1de59e0SpgoyetteMACHINE=or1k		MACHINE_ARCH=or1k
733f1de59e0SpgoyetteMACHINE=playstation2	MACHINE_ARCH=mipsel
734f1de59e0SpgoyetteMACHINE=pmax		MACHINE_ARCH=mips64el	ALIAS=pmax64
735f1de59e0SpgoyetteMACHINE=pmax		MACHINE_ARCH=mipsel	DEFAULT
736f1de59e0SpgoyetteMACHINE=prep		MACHINE_ARCH=powerpc
737f1de59e0SpgoyetteMACHINE=riscv		MACHINE_ARCH=riscv64	ALIAS=riscv64 DEFAULT
738f1de59e0SpgoyetteMACHINE=riscv		MACHINE_ARCH=riscv32	ALIAS=riscv32
739f1de59e0SpgoyetteMACHINE=rs6000		MACHINE_ARCH=powerpc
740f1de59e0SpgoyetteMACHINE=sandpoint	MACHINE_ARCH=powerpc
741f1de59e0SpgoyetteMACHINE=sbmips		MACHINE_ARCH=		NO_DEFAULT
742f1de59e0SpgoyetteMACHINE=sbmips		MACHINE_ARCH=mips64eb	ALIAS=sbmips64-eb
743f1de59e0SpgoyetteMACHINE=sbmips		MACHINE_ARCH=mips64el	ALIAS=sbmips64-el
744f1de59e0SpgoyetteMACHINE=sbmips		MACHINE_ARCH=mipseb	ALIAS=sbmips-eb
745f1de59e0SpgoyetteMACHINE=sbmips		MACHINE_ARCH=mipsel	ALIAS=sbmips-el
746f1de59e0SpgoyetteMACHINE=sgimips		MACHINE_ARCH=mips64eb	ALIAS=sgimips64
747f1de59e0SpgoyetteMACHINE=sgimips		MACHINE_ARCH=mipseb	DEFAULT
748f1de59e0SpgoyetteMACHINE=shark		MACHINE_ARCH=earmv4	ALIAS=eshark DEFAULT
749f1de59e0SpgoyetteMACHINE=sparc		MACHINE_ARCH=sparc
750f1de59e0SpgoyetteMACHINE=sparc64		MACHINE_ARCH=sparc64
751f1de59e0SpgoyetteMACHINE=sun2		MACHINE_ARCH=m68000
752f1de59e0SpgoyetteMACHINE=sun3		MACHINE_ARCH=m68k
753f1de59e0SpgoyetteMACHINE=vax		MACHINE_ARCH=vax
754b1992656SthorpejMACHINE=virt68k		MACHINE_ARCH=m68k
755f1de59e0SpgoyetteMACHINE=x68k		MACHINE_ARCH=m68k
756f1de59e0SpgoyetteMACHINE=zaurus		MACHINE_ARCH=earm	ALIAS=ezaurus DEFAULT
757f1de59e0Spgoyette'
758f1de59e0Spgoyette
759f1de59e0Spgoyette# getarch -- find the default MACHINE_ARCH for a MACHINE,
760f1de59e0Spgoyette# or convert an alias to a MACHINE/MACHINE_ARCH pair.
761f1de59e0Spgoyette#
762f1de59e0Spgoyette# Saves the original value of MACHINE in makewrappermachine before
763f1de59e0Spgoyette# alias processing.
764f1de59e0Spgoyette#
765f1de59e0Spgoyette# Sets MACHINE and MACHINE_ARCH if the input MACHINE value is
766f1de59e0Spgoyette# recognised as an alias, or recognised as a machine that has a default
767f1de59e0Spgoyette# MACHINE_ARCH (or that has only one possible MACHINE_ARCH).
768f1de59e0Spgoyette#
769f1de59e0Spgoyette# Leaves MACHINE and MACHINE_ARCH unchanged if MACHINE is recognised
770f1de59e0Spgoyette# as being associated with multiple MACHINE_ARCH values with no default.
771f1de59e0Spgoyette#
772f1de59e0Spgoyette# Bombs if MACHINE is not recognised.
773f1de59e0Spgoyette#
774f1de59e0Spgoyettegetarch()
775f1de59e0Spgoyette{
776f1de59e0Spgoyette	local IFS
7779e6ac23fSkre	local found=
778f1de59e0Spgoyette	local line
779f1de59e0Spgoyette
780f1de59e0Spgoyette	IFS="${nl}"
781f1de59e0Spgoyette	makewrappermachine="${MACHINE}"
7829e6ac23fSkre	for line in ${valid_MACHINE_ARCH}
7839e6ac23fSkre	do
784f1de59e0Spgoyette		line="${line%%#*}" # ignore comments
785f1de59e0Spgoyette		line="$( IFS=" ${tab}" ; echo $line )" # normalise white space
786f1de59e0Spgoyette		case "${line} " in
7879e6ac23fSkre		' ')
788f1de59e0Spgoyette			# skip blank lines or comment lines
789f1de59e0Spgoyette			continue
790f1de59e0Spgoyette			;;
791f1de59e0Spgoyette		*" ALIAS=${MACHINE} "*)
792f1de59e0Spgoyette			# Found a line with a matching ALIAS=<alias>.
793f1de59e0Spgoyette			found="$line"
794f1de59e0Spgoyette			break
795f1de59e0Spgoyette			;;
796f1de59e0Spgoyette		"MACHINE=${MACHINE} "*" NO_DEFAULT"*)
797f1de59e0Spgoyette			# Found an explicit "NO_DEFAULT" for this MACHINE.
798f1de59e0Spgoyette			found="$line"
799f1de59e0Spgoyette			break
800f1de59e0Spgoyette			;;
801f1de59e0Spgoyette		"MACHINE=${MACHINE} "*" DEFAULT"*)
802f1de59e0Spgoyette			# Found an explicit "DEFAULT" for this MACHINE.
803f1de59e0Spgoyette			found="$line"
804f1de59e0Spgoyette			break
805f1de59e0Spgoyette			;;
806f1de59e0Spgoyette		"MACHINE=${MACHINE} "*)
807f1de59e0Spgoyette			# Found a line for this MACHINE.  If it's the
808f1de59e0Spgoyette			# first such line, then tentatively accept it.
809f1de59e0Spgoyette			# If it's not the first matching line, then
810f1de59e0Spgoyette			# remember that there was more than one match.
811f1de59e0Spgoyette			case "$found" in
812f1de59e0Spgoyette			'')	found="$line" ;;
813f1de59e0Spgoyette			*)	found="MULTIPLE_MATCHES" ;;
814f1de59e0Spgoyette			esac
815f1de59e0Spgoyette			;;
816f1de59e0Spgoyette		esac
817f1de59e0Spgoyette	done
818f1de59e0Spgoyette
819f1de59e0Spgoyette	case "$found" in
820f1de59e0Spgoyette	*NO_DEFAULT*|*MULTIPLE_MATCHES*)
821f1de59e0Spgoyette		# MACHINE is OK, but MACHINE_ARCH is still unknown
822f1de59e0Spgoyette		return
823f1de59e0Spgoyette		;;
824f1de59e0Spgoyette	"MACHINE="*" MACHINE_ARCH="*)
825f1de59e0Spgoyette		# Obey the MACHINE= and MACHINE_ARCH= parts of the line.
8269e6ac23fSkre		IFS=' '
8279e6ac23fSkre		for frag in ${found}
8289e6ac23fSkre		do
829f1de59e0Spgoyette			case "$frag" in
830f1de59e0Spgoyette			MACHINE=*|MACHINE_ARCH=*)
831f1de59e0Spgoyette				eval "$frag"
832f1de59e0Spgoyette				;;
833f1de59e0Spgoyette			esac
834f1de59e0Spgoyette		done
835f1de59e0Spgoyette		;;
836f1de59e0Spgoyette	*)
837f1de59e0Spgoyette		bomb "Unknown target MACHINE: ${MACHINE}"
838f1de59e0Spgoyette		;;
839f1de59e0Spgoyette	esac
840f1de59e0Spgoyette}
841f1de59e0Spgoyette
842f1de59e0Spgoyette# validatearch -- check that the MACHINE/MACHINE_ARCH pair is supported.
843f1de59e0Spgoyette#
844f1de59e0Spgoyette# Bombs if the pair is not supported.
845f1de59e0Spgoyette#
846f1de59e0Spgoyettevalidatearch()
847f1de59e0Spgoyette{
848f1de59e0Spgoyette	local IFS
849f1de59e0Spgoyette	local line
850f1de59e0Spgoyette	local foundpair=false foundmachine=false foundarch=false
851f1de59e0Spgoyette
852f1de59e0Spgoyette	case "${MACHINE_ARCH}" in
853f1de59e0Spgoyette	"")
8549e6ac23fSkre		bomb "No MACHINE_ARCH provided." \
8559e6ac23fSkre		     "Use 'build.sh -m ${MACHINE} list-arch' to show options"
856f1de59e0Spgoyette		;;
857f1de59e0Spgoyette	esac
858f1de59e0Spgoyette
859f1de59e0Spgoyette	IFS="${nl}"
8609e6ac23fSkre	for line in ${valid_MACHINE_ARCH}
8619e6ac23fSkre	do
862f1de59e0Spgoyette		line="${line%%#*}" # ignore comments
863f1de59e0Spgoyette		line="$( IFS=" ${tab}" ; echo $line )" # normalise white space
864f1de59e0Spgoyette		case "${line} " in
8659e6ac23fSkre		' ')
866f1de59e0Spgoyette			# skip blank lines or comment lines
867f1de59e0Spgoyette			continue
868f1de59e0Spgoyette			;;
869f1de59e0Spgoyette		"MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH} "*)
870f1de59e0Spgoyette			foundpair=true
871f1de59e0Spgoyette			;;
872f1de59e0Spgoyette		"MACHINE=${MACHINE} "*)
873f1de59e0Spgoyette			foundmachine=true
874f1de59e0Spgoyette			;;
875f1de59e0Spgoyette		*"MACHINE_ARCH=${MACHINE_ARCH} "*)
876f1de59e0Spgoyette			foundarch=true
877f1de59e0Spgoyette			;;
878f1de59e0Spgoyette		esac
879f1de59e0Spgoyette	done
880f1de59e0Spgoyette
881f1de59e0Spgoyette	case "${foundpair}:${foundmachine}:${foundarch}" in
882f1de59e0Spgoyette	true:*)
883f1de59e0Spgoyette		: OK
884f1de59e0Spgoyette		;;
885f1de59e0Spgoyette	*:false:*)
886f1de59e0Spgoyette		bomb "Unknown target MACHINE: ${MACHINE}"
887f1de59e0Spgoyette		;;
888f1de59e0Spgoyette	*:*:false)
889f1de59e0Spgoyette		bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
890f1de59e0Spgoyette		;;
891f1de59e0Spgoyette	*)
892f1de59e0Spgoyette		bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
893f1de59e0Spgoyette		;;
894f1de59e0Spgoyette	esac
895f1de59e0Spgoyette}
896f1de59e0Spgoyette
897f1de59e0Spgoyette# listarch -- list valid MACHINE/MACHINE_ARCH/ALIAS values,
898f1de59e0Spgoyette# optionally restricted to those where the MACHINE and/or MACHINE_ARCH
899ba70c588Sandvar# match specified glob patterns.
900f1de59e0Spgoyette#
901f1de59e0Spgoyettelistarch()
902f1de59e0Spgoyette{
903e6351014Skre	local machglob="$1" archglob="$2"
904f1de59e0Spgoyette	local IFS
9059e6ac23fSkre	local wildcard='*'
906f1de59e0Spgoyette	local line xline frag
907f1de59e0Spgoyette	local line_matches_machine line_matches_arch
908f1de59e0Spgoyette	local found=false
909f1de59e0Spgoyette
910f1de59e0Spgoyette	# Empty machglob or archglob should match anything
911f1de59e0Spgoyette	: "${machglob:=${wildcard}}"
912f1de59e0Spgoyette	: "${archglob:=${wildcard}}"
913f1de59e0Spgoyette
914f1de59e0Spgoyette	IFS="${nl}"
9159e6ac23fSkre	for line in ${valid_MACHINE_ARCH}
9169e6ac23fSkre	do
917f1de59e0Spgoyette		line="${line%%#*}" # ignore comments
918f1de59e0Spgoyette		xline="$( IFS=" ${tab}" ; echo $line )" # normalise white space
919f1de59e0Spgoyette		[ -z "${xline}" ] && continue # skip blank or comment lines
920f1de59e0Spgoyette
921f1de59e0Spgoyette		line_matches_machine=false
922f1de59e0Spgoyette		line_matches_arch=false
923f1de59e0Spgoyette
9249e6ac23fSkre		IFS=' '
9259e6ac23fSkre		for frag in ${xline}
9269e6ac23fSkre		do
927f1de59e0Spgoyette			case "${frag}" in
928f1de59e0Spgoyette			MACHINE=${machglob})
929f1de59e0Spgoyette				line_matches_machine=true ;;
930f1de59e0Spgoyette			ALIAS=${machglob})
931f1de59e0Spgoyette				line_matches_machine=true ;;
932f1de59e0Spgoyette			MACHINE_ARCH=${archglob})
933f1de59e0Spgoyette				line_matches_arch=true ;;
934f1de59e0Spgoyette			esac
935f1de59e0Spgoyette		done
936f1de59e0Spgoyette
9379e6ac23fSkre		if $line_matches_machine && $line_matches_arch
9389e6ac23fSkre		then
939f1de59e0Spgoyette			found=true
940f1de59e0Spgoyette			echo "$line"
941f1de59e0Spgoyette		fi
942f1de59e0Spgoyette	done
9439e6ac23fSkre	if ! $found
9449e6ac23fSkre	then
945f1de59e0Spgoyette		echo >&2 "No match for" \
946f1de59e0Spgoyette		    "MACHINE=${machglob} MACHINE_ARCH=${archglob}"
947f1de59e0Spgoyette		return 1
948f1de59e0Spgoyette	fi
949f1de59e0Spgoyette	return 0
950f1de59e0Spgoyette}
951f1de59e0Spgoyette
952f1de59e0Spgoyette# nobomb_getmakevar --
9539d6c4a26Slukem# Given the name of a make variable in $1, show make's idea of the
954f1de59e0Spgoyette# value of that variable, or return 1 if there's an error.
955f1de59e0Spgoyette#
956f1de59e0Spgoyettenobomb_getmakevar()
957f1de59e0Spgoyette{
958f1de59e0Spgoyette	[ -x "${make}" ] || return 1
959f1de59e0Spgoyette	"${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
960f1de59e0Spgoyette_x_:
961f1de59e0Spgoyette	echo \${$1}
962f1de59e0Spgoyette.include <bsd.prog.mk>
963f1de59e0Spgoyette.include <bsd.kernobj.mk>
964f1de59e0SpgoyetteEOF
965f1de59e0Spgoyette}
966f1de59e0Spgoyette
967f1de59e0Spgoyette# bomb_getmakevar --
9689d6c4a26Slukem# Given the name of a make variable in $1, show make's idea of the
969f1de59e0Spgoyette# value of that variable, or bomb if there's an error.
970f1de59e0Spgoyette#
971f1de59e0Spgoyettebomb_getmakevar()
972f1de59e0Spgoyette{
973f1de59e0Spgoyette	[ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
974f1de59e0Spgoyette	nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
975f1de59e0Spgoyette}
976f1de59e0Spgoyette
977f1de59e0Spgoyette# getmakevar --
9789d6c4a26Slukem# Given the name of a make variable in $1, show make's idea of the
9799d6c4a26Slukem# value of that variable, or show a literal '$' followed by the
980f1de59e0Spgoyette# variable name if ${make} is not executable.  This is intended for use in
981f1de59e0Spgoyette# messages that need to be readable even if $make hasn't been built,
982f1de59e0Spgoyette# such as when build.sh is run with the "-n" option.
983f1de59e0Spgoyette#
984f1de59e0Spgoyettegetmakevar()
985f1de59e0Spgoyette{
9869e6ac23fSkre	if [ -x "${make}" ]
9879e6ac23fSkre	then
988f1de59e0Spgoyette		bomb_getmakevar "$1"
989f1de59e0Spgoyette	else
990f1de59e0Spgoyette		echo "\$$1"
991f1de59e0Spgoyette	fi
992f1de59e0Spgoyette}
993f1de59e0Spgoyette
994f1de59e0Spgoyettesetmakeenv()
995f1de59e0Spgoyette{
996f1de59e0Spgoyette	eval "$1='$2'; export $1"
997f1de59e0Spgoyette	makeenv="${makeenv} $1"
998f1de59e0Spgoyette}
999546bd46bSlukem
1000f1de59e0Spgoyettesafe_setmakeenv()
1001f1de59e0Spgoyette{
1002f1de59e0Spgoyette	case "$1" in
1003f1de59e0Spgoyette
1004f1de59e0Spgoyette	#	Look for any vars we want to prohibit here, like:
1005f1de59e0Spgoyette	# Bad | Dangerous)	usage "Cannot override $1 with -V";;
1006f1de59e0Spgoyette
1007f1de59e0Spgoyette	# That first char is OK has already been verified.
1008f1de59e0Spgoyette	*[!A-Za-z0-9_]*)	usage "Bad variable name (-V): '$1'";;
1009f1de59e0Spgoyette	esac
1010f1de59e0Spgoyette	setmakeenv "$@"
1011f1de59e0Spgoyette}
1012f1de59e0Spgoyette
1013f1de59e0Spgoyetteunsetmakeenv()
1014f1de59e0Spgoyette{
1015f1de59e0Spgoyette	eval "unset $1"
1016f1de59e0Spgoyette	makeenv="${makeenv} $1"
1017f1de59e0Spgoyette}
1018546bd46bSlukem
1019f1de59e0Spgoyettesafe_unsetmakeenv()
1020f1de59e0Spgoyette{
1021f1de59e0Spgoyette	case "$1" in
1022f1de59e0Spgoyette
1023f1de59e0Spgoyette	#	Look for any vars user should not be able to unset
1024f1de59e0Spgoyette	# Needed | Must_Have)	usage "Variable $1 cannot be unset";;
1025f1de59e0Spgoyette
1026f1de59e0Spgoyette	[!A-Za-z_]* | *[!A-Za-z0-9_]*)	usage "Bad variable name (-Z): '$1'";;
1027f1de59e0Spgoyette	esac
1028f1de59e0Spgoyette	unsetmakeenv "$1"
1029f1de59e0Spgoyette}
1030f1de59e0Spgoyette
103159040e53Sriastradh# Clear all variables defined in makeenv.  Used to run a subprocess
103259040e53Sriastradh# outside the usual NetBSD build's make environment.
103359040e53Sriastradh#
103459040e53Sriastradhclearmakeenv()
103559040e53Sriastradh{
103659040e53Sriastradh	local var
103759040e53Sriastradh
10389e6ac23fSkre	for var in ${makeenv}
10399e6ac23fSkre	do
104059040e53Sriastradh		unset ${var}
104159040e53Sriastradh	done
104259040e53Sriastradh}
104359040e53Sriastradh
1044f1de59e0Spgoyette# Given a variable name in $1, modify the variable in place as follows:
1045f1de59e0Spgoyette# For each space-separated word in the variable, call resolvepath.
1046546bd46bSlukem#
1047f1de59e0Spgoyetteresolvepaths()
1048f1de59e0Spgoyette{
1049e6351014Skre	local var="$1"
1050f1de59e0Spgoyette	local val
1051f1de59e0Spgoyette	eval val=\"\${${var}}\"
10529e6ac23fSkre	local newval=
1053f1de59e0Spgoyette	local word
1054e6351014Skre
10559e6ac23fSkre	for word in ${val}
10569e6ac23fSkre	do
1057f1de59e0Spgoyette		resolvepath word
1058f1de59e0Spgoyette		newval="${newval}${newval:+ }${word}"
1059f1de59e0Spgoyette	done
1060f1de59e0Spgoyette	eval ${var}=\"\${newval}\"
1061f1de59e0Spgoyette}
1062f1de59e0Spgoyette
1063f1de59e0Spgoyette# Given a variable name in $1, modify the variable in place as follows:
1064f1de59e0Spgoyette# Convert possibly-relative path to absolute path by prepending
1065f1de59e0Spgoyette# ${TOP} if necessary.  Also delete trailing "/", if any.
1066546bd46bSlukem#
1067f1de59e0Spgoyetteresolvepath()
1068f1de59e0Spgoyette{
1069e6351014Skre	local var="$1"
1070f1de59e0Spgoyette	local val
1071f1de59e0Spgoyette	eval val=\"\${${var}}\"
1072f1de59e0Spgoyette	case "${val}" in
1073f1de59e0Spgoyette	/)
1074f1de59e0Spgoyette		;;
1075f1de59e0Spgoyette	/*)
1076f1de59e0Spgoyette		val="${val%/}"
1077f1de59e0Spgoyette		;;
1078f1de59e0Spgoyette	*)
1079f1de59e0Spgoyette		val="${TOP}/${val%/}"
1080f1de59e0Spgoyette		;;
1081f1de59e0Spgoyette	esac
1082f1de59e0Spgoyette	eval ${var}=\"\${val}\"
1083f1de59e0Spgoyette}
1084f1de59e0Spgoyette
10859d6c4a26Slukem# Show synopsis to stdout.
10869d6c4a26Slukem#
1087262db514Slukemsynopsis()
1088f1de59e0Spgoyette{
1089f1de59e0Spgoyette	cat <<_usage_
1090f1de59e0Spgoyette
109157544d3eSlukemUsage: ${progname} [-EnoPRrUux] [-a ARCH] [-B BID] [-C EXTRAS]
109257544d3eSlukem                [-c COMPILER] [-D DEST] [-j NJOB] [-M MOBJ] [-m MACH]
109357544d3eSlukem                [-N NOISY] [-O OOBJ] [-R RELEASE] [-S SEED] [-T TOOLS]
109457544d3eSlukem                [-V VAR=[VALUE]] [-w WRAPPER] [-X X11SRC]
109557544d3eSlukem                [-Z VAR]
109657544d3eSlukem                OPERATION ...
1097262db514Slukem       ${progname} ( -h | -? )
1098f1de59e0Spgoyette
1099262db514Slukem_usage_
1100262db514Slukem}
1101262db514Slukem
11029d6c4a26Slukem# Show help to stdout.
1103262db514Slukem#
1104262db514Slukemhelp()
1105262db514Slukem{
1106262db514Slukem	synopsis
1107262db514Slukem	cat <<_usage_
110857544d3eSlukem Build OPERATIONs (all imply "obj" and "tools"):
1109f1de59e0Spgoyette    build               Run "make build".
1110f1de59e0Spgoyette    distribution        Run "make distribution" (includes DESTDIR/etc/ files).
1111f1de59e0Spgoyette    release             Run "make release" (includes kernels & distrib media).
1112f1de59e0Spgoyette
111357544d3eSlukem Other OPERATIONs:
11149d6c4a26Slukem    help                Show this help message, and exit.
11159e6ac23fSkre    makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper
11169e6ac23fSkre                        and ${toolprefix}make.
1117f1de59e0Spgoyette                        Always performed.
1118f1de59e0Spgoyette    cleandir            Run "make cleandir".  [Default unless -u is used]
1119043d88a7Sjmcneill    dtb                 Build devicetree blobs.
1120f1de59e0Spgoyette    obj                 Run "make obj".  [Default unless -o is used]
1121f1de59e0Spgoyette    tools               Build and install tools.
112257544d3eSlukem    install=IDIR        Run "make installworld" to IDIR to install all sets
112357544d3eSlukem                        except 'etc'.  Useful after "distribution" or "release".
112457544d3eSlukem    kernel=CONF         Build kernel with config file CONF.
112557544d3eSlukem    kernel.gdb=CONF     Build kernel (including netbsd.gdb) with config
112657544d3eSlukem                        file CONF.
112757544d3eSlukem    releasekernel=CONF  Install kernel built by kernel=CONF to RELEASEDIR.
112857544d3eSlukem    kernels             Build all kernels.
112957544d3eSlukem    installmodules=IDIR Run "make installmodules" to IDIR to install all
1130f1de59e0Spgoyette                        kernel modules.
1131f1de59e0Spgoyette    modules             Build kernel modules.
1132f1de59e0Spgoyette    rumptest            Do a linktest for rump (for developers).
1133f1de59e0Spgoyette    sets                Create binary sets in
1134f1de59e0Spgoyette                        RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
1135f1de59e0Spgoyette                        DESTDIR should be populated beforehand.
11368866db62Smrg    distsets            Same as "distribution sets".
1137f1de59e0Spgoyette    sourcesets          Create source sets in RELEASEDIR/source/sets.
1138f1de59e0Spgoyette    syspkgs             Create syspkgs in
1139f1de59e0Spgoyette                        RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
114059040e53Sriastradh    pkg=CATEGORY/PKG    (EXPERIMENT) Build a package CATEGORY/PKG from pkgsrc.
1141f1de59e0Spgoyette    iso-image           Create CD-ROM image in RELEASEDIR/images.
1142f1de59e0Spgoyette    iso-image-source    Create CD-ROM image with source in RELEASEDIR/images.
1143f1de59e0Spgoyette    live-image          Create bootable live image in
1144f1de59e0Spgoyette                        RELEASEDIR/RELEASEMACHINEDIR/installation/liveimage.
1145f1de59e0Spgoyette    install-image       Create bootable installation image in
1146f1de59e0Spgoyette                        RELEASEDIR/RELEASEMACHINEDIR/installation/installimage.
114757544d3eSlukem    disk-image=TARGET   Create bootable disk image in
114857544d3eSlukem                        RELEASEDIR/RELEASEMACHINEDIR/binary/gzimg/TARGET.img.gz.
1149439f74efSlukem    params              Create params file with various make(1) parameters.
1150439f74efSlukem    show-params         Show various make(1) parameters.
11519d6c4a26Slukem    list-arch           Show a list of valid MACHINE/MACHINE_ARCH values,
1152f1de59e0Spgoyette                        and exit.  The list may be narrowed by passing glob
1153f1de59e0Spgoyette                        patterns or exact values in MACHINE or MACHINE_ARCH.
115439cc76d6Srillig    mkrepro-timestamp   Show the latest source timestamp used for reproducible
115505937936Smartin                        builds and exit.  Requires -P or -V MKREPRO=yes.
11569b50f3d1Smartin    show-revisionid	Show the revision ID of the current directory
1157*16dd98ceSgutteridge			(in SCM-dependent format) and exit.
11589b50f3d1Smartin			Requires -P or -V MKREPRO=yes.
1159f1de59e0Spgoyette
1160f1de59e0Spgoyette Options:
116157544d3eSlukem    -a ARCH        Set MACHINE_ARCH=ARCH.  [Default: deduced from MACHINE]
116257544d3eSlukem    -B BID         Set BUILDID=BID.
116357544d3eSlukem    -C EXTRAS      Append EXTRAS to CDEXTRA for inclusion on CD-ROM.
116457544d3eSlukem    -c COMPILER    Select compiler from COMPILER:
11658039b48bSriastradh                       clang
11668039b48bSriastradh                       gcc
11678039b48bSriastradh                   [Default: gcc]
116857544d3eSlukem    -D DEST        Set DESTDIR=DEST.  [Default: destdir.\${MACHINE}]
1169f1de59e0Spgoyette    -E             Set "expert" mode; disables various safety checks.
11708866db62Smrg                   Should not be used without expert knowledge of the build
11718866db62Smrg                   system.
11729d6c4a26Slukem    -h             Show this help message, and exit.
117357544d3eSlukem    -j NJOB        Run up to NJOB jobs in parallel; see make(1) -j.
117457544d3eSlukem    -M MOBJ        Set obj root directory to MOBJ; sets MAKEOBJDIRPREFIX=MOBJ,
117557544d3eSlukem                   unsets MAKEOBJDIR.
117657544d3eSlukem    -m MACH        Set MACHINE=MACH.  Some MACH values are actually
1177f1de59e0Spgoyette                   aliases that set MACHINE/MACHINE_ARCH pairs.
1178f1de59e0Spgoyette                   [Default: deduced from the host system if the host
1179f1de59e0Spgoyette                   OS is NetBSD]
118039cc76d6Srillig    -N NOISY       Set the noisiness (MAKEVERBOSE) level of the build to NOISY:
118157544d3eSlukem                       0   Minimal output ("quiet").
118257544d3eSlukem                       1   Describe what is occurring.
11838866db62Smrg                       2   Describe what is occurring and echo the actual
118457544d3eSlukem                           command.
118557544d3eSlukem                       3   Ignore the effect of the "@" prefix in make
118657544d3eSlukem                           commands.
118757544d3eSlukem                       4   Trace shell commands using the shell's -x flag.
1188f1de59e0Spgoyette                   [Default: 2]
118957544d3eSlukem    -n             Show commands that would be executed, but do not execute
119057544d3eSlukem                   them.
119157544d3eSlukem    -O OOBJ        Set obj root directory to OOBJ; sets a MAKEOBJDIR pattern
119257544d3eSlukem                   using OOBJ, unsets MAKEOBJDIRPREFIX.
1193f1de59e0Spgoyette    -o             Set MKOBJDIRS=no; do not create objdirs at start of build.
1194f1de59e0Spgoyette    -P             Set MKREPRO and MKREPRO_TIMESTAMP to the latest source
1195f1de59e0Spgoyette                   CVS timestamp for reproducible builds.
119657544d3eSlukem    -R RELEASE     Set RELEASEDIR=RELEASE.  [Default: releasedir]
1197f1de59e0Spgoyette    -r             Remove contents of TOOLDIR and DESTDIR before building.
119857544d3eSlukem    -S SEED        Set BUILDSEED=SEED.  [Default: NetBSD-majorversion]
119957544d3eSlukem    -T TOOLS       Set TOOLDIR=TOOLS.  If unset, and TOOLDIR is not set
120057544d3eSlukem                   in the environment, ${toolprefix}make will be (re)built
1201f1de59e0Spgoyette                   unconditionally.
1202f1de59e0Spgoyette    -U             Set MKUNPRIVED=yes; build without requiring root privileges,
120357544d3eSlukem                   install from an unprivileged build with proper file
120457544d3eSlukem                   permissions.
1205f1de59e0Spgoyette    -u             Set MKUPDATE=yes; do not run "make cleandir" first.
1206f1de59e0Spgoyette                   Without this, everything is rebuilt, including the tools.
120757544d3eSlukem    -V VAR=[VALUE] Set variable VAR=VALUE.
120857544d3eSlukem    -w WRAPPER     Create ${toolprefix}make script as WRAPPER.
1209f1de59e0Spgoyette                   [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
121057544d3eSlukem    -X X11SRC      Set X11SRCDIR=X11SRC.  [Default: /usr/xsrc]
121157544d3eSlukem    -x             Set MKX11=yes; build X11 from X11SRCDIR.
121257544d3eSlukem    -Z VAR         Unset ("zap") variable VAR.
12139d6c4a26Slukem    -?             Show this help message, and exit.
1214f1de59e0Spgoyette
1215f1de59e0Spgoyette_usage_
1216546bd46bSlukem}
1217546bd46bSlukem
12189d6c4a26Slukem# Show optional error message, help to stderr, and exit 1.
1219546bd46bSlukem#
1220546bd46bSlukemusage()
1221546bd46bSlukem{
12229e6ac23fSkre	if [ -n "$*" ]
12239e6ac23fSkre	then
1224262db514Slukem		echo 1>&2 ""
1225262db514Slukem		echo 1>&2 "${progname}: $*"
1226546bd46bSlukem	fi
1227262db514Slukem	synopsis 1>&2
1228f1de59e0Spgoyette	exit 1
1229f1de59e0Spgoyette}
1230f1de59e0Spgoyette
1231f1de59e0Spgoyetteparseoptions()
1232f1de59e0Spgoyette{
12339e6ac23fSkre	opts=a:B:C:c:D:Ehj:M:m:N:nO:oPR:rS:T:UuV:w:X:xZ:
1234f1de59e0Spgoyette	opt_a=false
1235f1de59e0Spgoyette	opt_m=false
12369b50f3d1Smartin	local did_show_info=false
1237f1de59e0Spgoyette
12389e6ac23fSkre	if type getopts >/dev/null 2>&1
12399e6ac23fSkre	then
1240f1de59e0Spgoyette		# Use POSIX getopts.
1241f1de59e0Spgoyette		#
1242262db514Slukem		getoptcmd='getopts :${opts} opt && opt=-${opt}'
1243f1de59e0Spgoyette		optargcmd=':'
1244f1de59e0Spgoyette		optremcmd='shift $((${OPTIND} -1))'
1245f1de59e0Spgoyette	else
1246f1de59e0Spgoyette		type getopt >/dev/null 2>&1 ||
1247f1de59e0Spgoyette		    bomb "Shell does not support getopts or getopt"
1248f1de59e0Spgoyette
1249f1de59e0Spgoyette		# Use old-style getopt(1) (doesn't handle whitespace in args).
1250f1de59e0Spgoyette		#
1251f1de59e0Spgoyette		args="$(getopt ${opts} $*)"
1252f1de59e0Spgoyette		[ $? = 0 ] || usage
1253f1de59e0Spgoyette		set -- ${args}
1254f1de59e0Spgoyette
1255f1de59e0Spgoyette		getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
1256f1de59e0Spgoyette		optargcmd='OPTARG="$1"; shift'
1257f1de59e0Spgoyette		optremcmd=':'
1258f1de59e0Spgoyette	fi
1259f1de59e0Spgoyette
1260f1de59e0Spgoyette	# Parse command line options.
1261f1de59e0Spgoyette	#
12629e6ac23fSkre	while eval ${getoptcmd}
12639e6ac23fSkre	do
1264f1de59e0Spgoyette		case ${opt} in
1265f1de59e0Spgoyette
1266f1de59e0Spgoyette		-a)
1267f1de59e0Spgoyette			eval ${optargcmd}
1268f1de59e0Spgoyette			MACHINE_ARCH=${OPTARG}
1269f1de59e0Spgoyette			opt_a=true
1270f1de59e0Spgoyette			;;
1271f1de59e0Spgoyette
1272f1de59e0Spgoyette		-B)
1273f1de59e0Spgoyette			eval ${optargcmd}
1274f1de59e0Spgoyette			BUILDID=${OPTARG}
1275f1de59e0Spgoyette			;;
1276f1de59e0Spgoyette
1277f1de59e0Spgoyette		-C)
12789e6ac23fSkre			eval ${optargcmd}
12799e6ac23fSkre			resolvepaths OPTARG
1280f1de59e0Spgoyette			CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
1281f1de59e0Spgoyette			;;
1282f1de59e0Spgoyette
12838039b48bSriastradh		-c)
12848039b48bSriastradh			eval ${optargcmd}
12858039b48bSriastradh			case "${OPTARG}" in
12868039b48bSriastradh			gcc)	# default, no variables needed
12878039b48bSriastradh				;;
12888039b48bSriastradh			clang)	setmakeenv HAVE_LLVM yes
12898039b48bSriastradh				setmakeenv MKLLVM yes
12908039b48bSriastradh				setmakeenv MKGCC no
12918039b48bSriastradh				;;
12928039b48bSriastradh			#pcc)	...
12938039b48bSriastradh			#	;;
12948039b48bSriastradh			*)	bomb "Unknown compiler: ${OPTARG}"
12958039b48bSriastradh			esac
12968039b48bSriastradh			;;
12978039b48bSriastradh
1298f1de59e0Spgoyette		-D)
12999e6ac23fSkre			eval ${optargcmd}
13009e6ac23fSkre			resolvepath OPTARG
1301f1de59e0Spgoyette			setmakeenv DESTDIR "${OPTARG}"
1302f1de59e0Spgoyette			;;
1303f1de59e0Spgoyette
1304f1de59e0Spgoyette		-E)
1305f1de59e0Spgoyette			do_expertmode=true
1306f1de59e0Spgoyette			;;
1307f1de59e0Spgoyette
1308f1de59e0Spgoyette		-j)
1309f1de59e0Spgoyette			eval ${optargcmd}
1310f1de59e0Spgoyette			parallel="-j ${OPTARG}"
1311f1de59e0Spgoyette			;;
1312f1de59e0Spgoyette
1313f1de59e0Spgoyette		-M)
13149e6ac23fSkre			eval ${optargcmd}
13159e6ac23fSkre			resolvepath OPTARG
1316f1de59e0Spgoyette			case "${OPTARG}" in
1317f1de59e0Spgoyette			\$*)	usage "-M argument must not begin with '\$'"
1318f1de59e0Spgoyette				;;
1319f1de59e0Spgoyette			*\$*)	# can use resolvepath, but can't set TOP_objdir
1320f1de59e0Spgoyette				resolvepath OPTARG
1321f1de59e0Spgoyette				;;
1322f1de59e0Spgoyette			*)	resolvepath OPTARG
1323f1de59e0Spgoyette				TOP_objdir="${OPTARG}${TOP}"
1324f1de59e0Spgoyette				;;
1325f1de59e0Spgoyette			esac
1326f1de59e0Spgoyette			unsetmakeenv MAKEOBJDIR
1327f1de59e0Spgoyette			setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
1328f1de59e0Spgoyette			;;
1329f1de59e0Spgoyette
1330f1de59e0Spgoyette			# -m overrides MACHINE_ARCH unless "-a" is specified
1331f1de59e0Spgoyette		-m)
1332f1de59e0Spgoyette			eval ${optargcmd}
1333f1de59e0Spgoyette			MACHINE="${OPTARG}"
1334f1de59e0Spgoyette			opt_m=true
1335f1de59e0Spgoyette			;;
1336f1de59e0Spgoyette
1337f1de59e0Spgoyette		-N)
1338f1de59e0Spgoyette			eval ${optargcmd}
1339f1de59e0Spgoyette			case "${OPTARG}" in
1340f1de59e0Spgoyette			0|1|2|3|4)
1341f1de59e0Spgoyette				setmakeenv MAKEVERBOSE "${OPTARG}"
1342f1de59e0Spgoyette				;;
1343f1de59e0Spgoyette			*)
1344f1de59e0Spgoyette				usage "'${OPTARG}' is not a valid value for -N"
1345f1de59e0Spgoyette				;;
1346f1de59e0Spgoyette			esac
1347f1de59e0Spgoyette			;;
1348f1de59e0Spgoyette
1349f1de59e0Spgoyette		-n)
1350f1de59e0Spgoyette			runcmd=echo
1351f1de59e0Spgoyette			;;
1352f1de59e0Spgoyette
1353f1de59e0Spgoyette		-O)
1354f1de59e0Spgoyette			eval ${optargcmd}
1355f1de59e0Spgoyette			case "${OPTARG}" in
1356f1de59e0Spgoyette			*\$*)	usage "-O argument must not contain '\$'"
1357f1de59e0Spgoyette				;;
1358f1de59e0Spgoyette			*)	resolvepath OPTARG
1359f1de59e0Spgoyette				TOP_objdir="${OPTARG}"
1360f1de59e0Spgoyette				;;
1361f1de59e0Spgoyette			esac
1362f1de59e0Spgoyette			unsetmakeenv MAKEOBJDIRPREFIX
1363f1de59e0Spgoyette			setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
1364f1de59e0Spgoyette			;;
1365f1de59e0Spgoyette
1366f1de59e0Spgoyette		-o)
1367f1de59e0Spgoyette			MKOBJDIRS=no
1368f1de59e0Spgoyette			;;
1369f1de59e0Spgoyette
1370f1de59e0Spgoyette		-P)
1371f1de59e0Spgoyette			MKREPRO=yes
1372f1de59e0Spgoyette			;;
1373f1de59e0Spgoyette
1374f1de59e0Spgoyette		-R)
13759e6ac23fSkre			eval ${optargcmd}
13769e6ac23fSkre			resolvepath OPTARG
1377f1de59e0Spgoyette			setmakeenv RELEASEDIR "${OPTARG}"
1378f1de59e0Spgoyette			;;
1379f1de59e0Spgoyette
1380f1de59e0Spgoyette		-r)
1381f1de59e0Spgoyette			do_removedirs=true
1382f1de59e0Spgoyette			do_rebuildmake=true
1383f1de59e0Spgoyette			;;
1384f1de59e0Spgoyette
1385f1de59e0Spgoyette		-S)
1386f1de59e0Spgoyette			eval ${optargcmd}
1387f1de59e0Spgoyette			setmakeenv BUILDSEED "${OPTARG}"
1388f1de59e0Spgoyette			;;
1389f1de59e0Spgoyette
1390f1de59e0Spgoyette		-T)
13919e6ac23fSkre			eval ${optargcmd}
13929e6ac23fSkre			resolvepath OPTARG
1393f1de59e0Spgoyette			TOOLDIR="${OPTARG}"
1394f1de59e0Spgoyette			export TOOLDIR
1395f1de59e0Spgoyette			;;
1396f1de59e0Spgoyette
1397f1de59e0Spgoyette		-U)
1398f1de59e0Spgoyette			setmakeenv MKUNPRIVED yes
1399f1de59e0Spgoyette			;;
1400f1de59e0Spgoyette
1401f1de59e0Spgoyette		-u)
1402f1de59e0Spgoyette			setmakeenv MKUPDATE yes
1403f1de59e0Spgoyette			;;
1404f1de59e0Spgoyette
1405f1de59e0Spgoyette		-V)
1406f1de59e0Spgoyette			eval ${optargcmd}
1407f1de59e0Spgoyette			case "${OPTARG}" in
1408f1de59e0Spgoyette		    # XXX: consider restricting which variables can be changed?
1409f1de59e0Spgoyette			[a-zA-Z_]*=*)
1410f1de59e0Spgoyette				safe_setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
1411f1de59e0Spgoyette				;;
1412f1de59e0Spgoyette			[a-zA-Z_]*)
1413f1de59e0Spgoyette				safe_setmakeenv "${OPTARG}" ""
1414f1de59e0Spgoyette				;;
1415f1de59e0Spgoyette			*)
141657544d3eSlukem				usage "-V argument must be of the form 'VAR[=VALUE]'"
1417f1de59e0Spgoyette				;;
1418f1de59e0Spgoyette			esac
1419f1de59e0Spgoyette			;;
1420f1de59e0Spgoyette
1421f1de59e0Spgoyette		-w)
14229e6ac23fSkre			eval ${optargcmd}
14239e6ac23fSkre			resolvepath OPTARG
1424f1de59e0Spgoyette			makewrapper="${OPTARG}"
1425f1de59e0Spgoyette			;;
1426f1de59e0Spgoyette
1427f1de59e0Spgoyette		-X)
14289e6ac23fSkre			eval ${optargcmd}
14299e6ac23fSkre			resolvepath OPTARG
1430f1de59e0Spgoyette			setmakeenv X11SRCDIR "${OPTARG}"
1431f1de59e0Spgoyette			;;
1432f1de59e0Spgoyette
1433f1de59e0Spgoyette		-x)
1434f1de59e0Spgoyette			setmakeenv MKX11 yes
1435f1de59e0Spgoyette			;;
1436f1de59e0Spgoyette
1437f1de59e0Spgoyette		-Z)
1438f1de59e0Spgoyette			eval ${optargcmd}
1439f1de59e0Spgoyette		    # XXX: consider restricting which variables can be unset?
1440f1de59e0Spgoyette			safe_unsetmakeenv "${OPTARG}"
1441f1de59e0Spgoyette			;;
1442f1de59e0Spgoyette
1443f1de59e0Spgoyette		--)
1444f1de59e0Spgoyette			break
1445f1de59e0Spgoyette			;;
1446f1de59e0Spgoyette
1447262db514Slukem		-h)
1448546bd46bSlukem			help
1449546bd46bSlukem			exit 0
1450f1de59e0Spgoyette			;;
1451f1de59e0Spgoyette
1452262db514Slukem		'-?')
14539e6ac23fSkre			if [ "${OPTARG}" = '?' ]
14549e6ac23fSkre			then
1455262db514Slukem				help
1456262db514Slukem				exit 0
1457262db514Slukem			fi
1458262db514Slukem			usage "Unknown option -${OPTARG}"
1459262db514Slukem			;;
1460262db514Slukem
1461262db514Slukem		-:)
1462262db514Slukem			usage "Missing argument for option -${OPTARG}"
1463262db514Slukem			;;
1464262db514Slukem
1465262db514Slukem		*)
1466262db514Slukem			usage "Unimplemented option ${opt}"
1467262db514Slukem			;;
1468262db514Slukem
1469f1de59e0Spgoyette		esac
1470f1de59e0Spgoyette	done
1471f1de59e0Spgoyette
1472f1de59e0Spgoyette	# Validate operations.
1473f1de59e0Spgoyette	#
1474f1de59e0Spgoyette	eval ${optremcmd}
14759e6ac23fSkre	while [ $# -gt 0 ]
14769e6ac23fSkre	do
1477f1de59e0Spgoyette		op=$1; shift
1478f1de59e0Spgoyette		operations="${operations} ${op}"
1479f1de59e0Spgoyette
1480f1de59e0Spgoyette		case "${op}" in
1481f1de59e0Spgoyette
1482f1de59e0Spgoyette		help)
1483546bd46bSlukem			help
1484546bd46bSlukem			exit 0
1485f1de59e0Spgoyette			;;
1486f1de59e0Spgoyette
1487f1de59e0Spgoyette		list-arch)
1488f1de59e0Spgoyette			listarch "${MACHINE}" "${MACHINE_ARCH}"
148905937936Smartin			exit
149005937936Smartin			;;
149105937936Smartin		mkrepro-timestamp)
149205937936Smartin			setup_mkrepro quiet
14939e6ac23fSkre			echo "${MKREPRO_TIMESTAMP:-0}"
14949b50f3d1Smartin			did_show_info=true
14959b50f3d1Smartin			;;
14969b50f3d1Smartin
14979b50f3d1Smartin		show-revisionid)
14989b50f3d1Smartin			setup_mkrepro quiet
14999e6ac23fSkre			echo "${NETBSD_REVISIONID}"
15009b50f3d1Smartin			did_show_info=true
1501f1de59e0Spgoyette			;;
1502f1de59e0Spgoyette
1503f1de59e0Spgoyette		kernel=*|releasekernel=*|kernel.gdb=*)
1504f1de59e0Spgoyette			arg=${op#*=}
1505f1de59e0Spgoyette			op=${op%%=*}
1506f1de59e0Spgoyette			[ -n "${arg}" ] ||
1507262db514Slukem			    bomb "Must supply a kernel name with '${op}=...'"
1508f1de59e0Spgoyette			;;
1509f1de59e0Spgoyette
1510f1de59e0Spgoyette		disk-image=*)
1511f1de59e0Spgoyette			arg=${op#*=}
1512f1de59e0Spgoyette			op=disk_image
1513f1de59e0Spgoyette			[ -n "${arg}" ] ||
1514262db514Slukem			    bomb "Must supply a target name with '${op}=...'"
1515f1de59e0Spgoyette
1516f1de59e0Spgoyette			;;
1517f1de59e0Spgoyette
151859040e53Sriastradh		pkg=*)
151959040e53Sriastradh			arg=${op#*=}
152059040e53Sriastradh			op=${op%%=*}
152159040e53Sriastradh			[ -n "${arg}" ] ||
152259040e53Sriastradh			    bomb "Must supply category/package with 'pkg=...'"
152359040e53Sriastradh			;;
152459040e53Sriastradh
1525f1de59e0Spgoyette		install=*|installmodules=*)
1526f1de59e0Spgoyette			arg=${op#*=}
1527f1de59e0Spgoyette			op=${op%%=*}
1528f1de59e0Spgoyette			[ -n "${arg}" ] ||
1529262db514Slukem			    bomb "Must supply a directory with 'install=...'"
1530f1de59e0Spgoyette			;;
1531f1de59e0Spgoyette
15328866db62Smrg		distsets)
15338866db62Smrg			operations="$(echo "$operations" | sed 's/distsets/distribution sets/')"
15348866db62Smrg			do_sets=true
15358866db62Smrg			op=distribution
15368866db62Smrg			;;
15378866db62Smrg
1538f1de59e0Spgoyette		build|\
1539f1de59e0Spgoyette		cleandir|\
1540f1de59e0Spgoyette		distribution|\
1541043d88a7Sjmcneill		dtb|\
1542f1de59e0Spgoyette		install-image|\
1543f1de59e0Spgoyette		iso-image-source|\
1544f1de59e0Spgoyette		iso-image|\
1545f1de59e0Spgoyette		kernels|\
15460a37cd5eSchristos		libs|\
1547f1de59e0Spgoyette		live-image|\
1548f1de59e0Spgoyette		makewrapper|\
1549f1de59e0Spgoyette		modules|\
1550f1de59e0Spgoyette		obj|\
1551f1de59e0Spgoyette		params|\
1552f1de59e0Spgoyette		release|\
1553f1de59e0Spgoyette		rump|\
1554f1de59e0Spgoyette		rumptest|\
1555f1de59e0Spgoyette		sets|\
1556439f74efSlukem		show-params|\
1557f1de59e0Spgoyette		sourcesets|\
1558f1de59e0Spgoyette		syspkgs|\
1559f1de59e0Spgoyette		tools)
1560f1de59e0Spgoyette			;;
1561f1de59e0Spgoyette
1562f1de59e0Spgoyette		*)
156357544d3eSlukem			usage "Unknown OPERATION '${op}'"
1564f1de59e0Spgoyette			;;
1565f1de59e0Spgoyette
1566f1de59e0Spgoyette		esac
1567f1de59e0Spgoyette		# ${op} may contain chars that are not allowed in variable
1568f1de59e0Spgoyette		# names.  Replace them with '_' before setting do_${op}.
1569f1de59e0Spgoyette		op="$( echo "$op" | tr -s '.-' '__')"
1570f1de59e0Spgoyette		eval do_${op}=true
1571f1de59e0Spgoyette	done
15729b50f3d1Smartin
15739e6ac23fSkre	"$did_show_info" && [ "${MKREPRO_TIMESTAMP:-0}" -ne 0 ] && exit
15749b50f3d1Smartin
157557544d3eSlukem	[ -n "${operations}" ] || usage "Missing OPERATION to perform"
1576f1de59e0Spgoyette
1577f1de59e0Spgoyette	# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
1578f1de59e0Spgoyette	#
15799e6ac23fSkre	if [ -z "${MACHINE}" ]
15809e6ac23fSkre	then
15819e6ac23fSkre		[ "${uname_s}" = NetBSD ] || {
15829e6ac23fSkre		    bomb "MACHINE must be set, or -m must be used," \
15839e6ac23fSkre		         "for cross builds"
15849e6ac23fSkre		}
1585f1de59e0Spgoyette		MACHINE=${uname_m}
15865426352fSmrg		MACHINE_ARCH=${uname_p}
1587f1de59e0Spgoyette	fi
15889e6ac23fSkre	if $opt_m && ! $opt_a
15899e6ac23fSkre	then
1590f1de59e0Spgoyette		# Settings implied by the command line -m option
1591f1de59e0Spgoyette		# override MACHINE_ARCH from the environment (if any).
1592f1de59e0Spgoyette		getarch
1593f1de59e0Spgoyette	fi
1594f1de59e0Spgoyette	[ -n "${MACHINE_ARCH}" ] || getarch
1595f1de59e0Spgoyette	validatearch
1596f1de59e0Spgoyette
1597f1de59e0Spgoyette	# Set up default make(1) environment.
1598f1de59e0Spgoyette	#
1599f1de59e0Spgoyette	makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
1600f1de59e0Spgoyette	[ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
1601f1de59e0Spgoyette	[ -z "${BUILDINFO}" ] || makeenv="${makeenv} BUILDINFO"
1602f1de59e0Spgoyette	MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS}"
1603f1de59e0Spgoyette	MAKEFLAGS="${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
1604f1de59e0Spgoyette	export MAKEFLAGS MACHINE MACHINE_ARCH
16059e6ac23fSkre	setmakeenv USETOOLS yes
1606f1de59e0Spgoyette	setmakeenv MAKEWRAPPERMACHINE "${makewrappermachine:-${MACHINE}}"
16079fe22064Smartin	setmakeenv MAKE_OBJDIR_CHECK_WRITABLE no
1608f1de59e0Spgoyette}
1609f1de59e0Spgoyette
1610f1de59e0Spgoyette# sanitycheck --
1611f1de59e0Spgoyette# Sanity check after parsing command line options, before rebuildmake.
1612f1de59e0Spgoyette#
1613f1de59e0Spgoyettesanitycheck()
1614f1de59e0Spgoyette{
1615f1de59e0Spgoyette	# Install as non-root is a bad idea.
1616f1de59e0Spgoyette	#
16179e6ac23fSkre	if ${do_install} && [ "$id_u" -ne 0 ]
16189e6ac23fSkre	then
16199e6ac23fSkre		if ${do_expertmode}
16209e6ac23fSkre		then
1621262db514Slukem		    warning "Will install as an unprivileged user"
1622f1de59e0Spgoyette		else
1623262db514Slukem		    bomb "-E must be set for install as an unprivileged user"
1624f1de59e0Spgoyette		fi
1625f1de59e0Spgoyette	fi
1626f1de59e0Spgoyette
1627f1de59e0Spgoyette	# If the PATH contains any non-absolute components (including,
1628f1de59e0Spgoyette	# but not limited to, "." or ""), then complain.  As an exception,
1629f1de59e0Spgoyette	# allow "" or "." as the last component of the PATH.  This is fatal
1630f1de59e0Spgoyette	# if expert mode is not in effect.
1631f1de59e0Spgoyette	#
1632f1de59e0Spgoyette	local path="${PATH}"
1633f1de59e0Spgoyette	path="${path%:}"	# delete trailing ":"
1634f1de59e0Spgoyette	path="${path%:.}"	# delete trailing ":."
1635f1de59e0Spgoyette	case ":${path}:/" in
1636116200aeSjmcneill	*:[!/~]*)
16379e6ac23fSkre		if ${do_expertmode}
16389e6ac23fSkre		then
1639f1de59e0Spgoyette			warning "PATH contains non-absolute components"
1640f1de59e0Spgoyette		else
1641f1de59e0Spgoyette			bomb "PATH environment variable must not" \
1642f1de59e0Spgoyette			     "contain non-absolute components"
1643f1de59e0Spgoyette		fi
1644f1de59e0Spgoyette		;;
1645f1de59e0Spgoyette	esac
1646f1de59e0Spgoyette
16479e6ac23fSkre	while [ "${MKX11-no}" = yes ]		# not really a loop
16489e6ac23fSkre	do
1649f1de59e0Spgoyette		test -n "${X11SRCDIR}" && {
1650f1f38d3fSriastradh		    test -d "${X11SRCDIR}/external" ||
1651f1de59e0Spgoyette			bomb "X11SRCDIR (${X11SRCDIR}) does not exist (with -x)"
1652f1de59e0Spgoyette		    break
1653f1de59e0Spgoyette		}
1654f1de59e0Spgoyette		for _xd in \
1655f1de59e0Spgoyette		    "${NETBSDSRCDIR%/*}/xsrc" \
1656f1de59e0Spgoyette		    "${NETBSDSRCDIR}/xsrc" \
1657f1de59e0Spgoyette		    /usr/xsrc
1658f1de59e0Spgoyette		do
1659c931e716Sriastradh		    test -f "${_xd}/Makefile" &&
1660f1de59e0Spgoyette			setmakeenv X11SRCDIR "${_xd}" &&
1661f1de59e0Spgoyette			    break 2
1662f1de59e0Spgoyette		done
1663f1de59e0Spgoyette		bomb "Asked to build X11 but no xsrc"
1664f1de59e0Spgoyette	done
166559040e53Sriastradh
16669e6ac23fSkre	while $do_pkg				# not really a loop
16679e6ac23fSkre	do
166859040e53Sriastradh		test -n "${PKGSRCDIR}" && {
166959040e53Sriastradh		    test -f "${PKGSRCDIR}/mk/bsd.pkg.mk" ||
167059040e53Sriastradh			bomb "PKGSRCDIR (${PKGSRCDIR}) does not exist"
167159040e53Sriastradh		    break
167259040e53Sriastradh		}
167359040e53Sriastradh		for _pd in \
167459040e53Sriastradh		    "${NETBSDSRCDIR%/*}/pkgsrc" \
167559040e53Sriastradh		    "${NETBSDSRCDIR}/pkgsrc" \
167659040e53Sriastradh		    /usr/pkgsrc
167759040e53Sriastradh		do
167859040e53Sriastradh		    test -f "${_pd}/mk/bsd.pkg.mk" &&
167959040e53Sriastradh			setmakeenv PKGSRCDIR "${_pd}" &&
168059040e53Sriastradh			    break 2
168159040e53Sriastradh		done
168259040e53Sriastradh		bomb "Asked to build package but no pkgsrc"
168359040e53Sriastradh	done
16849e6ac23fSkre	if $do_pkg && [ "${MKX11-no}" = yes ]
16859e6ac23fSkre	then
16869f34be14Sriastradh		# See comment below about X11_TYPE in pkgsrc mk.conf.
16879f34be14Sriastradh		# (Feel free to remove this, and set X11_TYPE to
16889f34be14Sriastradh		# native/modular according to MKX11=yes/no, if you want
16899f34be14Sriastradh		# to do the work to make X11_TYPE=native cross-builds
16909f34be14Sriastradh		# work.)
16919f34be14Sriastradh		bomb "Experimental \`build.sh pkg=...'" \
16929f34be14Sriastradh		     "does not support -x/MKX11=yes"
16939f34be14Sriastradh	fi
1694f1de59e0Spgoyette}
1695f1de59e0Spgoyette
16969d6c4a26Slukem# print_tooldir_program --
16979d6c4a26Slukem# Try to find and show a path to an existing
1698f1de59e0Spgoyette# ${TOOLDIR}/bin/${toolprefix}program
16999d6c4a26Slukem#
1700f1de59e0Spgoyetteprint_tooldir_program()
1701f1de59e0Spgoyette{
1702f1de59e0Spgoyette	local possible_TOP_OBJ
1703f1de59e0Spgoyette	local possible_TOOLDIR
1704f1de59e0Spgoyette	local possible_program
1705f1de59e0Spgoyette	local tooldir_program
1706e6351014Skre	local program="${1}"
1707f1de59e0Spgoyette
17089e6ac23fSkre	if [ -n "${TOOLDIR}" ]
17099e6ac23fSkre	then
1710f1de59e0Spgoyette		echo "${TOOLDIR}/bin/${toolprefix}${program}"
1711f1de59e0Spgoyette		return
1712f1de59e0Spgoyette	fi
1713f1de59e0Spgoyette
1714f1de59e0Spgoyette	# Set host_ostype to something like "NetBSD-4.5.6-i386".  This
1715f1de59e0Spgoyette	# is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
1716f1de59e0Spgoyette	#
1717e6351014Skre	local host_ostype="${uname_s}-$(
1718f1de59e0Spgoyette			echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1719f1de59e0Spgoyette		)-$(
1720f1de59e0Spgoyette			echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1721e6351014Skre		)"
1722f1de59e0Spgoyette
1723f1de59e0Spgoyette	# Look in a few potential locations for
1724f1de59e0Spgoyette	# ${possible_TOOLDIR}/bin/${toolprefix}${program}.
1725f1de59e0Spgoyette	# If we find it, then set possible_program.
1726f1de59e0Spgoyette	#
1727f1de59e0Spgoyette	# In the usual case (without interference from environment
1728f1de59e0Spgoyette	# variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
1729f1de59e0Spgoyette	# "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
1730f1de59e0Spgoyette	#
1731f1de59e0Spgoyette	# In practice it's difficult to figure out the correct value
1732f1de59e0Spgoyette	# for _SRC_TOP_OBJ_.  In the easiest case, when the -M or -O
1733f1de59e0Spgoyette	# options were passed to build.sh, then ${TOP_objdir} will be
1734f1de59e0Spgoyette	# the correct value.  We also try a few other possibilities, but
1735f1de59e0Spgoyette	# we do not replicate all the logic of <bsd.obj.mk>.
1736f1de59e0Spgoyette	#
1737f1de59e0Spgoyette	for possible_TOP_OBJ in \
1738f1de59e0Spgoyette		"${TOP_objdir}" \
1739f1de59e0Spgoyette		"${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
1740f1de59e0Spgoyette		"${TOP}" \
1741f1de59e0Spgoyette		"${TOP}/obj" \
1742f1de59e0Spgoyette		"${TOP}/obj.${MACHINE}"
1743f1de59e0Spgoyette	do
1744f1de59e0Spgoyette	    [ -n "${possible_TOP_OBJ}" ] || continue
1745f1de59e0Spgoyette	    possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
1746f1de59e0Spgoyette	    possible_program="${possible_TOOLDIR}/bin/${toolprefix}${program}"
17479e6ac23fSkre	    if [ -x "${possible_program}" ]
17489e6ac23fSkre	    then
1749f1de59e0Spgoyette		echo ${possible_program}
17509e6ac23fSkre		return
1751f1de59e0Spgoyette	    fi
1752f1de59e0Spgoyette	done
17539e6ac23fSkre	echo ''
1754f1de59e0Spgoyette}
17559d6c4a26Slukem
1756f1de59e0Spgoyette# print_tooldir_make --
17579d6c4a26Slukem# Try to find and show a path to an existing
1758f1de59e0Spgoyette# ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
1759f1de59e0Spgoyette# new version of ${toolprefix}make has been built.
1760f1de59e0Spgoyette#
1761f1de59e0Spgoyette# * If TOOLDIR was set in the environment or on the command line, use
1762f1de59e0Spgoyette#   that value.
1763f1de59e0Spgoyette# * Otherwise try to guess what TOOLDIR would be if not overridden by
1764f1de59e0Spgoyette#   /etc/mk.conf, and check whether the resulting directory contains
1765f1de59e0Spgoyette#   a copy of ${toolprefix}make (this should work for everybody who
1766f1de59e0Spgoyette#   doesn't override TOOLDIR via /etc/mk.conf);
1767f1de59e0Spgoyette# * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
1768f1de59e0Spgoyette#   in the PATH (this might accidentally find a version of make that
1769f1de59e0Spgoyette#   does not understand the syntax used by NetBSD make, and that will
1770f1de59e0Spgoyette#   lead to failure in the next step);
1771f1de59e0Spgoyette# * If a copy of make was found above, try to use it with
1772f1de59e0Spgoyette#   nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
1773f1de59e0Spgoyette#   result only if it's a directory that already exists;
1774f1de59e0Spgoyette# * If a value of TOOLDIR was found above, and if
17759d6c4a26Slukem#   ${TOOLDIR}/bin/${toolprefix}make exists, show that value.
1776f1de59e0Spgoyette#
1777f1de59e0Spgoyetteprint_tooldir_make()
1778f1de59e0Spgoyette{
1779f1de59e0Spgoyette	local possible_make
1780f1de59e0Spgoyette	local possible_TOOLDIR
1781f1de59e0Spgoyette	local tooldir_make
1782f1de59e0Spgoyette
1783f1de59e0Spgoyette	possible_make=$(print_tooldir_program make)
1784f1de59e0Spgoyette	# If the above didn't work, search the PATH for a suitable
1785f1de59e0Spgoyette	# ${toolprefix}make, nbmake, bmake, or make.
1786f1de59e0Spgoyette	#
1787f1de59e0Spgoyette	: ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
1788f1de59e0Spgoyette	: ${possible_make:=$(find_in_PATH nbmake '')}
1789f1de59e0Spgoyette	: ${possible_make:=$(find_in_PATH bmake '')}
1790f1de59e0Spgoyette	: ${possible_make:=$(find_in_PATH make '')}
1791f1de59e0Spgoyette
1792f1de59e0Spgoyette	# At this point, we don't care whether possible_make is in the
1793f1de59e0Spgoyette	# correct TOOLDIR or not; we simply want it to be usable by
1794f1de59e0Spgoyette	# getmakevar to help us find the correct TOOLDIR.
1795f1de59e0Spgoyette	#
1796f1de59e0Spgoyette	# Use ${possible_make} with nobomb_getmakevar to try to find
1797f1de59e0Spgoyette	# the value of TOOLDIR.  Believe the result only if it's
1798f1de59e0Spgoyette	# a directory that already exists and contains bin/${toolprefix}make.
1799f1de59e0Spgoyette	#
18009e6ac23fSkre	if [ -x "${possible_make}" ]
18019e6ac23fSkre	then
18029e6ac23fSkre		possible_TOOLDIR=$(
1803f1de59e0Spgoyette			make="${possible_make}" \
1804f1de59e0Spgoyette			    nobomb_getmakevar TOOLDIR 2>/dev/null
18059e6ac23fSkre			)
18069e6ac23fSkre		if [ $? = 0 ] &&
18079e6ac23fSkre		   [ -n "${possible_TOOLDIR}" ] &&
18089e6ac23fSkre		   [ -d "${possible_TOOLDIR}" ]
1809f1de59e0Spgoyette		then
1810f1de59e0Spgoyette			tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
18119e6ac23fSkre			if [ -x "${tooldir_make}" ]
18129e6ac23fSkre			then
1813f1de59e0Spgoyette				echo "${tooldir_make}"
1814f1de59e0Spgoyette				return 0
1815f1de59e0Spgoyette			fi
1816f1de59e0Spgoyette		fi
1817f1de59e0Spgoyette	fi
1818f1de59e0Spgoyette	return 1
1819f1de59e0Spgoyette}
1820f1de59e0Spgoyette
1821f1de59e0Spgoyette# rebuildmake --
1822f1de59e0Spgoyette# Rebuild nbmake in a temporary directory if necessary.  Sets $make
1823f1de59e0Spgoyette# to a path to the nbmake executable.  Sets done_rebuildmake=true
1824f1de59e0Spgoyette# if nbmake was rebuilt.
1825f1de59e0Spgoyette#
1826f1de59e0Spgoyette# There is a cyclic dependency between building nbmake and choosing
1827f1de59e0Spgoyette# TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
1828f1de59e0Spgoyette# would like to use getmakevar to get the value of TOOLDIR; but we can't
1829f1de59e0Spgoyette# use getmakevar before we have an up to date version of nbmake; we
1830f1de59e0Spgoyette# might already have an up to date version of nbmake in TOOLDIR, but we
1831f1de59e0Spgoyette# don't yet know where TOOLDIR is.
1832f1de59e0Spgoyette#
1833f1de59e0Spgoyette# The default value of TOOLDIR also depends on the location of the top
1834f1de59e0Spgoyette# level object directory, so $(getmakevar TOOLDIR) invoked before or
1835f1de59e0Spgoyette# after making the top level object directory may produce different
1836f1de59e0Spgoyette# results.
1837f1de59e0Spgoyette#
1838f1de59e0Spgoyette# Strictly speaking, we should do the following:
1839f1de59e0Spgoyette#
1840f1de59e0Spgoyette#    1. build a new version of nbmake in a temporary directory;
1841f1de59e0Spgoyette#    2. use the temporary nbmake to create the top level obj directory;
1842f1de59e0Spgoyette#    3. use $(getmakevar TOOLDIR) with the temporary nbmake to
1843f1de59e0Spgoyette#       get the correct value of TOOLDIR;
1844f1de59e0Spgoyette#    4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
1845f1de59e0Spgoyette#
1846f1de59e0Spgoyette# However, people don't like building nbmake unnecessarily if their
1847f1de59e0Spgoyette# TOOLDIR has not changed since an earlier build.  We try to avoid
1848f1de59e0Spgoyette# rebuilding a temporary version of nbmake by taking some shortcuts to
1849f1de59e0Spgoyette# guess a value for TOOLDIR, looking for an existing version of nbmake
1850f1de59e0Spgoyette# in that TOOLDIR, and checking whether that nbmake is newer than the
1851f1de59e0Spgoyette# sources used to build it.
1852f1de59e0Spgoyette#
1853f1de59e0Spgoyetterebuildmake()
1854f1de59e0Spgoyette{
1855f1de59e0Spgoyette	make="$(print_tooldir_make)"
18569e6ac23fSkre	if [ -n "${make}" ] && [ -x "${make}" ]
18579e6ac23fSkre	then
18589e6ac23fSkre		for f in usr.bin/make/*.[ch]
18599e6ac23fSkre		do
18609e6ac23fSkre			if [ "${f}" -nt "${make}" ]
18619e6ac23fSkre			then
1862f1de59e0Spgoyette				statusmsg "${make} outdated" \
1863f1de59e0Spgoyette					"(older than ${f}), needs building."
1864f1de59e0Spgoyette				do_rebuildmake=true
1865f1de59e0Spgoyette				break
1866f1de59e0Spgoyette			fi
1867f1de59e0Spgoyette		done
1868f1de59e0Spgoyette	else
1869f1de59e0Spgoyette		statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
1870f1de59e0Spgoyette		do_rebuildmake=true
1871f1de59e0Spgoyette	fi
1872f1de59e0Spgoyette
1873f1de59e0Spgoyette	# Build bootstrap ${toolprefix}make if needed.
18749e6ac23fSkre	if ! ${do_rebuildmake}
18759e6ac23fSkre	then
1876f1de59e0Spgoyette		return
1877f1de59e0Spgoyette	fi
1878f1de59e0Spgoyette
187905b5ecc6Slukem	# Silent configure with MAKEVERBOSE==0
18809e6ac23fSkre	if [ ${MAKEVERBOSE:-2} -eq 0 ]
18819e6ac23fSkre	then
188205b5ecc6Slukem		configure_args=--silent
188305b5ecc6Slukem	fi
188405b5ecc6Slukem
1885f1de59e0Spgoyette	statusmsg "Bootstrapping ${toolprefix}make"
1886f1de59e0Spgoyette	${runcmd} cd "${tmpdir}"
1887f1de59e0Spgoyette	${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
1888f1de59e0Spgoyette		CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
188905b5ecc6Slukem	    ${HOST_SH} "${TOP}/tools/make/configure" ${configure_args} ||
1890f1de59e0Spgoyette	( cp ${tmpdir}/config.log ${tmpdir}-config.log
18919e6ac23fSkre	      bomb "Configure of ${toolprefix}make failed," \
18929e6ac23fSkre		   "see ${tmpdir}-config.log for details" )
1893f1de59e0Spgoyette	${runcmd} ${HOST_SH} buildmake.sh ||
1894f1de59e0Spgoyette	    bomb "Build of ${toolprefix}make failed"
1895f1de59e0Spgoyette	make="${tmpdir}/${toolprefix}make"
1896f1de59e0Spgoyette	${runcmd} cd "${TOP}"
1897ff40d26fSrillig	${runcmd} rm -f usr.bin/make/*.o
1898f1de59e0Spgoyette	done_rebuildmake=true
1899f1de59e0Spgoyette}
1900f1de59e0Spgoyette
1901f1de59e0Spgoyette# validatemakeparams --
1902f1de59e0Spgoyette# Perform some late sanity checks, after rebuildmake,
1903f1de59e0Spgoyette# but before createmakewrapper or any real work.
1904f1de59e0Spgoyette#
1905f1de59e0Spgoyette# Creates the top-level obj directory, because that
1906f1de59e0Spgoyette# is needed by some of the sanity checks.
1907f1de59e0Spgoyette#
19089d6c4a26Slukem# Shows status messages reporting the values of several variables.
1909f1de59e0Spgoyette#
1910f1de59e0Spgoyettevalidatemakeparams()
1911f1de59e0Spgoyette{
19123cb563b3Slukem	# Determine MAKECONF first, and set in the makewrapper.
19133cb563b3Slukem	# If set in the environment, then use that.
19143cb563b3Slukem	# else if ./mk.conf exists, then set MAKECONF to that,
19153cb563b3Slukem	# else use the default from share/mk/bsd.own.mk (/etc/mk.conf).
1916f1de59e0Spgoyette	#
19179e6ac23fSkre	if [ -n "${MAKECONF+1}" ]
19189e6ac23fSkre	then
19193cb563b3Slukem		setmakeenv MAKECONF "${MAKECONF}"
19203cb563b3Slukem		statusmsg2 "getenv MAKECONF:" "${MAKECONF}"
19219e6ac23fSkre	elif [ -f "${TOP}/mk.conf" ]
19229e6ac23fSkre	then
19233cb563b3Slukem		setmakeenv MAKECONF "${TOP}/mk.conf"
19243cb563b3Slukem		statusmsg2 "mk.conf MAKECONF:" "${MAKECONF}"
19253cb563b3Slukem	else
1926f1de59e0Spgoyette		MAKECONF=$(getmakevar MAKECONF)
19273cb563b3Slukem		setmakeenv MAKECONF "${MAKECONF}"
19283cb563b3Slukem		statusmsg2 "share/mk MAKECONF:" "${MAKECONF}"
19293cb563b3Slukem	fi
19309e6ac23fSkre	if [ -z "${MAKECONF}" ]
19319e6ac23fSkre	then
19323cb563b3Slukem		bomb "MAKECONF must not be empty"
19339e6ac23fSkre	elif [ -e "${MAKECONF}" ]
19349e6ac23fSkre	then
1935f1de59e0Spgoyette		statusmsg2 "MAKECONF file:" "${MAKECONF}"
1936f1de59e0Spgoyette	else
1937f1de59e0Spgoyette		statusmsg2 "MAKECONF file:" "${MAKECONF} (File not found)"
1938f1de59e0Spgoyette	fi
1939f1de59e0Spgoyette
1940f1de59e0Spgoyette	# Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE.
1941f1de59e0Spgoyette	# These may be set as build.sh options or in "mk.conf".
1942f1de59e0Spgoyette	# Don't export them as they're only used for tests in build.sh.
1943f1de59e0Spgoyette	#
1944f1de59e0Spgoyette	MKOBJDIRS=$(getmakevar MKOBJDIRS)
1945f1de59e0Spgoyette	MKUNPRIVED=$(getmakevar MKUNPRIVED)
1946f1de59e0Spgoyette	MKUPDATE=$(getmakevar MKUPDATE)
1947f1de59e0Spgoyette
1948f1de59e0Spgoyette	# Non-root should always use either the -U or -E flag.
1949f1de59e0Spgoyette	#
19509e6ac23fSkre	if ! ${do_expertmode} && [ "$id_u" -ne 0 ] && [ "${MKUNPRIVED}" = no ]
19519e6ac23fSkre	then
1952262db514Slukem		bomb "-U or -E must be set for build as an unprivileged user"
1953f1de59e0Spgoyette	fi
1954f1de59e0Spgoyette
19559e6ac23fSkre	if [ "${runcmd}" = echo ]
19569e6ac23fSkre	then
1957f1de59e0Spgoyette		TOOLCHAIN_MISSING=no
19589e6ac23fSkre		EXTERNAL_TOOLCHAIN=
1959f1de59e0Spgoyette	else
1960f1de59e0Spgoyette		TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
1961f1de59e0Spgoyette		EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
1962f1de59e0Spgoyette	fi
19639e6ac23fSkre	if [ "${TOOLCHAIN_MISSING}" = yes ] && [ -z "${EXTERNAL_TOOLCHAIN}" ]
19649e6ac23fSkre	then
19659e6ac23fSkre		${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain)" \
19669e6ac23fSkre						"is not yet available for"
1967f1de59e0Spgoyette		${runcmd} echo "	MACHINE:      ${MACHINE}"
1968f1de59e0Spgoyette		${runcmd} echo "	MACHINE_ARCH: ${MACHINE_ARCH}"
1969f1de59e0Spgoyette		${runcmd} echo ""
19709e6ac23fSkre		${runcmd} echo "All builds for this platform should be done" \
19719e6ac23fSkre						"via a traditional make"
19729e6ac23fSkre		${runcmd} echo "If you wish to use an external" \
19739e6ac23fSkre						"cross-toolchain, set"
19749e6ac23fSkre		${runcmd} echo "	EXTERNAL_TOOLCHAIN=<path to" \
19759e6ac23fSkre						"toolchain root>"
1976f1de59e0Spgoyette		${runcmd} echo "in either the environment or mk.conf and rerun"
1977f1de59e0Spgoyette		${runcmd} echo "	${progname} $*"
1978f1de59e0Spgoyette		exit 1
1979f1de59e0Spgoyette	fi
1980f1de59e0Spgoyette
19819e6ac23fSkre	if [ "${MKOBJDIRS}" != no ]
19829e6ac23fSkre	then
1983f1de59e0Spgoyette		# Create the top-level object directory.
1984f1de59e0Spgoyette		#
1985f1de59e0Spgoyette		# "make obj NOSUBDIR=" can handle most cases, but it
1986f1de59e0Spgoyette		# can't handle the case where MAKEOBJDIRPREFIX is set
1987f1de59e0Spgoyette		# while the corresponding directory does not exist
1988f1de59e0Spgoyette		# (rules in <bsd.obj.mk> would abort the build).  We
1989f1de59e0Spgoyette		# therefore have to handle the MAKEOBJDIRPREFIX case
1990f1de59e0Spgoyette		# without invoking "make obj".  The MAKEOBJDIR case
1991f1de59e0Spgoyette		# could be handled either way, but we choose to handle
1992f1de59e0Spgoyette		# it similarly to MAKEOBJDIRPREFIX.
1993f1de59e0Spgoyette		#
19949e6ac23fSkre		if [ -n "${TOP_obj}" ]
19959e6ac23fSkre		then
1996f1de59e0Spgoyette			# It must have been set by the "-M" or "-O"
1997f1de59e0Spgoyette			# command line options, so there's no need to
1998f1de59e0Spgoyette			# use getmakevar
1999f1de59e0Spgoyette			:
20009e6ac23fSkre		elif [ -n "$MAKEOBJDIRPREFIX" ]
20019e6ac23fSkre		then
2002f1de59e0Spgoyette			TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
20039e6ac23fSkre		elif [ -n "$MAKEOBJDIR" ]
20049e6ac23fSkre		then
2005f1de59e0Spgoyette			TOP_obj="$(getmakevar MAKEOBJDIR)"
2006f1de59e0Spgoyette		fi
20079e6ac23fSkre		if [ -n "$TOP_obj" ]
20089e6ac23fSkre		then
2009f1de59e0Spgoyette		    ${runcmd} mkdir -p "${TOP_obj}" ||
2010f1de59e0Spgoyette			bomb "Can't create top level object directory" \
2011f1de59e0Spgoyette			     "${TOP_obj}"
2012f1de59e0Spgoyette		else
20139e6ac23fSkre		    ${runcmd} "${make}" -m "${TOP}/share/mk" obj NOSUBDIR= ||
2014f1de59e0Spgoyette			    bomb "Can't create top level object directory" \
2015f1de59e0Spgoyette				 "using make obj"
2016f1de59e0Spgoyette		fi
2017f1de59e0Spgoyette
2018f1de59e0Spgoyette		# make obj in tools to ensure that the objdir for "tools"
2019f1de59e0Spgoyette		# is available.
2020f1de59e0Spgoyette		#
2021f1de59e0Spgoyette		${runcmd} cd tools
20229e6ac23fSkre		${runcmd} "${make}" -m "${TOP}/share/mk" obj NOSUBDIR= ||
2023f1de59e0Spgoyette		    bomb "Failed to make obj in tools"
2024f1de59e0Spgoyette		${runcmd} cd "${TOP}"
2025f1de59e0Spgoyette	fi
2026f1de59e0Spgoyette
2027f1de59e0Spgoyette	# Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
2028f1de59e0Spgoyette	# and bomb if they have changed from the values we had from the
2029f1de59e0Spgoyette	# command line or environment.
2030f1de59e0Spgoyette	#
2031f1de59e0Spgoyette	# This must be done after creating the top-level object directory.
2032f1de59e0Spgoyette	#
2033f1de59e0Spgoyette	for var in TOOLDIR DESTDIR RELEASEDIR
2034f1de59e0Spgoyette	do
2035f1de59e0Spgoyette		eval oldval=\"\$${var}\"
2036f1de59e0Spgoyette		newval="$(getmakevar $var)"
20379e6ac23fSkre		if ! $do_expertmode
20389e6ac23fSkre		then
2039f1de59e0Spgoyette			: ${_SRC_TOP_OBJ_:=$(getmakevar _SRC_TOP_OBJ_)}
2040f1de59e0Spgoyette			case "$var" in
2041f1de59e0Spgoyette			DESTDIR)
2042f1de59e0Spgoyette				: ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
2043f1de59e0Spgoyette				makeenv="${makeenv} DESTDIR"
2044f1de59e0Spgoyette				;;
2045f1de59e0Spgoyette			RELEASEDIR)
2046f1de59e0Spgoyette				: ${newval:=${_SRC_TOP_OBJ_}/releasedir}
2047f1de59e0Spgoyette				makeenv="${makeenv} RELEASEDIR"
2048f1de59e0Spgoyette				;;
2049f1de59e0Spgoyette			esac
2050f1de59e0Spgoyette		fi
20519e6ac23fSkre		if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]
20529e6ac23fSkre		then
2053f1de59e0Spgoyette			bomb "Value of ${var} has changed" \
2054f1de59e0Spgoyette			     "(was \"${oldval}\", now \"${newval}\")"
2055f1de59e0Spgoyette		fi
2056f1de59e0Spgoyette		eval ${var}=\"\${newval}\"
2057f1de59e0Spgoyette		eval export ${var}
2058f1de59e0Spgoyette		statusmsg2 "${var} path:" "${newval}"
2059f1de59e0Spgoyette	done
2060f1de59e0Spgoyette
2061f1de59e0Spgoyette	# RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
2062f1de59e0Spgoyette	RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
2063f1de59e0Spgoyette
2064f1de59e0Spgoyette	# Check validity of TOOLDIR and DESTDIR.
2065f1de59e0Spgoyette	#
20669e6ac23fSkre	if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = / ]
20679e6ac23fSkre	then
2068f1de59e0Spgoyette		bomb "TOOLDIR '${TOOLDIR}' invalid"
2069f1de59e0Spgoyette	fi
2070f1de59e0Spgoyette	removedirs="${TOOLDIR}"
2071f1de59e0Spgoyette
20729e6ac23fSkre	if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = / ]
20739e6ac23fSkre	then
20749e6ac23fSkre		if ${do_distribution} || ${do_release} ||
20759e6ac23fSkre		   [ "${uname_s}" != NetBSD ] ||
20769e6ac23fSkre		   [ "${uname_m}" != "${MACHINE}" ]
20779e6ac23fSkre		then
20789e6ac23fSkre			bomb "DESTDIR must != / for cross builds," \
20799e6ac23fSkre			     "or ${progname} 'distribution' or 'release'"
2080f1de59e0Spgoyette		fi
20819e6ac23fSkre		if ! ${do_expertmode}
20829e6ac23fSkre		then
2083f1de59e0Spgoyette			bomb "DESTDIR must != / for non -E (expert) builds"
2084f1de59e0Spgoyette		fi
2085f1de59e0Spgoyette		statusmsg "WARNING: Building to /, in expert mode."
20869e6ac23fSkre		statusmsg "         This may cause your system to break!"
20879e6ac23fSkre		statusmsg "         Reasons include:"
2088f1de59e0Spgoyette		statusmsg "           - your kernel is not up to date"
2089f1de59e0Spgoyette		statusmsg "           - the libraries or toolchain have changed"
2090f1de59e0Spgoyette		statusmsg "         YOU HAVE BEEN WARNED!"
2091f1de59e0Spgoyette	else
2092f1de59e0Spgoyette		removedirs="${removedirs} ${DESTDIR}"
2093f1de59e0Spgoyette	fi
20949e6ac23fSkre	if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]
20959e6ac23fSkre	then
2096262db514Slukem		bomb "Must set RELEASEDIR with 'releasekernel=...'"
2097f1de59e0Spgoyette	fi
2098f1de59e0Spgoyette
2099f1de59e0Spgoyette	# If a previous build.sh run used -U (and therefore created a
2100f1de59e0Spgoyette	# METALOG file), then most subsequent build.sh runs must also
2101f1de59e0Spgoyette	# use -U.  If DESTDIR is about to be removed, then don't perform
2102f1de59e0Spgoyette	# this check.
2103f1de59e0Spgoyette	#
2104f1de59e0Spgoyette	case "${do_removedirs} ${removedirs} " in
2105f1de59e0Spgoyette	true*" ${DESTDIR} "*)
2106f1de59e0Spgoyette		# DESTDIR is about to be removed
2107f1de59e0Spgoyette		;;
2108f1de59e0Spgoyette	*)
21099e6ac23fSkre		if [ -e "${DESTDIR}/METALOG" ] &&
21109e6ac23fSkre		   [ "${MKUNPRIVED}" = no ]
21119e6ac23fSkre		then
21129e6ac23fSkre			if $do_expertmode
21139e6ac23fSkre			then
2114262db514Slukem				warning "A previous build.sh run specified -U"
2115f1de59e0Spgoyette			else
21169e6ac23fSkre				bomb "A previous build.sh run specified -U;" \
21179e6ac23fSkre				     "you must specify it again now"
2118f1de59e0Spgoyette			fi
2119f1de59e0Spgoyette		fi
2120f1de59e0Spgoyette		;;
2121f1de59e0Spgoyette	esac
2122f1de59e0Spgoyette
2123f1de59e0Spgoyette	# live-image and install-image targets require binary sets
2124f1de59e0Spgoyette	# (actually DESTDIR/etc/mtree/set.* files) built with MKUNPRIVED.
2125f1de59e0Spgoyette	# If release operation is specified with live-image or install-image,
2126f1de59e0Spgoyette	# the release op should be performed with -U for later image ops.
2127f1de59e0Spgoyette	#
21289e6ac23fSkre	if ${do_release} &&
21299e6ac23fSkre	    { ${do_live_image} || ${do_install_image} ; } &&
21309e6ac23fSkre	    [ "${MKUNPRIVED}" = no ]
21319e6ac23fSkre	then
21329e6ac23fSkre		bomb "-U must be specified on building release" \
21339e6ac23fSkre		     "to create images later"
2134f1de59e0Spgoyette	fi
2135f1de59e0Spgoyette}
2136f1de59e0Spgoyette
2137f1de59e0Spgoyette
2138f1de59e0Spgoyettecreatemakewrapper()
2139f1de59e0Spgoyette{
2140f1de59e0Spgoyette	# Remove the target directories.
2141f1de59e0Spgoyette	#
21429e6ac23fSkre	if ${do_removedirs}
21439e6ac23fSkre	then
21449e6ac23fSkre		for f in ${removedirs}
21459e6ac23fSkre		do
2146f1de59e0Spgoyette			statusmsg "Removing ${f}"
2147f1de59e0Spgoyette			${runcmd} rm -r -f "${f}"
2148f1de59e0Spgoyette		done
2149f1de59e0Spgoyette	fi
2150f1de59e0Spgoyette
2151f1de59e0Spgoyette	# Recreate $TOOLDIR.
2152f1de59e0Spgoyette	#
2153f1de59e0Spgoyette	${runcmd} mkdir -p "${TOOLDIR}/bin" ||
2154f1de59e0Spgoyette	    bomb "mkdir of '${TOOLDIR}/bin' failed"
2155f1de59e0Spgoyette
2156f1de59e0Spgoyette	# If we did not previously rebuild ${toolprefix}make, then
2157f1de59e0Spgoyette	# check whether $make is still valid and the same as the output
2158f1de59e0Spgoyette	# from print_tooldir_make.  If not, then rebuild make now.  A
2159f1de59e0Spgoyette	# possible reason for this being necessary is that the actual
2160f1de59e0Spgoyette	# value of TOOLDIR might be different from the value guessed
2161f1de59e0Spgoyette	# before the top level obj dir was created.
2162f1de59e0Spgoyette	#
21639e6ac23fSkre	if ! ${done_rebuildmake} &&
21649e6ac23fSkre	   { ! [ -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] ; }
2165f1de59e0Spgoyette	then
2166f1de59e0Spgoyette		rebuildmake
2167f1de59e0Spgoyette	fi
2168f1de59e0Spgoyette
2169f1de59e0Spgoyette	# Install ${toolprefix}make if it was built.
2170f1de59e0Spgoyette	#
21719e6ac23fSkre	if ${done_rebuildmake}
21729e6ac23fSkre	then
2173f1de59e0Spgoyette		${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
2174f1de59e0Spgoyette		${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
2175f1de59e0Spgoyette		    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
2176f1de59e0Spgoyette		make="${TOOLDIR}/bin/${toolprefix}make"
2177f1de59e0Spgoyette		statusmsg "Created ${make}"
2178f1de59e0Spgoyette	fi
2179f1de59e0Spgoyette
2180f1de59e0Spgoyette	# Build a ${toolprefix}make wrapper script, usable by hand as
2181f1de59e0Spgoyette	# well as by build.sh.
2182f1de59e0Spgoyette	#
21839e6ac23fSkre	if [ -z "${makewrapper}" ]
21849e6ac23fSkre	then
21859e6ac23fSkre		makewrapper="${TOOLDIR}/bin/${toolprefix}make"
21869e6ac23fSkre		makewrapper="${makewrapper}-${makewrappermachine:-${MACHINE}}"
2187f1de59e0Spgoyette		[ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
2188f1de59e0Spgoyette	fi
2189f1de59e0Spgoyette
2190f1de59e0Spgoyette	${runcmd} rm -f "${makewrapper}"
21919e6ac23fSkre	if [ "${runcmd}" = echo ]
21929e6ac23fSkre	then
2193f1de59e0Spgoyette		echo 'cat <<EOF >'${makewrapper}
2194f1de59e0Spgoyette		makewrapout=
2195f1de59e0Spgoyette	else
2196f1de59e0Spgoyette		makewrapout=">>\${makewrapper}"
2197f1de59e0Spgoyette	fi
2198f1de59e0Spgoyette
2199f1de59e0Spgoyette	case "${KSH_VERSION:-${SH_VERSION}}" in
2200f1de59e0Spgoyette	*PD\ KSH*|*MIRBSD\ KSH*)
2201f1de59e0Spgoyette		set +o braceexpand
2202f1de59e0Spgoyette		;;
2203f1de59e0Spgoyette	esac
2204f1de59e0Spgoyette
2205f1de59e0Spgoyette	eval cat <<EOF ${makewrapout}
2206f1de59e0Spgoyette#! ${HOST_SH}
2207f1de59e0Spgoyette# Set proper variables to allow easy "make" building of a NetBSD subtree.
2208*16dd98ceSgutteridge# Generated from:  \$NetBSD: build.sh,v 1.388 2024/12/28 00:39:56 gutteridge Exp $
2209f1de59e0Spgoyette# with these arguments: ${_args}
2210f1de59e0Spgoyette#
2211f1de59e0Spgoyette
2212f1de59e0SpgoyetteEOF
2213f1de59e0Spgoyette	{
22149e6ac23fSkre		sorted_vars=$(
22159e6ac23fSkre			for var in ${makeenv}
22169e6ac23fSkre			do
22179e6ac23fSkre				echo "${var}"
22189e6ac23fSkre			done |
22199e6ac23fSkre				sort -u
22209e6ac23fSkre		)
22219e6ac23fSkre		for var in ${sorted_vars}
22229e6ac23fSkre		do
2223f1de59e0Spgoyette			eval val=\"\${${var}}\"
2224f1de59e0Spgoyette			eval is_set=\"\${${var}+set}\"
22259e6ac23fSkre			if [ -z "${is_set}" ]
22269e6ac23fSkre			then
2227f1de59e0Spgoyette				echo "unset ${var}"
2228f1de59e0Spgoyette			else
2229f1de59e0Spgoyette				qval="$(shell_quote "${val}")"
2230f1de59e0Spgoyette				echo "${var}=${qval}; export ${var}"
2231f1de59e0Spgoyette			fi
2232f1de59e0Spgoyette		done
2233f1de59e0Spgoyette
22349e6ac23fSkre		cat <<-EOF
2235f1de59e0Spgoyette
2236f1de59e0Spgoyette			exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
2237f1de59e0Spgoyette		EOF
2238f1de59e0Spgoyette	} | eval cat "${makewrapout}"
22399e6ac23fSkre	[ "${runcmd}" = echo ] && echo EOF
2240f1de59e0Spgoyette	${runcmd} chmod +x "${makewrapper}"
2241f1de59e0Spgoyette	statusmsg2 "Updated makewrapper:" "${makewrapper}"
2242f1de59e0Spgoyette}
2243f1de59e0Spgoyette
2244f1de59e0Spgoyettemake_in_dir()
2245f1de59e0Spgoyette{
2246e6351014Skre	local dir="$1"
2247e6351014Skre	local op="$2"
2248f1de59e0Spgoyette	${runcmd} cd "${dir}" ||
2249f1de59e0Spgoyette	    bomb "Failed to cd to \"${dir}\""
2250f1de59e0Spgoyette	${runcmd} "${makewrapper}" ${parallel} ${op} ||
2251f1de59e0Spgoyette	    bomb "Failed to make ${op} in \"${dir}\""
2252f1de59e0Spgoyette	${runcmd} cd "${TOP}" ||
2253f1de59e0Spgoyette	    bomb "Failed to cd back to \"${TOP}\""
2254f1de59e0Spgoyette}
2255f1de59e0Spgoyette
2256f1de59e0Spgoyettebuildtools()
2257f1de59e0Spgoyette{
22589e6ac23fSkre	if [ "${MKOBJDIRS}" != no ]
22599e6ac23fSkre	then
2260f1de59e0Spgoyette		${runcmd} "${makewrapper}" ${parallel} obj-tools ||
2261f1de59e0Spgoyette		    bomb "Failed to make obj-tools"
2262f1de59e0Spgoyette	fi
22639e6ac23fSkre	if [ "${MKUPDATE}" = no ]
22649e6ac23fSkre	then
2265f1de59e0Spgoyette		make_in_dir tools cleandir
2266f1de59e0Spgoyette	fi
2267f1de59e0Spgoyette	make_in_dir tools build_install
2268f1de59e0Spgoyette	statusmsg "Tools built to ${TOOLDIR}"
2269f1de59e0Spgoyette}
2270f1de59e0Spgoyette
22710a37cd5eSchristosbuildlibs()
22720a37cd5eSchristos{
22739e6ac23fSkre	if [ "${MKOBJDIRS}" != no ]
22749e6ac23fSkre	then
22750a37cd5eSchristos		${runcmd} "${makewrapper}" ${parallel} obj ||
22760a37cd5eSchristos		    bomb "Failed to make obj"
22770a37cd5eSchristos	fi
22789e6ac23fSkre	if [ "${MKUPDATE}" = no ]
22799e6ac23fSkre	then
22800a37cd5eSchristos		make_in_dir lib cleandir
22810a37cd5eSchristos	fi
22820a37cd5eSchristos	make_in_dir . do-distrib-dirs
22830a37cd5eSchristos	make_in_dir . includes
22840a37cd5eSchristos	make_in_dir . do-lib
22850a37cd5eSchristos	statusmsg "libs built"
22860a37cd5eSchristos}
22870a37cd5eSchristos
2288f1de59e0Spgoyettegetkernelconf()
2289f1de59e0Spgoyette{
22909e6ac23fSkre	kernelconf=$1
22919e6ac23fSkre	if [ "${MKOBJDIRS}" != no ]
22929e6ac23fSkre	then
2293f1de59e0Spgoyette		# The correct value of KERNOBJDIR might
2294f1de59e0Spgoyette		# depend on a prior "make obj" in
2295f1de59e0Spgoyette		# ${KERNSRCDIR}/${KERNARCHDIR}/compile.
2296f1de59e0Spgoyette		#
2297f1de59e0Spgoyette		KERNSRCDIR="$(getmakevar KERNSRCDIR)"
2298f1de59e0Spgoyette		KERNARCHDIR="$(getmakevar KERNARCHDIR)"
2299f1de59e0Spgoyette		make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
2300f1de59e0Spgoyette	fi
23019e6ac23fSkre	KERNCONFDIR=$(getmakevar KERNCONFDIR)
23029e6ac23fSkre	KERNOBJDIR=$(getmakevar KERNOBJDIR)
2303f1de59e0Spgoyette	case "${kernelconf}" in
2304f1de59e0Spgoyette	*/*)
23059e6ac23fSkre		kernelconfpath=${kernelconf}
23069e6ac23fSkre		kernelconfname=${kernelconf##*/}
2307f1de59e0Spgoyette		;;
2308f1de59e0Spgoyette	*)
23099e6ac23fSkre		kernelconfpath=${KERNCONFDIR}/${kernelconf}
23109e6ac23fSkre		kernelconfname=${kernelconf}
2311f1de59e0Spgoyette		;;
2312f1de59e0Spgoyette	esac
23139e6ac23fSkre	kernelbuildpath=${KERNOBJDIR}/${kernelconfname}
2314f1de59e0Spgoyette}
2315f1de59e0Spgoyette
2316f1de59e0Spgoyettediskimage()
2317f1de59e0Spgoyette{
23189e6ac23fSkre	ARG="$(echo "$1" | tr '[:lower:]' '[:upper:]')"
2319f1de59e0Spgoyette	[ -f "${DESTDIR}/etc/mtree/set.base" ] ||
2320f1de59e0Spgoyette	    bomb "The release binaries must be built first"
2321f1de59e0Spgoyette	kerneldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
2322f1de59e0Spgoyette	kernel="${kerneldir}/netbsd-${ARG}.gz"
2323f1de59e0Spgoyette	[ -f "${kernel}" ] ||
2324f1de59e0Spgoyette	    bomb "The kernel ${kernel} must be built first"
2325f1de59e0Spgoyette	make_in_dir "${NETBSDSRCDIR}/etc" "smp_${1}"
2326f1de59e0Spgoyette}
2327f1de59e0Spgoyette
2328f1de59e0Spgoyettebuildkernel()
2329f1de59e0Spgoyette{
23309e6ac23fSkre	if ! ${do_tools} && ! ${buildkernelwarned:-false}
23319e6ac23fSkre	then
2332f1de59e0Spgoyette		# Building tools every time we build a kernel is clearly
2333f1de59e0Spgoyette		# unnecessary.  We could try to figure out whether rebuilding
2334f1de59e0Spgoyette		# the tools is necessary this time, but it doesn't seem worth
2335f1de59e0Spgoyette		# the trouble.  Instead, we say it's the user's responsibility
2336f1de59e0Spgoyette		# to rebuild the tools if necessary.
2337f1de59e0Spgoyette		#
2338f1de59e0Spgoyette		statusmsg "Building kernel without building new tools"
2339f1de59e0Spgoyette		buildkernelwarned=true
2340f1de59e0Spgoyette	fi
2341f1de59e0Spgoyette	getkernelconf $1
2342f1de59e0Spgoyette	statusmsg2 "Building kernel:" "${kernelconf}"
2343f1de59e0Spgoyette	statusmsg2 "Build directory:" "${kernelbuildpath}"
2344f1de59e0Spgoyette	${runcmd} mkdir -p "${kernelbuildpath}" ||
2345f1de59e0Spgoyette	    bomb "Cannot mkdir: ${kernelbuildpath}"
23469e6ac23fSkre	if [ "${MKUPDATE}" = no ]
23479e6ac23fSkre	then
2348f1de59e0Spgoyette		make_in_dir "${kernelbuildpath}" cleandir
2349f1de59e0Spgoyette	fi
23509e6ac23fSkre	[ -x "${TOOLDIR}/bin/${toolprefix}config" ] ||
23519e6ac23fSkre		bomb "${TOOLDIR}/bin/${toolprefix}config does not exist." \
23529e6ac23fSkre		     "You need to \"$0 tools\" first"
2353f1de59e0Spgoyette	CONFIGOPTS=$(getmakevar CONFIGOPTS)
2354f1de59e0Spgoyette	${runcmd} "${TOOLDIR}/bin/${toolprefix}config" ${CONFIGOPTS} \
2355f1de59e0Spgoyette		-b "${kernelbuildpath}" -s "${TOP}/sys" ${configopts} \
2356f1de59e0Spgoyette		"${kernelconfpath}" ||
2357f1de59e0Spgoyette	    bomb "${toolprefix}config failed for ${kernelconf}"
2358f1de59e0Spgoyette	make_in_dir "${kernelbuildpath}" depend
2359f1de59e0Spgoyette	make_in_dir "${kernelbuildpath}" all
2360f1de59e0Spgoyette
23619e6ac23fSkre	if [ "${runcmd}" != echo ]
23629e6ac23fSkre	then
2363f1de59e0Spgoyette		statusmsg "Kernels built from ${kernelconf}:"
2364f1de59e0Spgoyette		kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
23659e6ac23fSkre		for kern in ${kernlist:-netbsd}
23669e6ac23fSkre		do
23679e6ac23fSkre			[ -f "${kernelbuildpath}/${kern}" ] &&
2368f1de59e0Spgoyette			    echo "  ${kernelbuildpath}/${kern}"
2369f1de59e0Spgoyette		done | tee -a "${results}"
2370f1de59e0Spgoyette	fi
2371f1de59e0Spgoyette}
2372f1de59e0Spgoyette
2373f1de59e0Spgoyettereleasekernel()
2374f1de59e0Spgoyette{
2375f1de59e0Spgoyette	getkernelconf $1
2376f1de59e0Spgoyette	kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
2377f1de59e0Spgoyette	${runcmd} mkdir -p "${kernelreldir}"
2378f1de59e0Spgoyette	kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
23799e6ac23fSkre	for kern in ${kernlist:-netbsd}
23809e6ac23fSkre	do
2381f1de59e0Spgoyette		builtkern="${kernelbuildpath}/${kern}"
2382f1de59e0Spgoyette		[ -f "${builtkern}" ] || continue
2383f1de59e0Spgoyette		releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
2384f1de59e0Spgoyette		statusmsg2 "Kernel copy:" "${releasekern}"
23859e6ac23fSkre		if [ "${runcmd}" = echo ]
23869e6ac23fSkre		then
2387f1de59e0Spgoyette			echo "gzip -c -9 < ${builtkern} > ${releasekern}"
2388f1de59e0Spgoyette		else
2389f1de59e0Spgoyette			gzip -c -9 < "${builtkern}" > "${releasekern}"
2390f1de59e0Spgoyette		fi
2391f1de59e0Spgoyette	done
2392f1de59e0Spgoyette}
2393f1de59e0Spgoyette
2394f1de59e0Spgoyettebuildkernels()
2395f1de59e0Spgoyette{
2396f1de59e0Spgoyette	allkernels=$( runcmd= make_in_dir etc '-V ${ALL_KERNELS}' )
23979e6ac23fSkre	for k in $allkernels
23989e6ac23fSkre	do
2399f1de59e0Spgoyette		buildkernel "${k}"
2400f1de59e0Spgoyette	done
2401f1de59e0Spgoyette}
2402f1de59e0Spgoyette
2403f1de59e0Spgoyettebuildmodules()
2404f1de59e0Spgoyette{
2405f1de59e0Spgoyette	setmakeenv MKBINUTILS no
24069e6ac23fSkre	if ! ${do_tools} && ! ${buildmoduleswarned:-false}
24079e6ac23fSkre	then
2408f1de59e0Spgoyette		# Building tools every time we build modules is clearly
2409f1de59e0Spgoyette		# unnecessary as well as a kernel.
2410f1de59e0Spgoyette		#
2411f1de59e0Spgoyette		statusmsg "Building modules without building new tools"
2412f1de59e0Spgoyette		buildmoduleswarned=true
2413f1de59e0Spgoyette	fi
2414f1de59e0Spgoyette
2415f1de59e0Spgoyette	statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
24169e6ac23fSkre	if [ "${MKOBJDIRS}" != no ]
24179e6ac23fSkre	then
2418f1de59e0Spgoyette		make_in_dir sys/modules obj
2419f1de59e0Spgoyette	fi
24209e6ac23fSkre	if [ "${MKUPDATE}" = no ]
24219e6ac23fSkre	then
2422f1de59e0Spgoyette		make_in_dir sys/modules cleandir
2423f1de59e0Spgoyette	fi
2424f1de59e0Spgoyette	make_in_dir sys/modules dependall
2425f1de59e0Spgoyette	make_in_dir sys/modules install
2426f1de59e0Spgoyette
24279e6ac23fSkre	statusmsg "Successful build of kernel modules for" \
24289e6ac23fSkre		  "NetBSD/${MACHINE} ${DISTRIBVER}"
2429f1de59e0Spgoyette}
2430f1de59e0Spgoyette
2431043d88a7Sjmcneillbuilddtb()
2432043d88a7Sjmcneill{
24339e6ac23fSkre	statusmsg "Building devicetree blobs for" \
24349e6ac23fSkre		  "NetBSD/${MACHINE} ${DISTRIBVER}"
24359e6ac23fSkre	if [ "${MKOBJDIRS}" != no ]
24369e6ac23fSkre	then
2437043d88a7Sjmcneill		make_in_dir sys/dtb obj
2438043d88a7Sjmcneill	fi
24399e6ac23fSkre	if [ "${MKUPDATE}" = no ]
24409e6ac23fSkre	then
2441043d88a7Sjmcneill		make_in_dir sys/dtb cleandir
2442043d88a7Sjmcneill	fi
2443043d88a7Sjmcneill	make_in_dir sys/dtb dependall
2444043d88a7Sjmcneill	make_in_dir sys/dtb install
2445043d88a7Sjmcneill
24469e6ac23fSkre	statusmsg "Successful build of devicetree blobs for" \
24479e6ac23fSkre		  "NetBSD/${MACHINE} ${DISTRIBVER}"
2448043d88a7Sjmcneill}
2449043d88a7Sjmcneill
245059040e53Sriastradhbuildpkg()
245159040e53Sriastradh{
245259040e53Sriastradh	local catpkg
245359040e53Sriastradh	local pkgroot
245459040e53Sriastradh	local makejobsarg
245559040e53Sriastradh	local makejobsvar
245659040e53Sriastradh	local quiet
245759040e53Sriastradh	local opsys_version
245859040e53Sriastradh
245959040e53Sriastradh	catpkg="$1"
246059040e53Sriastradh
246159040e53Sriastradh	pkgroot="${TOP_objdir:-${TOP}}/pkgroot"
246259040e53Sriastradh	${runcmd} mkdir -p "${pkgroot}" ||
246359040e53Sriastradh	    bomb "Can't create package root" "${pkgroot}"
246459040e53Sriastradh
246559040e53Sriastradh	# Get a symlink-free absolute path to pkg -- pkgsrc wants this.
246659040e53Sriastradh	#
246759040e53Sriastradh	# XXX See TOP= above regarding pwd -P.
246859040e53Sriastradh	pkgroot=$(unset PWD; cd "${pkgroot}" &&
246959040e53Sriastradh		((exec pwd -P 2>/dev/null) || (exec pwd 2>/dev/null)))
247059040e53Sriastradh
247159040e53Sriastradh	case $parallel in
247259040e53Sriastradh	"-j "*)
247359040e53Sriastradh		makejobsarg="--make-jobs ${parallel#-j }"
247459040e53Sriastradh		makejobsvar="MAKE_JOBS=${parallel#-j }"
247559040e53Sriastradh		;;
24769e6ac23fSkre	*)	makejobsarg=
24779e6ac23fSkre		makejobsvar=
247859040e53Sriastradh		;;
247959040e53Sriastradh	esac
248059040e53Sriastradh
24819e6ac23fSkre	if [ "${MAKEVERBOSE}" -eq 0 ]
24829e6ac23fSkre	then
248359040e53Sriastradh		quiet="--quiet"
248459040e53Sriastradh	else
24859e6ac23fSkre		quiet=
248659040e53Sriastradh	fi
248759040e53Sriastradh
248859040e53Sriastradh	# Derived from pkgsrc/mk/bsd.prefs.mk rev. 1.451.
248959040e53Sriastradh	opsys_version=$(echo "${DISTRIBVER}" |
24909e6ac23fSkre		awk -F. '{
24919e6ac23fSkre				major=int($1)
24929e6ac23fSkre				minor=int($2)
24939e6ac23fSkre				if (minor>=100) minor=99
24949e6ac23fSkre				patch=int($3)
24959e6ac23fSkre				if (patch>=100) patch=99
24969e6ac23fSkre				printf "%02d%02d%02d", major, minor, patch
24979e6ac23fSkre			}'
24989e6ac23fSkre	)
249959040e53Sriastradh
250059040e53Sriastradh	# Bootstrap pkgsrc if needed.
250159040e53Sriastradh	#
250259040e53Sriastradh	# XXX Redo this if it's out-of-date, not just if it's missing.
25039e6ac23fSkre	if ! [ -x "${pkgroot}/pkg/bin/bmake" ]
25049e6ac23fSkre	then
250559040e53Sriastradh		statusmsg "Bootstrapping pkgsrc"
250659040e53Sriastradh
250759040e53Sriastradh		cat >"${pkgroot}/mk.conf-fragment" <<EOF
250859040e53SriastradhUSE_CROSS_COMPILE?=	no
250959040e53SriastradhTOOLDIR=		${TOOLDIR}
251059040e53SriastradhCROSS_DESTDIR=		${DESTDIR}
251159040e53SriastradhCROSS_MACHINE_ARCH=	${MACHINE_ARCH}
251259040e53SriastradhCROSS_OPSYS=		NetBSD
251359040e53SriastradhCROSS_OS_VERSION=	${DISTRIBVER}
251459040e53SriastradhCROSS_OPSYS_VERSION=	${opsys_version}
251559040e53SriastradhCROSS_LOWER_OPSYS=	netbsd
251659040e53SriastradhCROSS_LOWER_OPSYS_VERSUFFIX=	# empty
251759040e53SriastradhCROSS_LOWER_OS_VARIANT=		# empty
251859040e53SriastradhCROSS_LOWER_VARIANT_VERSION=	# empty
251959040e53SriastradhCROSS_LOWER_VENDOR=		# empty
252059040e53SriastradhCROSS_OBJECT_FMT=	ELF
252159040e53Sriastradh
252259040e53SriastradhALLOW_VULNERABLE_PACKAGES=	yes
252359040e53SriastradhBINPKG_SITES=			# empty
252459040e53SriastradhFAILOVER_FETCH=			yes
252559040e53SriastradhFETCH_TIMEOUT=			1800
252659040e53SriastradhPASSIVE_FETCH=			yes
252759040e53Sriastradh
252859040e53SriastradhDISTDIR=		${pkgroot}/distfiles
252959040e53SriastradhPACKAGES=		${pkgroot}/packages
253059040e53SriastradhWRKOBJDIR=		${pkgroot}/work
253159040e53Sriastradh
25328468ef8dSriastradh# pkgsrc cross-builds are not set up to support native X, but also part
25338468ef8dSriastradh# of the point of pkgsrc cross-build infrastructure is to not need
25348468ef8dSriastradh# native X any more.
25359f34be14Sriastradh#
25369f34be14Sriastradh# (If you fix this, remove the bomb in build.sh pkg=... on MKX11=yes.)
25378468ef8dSriastradhX11_TYPE=		modular
25388468ef8dSriastradh
253959040e53Sriastradh.-include "${MAKECONF}"
254059040e53Sriastradh
254159040e53SriastradhMKDEBUG=		no	# interferes with pkgsrc builds
254259040e53SriastradhEOF
254359040e53Sriastradh
254459040e53Sriastradh		# XXX Set --abi for mips and whatever else needs it?
254559040e53Sriastradh		# XXX Unprivileged native tools, privileged cross.
25469e6ac23fSkre		(
25479e6ac23fSkre			cd "${PKGSRCDIR}" &&
25489e6ac23fSkre			clearmakeenv &&
25499e6ac23fSkre			./bootstrap/bootstrap \
255059040e53Sriastradh				${makejobsarg} \
255159040e53Sriastradh				--mk-fragment "${pkgroot}/mk.conf-fragment" \
255259040e53Sriastradh				--prefix "${pkgroot}/pkg" \
255359040e53Sriastradh				${quiet} \
255459040e53Sriastradh				--unprivileged \
25559e6ac23fSkre				--workdir "${pkgroot}/bootwork"
25569e6ac23fSkre		) ||
25579e6ac23fSkre			 bomb "Failed to bootstrap pkgsrc"
255859040e53Sriastradh	fi
255959040e53Sriastradh
256059040e53Sriastradh	# Build the package.
25619e6ac23fSkre	(
25629e6ac23fSkre		cd "${PKGSRCDIR}/${catpkg}" &&
25639e6ac23fSkre		clearmakeenv &&
256459040e53Sriastradh		"${pkgroot}/pkg/bin/bmake" package \
256559040e53Sriastradh			USE_CROSS_COMPILE=yes \
25669e6ac23fSkre			${makejobsvar}
25679e6ac23fSkre	) ||
25689e6ac23fSkre		bomb "Failed to build ${catpkg}"
256959040e53Sriastradh}
257059040e53Sriastradh
2571f1de59e0Spgoyetteinstallmodules()
2572f1de59e0Spgoyette{
2573f1de59e0Spgoyette	dir="$1"
2574f1de59e0Spgoyette	${runcmd} "${makewrapper}" INSTALLMODULESDIR="${dir}" installmodules ||
2575f1de59e0Spgoyette	    bomb "Failed to make installmodules to ${dir}"
2576f1de59e0Spgoyette	statusmsg "Successful installmodules to ${dir}"
2577f1de59e0Spgoyette}
2578f1de59e0Spgoyette
2579f1de59e0Spgoyetteinstallworld()
2580f1de59e0Spgoyette{
2581f1de59e0Spgoyette	dir="$1"
2582f1de59e0Spgoyette	${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
2583f1de59e0Spgoyette	    bomb "Failed to make installworld to ${dir}"
2584f1de59e0Spgoyette	statusmsg "Successful installworld to ${dir}"
2585f1de59e0Spgoyette}
2586f1de59e0Spgoyette
2587f1de59e0Spgoyette# Run rump build&link tests.
2588f1de59e0Spgoyette#
2589f1de59e0Spgoyette# To make this feasible for running without having to install includes and
2590f1de59e0Spgoyette# libraries into destdir (i.e. quick), we only run ld.  This is possible
2591f1de59e0Spgoyette# since the rump kernel is a closed namespace apart from calls to rumpuser.
2592f1de59e0Spgoyette# Therefore, if ld complains only about rumpuser symbols, rump kernel
2593f1de59e0Spgoyette# linking was successful.
2594f1de59e0Spgoyette#
2595f1de59e0Spgoyette# We test that rump links with a number of component configurations.
2596f1de59e0Spgoyette# These attempt to mimic what is encountered in the full build.
2597f1de59e0Spgoyette# See list below.  The list should probably be either autogenerated
2598f1de59e0Spgoyette# or managed elsewhere; keep it here until a better idea arises.
2599f1de59e0Spgoyette#
2600f1de59e0Spgoyette# Above all, note that THIS IS NOT A SUBSTITUTE FOR A FULL BUILD.
2601f1de59e0Spgoyette#
2602f1de59e0Spgoyette
26030534fe7cSuwe# XXX: uwe: kern/56599 - while riastradh addressed librump problems,
26040534fe7cSuwe# there are still unwanted dependencies:
26050534fe7cSuwe#    net -> net_net
26060534fe7cSuwe#    vfs -> fifo
26070534fe7cSuwe
26080534fe7cSuwe# -lrumpvfs -> $LRUMPVFS for now
26090534fe7cSuweLRUMPVFS="-lrumpvfs -lrumpvfs_nofifofs"
26100534fe7cSuwe
26110534fe7cSuweRUMP_LIBSETS="
26120534fe7cSuwe	-lrump,
26130534fe7cSuwe        -lrumpvfs
26140534fe7cSuwe            --no-whole-archive -lrumpvfs_nofifofs -lrump,
26150534fe7cSuwe	-lrumpkern_tty
26160534fe7cSuwe            --no-whole-archive $LRUMPVFS -lrump,
26170534fe7cSuwe	-lrumpfs_tmpfs
26180534fe7cSuwe            --no-whole-archive $LRUMPVFS -lrump,
26190534fe7cSuwe	-lrumpfs_ffs -lrumpfs_msdos
262048f6c7b4Suwe            --no-whole-archive $LRUMPVFS -lrumpdev_disk -lrumpdev -lrump,
2621f1de59e0Spgoyette	-lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet
26220534fe7cSuwe	    --no-whole-archive -lrump,
26230534fe7cSuwe	-lrumpfs_nfs
26240534fe7cSuwe	    --no-whole-archive $LRUMPVFS
26250534fe7cSuwe	    -lrumpnet_sockin -lrumpnet_virtif -lrumpnet_netinet
26260534fe7cSuwe            --start-group -lrumpnet_net -lrumpnet --end-group -lrump,
26270534fe7cSuwe	-lrumpdev_cgd -lrumpdev_raidframe -lrumpdev_rnd -lrumpdev_dm
262848f6c7b4Suwe            --no-whole-archive $LRUMPVFS -lrumpdev_disk -lrumpdev -lrumpkern_crypto -lrump
26290534fe7cSuwe"
26300534fe7cSuwe
2631f1de59e0Spgoyettedorump()
2632f1de59e0Spgoyette{
26339e6ac23fSkre	local doclean=
26349e6ac23fSkre	local doobjs=
2635f1de59e0Spgoyette
2636f1de59e0Spgoyette	export RUMPKERN_ONLY=1
2637f1de59e0Spgoyette	# create obj and distrib dirs
26389e6ac23fSkre	if [ "${MKOBJDIRS}" != no ]
26399e6ac23fSkre	then
2640f1de59e0Spgoyette		make_in_dir "${NETBSDSRCDIR}/etc/mtree" obj
2641f1de59e0Spgoyette		make_in_dir "${NETBSDSRCDIR}/sys/rump" obj
2642f1de59e0Spgoyette	fi
2643f1de59e0Spgoyette	${runcmd} "${makewrapper}" ${parallel} do-distrib-dirs \
2644262db514Slukem	    || bomb "Could not create distrib-dirs"
2645f1de59e0Spgoyette
26469e6ac23fSkre	[ "${MKUPDATE}" = no ] && doclean="cleandir"
2647f1de59e0Spgoyette	targlist="${doclean} ${doobjs} dependall install"
26489e6ac23fSkre
2649f1de59e0Spgoyette	# optimize: for test we build only static libs (3x test speedup)
26509e6ac23fSkre	if [ "${1}" = rumptest ]
26519e6ac23fSkre	then
2652f1de59e0Spgoyette		setmakeenv NOPIC 1
2653f1de59e0Spgoyette		setmakeenv NOPROFILE 1
2654f1de59e0Spgoyette	fi
26559e6ac23fSkre
26569e6ac23fSkre	for cmd in ${targlist}
26579e6ac23fSkre	do
2658f1de59e0Spgoyette		make_in_dir "${NETBSDSRCDIR}/sys/rump" ${cmd}
2659f1de59e0Spgoyette	done
2660f1de59e0Spgoyette
2661f1de59e0Spgoyette	# if we just wanted to build & install rump, we're done
26629e6ac23fSkre	[ "${1}" != rumptest ] && return
2663f1de59e0Spgoyette
26649e6ac23fSkre	${runcmd} cd "${NETBSDSRCDIR}/sys/rump/librump/rumpkern" ||
26659e6ac23fSkre		bomb "cd to rumpkern failed"
26669e6ac23fSkre
2667f1de59e0Spgoyette	md_quirks=`${runcmd} "${makewrapper}" -V '${_SYMQUIRK}'`
2668f1de59e0Spgoyette	# one little, two little, three little backslashes ...
2669f1de59e0Spgoyette	md_quirks="$(echo ${md_quirks} | sed 's,\\,\\\\,g'";s/'//g" )"
2670f1de59e0Spgoyette	${runcmd} cd "${TOP}" || bomb "cd to ${TOP} failed"
2671f1de59e0Spgoyette	tool_ld=`${runcmd} "${makewrapper}" -V '${LD}'`
2672f1de59e0Spgoyette
2673e6351014Skre	local oIFS="${IFS-UnSeTT}"
2674f1de59e0Spgoyette	IFS=","
26759e6ac23fSkre	for set in ${RUMP_LIBSETS}
26769e6ac23fSkre	do
2677e6351014Skre		case "${oIFS}" in
2678e6351014Skre		UnSeTT)	unset IFS;;
2679e6351014Skre		*)	IFS="${oIFS}";;
2680e6351014Skre		esac
2681f1de59e0Spgoyette		${runcmd} ${tool_ld} -nostdlib -L${DESTDIR}/usr/lib	\
26829e6ac23fSkre		    -static --whole-archive ${set} --no-whole-archive   \
26839e6ac23fSkre		    -lpthread -lc 2>&1 -o /tmp/rumptest.$$ |
2684f1de59e0Spgoyette		      awk -v quirks="${md_quirks}" '
2685f1de59e0Spgoyette			/undefined reference/ &&
2686f1de59e0Spgoyette			    !/more undefined references.*follow/{
2687f1de59e0Spgoyette				if (match($NF,
2688f1de59e0Spgoyette				    "`(rumpuser_|rumpcomp_|__" quirks ")") == 0)
2689f1de59e0Spgoyette					fails[NR] = $0
2690f1de59e0Spgoyette			}
2691f1de59e0Spgoyette			/cannot find -l/{fails[NR] = $0}
2692f1de59e0Spgoyette			/cannot open output file/{fails[NR] = $0}
2693f1de59e0Spgoyette			END{
2694f1de59e0Spgoyette				for (x in fails)
2695f1de59e0Spgoyette					print fails[x]
2696f1de59e0Spgoyette				exit x!=0
2697f1de59e0Spgoyette			}'
2698f1de59e0Spgoyette		[ $? -ne 0 ] && bomb "Testlink of rump failed: ${set}"
2699f1de59e0Spgoyette	done
2700f1de59e0Spgoyette	statusmsg "Rump build&link tests successful"
2701f1de59e0Spgoyette}
2702f1de59e0Spgoyette
2703585ea105Schristosrepro_date() {
270450d90726Sandvar	# try the bsd date fail back the linux one
27059cdb453dSchristos	date -u -r "$1" 2> /dev/null || date -u -d "@$1"
2706585ea105Schristos}
2707585ea105Schristos
2708f1de59e0Spgoyettesetup_mkrepro()
2709f1de59e0Spgoyette{
271005937936Smartin	local quiet="$1"
271105937936Smartin
27129e6ac23fSkre	if [ "${MKREPRO-no}" != yes ]
27139e6ac23fSkre	then
2714f1de59e0Spgoyette		return
2715f1de59e0Spgoyette	fi
27169e6ac23fSkre	if [ "${MKREPRO_TIMESTAMP-0}" -ne 0 ]
27179e6ac23fSkre	then
27189b50f3d1Smartin		return
271905937936Smartin	fi
272047bc05a6Schristos
2721e6351014Skre	local dirs="${NETBSDSRCDIR-/usr/src}/"
27229e6ac23fSkre	if [ "${MKX11-no}" = yes ]
27239e6ac23fSkre	then
2724f1de59e0Spgoyette		dirs="$dirs ${X11SRCDIR-/usr/xsrc}/"
2725f1de59e0Spgoyette	fi
272647bc05a6Schristos
27270a7d8541Smartin	MKREPRO_TIMESTAMP=0
27289b50f3d1Smartin	NETBSD_REVISIONID=
27290a7d8541Smartin	local d
27300a7d8541Smartin	local t
27319b50f3d1Smartin	local tag
27320a7d8541Smartin	local vcs
27339e6ac23fSkre	for d in ${dirs}
27349e6ac23fSkre	do
27359e6ac23fSkre		if [ -d "${d}CVS" ]
27369e6ac23fSkre		then
2737e6351014Skre			local cvslatest="$(print_tooldir_program cvslatest)"
27389e6ac23fSkre			if ! [ -x "${cvslatest}" ]
27399e6ac23fSkre			then
2740f1de59e0Spgoyette				buildtools
2741f1de59e0Spgoyette			fi
2742e6351014Skre			local nbdate="$(print_tooldir_program date)"
274347bc05a6Schristos
2744b307b818Schristos			local cvslatestflags=
2745e6351014Skre			if "${do_expertmode}"
27469e6ac23fSkre			then
2747b307b818Schristos				cvslatestflags=-i
2748b307b818Schristos			fi
274947bc05a6Schristos
27509e6ac23fSkre			t=$("${cvslatest}" ${cvslatestflags} "${d}") ||
27519e6ac23fSkre				bomb "${cvslatest} failed"
27529e6ac23fSkre			if [ -f "${d}CVS/Tag" ]
27539e6ac23fSkre			then
27549b50f3d1Smartin				tag=$( sed 's/^T//' < "${d}CVS/Tag" )
27559b50f3d1Smartin			else
27569b50f3d1Smartin				tag=HEAD
27579b50f3d1Smartin			fi
27589e6ac23fSkre			NETBSD_REVISIONID="${tag}-$(
27599e6ac23fSkre				${nbdate} -u -r ${t} '+%Y%m%d%H%M%S')"
276047bc05a6Schristos			vcs=cvs
27619e6ac23fSkre		elif [ -d "${d}.git" ] || [ -f "${d}.git" ]
27629e6ac23fSkre		then
27639e6ac23fSkre			t=$(cd "${d}" && git log -1 --format=%ct) ||
27649e6ac23fSkre				bomb "git log %ct failed"
27659e6ac23fSkre			NETBSD_REVISIONID=$(
27669e6ac23fSkre			   cd "${d}" && git log -1 --format=%H) ||
27679e6ac23fSkre				bomb "git log %H failed"
276847bc05a6Schristos			vcs=git
27699e6ac23fSkre		elif [ -d "${d}.hg" ]
27709e6ac23fSkre		then
27719e6ac23fSkre			t=$(hg --repo "$d" \
27729e6ac23fSkre			    log -r . --template '{date.unixtime}\n') ||
27739e6ac23fSkre				bomb "hg log failed"
27749e6ac23fSkre			NETBSD_REVISIONID=$(hg --repo "$d" \
27759e6ac23fSkre			    identify --template '{id}\n') ||
27769e6ac23fSkre				bomb "hg identify failed"
277735651030Sjoerg			vcs=hg
27789e6ac23fSkre		elif [ -f "${d}.hg_archival.txt" ]
27799e6ac23fSkre		then
27809e6ac23fSkre			local stat
27819e6ac23fSkre			stat=$(print_tooldir_program stat) ||
27829e6ac23fSkre				bomb "print_tooldir_program stat failed"
27839e6ac23fSkre			if ! [ -x "${stat}" ]
27849e6ac23fSkre			then
27850a7d8541Smartin				buildtools
27860a7d8541Smartin			fi
27870a7d8541Smartin
27889e6ac23fSkre			t=$("${stat}" -t '%s' -f '%m' "${d}.hg_archival.txt") ||
27899e6ac23fSkre				bomb "stat failed on ${d}.hg_archival.txt"
27909e6ac23fSkre			NETBSD_REVISIONID=$(
27919e6ac23fSkre			    awk '/^node:/ { print $2 }' <"${d}.hg_archival.txt"
27929e6ac23fSkre			  ) || bomb \
27939e6ac23fSkre			      "awk failed to find node: in ${d}.hg_archival.txt"
279447bc05a6Schristos			vcs=hg
279547bc05a6Schristos		else
279647bc05a6Schristos			bomb "Cannot determine VCS for '$d'"
279747bc05a6Schristos		fi
279847bc05a6Schristos
27999e6ac23fSkre		if [ -z "$t" ]
28009e6ac23fSkre		then
280147bc05a6Schristos			bomb "Failed to get timestamp for vcs=$vcs in '$d'"
280247bc05a6Schristos		fi
280347bc05a6Schristos
280447bc05a6Schristos		#echo "latest $d $vcs $t"
28059e6ac23fSkre		if [ "$t" -gt "$MKREPRO_TIMESTAMP" ]
28069e6ac23fSkre		then
280747bc05a6Schristos			MKREPRO_TIMESTAMP="$t"
280847bc05a6Schristos		fi
280947bc05a6Schristos	done
281047bc05a6Schristos
28112ac6a765Smartin	[ "${MKREPRO_TIMESTAMP}" -ne 0 ] || bomb "Failed to compute timestamp"
28129e6ac23fSkre	if [ -z "${quiet}" ]
28139e6ac23fSkre	then
281405937936Smartin		statusmsg2 "MKREPRO_TIMESTAMP" \
281505937936Smartin			"$(repro_date "${MKREPRO_TIMESTAMP}")"
281605937936Smartin	fi
28179b50f3d1Smartin	export MKREPRO MKREPRO_TIMESTAMP NETBSD_REVISIONID
2818f1de59e0Spgoyette}
2819f1de59e0Spgoyette
2820f1de59e0Spgoyettemain()
2821f1de59e0Spgoyette{
2822f1de59e0Spgoyette	initdefaults
28239e6ac23fSkre	_args=$*
2824f1de59e0Spgoyette	parseoptions "$@"
2825f1de59e0Spgoyette
2826f1de59e0Spgoyette	sanitycheck
2827f1de59e0Spgoyette
2828f1de59e0Spgoyette	build_start=$(date)
2829f1de59e0Spgoyette	statusmsg2 "${progname} command:" "$0 $*"
2830f1de59e0Spgoyette	statusmsg2 "${progname} started:" "${build_start}"
2831f1de59e0Spgoyette	statusmsg2 "NetBSD version:"   "${DISTRIBVER}"
2832f1de59e0Spgoyette	statusmsg2 "MACHINE:"          "${MACHINE}"
2833f1de59e0Spgoyette	statusmsg2 "MACHINE_ARCH:"     "${MACHINE_ARCH}"
2834f1de59e0Spgoyette	statusmsg2 "Build platform:"   "${uname_s} ${uname_r} ${uname_m}"
2835f1de59e0Spgoyette	statusmsg2 "HOST_SH:"          "${HOST_SH}"
28369e6ac23fSkre	if [ -n "${BUILDID}" ]
28379e6ac23fSkre	then
28389e6ac23fSkre		statusmsg2 BUILDID: "${BUILDID}"
2839f1de59e0Spgoyette	fi
28409e6ac23fSkre	if [ -n "${BUILDINFO}" ]
28419e6ac23fSkre	then
28429e6ac23fSkre		printf "%b\n" "${BUILDINFO}" |
28439e6ac23fSkre		while read -r line
28449e6ac23fSkre		do
2845f1de59e0Spgoyette			[ -s "${line}" ] && continue
28469e6ac23fSkre			statusmsg2 BUILDINFO: "${line}"
2847f1de59e0Spgoyette		done
2848f1de59e0Spgoyette	fi
2849f1de59e0Spgoyette
28509e6ac23fSkre	if [ -n "${MAKECONF+1}" ] && [ -z "${MAKECONF}" ]
28519e6ac23fSkre	then
28523cb563b3Slukem		bomb "MAKECONF must not be empty"
28533cb563b3Slukem	fi
28543cb563b3Slukem
2855f1de59e0Spgoyette	rebuildmake
2856f1de59e0Spgoyette	validatemakeparams
2857f1de59e0Spgoyette	createmakewrapper
2858f1de59e0Spgoyette	setup_mkrepro
2859f1de59e0Spgoyette
2860f1de59e0Spgoyette	# Perform the operations.
2861f1de59e0Spgoyette	#
28629e6ac23fSkre	for op in ${operations}
28639e6ac23fSkre	do
2864f1de59e0Spgoyette		case "${op}" in
2865f1de59e0Spgoyette
2866f1de59e0Spgoyette		makewrapper)
2867f1de59e0Spgoyette			# no-op
2868f1de59e0Spgoyette			;;
2869f1de59e0Spgoyette
2870f1de59e0Spgoyette		tools)
2871f1de59e0Spgoyette			buildtools
2872f1de59e0Spgoyette			;;
28730a37cd5eSchristos		libs)
28740a37cd5eSchristos			buildlibs
28750a37cd5eSchristos			;;
2876f1de59e0Spgoyette
2877f1de59e0Spgoyette		sets)
2878f1de59e0Spgoyette			statusmsg "Building sets from pre-populated ${DESTDIR}"
2879f1de59e0Spgoyette			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2880f1de59e0Spgoyette			    bomb "Failed to make ${op}"
2881f1de59e0Spgoyette			setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
2882f1de59e0Spgoyette			statusmsg "Built sets to ${setdir}"
2883f1de59e0Spgoyette			;;
2884f1de59e0Spgoyette
2885f1de59e0Spgoyette		build|distribution|release)
2886f1de59e0Spgoyette			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2887f1de59e0Spgoyette			    bomb "Failed to make ${op}"
2888f1de59e0Spgoyette			statusmsg "Successful make ${op}"
2889f1de59e0Spgoyette			;;
2890f1de59e0Spgoyette
2891439f74efSlukem		cleandir|obj|sourcesets|syspkgs|params|show-params)
2892f1de59e0Spgoyette			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2893f1de59e0Spgoyette			    bomb "Failed to make ${op}"
2894f1de59e0Spgoyette			statusmsg "Successful make ${op}"
2895f1de59e0Spgoyette			;;
2896f1de59e0Spgoyette
2897f1de59e0Spgoyette		iso-image|iso-image-source)
2898f1de59e0Spgoyette			${runcmd} "${makewrapper}" ${parallel} \
2899f1de59e0Spgoyette			    CDEXTRA="$CDEXTRA" ${op} ||
2900f1de59e0Spgoyette				bomb "Failed to make ${op}"
2901f1de59e0Spgoyette			statusmsg "Successful make ${op}"
2902f1de59e0Spgoyette			;;
2903f1de59e0Spgoyette
2904f1de59e0Spgoyette		live-image|install-image)
2905f1de59e0Spgoyette			# install-image and live-image require mtree spec files
29069e6ac23fSkre			# built with MKUNPRIVED.  Assume MKUNPRIVED build has
29079e6ac23fSkre			# been performed if METALOG file is created in DESTDIR.
29089e6ac23fSkre			if [ ! -e "${DESTDIR}/METALOG" ]
29099e6ac23fSkre			then
29109e6ac23fSkre				bomb "The release binaries must have been" \
29119e6ac23fSkre				     "built with -U to create images"
2912f1de59e0Spgoyette			fi
2913f1de59e0Spgoyette			${runcmd} "${makewrapper}" ${parallel} ${op} ||
2914f1de59e0Spgoyette			    bomb "Failed to make ${op}"
2915f1de59e0Spgoyette			statusmsg "Successful make ${op}"
2916f1de59e0Spgoyette			;;
2917f1de59e0Spgoyette		kernel=*)
2918f1de59e0Spgoyette			arg=${op#*=}
2919f1de59e0Spgoyette			buildkernel "${arg}"
2920f1de59e0Spgoyette			;;
2921f1de59e0Spgoyette		kernel.gdb=*)
2922f1de59e0Spgoyette			arg=${op#*=}
2923f1de59e0Spgoyette			configopts="-D DEBUG=-g"
2924f1de59e0Spgoyette			buildkernel "${arg}"
2925f1de59e0Spgoyette			;;
2926f1de59e0Spgoyette		releasekernel=*)
2927f1de59e0Spgoyette			arg=${op#*=}
2928f1de59e0Spgoyette			releasekernel "${arg}"
2929f1de59e0Spgoyette			;;
2930f1de59e0Spgoyette
2931f1de59e0Spgoyette		kernels)
2932f1de59e0Spgoyette			buildkernels
2933f1de59e0Spgoyette			;;
2934f1de59e0Spgoyette
2935f1de59e0Spgoyette		disk-image=*)
2936f1de59e0Spgoyette			arg=${op#*=}
2937f1de59e0Spgoyette			diskimage "${arg}"
2938f1de59e0Spgoyette			;;
2939f1de59e0Spgoyette
2940043d88a7Sjmcneill		dtb)
2941043d88a7Sjmcneill			builddtb
2942043d88a7Sjmcneill			;;
2943043d88a7Sjmcneill
2944f1de59e0Spgoyette		modules)
2945f1de59e0Spgoyette			buildmodules
2946f1de59e0Spgoyette			;;
2947f1de59e0Spgoyette
294859040e53Sriastradh		pkg=*)
294959040e53Sriastradh			arg=${op#*=}
29509e6ac23fSkre			if ! [ -d "${PKGSRCDIR}/${arg}" ]
29519e6ac23fSkre			then
295259040e53Sriastradh				bomb "no such package ${arg}"
295359040e53Sriastradh			fi
295459040e53Sriastradh			buildpkg "${arg}"
295559040e53Sriastradh			;;
295659040e53Sriastradh
2957f1de59e0Spgoyette		installmodules=*)
2958f1de59e0Spgoyette			arg=${op#*=}
29599e6ac23fSkre			if [ "${arg}" = / ] && {
29609e6ac23fSkre				[ "${uname_s}" != NetBSD ] ||
29619e6ac23fSkre				[ "${uname_m}" != "${MACHINE}" ]
29629e6ac23fSkre			    }
29639e6ac23fSkre			then
2964262db514Slukem				bomb "'${op}' must != / for cross builds"
2965f1de59e0Spgoyette			fi
2966f1de59e0Spgoyette			installmodules "${arg}"
2967f1de59e0Spgoyette			;;
2968f1de59e0Spgoyette
2969f1de59e0Spgoyette		install=*)
2970f1de59e0Spgoyette			arg=${op#*=}
29719e6ac23fSkre			if [ "${arg}" = / ] && {
29729e6ac23fSkre				[ "${uname_s}" != NetBSD ] ||
29739e6ac23fSkre				[ "${uname_m}" != "${MACHINE}" ]
29749e6ac23fSkre			   }
29759e6ac23fSkre			then
2976262db514Slukem				bomb "'${op}' must != / for cross builds"
2977f1de59e0Spgoyette			fi
2978f1de59e0Spgoyette			installworld "${arg}"
2979f1de59e0Spgoyette			;;
2980f1de59e0Spgoyette
2981dfff0eceSchristos		rump)
2982dfff0eceSchristos			make_in_dir . do-distrib-dirs
2983dfff0eceSchristos			make_in_dir . includes
2984dfff0eceSchristos			make_in_dir lib/csu dependall
2985dfff0eceSchristos			make_in_dir lib/csu install
2986dfff0eceSchristos			make_in_dir external/gpl3/gcc/lib/libgcc dependall
2987dfff0eceSchristos			make_in_dir external/gpl3/gcc/lib/libgcc install
2988dfff0eceSchristos			dorump "${op}"
2989dfff0eceSchristos			;;
2990dfff0eceSchristos
2991dfff0eceSchristos		rumptest)
2992f1de59e0Spgoyette			dorump "${op}"
2993f1de59e0Spgoyette			;;
2994f1de59e0Spgoyette
2995f1de59e0Spgoyette		*)
299657544d3eSlukem			bomb "Unknown OPERATION '${op}'"
2997f1de59e0Spgoyette			;;
2998f1de59e0Spgoyette
2999f1de59e0Spgoyette		esac
3000f1de59e0Spgoyette	done
3001f1de59e0Spgoyette
3002f1de59e0Spgoyette	statusmsg2 "${progname} ended:" "$(date)"
30039e6ac23fSkre	if [ -s "${results}" ]
30049e6ac23fSkre	then
3005f1de59e0Spgoyette		echo "===> Summary of results:"
3006f1de59e0Spgoyette		sed -e 's/^===>//;s/^/	/' "${results}"
3007f1de59e0Spgoyette		echo "===> ."
3008f1de59e0Spgoyette	fi
3009f1de59e0Spgoyette}
3010f1de59e0Spgoyette
3011f1de59e0Spgoyettemain "$@"
3012