build.sh revision 1.52 1 #! /bin/sh
2 # $NetBSD: build.sh,v 1.52 2002/03/14 18:33:04 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
10 bomb () {
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
18 TOP=`pwd`
19
20 getarch () {
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
60 validatearch () {
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
70 getmakevar () {
71 $make -m ${TOP}/share/mk -s -f- _x_ <<EOF
72 _x_:
73 echo \${$1}
74 .include <bsd.prog.mk>
75 EOF
76 }
77
78 resolvepath () {
79 case $OPTARG in
80 /*) ;;
81 *) OPTARG="$cwd/$OPTARG";;
82 esac
83 }
84
85 usage () {
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.
114 MAKEFLAGS=
115 buildtarget=build
116 cwd=`pwd`
117 do_buildsystem=true
118 do_buildonlytools=false
119 do_rebuildmake=false
120 do_removedirs=false
121 makeenv=
122 makewrapper=
123 opt_a=no
124 opts='a:B:bdhj:m:nortuw:D:O:R:T:U'
125 runcmd=
126
127 if type getopts >/dev/null 2>&1; then
128 # Use POSIX getopts.
129 getoptcmd='getopts $opts opt && opt=-$opt'
130 optargcmd=':'
131 else
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'
141 fi
142
143 # Parse command line options.
144 while 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;;
198 esac; done
199
200 # Set up MACHINE*. On a NetBSD host, these are allowed to be unset.
201 if [ -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`
207 fi
208 [ -n "$MACHINE_ARCH" ] || getarch
209 validatearch
210
211 # Set up default make(1) environment.
212 makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
213 if [ ! -z "$BUILDID" ]; then
214 makeenv="$makeenv BUILDID"
215 fi
216 MAKEFLAGS="-m $cwd/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
217 export 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 #
228 make=${TOOLDIR-nonexistent}/bin/nbmake
229 if [ -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
235 else
236 do_rebuildmake=true
237 fi
238
239 # Build bootstrap nbmake if needed.
240 if $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
256 fi
257
258 USE_NEW_TOOLCHAIN=`getmakevar USE_NEW_TOOLCHAIN`
259 if [ "${USE_NEW_TOOLCHAIN}" = "" ]; then
260 echo "ERROR: build.sh (new toolchain) is not yet enabled for"
261 echo
262 echo "MACHINE: ${MACHINE}"
263 echo "MACHINE_ARCH: ${MACHINE_ARCH}"
264 echo
265 echo "All builds for this platform should be done via a traditional make"
266 echo
267 echo "If you wish to test using the new toolchain set"
268 echo
269 echo "USE_NEW_TOOLCHAIN=yes"
270 echo
271 echo "in either the environment or mk.conf and rerun"
272 echo
273 echo "$0 $*"
274 exit 1
275 fi
276
277 # If TOOLDIR isn't already set, make objdirs in "tools" in case the
278 # default setting from <bsd.own.mk> is used.
279 if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
280 $runcmd cd tools
281 $runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= || exit 1
282 $runcmd cd ..
283 fi
284
285 #
286 # If setting -O to root an obj dir make sure the base directory is made
287 # before continuing as bsd.own.mk will need this to pick up _SRC_TOP_OBJ_
288 #
289 if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
290 $runcmd mkdir -p $makeobjdir
291 fi
292
293 # Find DESTDIR and TOOLDIR.
294 if [ "$runcmd" = "echo" ]; then
295 # shown symbolically with -n because these may come from mk.conf
296 DESTDIR='$DESTDIR'
297 TOOLDIR='$TOOLDIR'
298 else
299 DESTDIR=`getmakevar DESTDIR`; echo "===> DESTDIR path: $DESTDIR"
300 TOOLDIR=`getmakevar TOOLDIR`; echo "===> TOOLDIR path: $TOOLDIR"
301 export DESTDIR TOOLDIR
302 fi
303
304 # Check validity of TOOLDIR and DESTDIR.
305 if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
306 bomb "TOOLDIR '$TOOLDIR' invalid"
307 fi
308 removedirs="$TOOLDIR"
309
310 if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
311 if $do_buildsystem && \
312 ([ "$buildtarget" != "build" ] || \
313 [ "`uname -s 2>/dev/null`" != "NetBSD" ] || \
314 [ "`uname -m`" != "$MACHINE" ]); then
315 bomb "DESTDIR must be set to a non-root path for cross builds or -d or -R."
316 elif $do_buildsystem; then
317 echo "===> WARNING: Building to /."
318 echo "===> If your kernel is not up to date, this may cause the system to break!"
319 fi
320 else
321 removedirs="$removedirs $DESTDIR"
322 fi
323
324 # Remove the target directories.
325 if $do_removedirs; then
326 for f in $removedirs; do
327 echo "===> Removing $f"
328 $runcmd rm -r -f $f
329 done
330 fi
331
332 # Recreate $TOOLDIR.
333 mkdir -p $TOOLDIR/bin || bomb "mkdir of $TOOLDIR/bin failed"
334
335 # Install nbmake if it was built.
336 if $do_rebuildmake; then
337 $runcmd rm -f $TOOLDIR/bin/nbmake
338 $runcmd cp $make $TOOLDIR/bin/nbmake
339 $runcmd rm -r -f $tmpdir
340 trap 0 1 2 3 15
341 fi
342
343 # Build a nbmake wrapper script, usable by hand as well as by build.sh.
344 if [ -z "$makewrapper" ]; then
345 makewrapper=$TOOLDIR/bin/nbmake-$MACHINE
346 if [ ! -z "$BUILDID" ]; then
347 makewrapper=$makewrapper-$BUILDID
348 fi
349 fi
350
351 $runcmd rm -f $makewrapper
352 if [ "$runcmd" = "echo" ]; then
353 echo 'cat <<EOF >'$makewrapper
354 makewrapout=
355 else
356 makewrapout=">>$makewrapper"
357 fi
358
359 eval cat <<EOF $makewrapout
360 #! /bin/sh
361 # Set proper variables to allow easy "make" building of a NetBSD subtree.
362 # Generated from: \$NetBSD: build.sh,v 1.52 2002/03/14 18:33:04 thorpej Exp $
363 #
364
365 EOF
366 for f in $makeenv; do
367 eval echo "$f=\'\$`echo $f`\'\;\ export\ $f" $makewrapout
368 done
369 eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
370
371 eval cat <<EOF $makewrapout
372
373 exec \$TOOLDIR/bin/nbmake \${1+"\$@"}
374 EOF
375 [ "$runcmd" = "echo" ] && echo EOF
376 $runcmd chmod +x $makewrapper
377
378 if $do_buildsystem; then
379 ${runcmd-exec} $makewrapper $buildtarget
380 elif $do_buildonlytools; then
381 if [ "$MKOBJDIRS" != "no" ]; then
382 $runcmd $makewrapper obj-tools || exit 1
383 fi
384 $runcmd cd tools
385 if [ "$UPDATE" = "" ]; then
386 ${runcmd-exec} $makewrapper cleandir dependall install
387 else
388 ${runcmd-exec} $makewrapper dependall install
389 fi
390 fi
391