build.sh revision 1.75 1 #! /usr/bin/env sh
2 # $NetBSD: build.sh,v 1.75 2002/12/09 03:13:41 lukem 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 # Note, however, that due to the way the interpreter is invoked above,
10 # if a POSIX-compliant shell is the first in the PATH, you won't have
11 # to take any further action.
12 #
13
14 bomb () {
15 echo ""
16 echo "ERROR: $@"
17 echo "*** BUILD ABORTED ***"
18 exit 1
19 }
20 [ -d usr.bin/make ] || bomb "build.sh must be run from the top source level"
21 [ -f share/mk/bsd.own.mk ] || bomb "src/share/mk is missing; please re-fetch the source tree"
22
23 # If $PWD is a valid name of the current directory, POSIX mandates that pwd
24 # return it by default which causes problems in the presence of symlinks.
25 # Unsetting PWD is simpler than changing every occurrence of pwd to use -P.
26 #
27 # XXX Except that doesn't work on Solaris.
28 unset PWD
29 if [ "x`uname -s`" = "xSunOS" ]; then
30 TOP=`pwd -P`
31 else
32 TOP=`pwd`
33 fi
34
35 getarch () {
36 # Translate a MACHINE into a default MACHINE_ARCH.
37 case $MACHINE in
38 acorn26|acorn32|cats|netwinder|shark|*arm)
39 MACHINE_ARCH=arm;;
40
41 sun2)
42 MACHINE_ARCH=m68000;;
43
44 amiga|atari|cesfic|hp300|sun3|*68k)
45 MACHINE_ARCH=m68k;;
46
47 mipsco|newsmips|sbmips|sgimips)
48 MACHINE_ARCH=mipseb;;
49
50 algor|arc|cobalt|evbmips|hpcmips|playstation2|pmax)
51 MACHINE_ARCH=mipsel;;
52
53 pc532)
54 MACHINE_ARCH=ns32k;;
55
56 bebox|prep|sandpoint|walnut|*ppc)
57 MACHINE_ARCH=powerpc;;
58
59 evbsh3|mmeye)
60 MACHINE_ARCH=sh3eb;;
61
62 dreamcast|hpcsh)
63 MACHINE_ARCH=sh3el;;
64
65 hp700)
66 MACHINE_ARCH=hppa;;
67
68 evbsh5)
69 MACHINE_ARCH=sh5el;;
70
71 alpha|i386|sparc|sparc64|vax|x86_64)
72 MACHINE_ARCH=$MACHINE;;
73
74 *) bomb "unknown target MACHINE: $MACHINE";;
75 esac
76 }
77
78 validatearch () {
79 # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
80 case $MACHINE_ARCH in
81 alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|ns32k|powerpc|sh[35]e[bl]|sparc|sparc64|vax|x86_64)
82 ;;
83
84 *) bomb "unknown target MACHINE_ARCH: $MACHINE_ARCH";;
85 esac
86 }
87
88 getmakevar () {
89 $make -m ${TOP}/share/mk -s -f- _x_ <<EOF
90 _x_:
91 echo \${$1}
92 .include <bsd.prog.mk>
93 .include <bsd.kernobj.mk>
94 EOF
95 }
96
97 resolvepath () {
98 case $OPTARG in
99 /*) ;;
100 *) OPTARG="$TOP/$OPTARG";;
101 esac
102 }
103
104 usage () {
105 cat <<_usage_
106 Usage:
107 `basename $0` [-bdEnortUu] [-a arch] [-B buildid] [-D dest] [-j njob] [-k kernel]
108 [-M obj] [-m mach] [-O obj] [-R release] [-T tools] [-w wrapper]
109
110 -a arch set MACHINE_ARCH to arch (otherwise deduced from MACHINE)
111 -B buildid set BUILDID to buildid
112 -b build nbmake and nbmake wrapper script, if needed
113 -D dest set DESTDIR to dest
114 -d build a full distribution into DESTDIR (including etc files)
115 -E set "expert" mode; disables some DESTDIR checks
116 -j njob run up to njob jobs in parallel; see make(1)
117 -k kernel build a kernel using the named configuration file
118 -M obj set obj root directory to obj (sets MAKEOBJDIRPREFIX)
119 -m mach set MACHINE to mach (not required if NetBSD native)
120 -n show commands that would be executed, but do not execute them
121 -O obj set obj root directory to obj (sets a MAKEOBJDIR pattern)
122 -o set MKOBJDIRS=no (do not create objdirs at start of build)
123 -R release build a release (and set RELEASEDIR to release)
124 -r remove contents of TOOLDIR and DESTDIR before building
125 -T tools set TOOLDIR to tools
126 -t build and install tools only (implies -b)
127 -U set UNPRIVED
128 -u set UPDATE
129 -w wrapper create nbmake script at wrapper
130 (default TOOLDIR/bin/nbmake-MACHINE)
131
132 Note: if -T is unset and TOOLDIR is not set in the environment,
133 nbmake will be [re]built unconditionally.
134 _usage_
135 exit 1
136 }
137
138 # Set defaults.
139 MAKEFLAGS=
140 buildtarget=build
141 do_buildsystem=true
142 do_buildkernel=false
143 do_buildtools=false
144 do_rebuildmake=false
145 do_removedirs=false
146 expert_mode=false
147 makeenv=
148 makewrapper=
149 opt_a=no
150 opts='a:B:bD:dEhj:k:M:m:nO:oR:rT:tUuw:'
151 runcmd=
152
153 if type getopts >/dev/null 2>&1; then
154 # Use POSIX getopts.
155 getoptcmd='getopts $opts opt && opt=-$opt'
156 optargcmd=':'
157 else
158 type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh or bash"
159
160 # Use old-style getopt(1) (doesn't handle whitespace in args).
161 args="`getopt $opts $*`"
162 [ $? = 0 ] || usage
163 set -- $args
164
165 getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
166 optargcmd='OPTARG="$1"; shift'
167 fi
168
169 # Parse command line options.
170 while eval $getoptcmd; do case $opt in
171 -a) eval $optargcmd
172 MACHINE_ARCH=$OPTARG; opt_a=yes;;
173
174 -B) eval $optargcmd
175 BUILDID=$OPTARG;;
176
177 -b) do_buildsystem=false;;
178
179 -D) eval $optargcmd; resolvepath
180 DESTDIR="$OPTARG"; export DESTDIR
181 makeenv="$makeenv DESTDIR";;
182
183 -d) buildtarget=distribution;;
184
185 -E) expert_mode=true;;
186
187 -j) eval $optargcmd
188 parallel="-j $OPTARG";;
189
190 -k) do_buildkernel=true; do_buildsystem=false
191 eval $optargcmd
192 kernconfname=$OPTARG;;
193
194 -M) eval $optargcmd; resolvepath
195 MAKEOBJDIRPREFIX="$OPTARG"; export MAKEOBJDIRPREFIX
196 makeobjdir=$OPTARG
197 makeenv="$makeenv MAKEOBJDIRPREFIX";;
198
199 # -m overrides MACHINE_ARCH unless "-a" is specified
200 -m) eval $optargcmd
201 MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
202
203 -n) runcmd=echo;;
204
205 -O) eval $optargcmd; resolvepath
206 MAKEOBJDIR="\${.CURDIR:C,^$TOP,$OPTARG,}"; export MAKEOBJDIR
207 makeobjdir=$OPTARG
208 makeenv="$makeenv MAKEOBJDIR";;
209
210 -o) MKOBJDIRS=no;;
211
212 -R) eval $optargcmd; resolvepath
213 RELEASEDIR=$OPTARG; export RELEASEDIR
214 makeenv="$makeenv RELEASEDIR"
215 buildtarget=release;;
216
217 -r) do_removedirs=true; do_rebuildmake=true;;
218
219 -T) eval $optargcmd; resolvepath
220 TOOLDIR="$OPTARG"; export TOOLDIR;;
221
222 -t) do_buildtools=true; do_buildsystem=false;;
223
224 -U) UNPRIVED=yes; export UNPRIVED
225 makeenv="$makeenv UNPRIVED";;
226
227 -u) UPDATE=yes; export UPDATE
228 makeenv="$makeenv UPDATE";;
229
230 -w) eval $optargcmd; resolvepath
231 makewrapper="$OPTARG";;
232
233 --) break;;
234 -'?'|-h) usage;;
235 esac; done
236
237 # Set up MACHINE*. On a NetBSD host, these are allowed to be unset.
238 if [ -z "$MACHINE" ]; then
239 if [ "`uname -s 2>/dev/null`" != "NetBSD" ]; then
240 echo "MACHINE must be set, or -m must be used, for cross builds."
241 echo ""; usage
242 fi
243 MACHINE=`uname -m`
244 fi
245 [ -n "$MACHINE_ARCH" ] || getarch
246 validatearch
247
248 # Set up default make(1) environment.
249 makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
250 if [ ! -z "$BUILDID" ]; then
251 makeenv="$makeenv BUILDID"
252 fi
253 MAKEFLAGS="-m $TOP/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
254 export MAKEFLAGS MACHINE MACHINE_ARCH
255
256 # Test make source file timestamps against installed nbmake binary,
257 # if TOOLDIR is pre-set.
258 #
259 # Note that we do NOT try to grovel "mk.conf" here to find out if TOOLDIR
260 # is set there, because it can contain make variable expansions and other
261 # stuff only parseable *after* we have a working nbmake. So this logic
262 # can only work if the user has pre-set TOOLDIR in the environment or
263 # used the -T option to build.sh.
264 #
265 make="${TOOLDIR-nonexistent}/bin/nbmake"
266 if [ -x $make ]; then
267 for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
268 if [ $f -nt $make ]; then
269 do_rebuildmake=true; break
270 fi
271 done
272 else
273 do_rebuildmake=true
274 fi
275
276 # Build bootstrap nbmake if needed.
277 if $do_rebuildmake; then
278 $runcmd echo "===> Bootstrapping nbmake"
279 tmpdir="${TMPDIR-/tmp}/nbbuild$$"
280
281 $runcmd mkdir "$tmpdir" || bomb "cannot mkdir: $tmpdir"
282 trap "cd /; rm -r -f \"$tmpdir\"" 0
283 trap "exit 1" 1 2 3 15
284 $runcmd cd "$tmpdir"
285
286 $runcmd env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
287 CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
288 "$TOP/tools/make/configure" \
289 || bomb "configure of nbmake failed"
290 $runcmd sh buildmake.sh || bomb "build of nbmake failed"
291
292 make="$tmpdir/nbmake"
293 $runcmd cd "$TOP"
294 $runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
295 fi
296
297 EXTERNAL_TOOLCHAIN=`getmakevar EXTERNAL_TOOLCHAIN`
298 if [ "$runcmd" = "echo" ]; then
299 TOOLCHAIN_MISSING=no
300 else
301 TOOLCHAIN_MISSING=`getmakevar TOOLCHAIN_MISSING`
302 fi
303 if [ "${TOOLCHAIN_MISSING}" = "yes" -a \
304 "${EXTERNAL_TOOLCHAIN}" = "" ]; then
305 echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
306 echo
307 echo "MACHINE: ${MACHINE}"
308 echo "MACHINE_ARCH: ${MACHINE_ARCH}"
309 echo
310 echo "All builds for this platform should be done via a traditional make"
311 echo
312 echo "If you wish to use an external cross-toolchain, set"
313 echo
314 echo "EXTERNAL_TOOLCHAIN=<path to toolchain root>"
315 echo
316 echo "in either the environment or mk.conf and rerun"
317 echo
318 echo "$0 $*"
319 exit 1
320 fi
321
322 # If TOOLDIR isn't already set, make objdirs in "tools" in case the
323 # default setting from <bsd.own.mk> is used.
324 if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
325 $runcmd cd tools
326 $runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= \
327 || bomb "make obj failed in tools"
328 $runcmd cd "$TOP"
329 fi
330
331 #
332 # If setting -M or -O to root an obj dir make sure the base directory is made
333 # before continuing as bsd.own.mk will need this to pick up _SRC_TOP_OBJ_
334 #
335 if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
336 $runcmd mkdir -p "$makeobjdir"
337 fi
338
339 # Find DESTDIR and TOOLDIR.
340 if [ "$runcmd" = "echo" ]; then
341 # shown symbolically with -n because these may come from mk.conf
342 DESTDIR='$DESTDIR'
343 TOOLDIR='$TOOLDIR'
344 else
345 DESTDIR=`getmakevar DESTDIR`;
346 [ $? = 0 ] || bomb "getmakevar DESTDIR failed";
347 $runcmd echo "===> DESTDIR path: $DESTDIR"
348
349 TOOLDIR=`getmakevar TOOLDIR`;
350 [ $? = 0 ] || bomb "getmakevar TOOLDIR failed";
351 $runcmd echo "===> TOOLDIR path: $TOOLDIR"
352
353 export DESTDIR TOOLDIR
354 fi
355
356 # Check validity of TOOLDIR and DESTDIR.
357 if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
358 bomb "TOOLDIR '$TOOLDIR' invalid"
359 fi
360 removedirs="$TOOLDIR"
361
362 if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
363 if $do_buildsystem; then
364 if [ "$buildtarget" != "build" ] || \
365 [ "`uname -s 2>/dev/null`" != "NetBSD" ] || \
366 [ "`uname -m`" != "$MACHINE" ]; then
367 bomb "DESTDIR must be set to a non-root path for cross builds or -d or -R."
368 fi
369 if ! $expert_mode; then
370 bomb "DESTDIR must be set to a non-root path for non -E (expert) builds"
371 fi
372 $runcmd echo "===> WARNING: Building to /, in expert mode."
373 $runcmd echo "===> This may cause your system to break! Reasons include:"
374 $runcmd echo "===> - your kernel is not up to date"
375 $runcmd echo "===> - the libraries or toolchain have changed"
376 $runcmd echo "===> YOU HAVE BEEN WARNED!"
377 fi
378 else
379 removedirs="$removedirs $DESTDIR"
380 fi
381
382 # Remove the target directories.
383 if $do_removedirs; then
384 for f in $removedirs; do
385 $runcmd echo "===> Removing $f"
386 $runcmd rm -r -f $f
387 done
388 fi
389
390 # Recreate $TOOLDIR.
391 $runcmd mkdir -p "$TOOLDIR/bin" || bomb "mkdir of '$TOOLDIR/bin' failed"
392
393 # Install nbmake if it was built.
394 if $do_rebuildmake; then
395 $runcmd rm -f "$TOOLDIR/bin/nbmake"
396 $runcmd cp $make "$TOOLDIR/bin/nbmake" \
397 || bomb "failed to install \$TOOLDIR/bin/nbmake"
398 make="$TOOLDIR/bin/nbmake"
399 $runcmd rm -r -f "$tmpdir"
400 trap 0 1 2 3 15
401 fi
402
403 # Build a nbmake wrapper script, usable by hand as well as by build.sh.
404 if [ -z "$makewrapper" ]; then
405 makewrapper="$TOOLDIR/bin/nbmake-$MACHINE"
406 if [ ! -z "$BUILDID" ]; then
407 makewrapper="$makewrapper-$BUILDID"
408 fi
409 fi
410
411 $runcmd rm -f "$makewrapper"
412 if [ "$runcmd" = "echo" ]; then
413 echo 'cat <<EOF >'$makewrapper
414 makewrapout=
415 else
416 makewrapout=">>\$makewrapper"
417 fi
418
419 eval cat <<EOF $makewrapout
420 #! /bin/sh
421 # Set proper variables to allow easy "make" building of a NetBSD subtree.
422 # Generated from: \$NetBSD: build.sh,v 1.75 2002/12/09 03:13:41 lukem Exp $
423 #
424
425 EOF
426 for f in $makeenv; do
427 eval echo "$f=\'\$`echo $f`\'\;\ export\ $f" $makewrapout
428 done
429 eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
430
431 eval cat <<'EOF' $makewrapout
432
433 exec "$TOOLDIR/bin/nbmake" ${1+"$@"}
434 EOF
435 [ "$runcmd" = "echo" ] && echo EOF
436 $runcmd chmod +x "$makewrapper"
437
438 if $do_buildsystem; then
439 # Build everything.
440 ${runcmd-exec} "$makewrapper" $parallel $buildtarget \
441 || bomb "failed to make $buildtarget"
442 else
443 # One or more of do_buildtools and do_buildkernel
444 # might be set. Do them in the appropriate order.
445 if $do_buildtools; then
446 if [ "$MKOBJDIRS" != "no" ]; then
447 $runcmd "$makewrapper" $parallel obj-tools \
448 || bomb "failed to make obj-tools"
449 fi
450 $runcmd cd tools
451 if [ "$UPDATE" = "" ]; then
452 $runcmd "$makewrapper" cleandir dependall install \
453 || bomb "failed to make tools"
454 else
455 $runcmd "$makewrapper" dependall install \
456 || bomb "failed to make tools"
457 fi
458 fi
459 if $do_buildkernel; then
460 if ! $do_buildtools; then
461 # Building tools every time we build a kernel
462 # is clearly unnecessary. We could try to
463 # figure out whether rebuilding the tools is
464 # necessary this time, but it doesn't seem
465 # worth the trouble. Instead, we say it's the
466 # user's responsibility to rebuild the tools if
467 # necessary.
468 $runcmd echo "===> Building kernel" \
469 "without building new tools"
470 fi
471 $runcmd echo "===> Building kernel ${kernconfname}"
472 # The correct value of KERNOBJDIR might depend on a
473 # prior "make obj" in TOP/etc.
474 if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
475 $runcmd cd "$TOP/etc"
476 $runcmd "$makewrapper" obj \
477 || bomb "failed to make obj in etc"
478 $runcmd cd "$TOP"
479 fi
480 if [ "$runcmd" = "echo" ]; then
481 # shown symbolically with -n
482 # because getmakevar might not work yet
483 KERNCONFDIR='$KERNCONFDIR'
484 KERNOBJDIR='$KERNOBJDIR'
485 else
486 KERNCONFDIR="$( getmakevar KERNCONFDIR )"
487 [ $? = 0 ] || bomb "getmakevar KERNCONFDIR failed";
488 KERNOBJDIR="$( getmakevar KERNOBJDIR )"
489 [ $? = 0 ] || bomb "getmakevar KERNOBJDIR failed";
490 fi
491 case "${kernconfname}" in
492 */*)
493 kernconfpath="${kernconfname}"
494 kernconfbase="$( basename "${kernconfname}" )"
495 ;;
496 *)
497 kernconfpath="${KERNCONFDIR}/${kernconfname}"
498 kernconfbase="${kernconfname}"
499 ;;
500 esac
501 kernbuilddir="${KERNOBJDIR}/${kernconfbase}"
502 $runcmd echo "===> Kernel build directory: ${kernbuilddir}"
503 $runcmd mkdir -p "${kernbuilddir}" \
504 || bomb "cannot mkdir: ${kernbuilddir}"
505 if [ "$UPDATE" = "" ]; then
506 $runcmd cd "${kernbuilddir}"
507 $runcmd "$makewrapper" cleandir \
508 || bomb "make cleandir failed in " \
509 "${kernbuilddir}"
510 $runcmd cd "$TOP"
511 fi
512 $runcmd "${TOOLDIR}/bin/nbconfig" \
513 -b "${kernbuilddir}" \
514 -s "${TOP}/sys" "${kernconfpath}" \
515 || bomb "nbconfig failed for ${kernconfname}"
516 $runcmd cd "${kernbuilddir}"
517 $runcmd "$makewrapper" depend \
518 || bomb "make depend failed in ${kernbuilddir}"
519 $runcmd "$makewrapper" $parallel all \
520 || bomb "make all failed in ${kernbuilddir}"
521 $runcmd echo "===> New kernel should be in ${kernbuilddir}"
522 fi
523 fi
524