build.sh revision 1.55
1#! /bin/sh
2#  $NetBSD: build.sh,v 1.55 2002/04/02 16:34:49 thorpej Exp $
3#
4# Top level build wrapper, for a system containing no tools.
5#
6# This script should run on any POSIX-compliant shell.  For systems
7# with a strange /bin/sh, "ksh" or "bash" may be an ample alternative.
8#
9
10bomb () {
11	echo ""
12	echo "ERROR: $@"
13	echo "*** BUILD ABORTED ***"
14	exit 1
15}
16[ -d usr.bin/make ] || bomb "build.sh must be run from the top source level"
17
18TOP=`pwd`
19
20getarch () {
21	# Translate a MACHINE into a default MACHINE_ARCH.
22	case $MACHINE in
23		acorn26|acorn32|cats|netwinder|shark|*arm)
24			MACHINE_ARCH=arm;;
25
26		sun2)
27			MACHINE_ARCH=m68000;;
28
29		amiga|atari|cesfic|hp300|sun3|*68k)
30			MACHINE_ARCH=m68k;;
31
32		mipsco|newsmips|sbmips|sgimips)
33			MACHINE_ARCH=mipseb;;
34
35		algor|arc|cobalt|evbmips|hpcmips|playstation2|pmax)
36			MACHINE_ARCH=mipsel;;
37
38		pc532)
39			MACHINE_ARCH=ns32k;;
40
41		bebox|mvmeppc|prep|sandpoint|walnut|*ppc)
42			MACHINE_ARCH=powerpc;;
43
44		evbsh3|mmeye)
45			MACHINE_ARCH=sh3eb;;
46
47		dreamcast|hpcsh)
48			MACHINE_ARCH=sh3el;;
49
50		alpha|i386|sparc|sparc64|vax|x86_64)
51			MACHINE_ARCH=$MACHINE;;
52
53		*)	bomb "unknown target MACHINE: $MACHINE";;
54	esac
55}
56
57validatearch () {
58	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
59	case $MACHINE_ARCH in
60		alpha|arm|i386|m68000|m68k|mipse[bl]|powerpc|sh3e[bl]|sparc|sparc64|vax|x86_64)
61			;;
62
63		*)	bomb "unknown target MACHINE_ARCH: $MACHINE_ARCH";;
64	esac
65}
66
67getmakevar () {
68	$make -m ${TOP}/share/mk -s -f- _x_ <<EOF
69_x_:
70	echo \${$1}
71.include <bsd.prog.mk>
72EOF
73}
74
75resolvepath () {
76	case $OPTARG in
77	/*)	;;
78	*)	OPTARG="$cwd/$OPTARG";;
79	esac
80}
81
82usage () {
83	echo "Usage:"
84	echo "$0 [-bdorUu] [-a arch] [-B buildid] [-j njob] [-m mach] "
85	echo "   [-w wrapper] [-D dest] [-O obj] [-R release] [-T tools]"
86	echo ""
87	echo "    -a: set MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
88	echo "    -B: set BUILDID to buildid"
89	echo "    -b: build nbmake and nbmake wrapper script, if needed"
90	echo "    -D: set DESTDIR to dest"
91	echo "    -d: build a full distribution into DESTDIR (including etc files)"
92	echo "    -j: set NBUILDJOBS to njob"
93	echo "    -m: set MACHINE to mach (not required if NetBSD native)"
94	echo "    -n: show commands that would be executed, but do not execute them"
95	echo "    -O: set obj root directory to obj (sets a MAKEOBJDIR pattern)"
96	echo "    -o: set MKOBJDIRS=no (do not create objdirs at start of build)"
97	echo "    -R: build a release (and set RELEASEDIR to release)"
98	echo "    -r: remove contents of TOOLDIR and DESTDIR before building"
99	echo "    -T: set TOOLDIR to tools"
100	echo "    -t: build and install tools only (implies -b)"
101	echo "    -U: set UNPRIVED"
102	echo "    -u: set UPDATE"
103	echo "    -w: create nbmake script at wrapper (default TOOLDIR/bin/nbmake-MACHINE)"
104	echo ""
105	echo "Note: if -T is unset and TOOLDIR is not set in the environment,"
106	echo "      nbmake will be [re]built unconditionally."
107	exit 1
108}
109
110# Set defaults.
111MAKEFLAGS=
112buildtarget=build
113cwd=`pwd`
114do_buildsystem=true
115do_buildonlytools=false
116do_rebuildmake=false
117do_removedirs=false
118makeenv=
119makewrapper=
120opt_a=no
121opts='a:B:bdhj:m:nortuw:D:O:R:T:U'
122runcmd=
123
124if type getopts >/dev/null 2>&1; then
125	# Use POSIX getopts.
126	getoptcmd='getopts $opts opt && opt=-$opt'
127	optargcmd=':'
128else
129	type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh or bash"
130
131	# Use old-style getopt(1) (doesn't handle whitespace in args).
132	args="`getopt $opts $*`"
133	[ $? = 0 ] || usage
134	set -- $args
135
136	getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
137	optargcmd='OPTARG="$1"; shift'
138fi
139
140# Parse command line options.
141while eval $getoptcmd; do case $opt in
142	-a)	eval $optargcmd
143		MACHINE_ARCH=$OPTARG; opt_a=yes;;
144
145	-B)	eval $optargcmd
146		BUILDID=$OPTARG;;
147
148	-b)	do_buildsystem=false;;
149
150	-d)	buildtarget=distribution;;
151
152	-j)	eval $optargcmd
153		MAKEFLAGS="$MAKEFLAGS NBUILDJOBS=$OPTARG"; export MAKEFLAGS;;
154
155	# -m overrides MACHINE_ARCH unless "-a" is specified
156	-m)	eval $optargcmd
157		MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
158
159	-n)	runcmd=echo;;
160
161	-o)	MKOBJDIRS=no;;
162
163	-r)	do_removedirs=true; do_rebuildmake=true;;
164
165	-t)	do_buildonlytools=true; do_buildsystem=false;;
166
167	-U)	UNPRIVED=yes; export UNPRIVED
168		makeenv="$makeenv UNPRIVED";;
169
170	-u)	UPDATE=yes; export UPDATE
171		makeenv="$makeenv UPDATE";;
172
173	-w)	eval $optargcmd; resolvepath
174		makewrapper="$OPTARG";;
175
176	-D)	eval $optargcmd; resolvepath
177		DESTDIR="$OPTARG"; export DESTDIR
178		makeenv="$makeenv DESTDIR";;
179
180	-O)	eval $optargcmd; resolvepath
181		MAKEOBJDIR="\${.CURDIR:C,^$cwd,$OPTARG,}"; export MAKEOBJDIR
182		makeobjdir=$OPTARG
183		makeenv="$makeenv MAKEOBJDIR";;
184
185	-R)	eval $optargcmd; resolvepath
186		RELEASEDIR=$OPTARG; export RELEASEDIR
187		makeenv="$makeenv RELEASEDIR"
188		buildtarget=release;;
189
190	-T)	eval $optargcmd; resolvepath
191		TOOLDIR="$OPTARG"; export TOOLDIR;;
192
193	--)		break;;
194	-'?'|-h)	usage;;
195esac; done
196
197# Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
198if [ -z "$MACHINE" ]; then
199	if [ "`uname -s 2>/dev/null`" != "NetBSD" ]; then
200		echo "MACHINE must be set, or -m must be used, for cross builds."
201		echo ""; usage
202	fi
203	MACHINE=`uname -m`
204fi
205[ -n "$MACHINE_ARCH" ] || getarch
206validatearch
207
208# Set up default make(1) environment.
209makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
210if [ ! -z "$BUILDID" ]; then
211	makeenv="$makeenv BUILDID"
212fi
213MAKEFLAGS="-m $cwd/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
214export MAKEFLAGS MACHINE MACHINE_ARCH
215
216# Test make source file timestamps against installed nbmake binary,
217# if TOOLDIR is pre-set.
218#
219# Note that we do NOT try to grovel "mk.conf" here to find out if TOOLDIR
220# is set there, because it can contain make variable expansions and other
221# stuff only parseable *after* we have a working nbmake.  So this logic
222# can only work if the user has pre-set TOOLDIR in the environment or
223# used the -T option to build.sh.
224#
225make="${TOOLDIR-nonexistent}/bin/nbmake"
226if [ -x $make ]; then
227	for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
228		if [ $f -nt $make ]; then
229			do_rebuildmake=true; break
230		fi
231	done
232else
233	do_rebuildmake=true
234fi
235
236# Build bootstrap nbmake if needed.
237if $do_rebuildmake; then
238	$runcmd echo "===> Bootstrapping nbmake"
239	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
240
241	$runcmd mkdir "$tmpdir" || bomb "cannot mkdir: $tmpdir"
242	trap "cd /; rm -r -f \"$tmpdir\"" 0
243	trap "exit 1" 1 2 3 15
244	$runcmd cd "$tmpdir"
245
246	$runcmd env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
247		"$cwd/tools/make/configure" || bomb "configure of nbmake failed"
248	$runcmd sh buildmake.sh || bomb "build of nbmake failed"
249
250	make="$tmpdir/nbmake"
251	$runcmd cd "$cwd"
252	$runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
253fi
254
255if [ "$runcmd" = "echo" ]; then
256	USE_NEW_TOOLCHAIN=yes
257else
258	USE_NEW_TOOLCHAIN=`getmakevar USE_NEW_TOOLCHAIN`
259fi
260if [ "${USE_NEW_TOOLCHAIN}" = "" ]; then
261	echo "ERROR: build.sh (new toolchain) is not yet enabled for"
262	echo
263	echo "MACHINE: ${MACHINE}"
264	echo "MACHINE_ARCH: ${MACHINE_ARCH}"
265	echo
266	echo "All builds for this platform should be done via a traditional make"
267	echo
268	echo "If you wish to test using the new toolchain set"
269	echo
270	echo "USE_NEW_TOOLCHAIN=yes"
271	echo
272	echo "in either the environment or mk.conf and rerun"
273	echo
274	echo "$0 $*"
275	exit 1
276fi
277
278# If TOOLDIR isn't already set, make objdirs in "tools" in case the
279# default setting from <bsd.own.mk> is used.
280if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
281	$runcmd cd tools
282	$runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= || exit 1
283	$runcmd cd ..
284fi
285
286#
287# If setting -O to root an obj dir make sure the base directory is made
288# before continuing as bsd.own.mk will need this to pick up _SRC_TOP_OBJ_
289#
290if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
291	$runcmd mkdir -p "$makeobjdir"
292fi
293
294# Find DESTDIR and TOOLDIR.
295if [ "$runcmd" = "echo" ]; then
296	# shown symbolically with -n because these may come from mk.conf
297	DESTDIR='$DESTDIR'
298	TOOLDIR='$TOOLDIR'
299else
300	DESTDIR=`getmakevar DESTDIR`;
301	[ $? = 0 ] || bomb "getmakevar DESTDIR failed";
302	echo "===> DESTDIR path: $DESTDIR"
303
304	TOOLDIR=`getmakevar TOOLDIR`;
305	[ $? = 0 ] || bomb "getmakevar DESTDIR failed";
306	echo "===> TOOLDIR path: $TOOLDIR"
307
308	export DESTDIR TOOLDIR
309fi
310
311# Check validity of TOOLDIR and DESTDIR.
312if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
313	bomb "TOOLDIR '$TOOLDIR' invalid"
314fi
315removedirs="$TOOLDIR"
316
317if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
318	if $do_buildsystem && \
319	   ([ "$buildtarget" != "build" ] || \
320	    [ "`uname -s 2>/dev/null`" != "NetBSD" ] || \
321	    [ "`uname -m`" != "$MACHINE" ]); then
322		bomb "DESTDIR must be set to a non-root path for cross builds or -d or -R."
323	elif $do_buildsystem; then
324		echo "===> WARNING: Building to /."
325		echo "===> If your kernel is not up to date, this may cause the system to break!"
326	fi
327else
328	removedirs="$removedirs $DESTDIR"
329fi
330
331# Remove the target directories.
332if $do_removedirs; then
333	for f in $removedirs; do
334		echo "===> Removing $f"
335		$runcmd rm -r -f $f
336	done
337fi
338
339# Recreate $TOOLDIR.
340$runcmd mkdir -p "$TOOLDIR/bin" || bomb "mkdir of '$TOOLDIR/bin' failed"
341
342# Install nbmake if it was built.
343if $do_rebuildmake; then
344	$runcmd rm -f "$TOOLDIR/bin/nbmake"
345	$runcmd cp $make "$TOOLDIR/bin/nbmake"
346	$runcmd rm -r -f "$tmpdir"
347	trap 0 1 2 3 15
348fi
349
350# Build a nbmake wrapper script, usable by hand as well as by build.sh.
351if [ -z "$makewrapper" ]; then
352	makewrapper="$TOOLDIR/bin/nbmake-$MACHINE"
353	if [ ! -z "$BUILDID" ]; then
354		makewrapper="$makewrapper-$BUILDID"
355	fi
356fi
357
358$runcmd rm -f "$makewrapper"
359if [ "$runcmd" = "echo" ]; then
360	echo 'cat <<EOF >'$makewrapper
361	makewrapout=
362else
363	makewrapout=">>\$makewrapper"
364fi
365
366eval cat <<EOF $makewrapout
367#! /bin/sh
368# Set proper variables to allow easy "make" building of a NetBSD subtree.
369# Generated from:  \$NetBSD: build.sh,v 1.55 2002/04/02 16:34:49 thorpej Exp $
370#
371
372EOF
373for f in $makeenv; do
374	eval echo "$f=\'\$`echo $f`\'\;\ export\ $f" $makewrapout
375done
376eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
377
378eval cat <<'EOF' $makewrapout
379
380exec "$TOOLDIR/bin/nbmake" ${1+"$@"}
381EOF
382[ "$runcmd" = "echo" ] && echo EOF
383$runcmd chmod +x "$makewrapper"
384
385if $do_buildsystem; then
386	${runcmd-exec} "$makewrapper" $buildtarget
387elif $do_buildonlytools; then
388	if [ "$MKOBJDIRS" != "no" ]; then
389		$runcmd "$makewrapper" obj-tools || exit 1
390	fi
391	$runcmd cd tools
392	if [ "$UPDATE" = "" ]; then
393		${runcmd-exec} "$makewrapper" cleandir dependall install
394	else
395		${runcmd-exec} "$makewrapper" dependall install
396	fi
397fi
398