1 #! /bin/sh 2 # $NetBSD: build.sh,v 1.4 2001/10/24 02:39:56 tv 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" may be an ample alternative. 8 # 9 10 bomb () { 11 echo "" 12 echo "ERROR: $@" 13 echo "*** BUILD ABORTED ***" 14 exit 1 15 } 16 17 getarch () { 18 # Translate a MACHINE into a default MACHINE_ARCH. 19 case $MACHINE in 20 acorn32|cats|dnard|evbarm|hpcarm|netwinder) 21 MACHINE_ARCH=arm;; 22 23 sun2) 24 MACHINE_ARCH=m68000;; 25 26 amiga|atari|cesfic|hp300|sun3|*68k) 27 MACHINE_ARCH=m68k;; 28 29 mipsco|newsmips|sgimips) 30 MACHINE_ARCH=mipseb;; 31 32 algor|arc|cobalt|hpcmips|playstation2|pmax) 33 MACHINE_ARCH=mipsel;; 34 35 pc532) 36 MACHINE_ARCH=ns32k;; 37 38 bebox|prep|sandpoint|walnut|*ppc) 39 MACHINE_ARCH=powerpc;; 40 41 mmeye) 42 MACHINE_ARCH=sh3eb;; 43 44 dreamcast|evbsh3|hpcsh) 45 MACHINE_ARCH=sh3el;; 46 47 alpha|arm26|i386|sparc|sparc64|vax|x86_64) 48 MACHINE_ARCH=$MACHINE;; 49 50 *) bomb "unknown target MACHINE: $MACHINE";; 51 esac 52 } 53 54 # Emulate "mkdir -p" for systems that have an Old "mkdir". 55 mkdirp () { 56 IFS=/; set -- $@; unset IFS 57 _d= 58 if [ "$1" = "" ]; then _d=/; shift; fi 59 60 for _f in "$@"; do 61 if [ "$_f" != "" ]; then 62 [ -d "$_d$_f" ] || mkdir "$_d$_f" || return 1 63 _d="$_d$_f/" 64 fi 65 done 66 } 67 68 usage () { 69 echo "Usage:" 70 echo "$0 [-r] [-a arch] [-j njob] [-m mach] [-D dest] [-R release] [-T tools]" 71 echo " -m: set target MACHINE to mach (REQUIRED, or set in environment)" 72 echo " -D: set DESTDIR to dest (REQUIRED, or set in environment)" 73 echo " -T: set TOOLDIR to tools (REQUIRED, or set in environment)" 74 echo "" 75 echo " -a: set target MACHINE_ARCH to arch (otherwise deduced from MACHINE)" 76 echo " -j: set NBUILDJOBS to njob" 77 echo " -r: remove TOOLDIR and DESTDIR before the build" 78 echo " -R: build a release (set RELEASEDIR) to release" 79 exit 1 80 } 81 82 opts='a:hj:m:rD:R:T:' 83 84 if type getopts >/dev/null 2>&1; then 85 # Use POSIX getopts. 86 getoptcmd='getopts $opts opt && opt=-$opt' 87 optargcmd=':' 88 else 89 type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh" 90 91 # Use old-style getopt(1) (doesn't handle whitespace in args). 92 args="`getopt $opts $*`" 93 [ $? = 0 ] || usage 94 set -- $args 95 96 getoptcmd='[ $# -gt 0 ] && opt="$1" && shift' 97 optargcmd='OPTARG="$1"; shift' 98 fi 99 100 opt_a=no 101 while eval $getoptcmd; do case $opt in 102 -a) eval $optargcmd 103 MACHINE_ARCH=$OPTARG; opt_a=yes;; 104 105 -j) eval $optargcmd 106 buildjobs="NBUILDJOBS=$OPTARG";; 107 108 # -m overrides MACHINE_ARCH unless "-a" is specified 109 -m) eval $optargcmd 110 MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;; 111 112 -r) removedirs=true;; 113 114 -D) eval $optargcmd 115 DESTDIR="$OPTARG";; 116 117 -R) eval $optargcmd 118 releasedir="RELEASEDIR=$OPTARG"; buildtarget=release;; 119 120 -T) eval $optargcmd 121 TOOLDIR="$OPTARG";; 122 123 --) break;; 124 -'?'|-h) usage;; 125 esac; done 126 127 for var in MACHINE DESTDIR TOOLDIR; do 128 if ! eval 'test -n "$'$var'"'; then 129 echo "$var must be set in the environment before running build.sh." 130 echo ""; usage 131 fi 132 done 133 134 # Set up environment. 135 test -n "$MACHINE_ARCH" || getarch 136 test -d usr.bin/make || bomb "build.sh must be run from the top source level" 137 138 # Remove the target directories. 139 if ${removedirs-false}; then 140 echo "Removing DESTDIR and TOOLDIR...." 141 rm -rf $DESTDIR $TOOLDIR 142 fi 143 144 mkdirp $TOOLDIR/bin || bomb "mkdir of $TOOLDIR/bin failed" 145 146 # Test make source file timestamps against installed nbmake binary. 147 if [ -x $TOOLDIR/bin/nbmake ]; then 148 for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do 149 if [ $f -nt $TOOLDIR/bin/nbmake ]; then 150 rebuildmake=true; break 151 fi 152 done 153 else 154 rebuildmake=true 155 fi 156 157 # Build $TOOLDIR/bin/nbmake. 158 if ${rebuildmake-false}; then 159 echo "Building nbmake...." 160 161 # Go to a temporary directory in case building .o's happens. 162 srcdir=`pwd` 163 tmpdir=${TMPDIR-/tmp}/nbbuild$$ 164 165 mkdir $tmpdir || bomb "cannot mkdir: $tmpdir" 166 trap "rm -r -f $tmpdir" 0 167 trap "exit 1" 1 2 3 15 168 cd $tmpdir 169 170 ${HOST_CC-cc} ${HOST_CFLAGS} -DMAKE_BOOTSTRAP \ 171 -o $TOOLDIR/bin/nbmake -I$srcdir/usr.bin/make \ 172 $srcdir/usr.bin/make/*.c $srcdir/usr.bin/make/lst.lib/*.c \ 173 || bomb "build of nbmake failed" 174 175 # Clean up. 176 cd $srcdir 177 rm -r -f $tmpdir 178 trap 0 1 2 3 15 179 180 # Some compilers are just *that* braindead. 181 rm -f $srcdir/usr.bin/make/*.o $srcdir/usr.bin/make/lst.lib/*.o 182 fi 183 184 # Build a nbmake wrapper script, usable by hand as well as by build.sh. 185 makeprog=$TOOLDIR/bin/nbmake-$MACHINE 186 187 if ${rebuildmake-false} || [ ! -f $makeprog ] || [ $makeprog -ot build.sh ]; then 188 rm -f $makeprog 189 cat >$makeprog <<EOF 190 #! /bin/sh 191 # Set proper variables to allow easy "make" building of a NetBSD subtree. 192 # Generated from: \$NetBSD: build.sh,v 1.4 2001/10/24 02:39:56 tv Exp $ 193 # 194 exec $TOOLDIR/bin/nbmake MACHINE=$MACHINE MACHINE_ARCH=$MACHINE_ARCH \ 195 USETOOLS=yes USE_NEW_TOOLCHAIN=yes \${1+\$@} 196 EOF 197 chmod +x $makeprog 198 fi 199 200 exec $makeprog -m `pwd`/share/mk ${buildtarget-build} \ 201 MKTOOLS=yes DESTDIR="$DESTDIR" TOOLDIR="$TOOLDIR" \ 202 $buildjobs $releasedir 203