build.sh revision 1.8
1#! /bin/sh
2#  $NetBSD: build.sh,v 1.8 2001/10/29 19:47:51 tv 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" may be an ample alternative.
8#
9
10bomb () {
11	echo ""
12	echo "ERROR: $@"
13	echo "*** BUILD ABORTED ***"
14	exit 1
15}
16
17getarch () {
18	# Translate a MACHINE into a default MACHINE_ARCH.
19	case $MACHINE in
20		arm26|dnard|evbarm|hpcarm|netwinder)
21			MACHINE_ARCH=arm;;
22
23		acorn32|arm32|cats)
24			MACHINE_ARCH=arm32;;
25
26		sun2)
27			MACHINE_ARCH=m68000;;
28
29		amiga|atari|cesfic|hp300|sun3|*68k)
30			MACHINE_ARCH=m68k;;
31
32		mipsco|newsmips|sgimips)
33			MACHINE_ARCH=mipseb;;
34
35		algor|arc|cobalt|hpcmips|playstation2|pmax)
36			MACHINE_ARCH=mipsel;;
37
38		pc532)
39			MACHINE_ARCH=ns32k;;
40
41		bebox|prep|sandpoint|walnut|*ppc)
42			MACHINE_ARCH=powerpc;;
43
44		mmeye)
45			MACHINE_ARCH=sh3eb;;
46
47		dreamcast|evbsh3|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
57# Emulate "mkdir -p" for systems that have an Old "mkdir".
58mkdirp () {
59	IFS=/; set -- $@; unset IFS
60	_d=
61	if [ "$1" = "" ]; then _d=/; shift; fi
62
63	for _f in "$@"; do
64		if [ "$_f" != "" ]; then
65			[ -d "$_d$_f" ] || mkdir "$_d$_f" || return 1
66			_d="$_d$_f/"
67		fi
68	done
69}
70
71usage () {
72	echo "Usage:"
73	echo "$0 [-r] [-a arch] [-j njob] [-m mach] [-D dest] [-R release] [-T tools]"
74	echo "    -m: set target MACHINE to mach (REQUIRED, or set in environment)"
75	echo "    -D: set DESTDIR to dest (REQUIRED, or set in environment)"
76	echo "    -T: set TOOLDIR to tools (REQUIRED, or set in environment)"
77	echo ""
78	echo "    -a: set target MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
79	echo "    -j: set NBUILDJOBS to njob"
80	echo "    -r: remove TOOLDIR and DESTDIR before the build"
81	echo "    -R: build a release (set RELEASEDIR) to release"
82	exit 1
83}
84
85opts='a:hj:m:rD:R:T:'
86
87if type getopts >/dev/null 2>&1; then
88	# Use POSIX getopts.
89	getoptcmd='getopts $opts opt && opt=-$opt'
90	optargcmd=':'
91else
92	type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh"
93
94	# Use old-style getopt(1) (doesn't handle whitespace in args).
95	args="`getopt $opts $*`"
96	[ $? = 0 ] || usage
97	set -- $args
98
99	getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
100	optargcmd='OPTARG="$1"; shift'
101fi
102	
103opt_a=no
104while eval $getoptcmd; do case $opt in
105	-a)	eval $optargcmd
106		MACHINE_ARCH=$OPTARG; opt_a=yes;;
107
108	-j)	eval $optargcmd
109		buildjobs="NBUILDJOBS=$OPTARG";;
110
111	# -m overrides MACHINE_ARCH unless "-a" is specified
112	-m)	eval $optargcmd
113		MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
114
115	-r)	removedirs=true;;
116
117	-D)	eval $optargcmd
118		DESTDIR="$OPTARG";;
119
120	-R)	eval $optargcmd
121		releasedir="RELEASEDIR=$OPTARG"; buildtarget=release;;
122
123	-T)	eval $optargcmd
124		TOOLDIR="$OPTARG";;
125
126	--)		break;;
127	-'?'|-h)	usage;;
128esac; done
129
130for var in MACHINE DESTDIR TOOLDIR; do
131	if ! eval 'test -n "$'$var'"'; then
132		echo "$var must be set in the environment before running build.sh."
133		echo ""; usage
134	fi
135done
136
137# Set up environment.
138test -n "$MACHINE_ARCH" || getarch
139test -d usr.bin/make || bomb "build.sh must be run from the top source level"
140
141# Remove the target directories.
142if ${removedirs-false}; then
143	echo "Removing DESTDIR and TOOLDIR...."
144	rm -rf $DESTDIR $TOOLDIR
145fi
146
147mkdirp $TOOLDIR/bin || bomb "mkdir of $TOOLDIR/bin failed"
148
149# Test make source file timestamps against installed nbmake binary.
150if [ -x $TOOLDIR/bin/nbmake ]; then
151	for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
152		if [ $f -nt $TOOLDIR/bin/nbmake ]; then
153			rebuildmake=true; break
154		fi
155	done
156else
157	rebuildmake=true
158fi
159
160# Build $TOOLDIR/bin/nbmake.
161if ${rebuildmake-false}; then
162	echo "Building nbmake...."
163
164	# Go to a temporary directory in case building .o's happens.
165	srcdir=`pwd`
166	tmpdir=${TMPDIR-/tmp}/nbbuild$$
167
168	mkdir $tmpdir || bomb "cannot mkdir: $tmpdir"
169	trap "rm -r -f $tmpdir" 0
170	trap "exit 1" 1 2 3 15
171	cd $tmpdir
172
173	${HOST_CC-cc} ${HOST_CFLAGS} -DMAKE_BOOTSTRAP \
174		-o $TOOLDIR/bin/nbmake -I$srcdir/usr.bin/make \
175		$srcdir/usr.bin/make/*.c $srcdir/usr.bin/make/lst.lib/*.c \
176		|| bomb "build of nbmake failed"
177
178	# Clean up.
179	cd $srcdir
180	rm -r -f $tmpdir
181	trap 0 1 2 3 15
182
183	# Some compilers are just *that* braindead.
184	rm -f $srcdir/usr.bin/make/*.o $srcdir/usr.bin/make/lst.lib/*.o
185fi
186
187# Build a nbmake wrapper script, usable by hand as well as by build.sh.
188makeprog=$TOOLDIR/bin/nbmake-$MACHINE
189
190if ${rebuildmake-false} || [ ! -f $makeprog ] || [ $makeprog -ot build.sh ]; then
191	rm -f $makeprog
192	cat >$makeprog <<EOF
193#! /bin/sh
194# Set proper variables to allow easy "make" building of a NetBSD subtree.
195# Generated from:  \$NetBSD: build.sh,v 1.8 2001/10/29 19:47:51 tv Exp $
196#
197exec $TOOLDIR/bin/nbmake MACHINE=$MACHINE MACHINE_ARCH=$MACHINE_ARCH \
198USETOOLS=yes USE_NEW_TOOLCHAIN=yes TOOLDIR="$TOOLDIR" \${1+\$@}
199EOF
200	chmod +x $makeprog
201fi
202
203exec $makeprog -m `pwd`/share/mk ${buildtarget-build} \
204	MKTOOLS=yes DESTDIR="$DESTDIR" TOOLDIR="$TOOLDIR" \
205	$buildjobs $releasedir
206