Annotation of src/build.sh, Revision 1.52
1.1 tv 1: #! /bin/sh
1.52 ! thorpej 2: # $NetBSD: build.sh,v 1.51 2002/03/07 14:59:04 simonb 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: #
9:
10: bomb () {
11: echo ""
12: echo "ERROR: $@"
13: echo "*** BUILD ABORTED ***"
14: exit 1
15: }
1.15 tv 16: [ -d usr.bin/make ] || bomb "build.sh must be run from the top source level"
1.1 tv 17:
1.29 jmc 18: TOP=`pwd`
19:
1.1 tv 20: getarch () {
21: # Translate a MACHINE into a default MACHINE_ARCH.
22: case $MACHINE in
1.43 thorpej 23: arm26|evbarm|hpcarm|netwinder)
1.1 tv 24: MACHINE_ARCH=arm;;
25:
1.43 thorpej 26: acorn32|cats|shark)
1.5 bjh21 27: MACHINE_ARCH=arm32;;
28:
1.1 tv 29: sun2)
30: MACHINE_ARCH=m68000;;
31:
32: amiga|atari|cesfic|hp300|sun3|*68k)
33: MACHINE_ARCH=m68k;;
34:
1.50 simonb 35: mipsco|newsmips|sbmips|sgimips)
1.1 tv 36: MACHINE_ARCH=mipseb;;
37:
1.51 simonb 38: algor|arc|cobalt|evbmips|hpcmips|playstation2|pmax)
1.1 tv 39: MACHINE_ARCH=mipsel;;
40:
41: pc532)
42: MACHINE_ARCH=ns32k;;
43:
1.45 scw 44: bebox|mvmeppc|prep|sandpoint|walnut|*ppc)
1.1 tv 45: MACHINE_ARCH=powerpc;;
46:
1.22 msaitoh 47: evbsh3|mmeye)
1.1 tv 48: MACHINE_ARCH=sh3eb;;
49:
1.22 msaitoh 50: dreamcast|hpcsh)
1.1 tv 51: MACHINE_ARCH=sh3el;;
52:
1.5 bjh21 53: alpha|i386|sparc|sparc64|vax|x86_64)
1.4 tv 54: MACHINE_ARCH=$MACHINE;;
55:
56: *) bomb "unknown target MACHINE: $MACHINE";;
1.1 tv 57: esac
58: }
59:
1.47 tv 60: validatearch () {
61: # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
62: case $MACHINE_ARCH in
1.48 mjl 63: alpha|arm|i386|m68000|m68k|mipse[bl]|powerpc|sh3e[bl]|sparc|sparc64|vax|x86_64)
1.47 tv 64: ;;
65:
66: *) bomb "unknown target MACHINE_ARCH: $MACHINE_ARCH";;
67: esac
68: }
69:
1.15 tv 70: getmakevar () {
1.30 jmc 71: $make -m ${TOP}/share/mk -s -f- _x_ <<EOF
1.15 tv 72: _x_:
73: echo \${$1}
74: .include <bsd.prog.mk>
75: EOF
76: }
77:
1.10 tv 78: resolvepath () {
79: case $OPTARG in
80: /*) ;;
81: *) OPTARG="$cwd/$OPTARG";;
82: esac
83: }
84:
1.1 tv 85: usage () {
86: echo "Usage:"
1.52 ! thorpej 87: echo "$0 [-bdorUu] [-a arch] [-B buildid] [-j njob] [-m mach] "
! 88: echo " [-w wrapper] [-D dest] [-O obj] [-R release] [-T tools]"
1.4 tv 89: echo ""
1.15 tv 90: echo " -a: set MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
1.52 ! thorpej 91: echo " -B: set BUILDID to buildid"
1.15 tv 92: echo " -b: build nbmake and nbmake wrapper script, if needed"
1.36 wiz 93: echo " -D: set DESTDIR to dest"
1.26 thorpej 94: echo " -d: build a full distribution into DESTDIR (including etc files)"
1.3 tv 95: echo " -j: set NBUILDJOBS to njob"
1.15 tv 96: echo " -m: set MACHINE to mach (not required if NetBSD native)"
1.12 tv 97: echo " -n: show commands that would be executed, but do not execute them"
1.36 wiz 98: echo " -O: set obj root directory to obj (sets a MAKEOBJDIR pattern)"
1.15 tv 99: echo " -o: set MKOBJDIRS=no (do not create objdirs at start of build)"
1.36 wiz 100: echo " -R: build a release (and set RELEASEDIR to release)"
1.15 tv 101: echo " -r: remove contents of TOOLDIR and DESTDIR before building"
1.36 wiz 102: echo " -T: set TOOLDIR to tools"
1.16 thorpej 103: echo " -t: build and install tools only (implies -b)"
1.44 lukem 104: echo " -U: set UNPRIVED"
1.18 tv 105: echo " -u: set UPDATE"
1.15 tv 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."
1.1 tv 110: exit 1
111: }
112:
1.10 tv 113: # Set defaults.
1.18 tv 114: MAKEFLAGS=
1.12 tv 115: buildtarget=build
1.10 tv 116: cwd=`pwd`
117: do_buildsystem=true
1.16 thorpej 118: do_buildonlytools=false
1.10 tv 119: do_rebuildmake=false
120: do_removedirs=false
1.18 tv 121: makeenv=
122: makewrapper=
1.10 tv 123: opt_a=no
1.52 ! thorpej 124: opts='a:B:bdhj:m:nortuw:D:O:R:T:U'
1.18 tv 125: runcmd=
1.4 tv 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
1.33 tv 132: type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh or bash"
1.2 tv 133:
1.4 tv 134: # Use old-style getopt(1) (doesn't handle whitespace in args).
135: args="`getopt $opts $*`"
136: [ $? = 0 ] || usage
137: set -- $args
1.1 tv 138:
1.4 tv 139: getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
140: optargcmd='OPTARG="$1"; shift'
141: fi
1.10 tv 142:
143: # Parse command line options.
1.4 tv 144: while eval $getoptcmd; do case $opt in
145: -a) eval $optargcmd
146: MACHINE_ARCH=$OPTARG; opt_a=yes;;
1.1 tv 147:
1.52 ! thorpej 148: -B) eval $optargcmd
! 149: BUILDID=$OPTARG;;
! 150:
1.10 tv 151: -b) do_buildsystem=false;;
1.9 jmc 152:
1.26 thorpej 153: -d) buildtarget=distribution;;
154:
1.4 tv 155: -j) eval $optargcmd
1.18 tv 156: MAKEFLAGS="$MAKEFLAGS NBUILDJOBS=$OPTARG"; export MAKEFLAGS;;
1.1 tv 157:
1.4 tv 158: # -m overrides MACHINE_ARCH unless "-a" is specified
159: -m) eval $optargcmd
160: MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
1.3 tv 161:
1.10 tv 162: -n) runcmd=echo;;
163:
1.12 tv 164: -o) MKOBJDIRS=no;;
165:
1.10 tv 166: -r) do_removedirs=true; do_rebuildmake=true;;
1.1 tv 167:
1.16 thorpej 168: -t) do_buildonlytools=true; do_buildsystem=false;;
169:
1.44 lukem 170: -U) UNPRIVED=yes; export UNPRIVED
171: makeenv="$makeenv UNPRIVED";;
172:
1.18 tv 173: -u) UPDATE=yes; export UPDATE
174: makeenv="$makeenv UPDATE";;
1.15 tv 175:
176: -w) eval $optargcmd; resolvepath
177: makewrapper="$OPTARG";;
1.12 tv 178:
1.10 tv 179: -D) eval $optargcmd; resolvepath
1.19 dillo 180: DESTDIR="$OPTARG"; export DESTDIR
1.18 tv 181: makeenv="$makeenv DESTDIR";;
1.4 tv 182:
1.12 tv 183: -O) eval $optargcmd; resolvepath
1.15 tv 184: MAKEOBJDIR="\${.CURDIR:C,^$cwd,$OPTARG,}"; export MAKEOBJDIR
1.27 jmc 185: makeobjdir=$OPTARG
1.18 tv 186: makeenv="$makeenv MAKEOBJDIR";;
1.12 tv 187:
1.10 tv 188: -R) eval $optargcmd; resolvepath
1.18 tv 189: RELEASEDIR=$OPTARG; export RELEASEDIR
190: makeenv="$makeenv RELEASEDIR"
1.15 tv 191: buildtarget=release;;
1.4 tv 192:
1.10 tv 193: -T) eval $optargcmd; resolvepath
1.18 tv 194: TOOLDIR="$OPTARG"; export TOOLDIR;;
1.4 tv 195:
196: --) break;;
197: -'?'|-h) usage;;
1.1 tv 198: esac; done
199:
1.15 tv 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."
1.4 tv 204: echo ""; usage
205: fi
1.15 tv 206: MACHINE=`uname -m`
207: fi
1.10 tv 208: [ -n "$MACHINE_ARCH" ] || getarch
1.47 tv 209: validatearch
1.3 tv 210:
1.15 tv 211: # Set up default make(1) environment.
1.18 tv 212: makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
1.52 ! thorpej 213: if [ ! -z "$BUILDID" ]; then
! 214: makeenv="$makeenv BUILDID"
! 215: fi
1.18 tv 216: MAKEFLAGS="-m $cwd/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
217: export MAKEFLAGS MACHINE MACHINE_ARCH
1.15 tv 218:
219: # Test make source file timestamps against installed nbmake binary,
220: # if TOOLDIR is pre-set.
1.46 tv 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: #
1.15 tv 228: make=${TOOLDIR-nonexistent}/bin/nbmake
229: if [ -x $make ]; then
1.1 tv 230: for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
1.15 tv 231: if [ $f -nt $make ]; then
1.10 tv 232: do_rebuildmake=true; break
1.1 tv 233: fi
234: done
235: else
1.10 tv 236: do_rebuildmake=true
1.1 tv 237: fi
238:
1.15 tv 239: # Build bootstrap nbmake if needed.
1.10 tv 240: if $do_rebuildmake; then
1.18 tv 241: $runcmd echo "===> Bootstrapping nbmake"
1.4 tv 242: tmpdir=${TMPDIR-/tmp}/nbbuild$$
243:
1.10 tv 244: $runcmd mkdir $tmpdir || bomb "cannot mkdir: $tmpdir"
1.41 tv 245: trap "cd /; rm -r -f $tmpdir" 0
1.4 tv 246: trap "exit 1" 1 2 3 15
1.10 tv 247: $runcmd cd $tmpdir
1.1 tv 248:
1.40 tv 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"
1.39 tv 251: $runcmd sh buildmake.sh || bomb "build of nbmake failed"
1.1 tv 252:
1.15 tv 253: make=$tmpdir/nbmake
1.10 tv 254: $runcmd cd $cwd
1.15 tv 255: $runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
256: fi
257:
1.30 jmc 258: USE_NEW_TOOLCHAIN=`getmakevar USE_NEW_TOOLCHAIN`
1.35 jmc 259: if [ "${USE_NEW_TOOLCHAIN}" = "" ]; then
1.30 jmc 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:
1.15 tv 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
1.18 tv 280: $runcmd cd tools
1.29 jmc 281: $runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= || exit 1
1.18 tv 282: $runcmd cd ..
1.15 tv 283: fi
284:
1.27 jmc 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
1.49 tv 290: $runcmd mkdir -p $makeobjdir
1.27 jmc 291: fi
292:
1.15 tv 293: # Find DESTDIR and TOOLDIR.
294: if [ "$runcmd" = "echo" ]; then
1.24 tv 295: # shown symbolically with -n because these may come from mk.conf
296: DESTDIR='$DESTDIR'
297: TOOLDIR='$TOOLDIR'
1.15 tv 298: else
299: DESTDIR=`getmakevar DESTDIR`; echo "===> DESTDIR path: $DESTDIR"
300: TOOLDIR=`getmakevar TOOLDIR`; echo "===> TOOLDIR path: $TOOLDIR"
1.18 tv 301: export DESTDIR TOOLDIR
1.15 tv 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
1.16 thorpej 311: if $do_buildsystem && \
1.26 thorpej 312: ([ "$buildtarget" != "build" ] || \
1.16 thorpej 313: [ "`uname -s 2>/dev/null`" != "NetBSD" ] || \
314: [ "`uname -m`" != "$MACHINE" ]); then
1.26 thorpej 315: bomb "DESTDIR must be set to a non-root path for cross builds or -d or -R."
1.16 thorpej 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!"
1.15 tv 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.
1.49 tv 333: mkdir -p $TOOLDIR/bin || bomb "mkdir of $TOOLDIR/bin failed"
1.15 tv 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
1.10 tv 339: $runcmd rm -r -f $tmpdir
1.4 tv 340: trap 0 1 2 3 15
1.1 tv 341: fi
342:
1.4 tv 343: # Build a nbmake wrapper script, usable by hand as well as by build.sh.
1.15 tv 344: if [ -z "$makewrapper" ]; then
345: makewrapper=$TOOLDIR/bin/nbmake-$MACHINE
1.52 ! thorpej 346: if [ ! -z "$BUILDID" ]; then
! 347: makewrapper=$makewrapper-$BUILDID
! 348: fi
1.15 tv 349: fi
1.4 tv 350:
1.21 jmc 351: $runcmd rm -f $makewrapper
352: if [ "$runcmd" = "echo" ]; then
353: echo 'cat <<EOF >'$makewrapper
354: makewrapout=
355: else
356: makewrapout=">>$makewrapper"
357: fi
1.18 tv 358:
1.21 jmc 359: eval cat <<EOF $makewrapout
1.4 tv 360: #! /bin/sh
361: # Set proper variables to allow easy "make" building of a NetBSD subtree.
1.52 ! thorpej 362: # Generated from: \$NetBSD: build.sh,v 1.51 2002/03/07 14:59:04 simonb Exp $
1.4 tv 363: #
1.18 tv 364:
365: EOF
1.21 jmc 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
1.18 tv 370:
1.21 jmc 371: eval cat <<EOF $makewrapout
1.18 tv 372:
373: exec \$TOOLDIR/bin/nbmake \${1+"\$@"}
1.4 tv 374: EOF
1.21 jmc 375: [ "$runcmd" = "echo" ] && echo EOF
376: $runcmd chmod +x $makewrapper
1.4 tv 377:
1.10 tv 378: if $do_buildsystem; then
1.15 tv 379: ${runcmd-exec} $makewrapper $buildtarget
1.16 thorpej 380: elif $do_buildonlytools; then
381: if [ "$MKOBJDIRS" != "no" ]; then
1.20 thorpej 382: $runcmd $makewrapper obj-tools || exit 1
1.16 thorpej 383: fi
1.18 tv 384: $runcmd cd tools
1.31 jmc 385: if [ "$UPDATE" = "" ]; then
386: ${runcmd-exec} $makewrapper cleandir dependall install
387: else
388: ${runcmd-exec} $makewrapper dependall install
389: fi
1.9 jmc 390: fi
CVSweb <webmaster@jp.NetBSD.org>