build.sh revision 1.11
1#! /bin/sh
2#  $NetBSD: build.sh,v 1.11 2001/10/31 00:08:03 reinoud 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|acorn32|dnard|evbarm|hpcarm|netwinder)
21			MACHINE_ARCH=arm;;
22
23		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" ] || $runcmd mkdir "$_d$_f" || return 1
66			_d="$_d$_f/"
67		fi
68	done
69}
70
71resolvepath () {
72	case $OPTARG in
73	/*)	;;
74	*)	OPTARG="$cwd/$OPTARG";;
75	esac
76}
77
78usage () {
79	echo "Usage:"
80	echo "$0 [-r] [-a arch] [-j njob] [-m mach] [-D dest] [-R release] [-T tools]"
81	echo "    -m: set target MACHINE to mach (REQUIRED)"
82	echo "    -D: set DESTDIR to dest (REQUIRED unless -b is specified)"
83	echo "    -T: set TOOLDIR to tools (REQUIRED)"
84	echo ""
85	echo "    -a: set target MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
86	echo "    -b: do not build the system; just build nbmake if required."
87	echo "    -j: set NBUILDJOBS to njob"
88	echo "    -n: show the commands that would be executed, but do not execute them"
89	echo "    -r: remove TOOLDIR and DESTDIR before the build"
90	echo "    -R: build a release (set RELEASEDIR) to release"
91	exit 1
92}
93
94# Set defaults.
95cwd=`pwd`
96do_buildsystem=true
97do_rebuildmake=false
98do_removedirs=false
99opt_a=no
100opts='a:bhj:m:nrD:R:T:'
101runcmd=''
102
103if type getopts >/dev/null 2>&1; then
104	# Use POSIX getopts.
105	getoptcmd='getopts $opts opt && opt=-$opt'
106	optargcmd=':'
107else
108	type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh"
109
110	# Use old-style getopt(1) (doesn't handle whitespace in args).
111	args="`getopt $opts $*`"
112	[ $? = 0 ] || usage
113	set -- $args
114
115	getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
116	optargcmd='OPTARG="$1"; shift'
117fi
118
119# Parse command line options.
120while eval $getoptcmd; do case $opt in
121	-a)	eval $optargcmd
122		MACHINE_ARCH=$OPTARG; opt_a=yes;;
123
124	-b)	do_buildsystem=false;;
125
126	-j)	eval $optargcmd
127		buildjobflag="NBUILDJOBS=$OPTARG";;
128
129	# -m overrides MACHINE_ARCH unless "-a" is specified
130	-m)	eval $optargcmd
131		MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
132
133	-n)	runcmd=echo;;
134
135	-r)	do_removedirs=true; do_rebuildmake=true;;
136
137	-D)	eval $optargcmd; resolvepath
138		DESTDIR="$OPTARG";;
139
140	-R)	eval $optargcmd; resolvepath
141		releasedirflag="RELEASEDIR=$OPTARG"; buildtarget=release;;
142
143	-T)	eval $optargcmd; resolvepath
144		TOOLDIR="$OPTARG";;
145
146	--)		break;;
147	-'?'|-h)	usage;;
148esac; done
149
150# Check required environment; DESTDIR only needed if building.
151checkvars='MACHINE TOOLDIR'
152$do_buildsystem && checkvars="$checkvars DESTDIR"
153
154for var in $checkvars; do
155	if ! eval '[ -n "$'$var'" ]'; then
156		echo "$var must be set in the environment before running build.sh."
157		echo ""; usage
158	fi
159done
160
161# Set up environment.
162[ -n "$MACHINE_ARCH" ] || getarch
163[ -d usr.bin/make ] || bomb "build.sh must be run from the top source level"
164
165# Remove the target directories.
166if $do_removedirs; then
167	echo "Removing DESTDIR and TOOLDIR...."
168	$runcmd rm -rf $DESTDIR $TOOLDIR
169fi
170
171mkdirp $TOOLDIR/bin || bomb "mkdir of $TOOLDIR/bin failed"
172
173# Test make source file timestamps against installed nbmake binary.
174if [ -x $TOOLDIR/bin/nbmake ]; then
175	for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
176		if [ $f -nt $TOOLDIR/bin/nbmake ]; then
177			do_rebuildmake=true; break
178		fi
179	done
180else
181	do_rebuildmake=true
182fi
183
184# Build $TOOLDIR/bin/nbmake.
185if $do_rebuildmake; then
186	echo "Building nbmake...."
187
188	# Go to a temporary directory in case building .o's happens.
189	tmpdir=${TMPDIR-/tmp}/nbbuild$$
190
191	$runcmd mkdir $tmpdir || bomb "cannot mkdir: $tmpdir"
192	trap "rm -r -f $tmpdir" 0
193	trap "exit 1" 1 2 3 15
194	$runcmd cd $tmpdir
195
196	${runcmd-eval} "${HOST_CC-cc} ${HOST_CFLAGS} -DMAKE_BOOTSTRAP \
197		-o $TOOLDIR/bin/nbmake -I$cwd/usr.bin/make \
198		$cwd/usr.bin/make/*.c $cwd/usr.bin/make/lst.lib/*.c" \
199		|| bomb "build of nbmake failed"
200
201	# Clean up.
202	$runcmd cd $cwd
203	$runcmd rm -r -f $tmpdir
204	trap 0 1 2 3 15
205
206	# Some compilers are just *that* braindead.
207	${runcmd-eval} "rm -f $cwd/usr.bin/make/*.o \
208		$cwd/usr.bin/make/lst.lib/*.o"
209fi
210
211# Build a nbmake wrapper script, usable by hand as well as by build.sh.
212makeprog=$TOOLDIR/bin/nbmake-$MACHINE
213
214if $do_rebuildmake || [ ! -f $makeprog ] || [ $makeprog -ot build.sh ]; then
215	rm -f $makeprog
216	if [ "$runcmd" = "echo" ]; then
217		mkscriptcmd='echo "cat >$makeprog <<EOF" && cat'
218	else
219		mkscriptcmd="cat >$makeprog"
220	fi
221	eval $mkscriptcmd <<EOF
222#! /bin/sh
223# Set proper variables to allow easy "make" building of a NetBSD subtree.
224# Generated from:  \$NetBSD: build.sh,v 1.11 2001/10/31 00:08:03 reinoud Exp $
225#
226exec $TOOLDIR/bin/nbmake MACHINE=$MACHINE MACHINE_ARCH=$MACHINE_ARCH \
227USETOOLS=yes USE_NEW_TOOLCHAIN=yes TOOLDIR="$TOOLDIR" \${1+\$@}
228EOF
229	[ "$runcmd" = "echo" ] && echo EOF
230	$runcmd chmod +x $makeprog
231fi
232
233if $do_buildsystem; then
234	${runcmd-exec} $makeprog -m `pwd`/share/mk ${buildtarget-build} \
235		MKTOOLS=yes DESTDIR="$DESTDIR" TOOLDIR="$TOOLDIR" \
236		$buildjobflag $releasedirflag
237fi
238