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