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