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="August 8th, 2011"
24 CROSSGCC_VERSION="1.04"
37 BINUTILS_VERSION=2.20.1
40 W32API_VERSION_SHORT=3.17
45 GMP_ARCHIVE="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
46 MPFR_ARCHIVE="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
47 MPC_ARCHIVE="http://www.multiprecision.org/mpc/download/mpc-${MPC_VERSION}.tar.gz"
48 LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
49 GCC_ARCHIVE="ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
50 BINUTILS_ARCHIVE="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
51 GDB_ARCHIVE="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
52 W32API_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/Win32-API/w32api-${W32API_VERSION_SHORT}/w32api-${W32API_VERSION}-mingw32-src.tar.lzma"
53 MINGWRT_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/MinGW-RT/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
54 IASL_ARCHIVE="http://www.acpica.org/download/acpica-unix-${IASL_VERSION}.tar.gz"
56 GMP_DIR="gmp-${GMP_VERSION}"
57 MPFR_DIR="mpfr-${MPFR_VERSION}"
58 MPC_DIR="mpc-${MPC_VERSION}"
59 LIBELF_DIR="libelf-${LIBELF_VERSION}"
60 GCC_DIR="gcc-${GCC_VERSION}"
61 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
62 GDB_DIR="gdb-${GDB_VERSION}"
63 W32API_DIR="w32api-${W32API_VERSION}-mingw32"
64 MINGWRT_DIR="mingwrt-${MINGWRT_VERSION}-mingw32"
65 IASL_DIR="acpica-unix-${IASL_VERSION}"
83 # result: GNU version of that tool on stdout
84 # or no output if no GNU version was found
85 for i in "$1" "g$1" "gnu$1"; do
86 if test -x "`which $i 2>/dev/null`"; then
87 if test `$i --version 2>/dev/null |grep -c GNU` -gt 0; then
93 printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
97 TAR=`searchgnu tar` || exit $?
98 PATCH=`searchgnu patch` || exit $?
99 MAKE=`searchgnu make` || exit $?
103 printf "Cleaning up temporary files... "
104 rm -rf build-* combined gcc-* gmp-* mpfr-* mpc-* libelf-* binutils-* gdb-* w32api-* mingwrt-* acpica-*
105 printf "${green}ok${NC}\n"
110 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-G]\n"
111 printf " $0 [-V|--version]\n"
112 printf " $0 [-h|--help]\n\n"
115 printf " [-V|--version] print version number and exit\n"
116 printf " [-h|--help] print this help and exit\n"
117 printf " [-c|--clean] remove temporary files before build\n"
118 printf " [-t|--savetemps] don't remove temporary files after build\n"
119 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
120 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
121 printf " (defaults to $TARGETARCH)\n"
122 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
123 printf " (defaults to $TARGETDIR)\n\n"
124 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
125 printf " (for RPM builds, default unset)\n\n"
126 printf " [-G|--skip-gdb] don't build GNU debugger\n"
131 # version tag is always printed, so just print the license here
134 Copyright (C) 2008-2010 by coresystems GmbH
135 Copyright (C) 2011 by Sage Electronic Engineering
137 This program is free software; you can redistribute it and/or modify
138 it under the terms of the GNU General Public License as published by
139 the Free Software Foundation; version 2 of the License.
141 This program is distributed in the hope that it will be useful,
142 but WITHOUT ANY WARRANTY; without even the implied warranty of
143 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
144 GNU General Public License for more details.
149 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
151 # Look if we have getopt. If not, build it.
153 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
155 # parse parameters.. try to find out whether we're running GNU getopt
156 getoptbrand="`getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,'`"
157 if [ "${getoptbrand}" = "getopt" ]; then
158 # Detected GNU getopt that supports long options.
159 args=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps,skip-gdb Vhcd:p:j:D:tG -- "$@"`
162 # Detected non-GNU getopt
163 args=`getopt Vhcd:p:j:D:tG $*`
174 -V|--version) shift; myversion; exit 0;;
175 -h|--help) shift; myhelp; exit 0;;
176 -c|--clean) shift; cleanup;;
177 -t|--savetemps) shift; SAVETEMPS=1;;
178 -d|--directory) shift; TARGETDIR="$1"; shift;;
179 -p|--platform) shift; TARGETARCH="$1"; shift;;
180 -D|--destdir) shift; DESTDIR="$1"; shift;;
181 -j|--jobs) shift; JOBS="-j $1"; shift;;
182 -G|--skip-gdb) shift; SKIPGDB=1;;
184 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
190 if [ $SKIPGDB -eq 1 ]; then
191 printf "Will skip GDB ... ${green}ok${NC}\n"
196 if [ "$TARGETARCH" = "i386-mingw32" ]; then
197 MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
200 if [ ${GCC_VERSION} = "4.5.0" -o ${GCC_VERSION} = "4.6.0" ]; then
201 # coreboot does not like the GOLD linker
202 # USE_GOLD="--enable-gold"
204 GCC_OPTIONS="--enable-lto"
207 if [ ${GCC_VERSION} = "4.6.0" ]; then
208 if [ ! -r tarballs/gcc-core-${GCC_VERSION}.tar.bz2 ]; then
209 printf "Pre-Release GCC ${GCC_VERSION}, checking out subversion trunk\n"
210 mkdir -p tarballs/.tmp
212 rm -rf gcc-${GCC_VERSION}
213 svn export -q svn://gcc.gnu.org/svn/gcc/trunk gcc-${GCC_VERSION}
214 printf "done. Now creating tar ball...\n"
215 tar cjf ../gcc-core-${GCC_VERSION}.tar.bz2 gcc-${GCC_VERSION}
216 printf "done. Now cleaning up...\n"
224 printf "Downloading tar balls ... \n"
226 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES $IASL_ARCHIVE; do
227 FILE=`basename $ARCHIVE`
229 test -f tarballs/$FILE && printf "(cached)" || (
230 printf "(downloading)"
234 test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
235 test -f tarballs/$FILE || exit 1
238 printf "Downloaded tar balls ... "
239 printf "${green}ok${NC}\n"
242 if [ "$TARGETARCH" = "i386-mingw32" ]; then
243 MINGW_PACKAGES="W32API MINGWRT"
246 printf "Unpacking and patching ... \n"
247 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS $GDB_PACKAGE $MINGW_PACKAGES IASL; do
248 archive=$PACKAGE"_ARCHIVE"
249 archive="`eval echo '$'$archive`"
251 dir="`eval echo '$'${dir}`"
253 printf " * `basename $archive`\n"
255 suffix=`echo $archive | sed 's,.*\.,,'`
256 test "$suffix" = "gz" && FLAGS=zxf
257 test "$suffix" = "bz2" && FLAGS=jxf
258 test "$suffix" = "lzma" && FLAGS="--lzma -xf"
259 $TAR $FLAGS tarballs/`basename $archive`
260 for patch in patches/${dir}_*.patch; do
261 test -r $patch || continue
262 printf " o `basename $patch`\n"
263 $PATCH -s -N -p0 < `echo $patch`
267 printf "Unpacked and patched ... "
268 printf "${green}ok${NC}\n"
270 if [ "$TARGETARCH" = "i386-mingw32" ]; then
271 mkdir -p $TARGETDIR/i386-mingw32/sys-include
272 mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
275 if [ `uname` = "Darwin" ]; then
276 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
278 # generally the OS X compiler can create x64 binaries.
279 # Per default it generated i386 binaries in 10.5 and x64
280 # binaries in 10.6 (even if the kernel is 32bit)
281 # For some weird reason, 10.5 autodetects an ABI=64 though
282 # so we're setting the ABI explicitly here.
283 if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
290 #touch .architecture_check.c
291 #gcc .architecture_check.c -c -o .architecture_check.o
292 #ARCH=`file .architecture_check.o |cut -f5 -d\ `
293 #test "$ARCH" = "x86_64" && OPTIONS="ABI=64"
294 #rm .architecture_check.c .architecture_check.o
297 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc
298 if [ $SKIPGDB -eq 0 ]; then
301 if [ -f build-gmp/.success ]; then
302 printf "Skipping GMP as it is already built\n"
304 printf "Building GMP ${GMP_VERSION} ... "
308 ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
310 $MAKE $JOBS || touch .failed
311 $MAKE install DESTDIR=$DESTDIR || touch .failed
312 if [ ! -f .failed ]; then touch .success; fi
313 ) > build-gmp/crossgcc-build.log 2>&1
314 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
315 test -r build-gmp/.failed && exit 1
318 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
319 # # create compat link
320 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
323 # Now set CFLAGS to match GMP CFLAGS.
324 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
326 if [ -f build-mpfr/.success ]; then
327 printf "Skipping MPFR as it is already built\n"
329 printf "Building MPFR ${MPFR_VERSION} ... "
331 test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
334 ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
335 --infodir=$TARGETDIR/info \
336 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
337 $MAKE $JOBS || touch .failed
338 $MAKE install DESTDIR=$DESTDIR || touch .failed
340 # work around build problem of libgmp.la
341 if [ "$DESTDIR" != "" ]; then
342 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
345 if [ ! -f .failed ]; then touch .success; fi
346 ) > build-mpfr/crossgcc-build.log 2>&1
347 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
348 test -r build-mpfr/.failed && exit 1
351 if [ -f build-mpc/.success ]; then
352 printf "Skipping MPC as it is already built\n"
354 printf "Building MPC ${MPC_VERSION} ... "
356 #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
359 ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
360 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
361 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
362 $MAKE $JOBS || touch .failed
363 $MAKE install DESTDIR=$DESTDIR || touch .failed
365 if [ ! -f .failed ]; then touch .success; fi
366 ) > build-mpc/crossgcc-build.log 2>&1
367 test -r build-mpc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
368 test -r build-mpc/.failed && exit 1
371 if [ -f build-libelf/.success ]; then
372 printf "Skipping libelf as it is already built\n"
374 printf "Building libelf ${LIBELF_VERSION} ... "
379 CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
380 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
381 $MAKE $JOBS || touch .failed
382 $MAKE install DESTDIR=$DESTDIR || touch .failed
384 if [ ! -f .failed ]; then touch .success; fi
385 ) > build-libelf/crossgcc-build.log 2>&1
386 test -r build-libelf/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
387 test -r build-libelf/.failed && exit 1
390 if [ -f build-binutils/.success ]; then
391 printf "Skipping binutils as it is already built\n"
393 printf "Building binutils ${BINUTILS_VERSION} ... "
397 ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
398 --disable-werror --disable-nls $USE_GOLD \
399 CFLAGS="$HOSTCFLAGS" || touch .failed
400 $MAKE $JOBS || touch .failed
401 $MAKE install DESTDIR=$DESTDIR || touch .failed
402 if [ ! -f .failed ]; then touch .success; fi
403 ) > build-binutils/crossgcc-build.log 2>&1
404 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
405 test -r build-binutils/.failed && exit 1
408 if [ -f build-gcc/.success ]; then
409 printf "Skipping GCC as it is already built\n"
411 printf "Building GCC ${GCC_VERSION} ... "
414 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
416 # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
417 # both target and host object files. This is pretty misdesigned.
418 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
419 # but it does not seem to work properly. At least the host library
420 # libiberty is not compiled with CFLAGS_FOR_BUILD.
421 CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" CFLAGS_FOR_BUILD="$HOSTCFLAGS" `readlink -f ../gcc-${GCC_VERSION}/configure` \
422 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
423 --target=${TARGETARCH} --disable-werror --disable-shared \
424 --disable-libssp --disable-bootstrap --disable-nls \
425 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
426 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
427 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
428 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
430 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
431 $MAKE install DESTDIR=$DESTDIR || touch .failed
432 if [ ! -f .failed ]; then touch .success; fi
433 ) > build-gcc/crossgcc-build.log 2>&1
434 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
435 test -r build-gcc/.failed && exit 1
438 if [ -f build-gdb/.success ]; then
439 printf "Skipping GDB as it is already built\n"
440 elif [ $SKIPGDB -eq 1 ]; then
441 printf "Skipping GDB as requested by command line\n"
443 printf "Building GDB ${GDB_VERSION} ... "
446 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
448 CFLAGS="$HOSTCFLAGS" ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
449 --without-python --disable-werror --disable-nls
450 $MAKE $JOBS || touch .failed
451 $MAKE install DESTDIR=$DESTDIR || touch .failed
452 if [ ! -f .failed ]; then touch .success; fi
453 ) > build-gdb/crossgcc-build.log 2>&1
454 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
455 test -r build-gdb/.failed && exit 1
458 if [ -f $IASL_DIR/compiler/.success ]; then
459 printf "Skipping IASL as it is already built\n"
461 printf "Building IASL ${IASL_VERSION} ... "
463 cd $IASL_DIR/compiler
464 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
467 $MAKE || touch .failed
468 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch .failed
469 cp iasl $DESTDIR$TARGETDIR/bin || touch .failed
470 if [ ! -f .failed ]; then touch .success; fi
471 ) > $IASL_DIR/compiler/crossgcc-build.log 2>&1
472 test -r $IASL_DIR/compiler/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
473 test -r $IASL_DIR/compiler/.failed && exit 1
476 if [ $SAVETEMPS -eq 0 ]; then
477 printf "Cleaning up... "
478 rm -rf ${GMP_DIR} build-gmp
479 rm -rf ${MPFR_DIR} build-mpfr
480 rm -rf ${MPC_DIR} build-mpc
481 rm -rf ${LIBELF_DIR} build-libelf
482 rm -rf ${BINUTILS_DIR} build-binutils
483 rm -rf ${GCC_DIR} build-gcc
484 rm -rf ${GDB_DIR} build-gdb
486 printf "${green}ok${NC}\n"
488 printf "Leaving temporary files around... ${green}ok${NC}\n"
491 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"