Annotation of src/build.sh, Revision 1.67
1.67 ! thorpej 1: #! /usr/bin/env sh
! 2: # $NetBSD: build.sh,v 1.66 2002/09/13 22:17:04 thorpej Exp $
1.1 tv 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
1.33 tv 7: # with a strange /bin/sh, "ksh" or "bash" may be an ample alternative.
1.1 tv 8: #
1.67 ! thorpej 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: #
1.1 tv 13:
14: bomb () {
15: echo ""
16: echo "ERROR: $@"
17: echo "*** BUILD ABORTED ***"
18: exit 1
19: }
1.15 tv 20: [ -d usr.bin/make ] || bomb "build.sh must be run from the top source level"
1.56 tv 21: [ -f share/mk/bsd.own.mk ] || bomb "src/share/mk is missing; please re-fetch the source tree"
1.1 tv 22:
1.64 bjh21 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.
1.67 ! thorpej 26: #
! 27: # XXX Except that doesn't work on Solaris.
1.64 bjh21 28: unset PWD
1.67 ! thorpej 29: if [ "x`uname -s`" = "xSunOS" ]; then
! 30: TOP=`pwd -P`
! 31: else
! 32: TOP=`pwd`
! 33: fi
1.29 jmc 34:
1.1 tv 35: getarch () {
36: # Translate a MACHINE into a default MACHINE_ARCH.
37: case $MACHINE in
1.55 thorpej 38: acorn26|acorn32|cats|netwinder|shark|*arm)
1.1 tv 39: MACHINE_ARCH=arm;;
40:
41: sun2)
42: MACHINE_ARCH=m68000;;
43:
44: amiga|atari|cesfic|hp300|sun3|*68k)
45: MACHINE_ARCH=m68k;;
46:
1.50 simonb 47: mipsco|newsmips|sbmips|sgimips)
1.1 tv 48: MACHINE_ARCH=mipseb;;
49:
1.51 simonb 50: algor|arc|cobalt|evbmips|hpcmips|playstation2|pmax)
1.1 tv 51: MACHINE_ARCH=mipsel;;
52:
53: pc532)
54: MACHINE_ARCH=ns32k;;
55:
1.61 augustss 56: bebox|prep|sandpoint|walnut|*ppc)
1.1 tv 57: MACHINE_ARCH=powerpc;;
58:
1.22 msaitoh 59: evbsh3|mmeye)
1.1 tv 60: MACHINE_ARCH=sh3eb;;
61:
1.22 msaitoh 62: dreamcast|hpcsh)
1.1 tv 63: MACHINE_ARCH=sh3el;;
64:
1.62 fredette 65: hp700)
66: MACHINE_ARCH=hppa;;
67:
1.5 bjh21 68: alpha|i386|sparc|sparc64|vax|x86_64)
1.4 tv 69: MACHINE_ARCH=$MACHINE;;
70:
71: *) bomb "unknown target MACHINE: $MACHINE";;
1.1 tv 72: esac
73: }
74:
1.47 tv 75: validatearch () {
76: # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
77: case $MACHINE_ARCH in
1.62 fredette 78: alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|ns32k|powerpc|sh3e[bl]|sparc|sparc64|vax|x86_64)
1.47 tv 79: ;;
80:
81: *) bomb "unknown target MACHINE_ARCH: $MACHINE_ARCH";;
82: esac
83: }
84:
1.15 tv 85: getmakevar () {
1.30 jmc 86: $make -m ${TOP}/share/mk -s -f- _x_ <<EOF
1.15 tv 87: _x_:
88: echo \${$1}
89: .include <bsd.prog.mk>
90: EOF
91: }
92:
1.10 tv 93: resolvepath () {
94: case $OPTARG in
95: /*) ;;
1.67 ! thorpej 96: *) OPTARG="$TOP/$OPTARG";;
1.10 tv 97: esac
98: }
99:
1.1 tv 100: usage () {
101: echo "Usage:"
1.52 thorpej 102: echo "$0 [-bdorUu] [-a arch] [-B buildid] [-j njob] [-m mach] "
1.58 lukem 103: echo " [-w wrapper] [-D dest] [-M obj] [-O obj] [-R release] [-T tools]"
1.4 tv 104: echo ""
1.15 tv 105: echo " -a: set MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
1.52 thorpej 106: echo " -B: set BUILDID to buildid"
1.15 tv 107: echo " -b: build nbmake and nbmake wrapper script, if needed"
1.36 wiz 108: echo " -D: set DESTDIR to dest"
1.26 thorpej 109: echo " -d: build a full distribution into DESTDIR (including etc files)"
1.57 sommerfe 110: echo " -j: Run up to njob jobs in parallel; see make(1)"
1.58 lukem 111: echo " -M: set obj root directory to obj (sets MAKEOBJDIRPREFIX)"
1.15 tv 112: echo " -m: set MACHINE to mach (not required if NetBSD native)"
1.12 tv 113: echo " -n: show commands that would be executed, but do not execute them"
1.36 wiz 114: echo " -O: set obj root directory to obj (sets a MAKEOBJDIR pattern)"
1.15 tv 115: echo " -o: set MKOBJDIRS=no (do not create objdirs at start of build)"
1.36 wiz 116: echo " -R: build a release (and set RELEASEDIR to release)"
1.15 tv 117: echo " -r: remove contents of TOOLDIR and DESTDIR before building"
1.36 wiz 118: echo " -T: set TOOLDIR to tools"
1.16 thorpej 119: echo " -t: build and install tools only (implies -b)"
1.44 lukem 120: echo " -U: set UNPRIVED"
1.18 tv 121: echo " -u: set UPDATE"
1.15 tv 122: echo " -w: create nbmake script at wrapper (default TOOLDIR/bin/nbmake-MACHINE)"
123: echo ""
124: echo "Note: if -T is unset and TOOLDIR is not set in the environment,"
125: echo " nbmake will be [re]built unconditionally."
1.1 tv 126: exit 1
127: }
128:
1.10 tv 129: # Set defaults.
1.18 tv 130: MAKEFLAGS=
1.12 tv 131: buildtarget=build
1.10 tv 132: do_buildsystem=true
1.16 thorpej 133: do_buildonlytools=false
1.10 tv 134: do_rebuildmake=false
135: do_removedirs=false
1.18 tv 136: makeenv=
137: makewrapper=
1.10 tv 138: opt_a=no
1.58 lukem 139: opts='a:B:bdhj:m:nortuw:D:M:O:R:T:U'
1.18 tv 140: runcmd=
1.4 tv 141:
142: if type getopts >/dev/null 2>&1; then
143: # Use POSIX getopts.
144: getoptcmd='getopts $opts opt && opt=-$opt'
145: optargcmd=':'
146: else
1.33 tv 147: type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh or bash"
1.2 tv 148:
1.4 tv 149: # Use old-style getopt(1) (doesn't handle whitespace in args).
150: args="`getopt $opts $*`"
151: [ $? = 0 ] || usage
152: set -- $args
1.1 tv 153:
1.4 tv 154: getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
155: optargcmd='OPTARG="$1"; shift'
156: fi
1.10 tv 157:
158: # Parse command line options.
1.4 tv 159: while eval $getoptcmd; do case $opt in
160: -a) eval $optargcmd
161: MACHINE_ARCH=$OPTARG; opt_a=yes;;
1.1 tv 162:
1.52 thorpej 163: -B) eval $optargcmd
164: BUILDID=$OPTARG;;
165:
1.10 tv 166: -b) do_buildsystem=false;;
1.9 jmc 167:
1.26 thorpej 168: -d) buildtarget=distribution;;
169:
1.4 tv 170: -j) eval $optargcmd
1.57 sommerfe 171: parallel="-j $OPTARG";;
1.1 tv 172:
1.4 tv 173: # -m overrides MACHINE_ARCH unless "-a" is specified
174: -m) eval $optargcmd
175: MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
1.3 tv 176:
1.10 tv 177: -n) runcmd=echo;;
178:
1.12 tv 179: -o) MKOBJDIRS=no;;
180:
1.10 tv 181: -r) do_removedirs=true; do_rebuildmake=true;;
1.1 tv 182:
1.16 thorpej 183: -t) do_buildonlytools=true; do_buildsystem=false;;
184:
1.44 lukem 185: -U) UNPRIVED=yes; export UNPRIVED
186: makeenv="$makeenv UNPRIVED";;
187:
1.18 tv 188: -u) UPDATE=yes; export UPDATE
189: makeenv="$makeenv UPDATE";;
1.15 tv 190:
191: -w) eval $optargcmd; resolvepath
192: makewrapper="$OPTARG";;
1.12 tv 193:
1.10 tv 194: -D) eval $optargcmd; resolvepath
1.19 dillo 195: DESTDIR="$OPTARG"; export DESTDIR
1.18 tv 196: makeenv="$makeenv DESTDIR";;
1.4 tv 197:
1.58 lukem 198: -M) eval $optargcmd; resolvepath
199: MAKEOBJDIRPREFIX="$OPTARG"; export MAKEOBJDIRPREFIX
200: makeobjdir=$OPTARG
201: makeenv="$makeenv MAKEOBJDIRPREFIX";;
202:
1.12 tv 203: -O) eval $optargcmd; resolvepath
1.67 ! thorpej 204: MAKEOBJDIR="\${.CURDIR:C,^$TOP,$OPTARG,}"; export MAKEOBJDIR
1.27 jmc 205: makeobjdir=$OPTARG
1.18 tv 206: makeenv="$makeenv MAKEOBJDIR";;
1.12 tv 207:
1.10 tv 208: -R) eval $optargcmd; resolvepath
1.18 tv 209: RELEASEDIR=$OPTARG; export RELEASEDIR
210: makeenv="$makeenv RELEASEDIR"
1.15 tv 211: buildtarget=release;;
1.4 tv 212:
1.10 tv 213: -T) eval $optargcmd; resolvepath
1.18 tv 214: TOOLDIR="$OPTARG"; export TOOLDIR;;
1.4 tv 215:
216: --) break;;
217: -'?'|-h) usage;;
1.1 tv 218: esac; done
219:
1.15 tv 220: # Set up MACHINE*. On a NetBSD host, these are allowed to be unset.
221: if [ -z "$MACHINE" ]; then
222: if [ "`uname -s 2>/dev/null`" != "NetBSD" ]; then
223: echo "MACHINE must be set, or -m must be used, for cross builds."
1.4 tv 224: echo ""; usage
225: fi
1.15 tv 226: MACHINE=`uname -m`
227: fi
1.10 tv 228: [ -n "$MACHINE_ARCH" ] || getarch
1.47 tv 229: validatearch
1.3 tv 230:
1.15 tv 231: # Set up default make(1) environment.
1.18 tv 232: makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
1.52 thorpej 233: if [ ! -z "$BUILDID" ]; then
234: makeenv="$makeenv BUILDID"
235: fi
1.67 ! thorpej 236: MAKEFLAGS="-m $TOP/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
1.18 tv 237: export MAKEFLAGS MACHINE MACHINE_ARCH
1.15 tv 238:
239: # Test make source file timestamps against installed nbmake binary,
240: # if TOOLDIR is pre-set.
1.46 tv 241: #
242: # Note that we do NOT try to grovel "mk.conf" here to find out if TOOLDIR
243: # is set there, because it can contain make variable expansions and other
244: # stuff only parseable *after* we have a working nbmake. So this logic
245: # can only work if the user has pre-set TOOLDIR in the environment or
246: # used the -T option to build.sh.
247: #
1.53 pk 248: make="${TOOLDIR-nonexistent}/bin/nbmake"
1.15 tv 249: if [ -x $make ]; then
1.1 tv 250: for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
1.66 thorpej 251: if [ $f -nt $make ]; then
1.10 tv 252: do_rebuildmake=true; break
1.1 tv 253: fi
254: done
255: else
1.10 tv 256: do_rebuildmake=true
1.1 tv 257: fi
258:
1.15 tv 259: # Build bootstrap nbmake if needed.
1.10 tv 260: if $do_rebuildmake; then
1.18 tv 261: $runcmd echo "===> Bootstrapping nbmake"
1.53 pk 262: tmpdir="${TMPDIR-/tmp}/nbbuild$$"
1.4 tv 263:
1.53 pk 264: $runcmd mkdir "$tmpdir" || bomb "cannot mkdir: $tmpdir"
265: trap "cd /; rm -r -f \"$tmpdir\"" 0
1.4 tv 266: trap "exit 1" 1 2 3 15
1.53 pk 267: $runcmd cd "$tmpdir"
1.1 tv 268:
1.40 tv 269: $runcmd env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
1.67 ! thorpej 270: "$TOP/tools/make/configure" || bomb "configure of nbmake failed"
1.39 tv 271: $runcmd sh buildmake.sh || bomb "build of nbmake failed"
1.1 tv 272:
1.53 pk 273: make="$tmpdir/nbmake"
1.67 ! thorpej 274: $runcmd cd "$TOP"
1.15 tv 275: $runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
276: fi
277:
1.53 pk 278: if [ "$runcmd" = "echo" ]; then
279: USE_NEW_TOOLCHAIN=yes
280: else
281: USE_NEW_TOOLCHAIN=`getmakevar USE_NEW_TOOLCHAIN`
282: fi
1.35 jmc 283: if [ "${USE_NEW_TOOLCHAIN}" = "" ]; then
1.30 jmc 284: echo "ERROR: build.sh (new toolchain) is not yet enabled for"
285: echo
286: echo "MACHINE: ${MACHINE}"
287: echo "MACHINE_ARCH: ${MACHINE_ARCH}"
288: echo
289: echo "All builds for this platform should be done via a traditional make"
290: echo
291: echo "If you wish to test using the new toolchain set"
292: echo
293: echo "USE_NEW_TOOLCHAIN=yes"
294: echo
295: echo "in either the environment or mk.conf and rerun"
296: echo
297: echo "$0 $*"
298: exit 1
299: fi
300:
1.15 tv 301: # If TOOLDIR isn't already set, make objdirs in "tools" in case the
302: # default setting from <bsd.own.mk> is used.
303: if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
1.18 tv 304: $runcmd cd tools
1.29 jmc 305: $runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= || exit 1
1.18 tv 306: $runcmd cd ..
1.15 tv 307: fi
308:
1.27 jmc 309: #
1.58 lukem 310: # If setting -M or -O to root an obj dir make sure the base directory is made
1.27 jmc 311: # before continuing as bsd.own.mk will need this to pick up _SRC_TOP_OBJ_
312: #
313: if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
1.53 pk 314: $runcmd mkdir -p "$makeobjdir"
1.27 jmc 315: fi
316:
1.15 tv 317: # Find DESTDIR and TOOLDIR.
318: if [ "$runcmd" = "echo" ]; then
1.24 tv 319: # shown symbolically with -n because these may come from mk.conf
320: DESTDIR='$DESTDIR'
321: TOOLDIR='$TOOLDIR'
1.15 tv 322: else
1.53 pk 323: DESTDIR=`getmakevar DESTDIR`;
324: [ $? = 0 ] || bomb "getmakevar DESTDIR failed";
325: echo "===> DESTDIR path: $DESTDIR"
326:
327: TOOLDIR=`getmakevar TOOLDIR`;
1.63 pooka 328: [ $? = 0 ] || bomb "getmakevar TOOLDIR failed";
1.53 pk 329: echo "===> TOOLDIR path: $TOOLDIR"
330:
1.18 tv 331: export DESTDIR TOOLDIR
1.15 tv 332: fi
333:
334: # Check validity of TOOLDIR and DESTDIR.
335: if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
336: bomb "TOOLDIR '$TOOLDIR' invalid"
337: fi
338: removedirs="$TOOLDIR"
339:
340: if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
1.16 thorpej 341: if $do_buildsystem && \
1.26 thorpej 342: ([ "$buildtarget" != "build" ] || \
1.16 thorpej 343: [ "`uname -s 2>/dev/null`" != "NetBSD" ] || \
344: [ "`uname -m`" != "$MACHINE" ]); then
1.26 thorpej 345: bomb "DESTDIR must be set to a non-root path for cross builds or -d or -R."
1.16 thorpej 346: elif $do_buildsystem; then
347: echo "===> WARNING: Building to /."
348: echo "===> If your kernel is not up to date, this may cause the system to break!"
1.15 tv 349: fi
350: else
351: removedirs="$removedirs $DESTDIR"
352: fi
353:
354: # Remove the target directories.
355: if $do_removedirs; then
356: for f in $removedirs; do
357: echo "===> Removing $f"
358: $runcmd rm -r -f $f
359: done
360: fi
361:
362: # Recreate $TOOLDIR.
1.53 pk 363: $runcmd mkdir -p "$TOOLDIR/bin" || bomb "mkdir of '$TOOLDIR/bin' failed"
1.15 tv 364:
365: # Install nbmake if it was built.
366: if $do_rebuildmake; then
1.53 pk 367: $runcmd rm -f "$TOOLDIR/bin/nbmake"
368: $runcmd cp $make "$TOOLDIR/bin/nbmake"
369: $runcmd rm -r -f "$tmpdir"
1.4 tv 370: trap 0 1 2 3 15
1.1 tv 371: fi
372:
1.4 tv 373: # Build a nbmake wrapper script, usable by hand as well as by build.sh.
1.15 tv 374: if [ -z "$makewrapper" ]; then
1.53 pk 375: makewrapper="$TOOLDIR/bin/nbmake-$MACHINE"
1.52 thorpej 376: if [ ! -z "$BUILDID" ]; then
1.53 pk 377: makewrapper="$makewrapper-$BUILDID"
1.52 thorpej 378: fi
1.15 tv 379: fi
1.4 tv 380:
1.53 pk 381: $runcmd rm -f "$makewrapper"
1.21 jmc 382: if [ "$runcmd" = "echo" ]; then
383: echo 'cat <<EOF >'$makewrapper
384: makewrapout=
385: else
1.53 pk 386: makewrapout=">>\$makewrapper"
1.21 jmc 387: fi
1.18 tv 388:
1.21 jmc 389: eval cat <<EOF $makewrapout
1.4 tv 390: #! /bin/sh
391: # Set proper variables to allow easy "make" building of a NetBSD subtree.
1.67 ! thorpej 392: # Generated from: \$NetBSD: build.sh,v 1.66 2002/09/13 22:17:04 thorpej Exp $
1.4 tv 393: #
1.18 tv 394:
395: EOF
1.21 jmc 396: for f in $makeenv; do
397: eval echo "$f=\'\$`echo $f`\'\;\ export\ $f" $makewrapout
398: done
399: eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
1.18 tv 400:
1.53 pk 401: eval cat <<'EOF' $makewrapout
1.18 tv 402:
1.53 pk 403: exec "$TOOLDIR/bin/nbmake" ${1+"$@"}
1.4 tv 404: EOF
1.21 jmc 405: [ "$runcmd" = "echo" ] && echo EOF
1.53 pk 406: $runcmd chmod +x "$makewrapper"
1.4 tv 407:
1.10 tv 408: if $do_buildsystem; then
1.57 sommerfe 409: ${runcmd-exec} "$makewrapper" $parallel $buildtarget
1.16 thorpej 410: elif $do_buildonlytools; then
411: if [ "$MKOBJDIRS" != "no" ]; then
1.57 sommerfe 412: $runcmd "$makewrapper" $parallel obj-tools || exit 1
1.16 thorpej 413: fi
1.18 tv 414: $runcmd cd tools
1.31 jmc 415: if [ "$UPDATE" = "" ]; then
1.53 pk 416: ${runcmd-exec} "$makewrapper" cleandir dependall install
1.31 jmc 417: else
1.53 pk 418: ${runcmd-exec} "$makewrapper" dependall install
1.31 jmc 419: fi
1.9 jmc 420: fi
CVSweb <webmaster@jp.NetBSD.org>