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