buildgcc: Add option to use ccache
[coreboot.git] / util / crossgcc / buildgcc
1 #!/bin/sh
2 #
3 # Copyright (C) 2008-2010 by coresystems GmbH
4 # written by Patrick Georgi <patrick.georgi@coresystems.de> and
5 #            Stefan Reinauer <stefan.reinauer@coresystems.de>
6 #
7 # Copyright (C) 2011 by Sage Electronic Engineering
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA
21 #
22
23 CROSSGCC_DATE="November 1st, 2011"
24 CROSSGCC_VERSION="1.07"
25
26 # default settings
27 TARGETDIR=`pwd`/xgcc
28 TARGETARCH=i386-elf
29 DESTDIR=
30
31 # version numbers
32 GMP_VERSION=5.0.2
33 MPFR_VERSION=3.1.0
34 MPC_VERSION=0.9
35 LIBELF_VERSION=0.8.13
36 GCC_VERSION=4.6.2
37 GCC_AUTOCONF_VERSION=2.64
38 BINUTILS_VERSION=2.21.1
39 GDB_VERSION=7.3.1
40 W32API_VERSION=3.17-2
41 W32API_VERSION_SHORT=3.17
42 MINGWRT_VERSION=3.18
43 IASL_VERSION=20110922
44 PYTHON_VERSION=2.7.2
45 EXPAT_VERSION=2.0.1
46
47 # archive locations
48 GMP_ARCHIVE="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
49 MPFR_ARCHIVE="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
50 MPC_ARCHIVE="http://www.multiprecision.org/mpc/download/mpc-${MPC_VERSION}.tar.gz"
51 LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
52 GCC_ARCHIVE="ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
53 BINUTILS_ARCHIVE="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
54 GDB_ARCHIVE="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
55 W32API_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/Win32-API/w32api-${W32API_VERSION_SHORT}/w32api-${W32API_VERSION}-mingw32-src.tar.lzma"
56 MINGWRT_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/MinGW-RT/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
57 IASL_ARCHIVE="http://www.acpica.org/download/acpica-unix-${IASL_VERSION}.tar.gz"
58 PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.bz2"
59 EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.gz"
60
61 GMP_DIR="gmp-${GMP_VERSION}"
62 MPFR_DIR="mpfr-${MPFR_VERSION}"
63 MPC_DIR="mpc-${MPC_VERSION}"
64 LIBELF_DIR="libelf-${LIBELF_VERSION}"
65 GCC_DIR="gcc-${GCC_VERSION}"
66 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
67 GDB_DIR="gdb-${GDB_VERSION}"
68 W32API_DIR="w32api-${W32API_VERSION}-mingw32"
69 MINGWRT_DIR="mingwrt-${MINGWRT_VERSION}-mingw32"
70 IASL_DIR="acpica-unix-${IASL_VERSION}"
71 PYTHON_DIR="Python-${PYTHON_VERSION}"
72 EXPAT_DIR="expat-${EXPAT_VERSION}"
73
74 SAVETEMPS=0
75 SKIPGDB=1
76 SKIPPYTHON=1
77
78 red='\033[0;31m'
79 RED='\033[1;31m'
80 green='\033[0;32m'
81 GREEN='\033[1;32m'
82 blue='\033[0;34m'
83 BLUE='\033[1;34m'
84 cyan='\033[0;36m'
85 CYAN='\033[1;36m'
86 NC='\033[0m' # No Color
87
88 searchgnu()
89 {
90         # $1 short name
91         # result: GNU version of that tool on stdout
92         #         or no output if no GNU version was found
93         for i in "$1" "g$1" "gnu$1"; do
94                 if test -x "`which $i 2>/dev/null`"; then
95                         if test `$i --version 2>/dev/null |grep -c GNU` \
96                             -gt 0; then
97                                 echo $i
98                                 return
99                         fi
100                 fi
101         done
102         printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
103         exit 1
104 }
105
106 TAR=`searchgnu tar` || exit $?
107 PATCH=`searchgnu patch` || exit $?
108 MAKE=`searchgnu make` || exit $?
109
110 cleanup()
111 {
112         printf "Cleaning up temporary files... "
113         rm -rf build-* combined gcc-* gmp-* mpfr-* mpc-* libelf-* binutils-*
114         rm -rf gdb-* w32api-* mingwrt-* acpica-* python-* expat-*
115         printf "${green}ok${NC}\n"
116 }
117
118 myhelp()
119 {
120         printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-G] [-S]\n"
121         printf "       $0 [-V|--version]\n"
122         printf "       $0 [-h|--help]\n\n"
123
124         printf "Options:\n"
125         printf "    [-V|--version]                print version number and exit\n"
126         printf "    [-h|--help]                   print this help and exit\n"
127         printf "    [-c|--clean]                  remove temporary files before build\n"
128         printf "    [-t|--savetemps]              don't remove temporary files after build\n"
129         printf "    [-y|--ccache]                 Use ccache when building cross compiler\n"
130         printf "    [-j|--jobs <num>]             run <num> jobs in parallel in make\n"
131         printf "    [-p|--platform <platform>]    target platform to build cross compiler for\n"
132         printf "                                  (defaults to $TARGETARCH)\n"
133         printf "    [-d|--directory <target dir>] target directory to install cross compiler to\n"
134         printf "                                  (defaults to $TARGETDIR)\n\n"
135         printf "    [-D|--destdir <dest dir>]     destination directory to install cross compiler to\n"
136         printf "                                  (for RPM builds, default unset)\n"
137         printf "    [-G|--gdb]                    build GNU debugger\n"
138         printf "    [-S|--scripting]              build scripting support for GDB\n\n"
139 }
140
141 myversion()
142 {
143         # version tag is always printed, so just print the license here
144
145         cat << EOF
146 Copyright (C) 2008-2010 by coresystems GmbH
147 Copyright (C) 2011 by Sage Electronic Engineering
148
149 This program is free software; you can redistribute it and/or modify
150 it under the terms of the GNU General Public License as published by
151 the Free Software Foundation; version 2 of the License.
152
153 This program is distributed in the hope that it will be useful,
154 but WITHOUT ANY WARRANTY; without even the implied warranty of
155 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
156 GNU General Public License for more details.
157
158 EOF
159 }
160
161 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
162
163 # Look if we have getopt. If not, build it.
164 export PATH=$PATH:.
165 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
166
167 # parse parameters.. try to find out whether we're running GNU getopt
168 getoptbrand="`getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,'`"
169 if [ "${getoptbrand}" = "getopt" ]; then
170         # Detected GNU getopt that supports long options.
171         args=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps,skip-gdb,ccache Vhcd:p:j:D:tGy -- "$@"`
172         eval set "$args"
173 else
174         # Detected non-GNU getopt
175         args=`getopt Vhcd:p:j:D:tGy $*`
176         set -- $args
177 fi
178
179 if [ $? != 0 ]; then
180         myhelp
181         exit 1
182 fi
183
184 while true ; do
185         case "$1" in
186                 -V|--version)   shift; myversion; exit 0;;
187                 -h|--help)      shift; myhelp; exit 0;;
188                 -c|--clean)     shift; cleanup;;
189                 -t|--savetemps) shift; SAVETEMPS=1;;
190                 -d|--directory) shift; TARGETDIR="$1"; shift;;
191                 -p|--platform)  shift; TARGETARCH="$1"; shift;;
192                 -D|--destdir)   shift; DESTDIR="$1"; shift;;
193                 -j|--jobs)      shift; JOBS="-j $1"; shift;;
194                 -G|--gdb)       shift; SKIPGDB=0;;
195                 -S|--scripting) shift; SKIPPYTHON=0;;
196                 -y|--ccache)    shift; USECCACHE=1;;
197                 --)             shift; break;;
198                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
199                 *)              break;;
200         esac
201 done
202
203 GDB_PACKAGE="GDB"
204 PYTHON_PACKAGE="PYTHON"
205 EXPAT_PACKAGE="EXPAT"
206 if [ $SKIPGDB -eq 1 ]; then
207         printf "Will skip GDB ... ${green}ok${NC}\n"
208         GDB_ARCHIVE=""
209         GDB_PACKAGE=""
210         if [ $SKIPPYTHON -eq 0 ]; then
211                 printf "Python scripting needs GDB ... disabling ... ${green}ok${NC}\n"
212                 SKIPPYTHON=1
213         fi
214 fi
215 if [ $SKIPPYTHON -eq 1 ]; then
216         PYTHON_ARCHIVE=""
217         PYTHON_PACKAGE=""
218         EXPAT_ARCHIVE=""
219         EXPAT_PACKAGE=""
220 fi
221
222 MINGW_ARCHIVES=""
223 if [ "$TARGETARCH" = "i386-mingw32" ]; then
224         MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
225 fi
226
227 # coreboot does not like the GOLD linker
228 # USE_GOLD="--enable-gold"
229 USE_GOLD=""
230 GCC_OPTIONS="--enable-lto"
231
232 if [ ${GCC_VERSION} = "trunk" ]; then
233   if [ ! -r tarballs/gcc-core-${GCC_VERSION}.tar.bz2 ]; then
234     printf "Pre-Release GCC ${GCC_VERSION}, checking out subversion trunk\n"
235     mkdir -p tarballs/.tmp
236     cd tarballs/.tmp
237     rm -rf gcc-${GCC_VERSION}
238     svn export -q svn://gcc.gnu.org/svn/gcc/trunk gcc-${GCC_VERSION}
239     printf "done. Now creating tar ball...\n"
240     tar cjf ../gcc-core-${GCC_VERSION}.tar.bz2 gcc-${GCC_VERSION}
241     printf "done. Now cleaning up...\n"
242     cd ..
243     rm -rf .tmp
244     cd ..
245     printf "done.\n"
246   fi
247 fi
248
249 printf "Downloading tar balls ... \n"
250 mkdir -p tarballs
251 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE  \
252             $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES \
253             $IASL_ARCHIVE $PYTHON_ARCHIVE $EXPAT_ARCHIVE; do
254         FILE=`basename $ARCHIVE`
255         printf " * $FILE "
256         test -f tarballs/$FILE && printf "(cached)" || (
257                 printf "(downloading)"
258                 cd tarballs
259                 wget -q $ARCHIVE
260         )
261         test -f tarballs/$FILE || \
262                 printf "\n${RED}Failed to download $FILE.${red}\n"
263         test -f tarballs/$FILE || exit 1
264         printf "\n"
265 done
266 printf "Downloaded tar balls ... "
267 printf "${green}ok${NC}\n"
268
269 MINGW_PACKAGES=""
270 if [ "$TARGETARCH" = "i386-mingw32" ]; then
271         MINGW_PACKAGES="W32API MINGWRT"
272 fi
273
274 printf "Unpacking and patching ... \n"
275 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS $PYTHON_PACKAGE \
276             $EXPAT_PACKAGE $GDB_PACKAGE $MINGW_PACKAGES IASL; do
277         archive=$PACKAGE"_ARCHIVE"
278         archive="`eval echo '$'$archive`"
279         dir=$PACKAGE"_DIR"
280         dir="`eval echo '$'${dir}`"
281         test -d ${dir} || (
282                 printf " * `basename $archive`\n"
283                 FLAGS=zxf
284                 suffix=`echo $archive | sed 's,.*\.,,'`
285                 test "$suffix" = "gz" && FLAGS=zxf
286                 test "$suffix" = "bz2" && FLAGS=jxf
287                 test "$suffix" = "lzma" && FLAGS="--lzma -xf"
288                 $TAR $FLAGS tarballs/`basename $archive`
289                 for patch in patches/${dir}_*.patch; do
290                         test -r $patch || continue
291                         printf "   o `basename $patch`\n"
292                         $PATCH -s -N -p0 < `echo $patch` || \
293                                 printf "\n${RED}Failed $patch.${red}\n"
294                 done
295         )
296 done
297 printf "Unpacked and patched ... "
298 printf "${green}ok${NC}\n"
299
300 if [ "$TARGETARCH" = "i386-mingw32" ]; then
301         mkdir -p $TARGETDIR/i386-mingw32/sys-include
302         mv $MINGWRT_DIR/include/* $W32API_DIR/include/* \
303                                   $TARGETDIR/i386-mingw32/sys-include
304 fi
305
306 CC=cc
307 if [ `uname` = "Darwin" ]; then
308         #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
309
310         # generally the OS X compiler can create x64 binaries.
311         # Per default it generated i386 binaries in 10.5 and x64
312         # binaries in 10.6 (even if the kernel is 32bit)
313         # For some weird reason, 10.5 autodetects an ABI=64 though
314         # so we're setting the ABI explicitly here.
315         if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
316                 OPTIONS="ABI=64"
317         else
318                 OPTIONS="ABI=32"
319         fi
320
321         # In Xcode 4 the default compiler was switched to gcc-llvm.
322         # However, this compiler fails to compile gcc 4.6.x. As a
323         # workaround it's possible to compile gcc with gcc-4.2 or
324         # clang.
325         if $CC -v 2>&1 | grep -q LLVM; then
326                 CC=clang
327         fi
328 fi
329
330 if [ "$USECCACHE" = 1 ]; then
331         CC="ccache $CC"
332 fi
333
334 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils \
335                 build-gcc build-python build-expat
336 if [ $SKIPGDB -eq 0 ]; then
337         mkdir -p build-gdb
338 fi
339 if [ -f build-gmp/.success ]; then
340         printf "Skipping GMP as it is already built\n"
341 else
342 printf "Building GMP ${GMP_VERSION} ... "
343 (
344         cd build-gmp
345         rm -f .failed
346         CC="$CC" ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
347                 || touch .failed
348         $MAKE $JOBS || touch .failed
349         $MAKE install DESTDIR=$DESTDIR || touch .failed
350         if [ ! -f .failed ]; then touch .success; fi
351 ) > build-gmp/crossgcc-build.log 2>&1
352 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || \
353         printf "${green}ok${NC}\n"
354 test -r build-gmp/.failed && exit 1
355 fi
356
357 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
358 #       # create compat link
359 #       ln -s $DESTDIR$TARGETDIR $TARGETDIR
360 #fi
361
362 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
363 # as GCC 4.6.x fails if it's there.
364 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
365             sed s,-pedantic,,`
366
367 if [ -f build-mpfr/.success ]; then
368         printf "Skipping MPFR as it is already built\n"
369 else
370 printf "Building MPFR ${MPFR_VERSION} ... "
371 (
372         test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
373         cd build-mpfr
374         rm -f .failed
375         CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
376                 --infodir=$TARGETDIR/info \
377                 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
378                 touch .failed
379         $MAKE $JOBS || touch .failed
380         $MAKE install DESTDIR=$DESTDIR || touch .failed
381
382         # work around build problem of libgmp.la
383         if [ "$DESTDIR" != "" ]; then
384             perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
385         fi
386
387         if [ ! -f .failed ]; then touch .success; fi
388 ) > build-mpfr/crossgcc-build.log 2>&1
389 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || \
390              printf "${green}ok${NC}\n"
391 test -r build-mpfr/.failed && exit 1
392 fi
393
394 if [ -f build-mpc/.success ]; then
395         printf "Skipping MPC as it is already built\n"
396 else
397 printf "Building MPC ${MPC_VERSION} ... "
398 (
399         #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
400         cd build-mpc
401         rm -f .failed
402         CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
403                 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
404                 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
405                 touch .failed
406         $MAKE $JOBS || touch .failed
407         $MAKE install DESTDIR=$DESTDIR || touch .failed
408
409         if [ ! -f .failed ]; then touch .success; fi
410 ) > build-mpc/crossgcc-build.log 2>&1
411 test -r build-mpc/.failed && printf "${RED}failed${NC}\n" || \
412              printf "${green}ok${NC}\n"
413 test -r build-mpc/.failed && exit 1
414 fi
415
416 if [ -f build-libelf/.success ]; then
417         printf "Skipping libelf as it is already built\n"
418 else
419 printf "Building libelf ${LIBELF_VERSION} ... "
420 (
421         cd build-libelf
422         rm -f .failed
423         echo "$HOSTCFLAGS"
424         CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
425         ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
426                 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
427         $MAKE $JOBS || touch .failed
428         $MAKE install DESTDIR=$DESTDIR || touch .failed
429
430         if [ ! -f .failed ]; then touch .success; fi
431 ) > build-libelf/crossgcc-build.log 2>&1
432 test -r build-libelf/.failed && printf "${RED}failed${NC}\n" || \
433              printf "${green}ok${NC}\n"
434 test -r build-libelf/.failed && exit 1
435 fi
436
437 if [ -f build-binutils/.success ]; then
438         printf "Skipping binutils as it is already built\n"
439 else
440 printf "Building binutils ${BINUTILS_VERSION} ... "
441 (
442         # What a pain: binutils don't come with configure
443         # script anymore. Create it:
444         cd binutils-${BINUTILS_VERSION}/
445         autoconf
446         cd ..
447         # Now build binutils
448         cd build-binutils
449         rm -f .failed
450         CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
451                 --target=${TARGETARCH} --disable-werror --disable-nls \
452                 $USE_GOLD CFLAGS="$HOSTCFLAGS" || touch .failed
453         $MAKE $JOBS || touch .failed
454         $MAKE install DESTDIR=$DESTDIR || touch .failed
455         if [ ! -f .failed ]; then touch .success; fi
456 ) > build-binutils/crossgcc-build.log 2>&1
457 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || \
458              printf "${green}ok${NC}\n"
459 test -r build-binutils/.failed && exit 1
460 fi
461
462 if [ -f build-gcc/.success ]; then
463         printf "Skipping GCC as it is already built\n"
464 else
465 printf "Building GCC ${GCC_VERSION} ... "
466 (
467         # Even worse than binutils: GCC does not come with configure
468         # script anymore, but also enforces an obsolete autoconf version
469         # to create it. This is a poster child of how autotools help make
470         # software portable.
471         cd gcc-${GCC_VERSION}
472         sed '/dnl Ensure exactly this Autoconf version is used/d' \
473                 config/override.m4 > config/override.m4.new
474         autoconf_version=`autoconf -V | grep "autoconf" | tr ' ' '\n' | tail -1`
475         sed "s/${GCC_AUTOCONF_VERSION}/${autoconf_version}/g" \
476                 config/override.m4.new > config/override.m4
477         autoconf
478         cd ..
479         # Now, finally, we can build gcc:
480         cd build-gcc
481         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
482         rm -f .failed
483         # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
484         # both target and host object files. This is pretty misdesigned.
485         # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
486         # but it does not seem to work properly. At least the host library
487         # libiberty is not compiled with CFLAGS_FOR_BUILD.
488         CC="$CC" CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" \
489                 CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
490                 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
491                 --target=${TARGETARCH} --disable-werror --disable-shared \
492                 --disable-libssp --disable-bootstrap --disable-nls \
493                 --disable-libquadmath \
494                 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
495                 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
496                 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
497                 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
498                 || touch .failed
499         $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
500         $MAKE install DESTDIR=$DESTDIR || touch .failed
501         if [ ! -f .failed ]; then touch .success; fi
502 ) > build-gcc/crossgcc-build.log 2>&1
503 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || \
504              printf "${green}ok${NC}\n"
505 test -r build-gcc/.failed && exit 1
506 fi
507
508 if [ -f build-expat/.success ]; then
509         printf "Skipping Expat as it is already built\n"
510 elif [ $SKIPPYTHON -eq 1 ]; then
511         printf "Skipping Expat (Python scripting not enabled)\n"
512 else
513 printf "Building Expat ${EXPAT_VERSION} ... "
514 (
515         cd build-expat
516         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
517         rm -f .failed
518         CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
519                 --prefix=$TARGETDIR --target=${TARGETARCH} || touch .failed
520         $MAKE || touch .failed
521         $MAKE install DESTDIR=$DESTDIR || touch .failed
522         if [ ! -f .failed ]; then touch .success; fi
523 ) &> build-expat/crossgcc-build.log
524 test -r build-expat/.failed && printf "${RED}failed${NC}\n" || \
525              printf "${green}ok${NC}\n"
526 test -r build-expat/.failed && exit 1
527 fi
528
529
530 if [ -f build-python/.success ]; then
531         printf "Skipping Python as it is already built\n"
532 elif [ $SKIPPYTHON -eq 1 ]; then
533         printf "Skipping Python (Python scripting not enabled)\n"
534 else
535 printf "Building Python ${PYTHON_VERSION} ... "
536 (
537         cd build-python
538         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
539         rm -f .failed
540         CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
541                 --target=${TARGETARCH} || touch .failed
542         $MAKE $JOBS || touch .failed
543         $MAKE install DESTDIR=$DESTDIR || touch .failed
544         if [ ! -f .failed ]; then touch .success; fi
545 ) &> build-gdb/crossgcc-build.log
546 test -r build-python/.failed && printf "${RED}failed${NC}\n" || \
547              printf "${green}ok${NC}\n"
548 test -r build-python/.failed && exit 1
549 fi
550
551
552 if [ -f build-gdb/.success ]; then
553         printf "Skipping GDB as it is already built\n"
554 elif [ $SKIPGDB -eq 1 ]; then
555         printf "Skipping GDB (GDB support not enabled)\n"
556 else
557 printf "Building GDB ${GDB_VERSION} ... "
558 (
559         cd build-gdb
560         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
561         export PYTHONHOME=$DESTDIR$TARGETDIR
562         rm -f .failed
563         LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
564                  -lpthread -ldl -lutil" \
565         CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
566         ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR \
567                 --target=${TARGETARCH} --disable-werror --disable-nls
568         $MAKE $JOBS || touch .failed
569         $MAKE install DESTDIR=$DESTDIR || touch .failed
570         if [ ! -f .failed ]; then touch .success; fi
571 ) > build-gdb/crossgcc-build.log 2>&1
572 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || \
573              printf "${green}ok${NC}\n"
574 test -r build-gdb/.failed && exit 1
575 fi
576
577 if [ -f $IASL_DIR/compiler/.success ]; then
578         printf "Skipping IASL as it is already built\n"
579 else
580 printf "Building IASL ${IASL_VERSION} ... "
581 (
582         cd $IASL_DIR/compiler
583         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
584         rm -f .failed
585         CFLAGS="$HOSTCFLAGS"
586         $MAKE CC="$CC" || touch .failed
587         rm -f $DESTDIR$TARGETDIR/bin/iasl || touch .failed
588         cp iasl $DESTDIR$TARGETDIR/bin || touch .failed
589         if [ ! -f .failed ]; then touch .success; fi
590 ) > $IASL_DIR/compiler/crossgcc-build.log 2>&1
591 test -r $IASL_DIR/compiler/.failed && printf "${RED}failed${NC}\n" || \
592              printf "${green}ok${NC}\n"
593 test -r $IASL_DIR/compiler/.failed && exit 1
594 fi
595
596 if [ $SAVETEMPS -eq 0 ]; then
597         printf "Cleaning up... "
598         rm -rf ${GMP_DIR} build-gmp
599         rm -rf ${MPFR_DIR} build-mpfr
600         rm -rf ${MPC_DIR} build-mpc
601         rm -rf ${LIBELF_DIR} build-libelf
602         rm -rf ${BINUTILS_DIR} build-binutils
603         rm -rf ${GCC_DIR} build-gcc
604         rm -rf ${GDB_DIR} build-gdb
605         rm -rf ${EXPAT_DIR} build-expat
606         rm -rf ${PYTHON_DIR} build-python
607         rm -rf ${IASL_DIR}
608         printf "${green}ok${NC}\n"
609 else
610         printf "Leaving temporary files around... ${green}ok${NC}\n"
611 fi
612
613 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"
614
615