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>
7 # Copyright (C) 2011 by Sage Electronic Engineering
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 CROSSGCC_DATE="October 10th, 2011"
24 CROSSGCC_VERSION="1.05"
26 # To enable colors on dash(1)
27 if [ -x /usr/bin/printf ]; then
28 alias printf=/usr/bin/printf
42 GCC_AUTOCONF_VERSION=2.64
43 BINUTILS_VERSION=2.21.1
46 W32API_VERSION_SHORT=3.17
51 GMP_ARCHIVE="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
52 MPFR_ARCHIVE="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
53 MPC_ARCHIVE="http://www.multiprecision.org/mpc/download/mpc-${MPC_VERSION}.tar.gz"
54 LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
55 GCC_ARCHIVE="ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
56 BINUTILS_ARCHIVE="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
57 GDB_ARCHIVE="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
58 W32API_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/Win32-API/w32api-${W32API_VERSION_SHORT}/w32api-${W32API_VERSION}-mingw32-src.tar.lzma"
59 MINGWRT_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/MinGW-RT/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
60 IASL_ARCHIVE="http://www.acpica.org/download/acpica-unix-${IASL_VERSION}.tar.gz"
62 GMP_DIR="gmp-${GMP_VERSION}"
63 MPFR_DIR="mpfr-${MPFR_VERSION}"
64 MPC_DIR="mpc-${MPC_VERSION}"
65 LIBELF_DIR="libelf-${LIBELF_VERSION}"
66 GCC_DIR="gcc-${GCC_VERSION}"
67 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
68 GDB_DIR="gdb-${GDB_VERSION}"
69 W32API_DIR="w32api-${W32API_VERSION}-mingw32"
70 MINGWRT_DIR="mingwrt-${MINGWRT_VERSION}-mingw32"
71 IASL_DIR="acpica-unix-${IASL_VERSION}"
89 # result: GNU version of that tool on stdout
90 # or no output if no GNU version was found
91 for i in "$1" "g$1" "gnu$1"; do
92 if test -x "`which $i 2>/dev/null`"; then
93 if test `$i --version 2>/dev/null |grep -c GNU` -gt 0; then
99 printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
103 TAR=`searchgnu tar` || exit $?
104 PATCH=`searchgnu patch` || exit $?
105 MAKE=`searchgnu make` || exit $?
109 printf "Cleaning up temporary files... "
110 rm -rf build-* combined gcc-* gmp-* mpfr-* mpc-* libelf-* binutils-* gdb-* w32api-* mingwrt-* acpica-*
111 printf "${green}ok${NC}\n"
116 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-G]\n"
117 printf " $0 [-V|--version]\n"
118 printf " $0 [-h|--help]\n\n"
121 printf " [-V|--version] print version number and exit\n"
122 printf " [-h|--help] print this help and exit\n"
123 printf " [-c|--clean] remove temporary files before build\n"
124 printf " [-t|--savetemps] don't remove temporary files after build\n"
125 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
126 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
127 printf " (defaults to $TARGETARCH)\n"
128 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
129 printf " (defaults to $TARGETDIR)\n\n"
130 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
131 printf " (for RPM builds, default unset)\n\n"
132 printf " [-G|--skip-gdb] don't build GNU debugger\n"
137 # version tag is always printed, so just print the license here
140 Copyright (C) 2008-2010 by coresystems GmbH
141 Copyright (C) 2011 by Sage Electronic Engineering
143 This program is free software; you can redistribute it and/or modify
144 it under the terms of the GNU General Public License as published by
145 the Free Software Foundation; version 2 of the License.
147 This program is distributed in the hope that it will be useful,
148 but WITHOUT ANY WARRANTY; without even the implied warranty of
149 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
150 GNU General Public License for more details.
155 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
157 # Look if we have getopt. If not, build it.
159 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
161 # parse parameters.. try to find out whether we're running GNU getopt
162 getoptbrand="`getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,'`"
163 if [ "${getoptbrand}" = "getopt" ]; then
164 # Detected GNU getopt that supports long options.
165 args=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps,skip-gdb Vhcd:p:j:D:tG -- "$@"`
168 # Detected non-GNU getopt
169 args=`getopt Vhcd:p:j:D:tG $*`
180 -V|--version) shift; myversion; exit 0;;
181 -h|--help) shift; myhelp; exit 0;;
182 -c|--clean) shift; cleanup;;
183 -t|--savetemps) shift; SAVETEMPS=1;;
184 -d|--directory) shift; TARGETDIR="$1"; shift;;
185 -p|--platform) shift; TARGETARCH="$1"; shift;;
186 -D|--destdir) shift; DESTDIR="$1"; shift;;
187 -j|--jobs) shift; JOBS="-j $1"; shift;;
188 -G|--skip-gdb) shift; SKIPGDB=1;;
190 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
196 if [ $SKIPGDB -eq 1 ]; then
197 printf "Will skip GDB ... ${green}ok${NC}\n"
203 if [ "$TARGETARCH" = "i386-mingw32" ]; then
204 MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
207 # coreboot does not like the GOLD linker
208 # USE_GOLD="--enable-gold"
210 GCC_OPTIONS="--enable-lto"
212 if [ ${GCC_VERSION} = "4.6.2" ]; then
213 if [ ! -r tarballs/gcc-core-${GCC_VERSION}.tar.bz2 ]; then
214 printf "Pre-Release GCC ${GCC_VERSION}, checking out subversion trunk\n"
215 mkdir -p tarballs/.tmp
217 rm -rf gcc-${GCC_VERSION}
218 svn export -q svn://gcc.gnu.org/svn/gcc/trunk gcc-${GCC_VERSION}
219 printf "done. Now creating tar ball...\n"
220 tar cjf ../gcc-core-${GCC_VERSION}.tar.bz2 gcc-${GCC_VERSION}
221 printf "done. Now cleaning up...\n"
229 printf "Downloading tar balls ... \n"
231 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES $IASL_ARCHIVE; do
232 FILE=`basename $ARCHIVE`
234 test -f tarballs/$FILE && printf "(cached)" || (
235 printf "(downloading)"
239 test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
240 test -f tarballs/$FILE || exit 1
243 printf "Downloaded tar balls ... "
244 printf "${green}ok${NC}\n"
247 if [ "$TARGETARCH" = "i386-mingw32" ]; then
248 MINGW_PACKAGES="W32API MINGWRT"
251 printf "Unpacking and patching ... \n"
252 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS $GDB_PACKAGE $MINGW_PACKAGES IASL; do
253 archive=$PACKAGE"_ARCHIVE"
254 archive="`eval echo '$'$archive`"
256 dir="`eval echo '$'${dir}`"
258 printf " * `basename $archive`\n"
260 suffix=`echo $archive | sed 's,.*\.,,'`
261 test "$suffix" = "gz" && FLAGS=zxf
262 test "$suffix" = "bz2" && FLAGS=jxf
263 test "$suffix" = "lzma" && FLAGS="--lzma -xf"
264 $TAR $FLAGS tarballs/`basename $archive`
265 for patch in patches/${dir}_*.patch; do
266 test -r $patch || continue
267 printf " o `basename $patch`\n"
268 $PATCH -s -N -p0 < `echo $patch`
272 printf "Unpacked and patched ... "
273 printf "${green}ok${NC}\n"
275 if [ "$TARGETARCH" = "i386-mingw32" ]; then
276 mkdir -p $TARGETDIR/i386-mingw32/sys-include
277 mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
281 if [ `uname` = "Darwin" ]; then
282 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
284 # generally the OS X compiler can create x64 binaries.
285 # Per default it generated i386 binaries in 10.5 and x64
286 # binaries in 10.6 (even if the kernel is 32bit)
287 # For some weird reason, 10.5 autodetects an ABI=64 though
288 # so we're setting the ABI explicitly here.
289 if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
295 # In Xcode 4 the default compiler was switched to gcc-llvm.
296 # However, this compiler fails to compile gcc 4.6.x. As a
297 # workaround it's possible to compile gcc with gcc-4.2 or
299 if $CC -v 2>&1 | grep -q LLVM; then
304 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc
305 if [ $SKIPGDB -eq 0 ]; then
308 if [ -f build-gmp/.success ]; then
309 printf "Skipping GMP as it is already built\n"
311 printf "Building GMP ${GMP_VERSION} ... "
315 ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
317 $MAKE $JOBS || touch .failed
318 $MAKE install DESTDIR=$DESTDIR || touch .failed
319 if [ ! -f .failed ]; then touch .success; fi
320 ) > build-gmp/crossgcc-build.log 2>&1
321 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
322 test -r build-gmp/.failed && exit 1
325 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
326 # # create compat link
327 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
330 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
331 # as GCC 4.6.x fails if it's there.
332 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
335 if [ -f build-mpfr/.success ]; then
336 printf "Skipping MPFR as it is already built\n"
338 printf "Building MPFR ${MPFR_VERSION} ... "
340 test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
343 ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
344 --infodir=$TARGETDIR/info \
345 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
346 $MAKE $JOBS || touch .failed
347 $MAKE install DESTDIR=$DESTDIR || touch .failed
349 # work around build problem of libgmp.la
350 if [ "$DESTDIR" != "" ]; then
351 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
354 if [ ! -f .failed ]; then touch .success; fi
355 ) > build-mpfr/crossgcc-build.log 2>&1
356 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
357 test -r build-mpfr/.failed && exit 1
360 if [ -f build-mpc/.success ]; then
361 printf "Skipping MPC as it is already built\n"
363 printf "Building MPC ${MPC_VERSION} ... "
365 #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
368 ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
369 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
370 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
371 $MAKE $JOBS || touch .failed
372 $MAKE install DESTDIR=$DESTDIR || touch .failed
374 if [ ! -f .failed ]; then touch .success; fi
375 ) > build-mpc/crossgcc-build.log 2>&1
376 test -r build-mpc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
377 test -r build-mpc/.failed && exit 1
380 if [ -f build-libelf/.success ]; then
381 printf "Skipping libelf as it is already built\n"
383 printf "Building libelf ${LIBELF_VERSION} ... "
388 CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
389 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
390 $MAKE $JOBS || touch .failed
391 $MAKE install DESTDIR=$DESTDIR || touch .failed
393 if [ ! -f .failed ]; then touch .success; fi
394 ) > build-libelf/crossgcc-build.log 2>&1
395 test -r build-libelf/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
396 test -r build-libelf/.failed && exit 1
399 if [ -f build-binutils/.success ]; then
400 printf "Skipping binutils as it is already built\n"
402 printf "Building binutils ${BINUTILS_VERSION} ... "
404 # What a pain: binutils don't come with configure
405 # script anymore. Create it:
406 cd binutils-${BINUTILS_VERSION}/
412 ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
413 --disable-werror --disable-nls $USE_GOLD \
414 CFLAGS="$HOSTCFLAGS" || touch .failed
415 $MAKE $JOBS || touch .failed
416 $MAKE install DESTDIR=$DESTDIR || touch .failed
417 if [ ! -f .failed ]; then touch .success; fi
418 ) > build-binutils/crossgcc-build.log 2>&1
419 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
420 test -r build-binutils/.failed && exit 1
423 if [ -f build-gcc/.success ]; then
424 printf "Skipping GCC as it is already built\n"
426 printf "Building GCC ${GCC_VERSION} ... "
428 # Even worse than binutils: GCC does not come with configure
429 # script anymore, but also enforces an obsolete autoconf version
430 # to create it. This is a poster child of how autotools help make
432 cd gcc-${GCC_VERSION}
433 sed '/dnl Ensure exactly this Autoconf version is used/d' \
434 config/override.m4 > config/override.m4.new
435 autoconf_version=`autoconf -V | grep "autoconf" | tr ' ' '\n' | tail -1`
436 sed "s/${GCC_AUTOCONF_VERSION}/${autoconf_version}/g" \
437 config/override.m4.new > config/override.m4
440 # Now, finally, we can build gcc:
442 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
444 # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
445 # both target and host object files. This is pretty misdesigned.
446 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
447 # but it does not seem to work properly. At least the host library
448 # libiberty is not compiled with CFLAGS_FOR_BUILD.
449 CC="$CC" CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" \
450 CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
451 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
452 --target=${TARGETARCH} --disable-werror --disable-shared \
453 --disable-libssp --disable-bootstrap --disable-nls \
454 --disable-libquadmath \
455 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
456 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
457 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
458 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
460 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
461 $MAKE install DESTDIR=$DESTDIR || touch .failed
462 if [ ! -f .failed ]; then touch .success; fi
463 ) > build-gcc/crossgcc-build.log 2>&1
464 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
465 test -r build-gcc/.failed && exit 1
468 if [ -f build-gdb/.success ]; then
469 printf "Skipping GDB as it is already built\n"
470 elif [ $SKIPGDB -eq 1 ]; then
471 printf "Skipping GDB as requested by command line\n"
473 printf "Building GDB ${GDB_VERSION} ... "
476 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
478 CFLAGS="$HOSTCFLAGS" ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
479 --without-python --disable-werror --disable-nls
480 $MAKE $JOBS || touch .failed
481 $MAKE install DESTDIR=$DESTDIR || touch .failed
482 if [ ! -f .failed ]; then touch .success; fi
483 ) > build-gdb/crossgcc-build.log 2>&1
484 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
485 test -r build-gdb/.failed && exit 1
488 if [ -f $IASL_DIR/compiler/.success ]; then
489 printf "Skipping IASL as it is already built\n"
491 printf "Building IASL ${IASL_VERSION} ... "
493 cd $IASL_DIR/compiler
494 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
497 $MAKE || touch .failed
498 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch .failed
499 cp iasl $DESTDIR$TARGETDIR/bin || touch .failed
500 if [ ! -f .failed ]; then touch .success; fi
501 ) > $IASL_DIR/compiler/crossgcc-build.log 2>&1
502 test -r $IASL_DIR/compiler/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
503 test -r $IASL_DIR/compiler/.failed && exit 1
506 if [ $SAVETEMPS -eq 0 ]; then
507 printf "Cleaning up... "
508 rm -rf ${GMP_DIR} build-gmp
509 rm -rf ${MPFR_DIR} build-mpfr
510 rm -rf ${MPC_DIR} build-mpc
511 rm -rf ${LIBELF_DIR} build-libelf
512 rm -rf ${BINUTILS_DIR} build-binutils
513 rm -rf ${GCC_DIR} build-gcc
514 rm -rf ${GDB_DIR} build-gdb
516 printf "${green}ok${NC}\n"
518 printf "Leaving temporary files around... ${green}ok${NC}\n"
521 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"