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"
37 GCC_AUTOCONF_VERSION
=2.64
38 BINUTILS_VERSION
=2.21.1
41 W32API_VERSION_SHORT
=3.17
46 GMP_ARCHIVE
="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
47 MPFR_ARCHIVE
="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
48 MPC_ARCHIVE
="http://www.multiprecision.org/mpc/download/mpc-${MPC_VERSION}.tar.gz"
49 LIBELF_ARCHIVE
="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
50 GCC_ARCHIVE
="ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
51 BINUTILS_ARCHIVE
="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
52 GDB_ARCHIVE
="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
53 W32API_ARCHIVE
="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/Win32-API/w32api-${W32API_VERSION_SHORT}/w32api-${W32API_VERSION}-mingw32-src.tar.lzma"
54 MINGWRT_ARCHIVE
="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/MinGW-RT/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
55 IASL_ARCHIVE
="http://www.acpica.org/download/acpica-unix-${IASL_VERSION}.tar.gz"
57 GMP_DIR
="gmp-${GMP_VERSION}"
58 MPFR_DIR
="mpfr-${MPFR_VERSION}"
59 MPC_DIR
="mpc-${MPC_VERSION}"
60 LIBELF_DIR
="libelf-${LIBELF_VERSION}"
61 GCC_DIR
="gcc-${GCC_VERSION}"
62 BINUTILS_DIR
="binutils-${BINUTILS_VERSION}"
63 GDB_DIR
="gdb-${GDB_VERSION}"
64 W32API_DIR
="w32api-${W32API_VERSION}-mingw32"
65 MINGWRT_DIR
="mingwrt-${MINGWRT_VERSION}-mingw32"
66 IASL_DIR
="acpica-unix-${IASL_VERSION}"
79 NC
='\033[0m' # No Color
84 # result: GNU version of that tool on stdout
85 # or no output if no GNU version was found
86 for i
in "$1" "g$1" "gnu$1"; do
87 if test -x "`which $i 2>/dev/null`"; then
88 if test `$i --version 2>/dev/null |grep -c GNU` -gt 0; then
94 printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
98 TAR
=`searchgnu tar` ||
exit $?
99 PATCH
=`searchgnu patch` ||
exit $?
100 MAKE
=`searchgnu make` ||
exit $?
104 printf "Cleaning up temporary files... "
105 rm -rf build-
* combined gcc-
* gmp-
* mpfr-
* mpc-
* libelf-
* binutils-
* gdb-
* w32api-
* mingwrt-
* acpica-
*
106 printf "${green}ok${NC}\n"
111 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-G]\n"
112 printf " $0 [-V|--version]\n"
113 printf " $0 [-h|--help]\n\n"
116 printf " [-V|--version] print version number and exit\n"
117 printf " [-h|--help] print this help and exit\n"
118 printf " [-c|--clean] remove temporary files before build\n"
119 printf " [-t|--savetemps] don't remove temporary files after build\n"
120 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
121 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
122 printf " (defaults to $TARGETARCH)\n"
123 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
124 printf " (defaults to $TARGETDIR)\n\n"
125 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
126 printf " (for RPM builds, default unset)\n\n"
127 printf " [-G|--skip-gdb] don't build GNU debugger\n"
132 # version tag is always printed, so just print the license here
135 Copyright (C) 2008-2010 by coresystems GmbH
136 Copyright (C) 2011 by Sage Electronic Engineering
138 This program is free software; you can redistribute it and/or modify
139 it under the terms of the GNU General Public License as published by
140 the Free Software Foundation; version 2 of the License.
142 This program is distributed in the hope that it will be useful,
143 but WITHOUT ANY WARRANTY; without even the implied warranty of
144 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
145 GNU General Public License for more details.
150 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
152 # Look if we have getopt. If not, build it.
154 getopt
- > /dev
/null
2>/dev
/null || gcc
-o getopt getopt.c
156 # parse parameters.. try to find out whether we're running GNU getopt
157 getoptbrand
="`getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,'`"
158 if [ "${getoptbrand}" = "getopt" ]; then
159 # Detected GNU getopt that supports long options.
160 args
=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps,skip-gdb Vhcd:p:j:D:tG -- "$@"`
163 # Detected non-GNU getopt
164 args
=`getopt Vhcd:p:j:D:tG $*`
175 -V|
--version) shift; myversion
; exit 0;;
176 -h|
--help) shift; myhelp
; exit 0;;
177 -c|
--clean) shift; cleanup
;;
178 -t|
--savetemps) shift; SAVETEMPS
=1;;
179 -d|
--directory) shift; TARGETDIR
="$1"; shift;;
180 -p|
--platform) shift; TARGETARCH
="$1"; shift;;
181 -D|
--destdir) shift; DESTDIR
="$1"; shift;;
182 -j|
--jobs) shift; JOBS
="-j $1"; shift;;
183 -G|
--skip-gdb) shift; SKIPGDB
=1;;
185 -*) printf "Invalid option\n\n"; myhelp
; exit 1;;
191 if [ $SKIPGDB -eq 1 ]; then
192 printf "Will skip GDB ... ${green}ok${NC}\n"
198 if [ "$TARGETARCH" = "i386-mingw32" ]; then
199 MINGW_ARCHIVES
="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
202 # coreboot does not like the GOLD linker
203 # USE_GOLD="--enable-gold"
205 GCC_OPTIONS
="--enable-lto"
207 if [ ${GCC_VERSION} = "4.6.2" ]; 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
276 if [ `uname` = "Darwin" ]; then
277 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
279 # generally the OS X compiler can create x64 binaries.
280 # Per default it generated i386 binaries in 10.5 and x64
281 # binaries in 10.6 (even if the kernel is 32bit)
282 # For some weird reason, 10.5 autodetects an ABI=64 though
283 # so we're setting the ABI explicitly here.
284 if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
290 # In Xcode 4 the default compiler was switched to gcc-llvm.
291 # However, this compiler fails to compile gcc 4.6.x. As a
292 # workaround it's possible to compile gcc with gcc-4.2 or
294 if $CC -v 2>&1 |
grep -q LLVM
; then
299 mkdir
-p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc
300 if [ $SKIPGDB -eq 0 ]; then
303 if [ -f build-gmp
/.success
]; then
304 printf "Skipping GMP as it is already built\n"
306 printf "Building GMP ${GMP_VERSION} ... "
310 ..
/${GMP_DIR}/configure
--disable-shared --prefix=$TARGETDIR $OPTIONS \
312 $MAKE $JOBS ||
touch .failed
313 $MAKE install DESTDIR
=$DESTDIR ||
touch .failed
314 if [ ! -f .failed
]; then touch .success
; fi
315 ) > build-gmp
/crossgcc-build.log
2>&1
316 test -r build-gmp
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
317 test -r build-gmp
/.failed
&& exit 1
320 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
321 # # create compat link
322 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
325 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
326 # as GCC 4.6.x fails if it's there.
327 HOSTCFLAGS
=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
330 if [ -f build-mpfr
/.success
]; then
331 printf "Skipping MPFR as it is already built\n"
333 printf "Building MPFR ${MPFR_VERSION} ... "
335 test `uname` = "Darwin" && CFLAGS
="$CFLAGS -force_cpusubtype_ALL"
338 ..
/${MPFR_DIR}/configure
--disable-shared --prefix=$TARGETDIR \
339 --infodir=$TARGETDIR/info \
340 --with-gmp=$DESTDIR$TARGETDIR CFLAGS
="$HOSTCFLAGS" ||
touch .failed
341 $MAKE $JOBS ||
touch .failed
342 $MAKE install DESTDIR
=$DESTDIR ||
touch .failed
344 # work around build problem of libgmp.la
345 if [ "$DESTDIR" != "" ]; then
346 perl
-pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
349 if [ ! -f .failed
]; then touch .success
; fi
350 ) > build-mpfr
/crossgcc-build.log
2>&1
351 test -r build-mpfr
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
352 test -r build-mpfr
/.failed
&& exit 1
355 if [ -f build-mpc
/.success
]; then
356 printf "Skipping MPC as it is already built\n"
358 printf "Building MPC ${MPC_VERSION} ... "
360 #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
363 ..
/${MPC_DIR}/configure
--disable-shared --prefix=$TARGETDIR \
364 --infodir=$TARGETDIR/info
--with-mpfr=$DESTDIR$TARGETDIR \
365 --with-gmp=$DESTDIR$TARGETDIR CFLAGS
="$HOSTCFLAGS" ||
touch .failed
366 $MAKE $JOBS ||
touch .failed
367 $MAKE install DESTDIR
=$DESTDIR ||
touch .failed
369 if [ ! -f .failed
]; then touch .success
; fi
370 ) > build-mpc
/crossgcc-build.log
2>&1
371 test -r build-mpc
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
372 test -r build-mpc
/.failed
&& exit 1
375 if [ -f build-libelf
/.success
]; then
376 printf "Skipping libelf as it is already built\n"
378 printf "Building libelf ${LIBELF_VERSION} ... "
383 CFLAGS
="$HOSTCFLAGS" libelf_cv_elf_h_works
=no ..
/${LIBELF_DIR}/configure
--disable-shared --prefix=$TARGETDIR \
384 --infodir=$TARGETDIR/info CFLAGS
="$HOSTCFLAGS" ||
touch .failed
385 $MAKE $JOBS ||
touch .failed
386 $MAKE install DESTDIR
=$DESTDIR ||
touch .failed
388 if [ ! -f .failed
]; then touch .success
; fi
389 ) > build-libelf
/crossgcc-build.log
2>&1
390 test -r build-libelf
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
391 test -r build-libelf
/.failed
&& exit 1
394 if [ -f build-binutils
/.success
]; then
395 printf "Skipping binutils as it is already built\n"
397 printf "Building binutils ${BINUTILS_VERSION} ... "
399 # What a pain: binutils don't come with configure
400 # script anymore. Create it:
401 cd binutils-
${BINUTILS_VERSION}/
407 ..
/binutils-
${BINUTILS_VERSION}/configure
--prefix=$TARGETDIR --target=${TARGETARCH} \
408 --disable-werror --disable-nls $USE_GOLD \
409 CFLAGS
="$HOSTCFLAGS" ||
touch .failed
410 $MAKE $JOBS ||
touch .failed
411 $MAKE install DESTDIR
=$DESTDIR ||
touch .failed
412 if [ ! -f .failed
]; then touch .success
; fi
413 ) > build-binutils
/crossgcc-build.log
2>&1
414 test -r build-binutils
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
415 test -r build-binutils
/.failed
&& exit 1
418 if [ -f build-gcc
/.success
]; then
419 printf "Skipping GCC as it is already built\n"
421 printf "Building GCC ${GCC_VERSION} ... "
423 # Even worse than binutils: GCC does not come with configure
424 # script anymore, but also enforces an obsolete autoconf version
425 # to create it. This is a poster child of how autotools help make
427 cd gcc-
${GCC_VERSION}
428 sed '/dnl Ensure exactly this Autoconf version is used/d' \
429 config
/override.
m4 > config
/override.
m4.new
430 autoconf_version
=`autoconf -V | grep "autoconf" | tr ' ' '\n' | tail -1`
431 sed "s/${GCC_AUTOCONF_VERSION}/${autoconf_version}/g" \
432 config
/override.
m4.new
> config
/override.
m4
435 # Now, finally, we can build gcc:
437 export PATH
=$PATH:$DESTDIR$TARGETDIR/bin
439 # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
440 # both target and host object files. This is pretty misdesigned.
441 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
442 # but it does not seem to work properly. At least the host library
443 # libiberty is not compiled with CFLAGS_FOR_BUILD.
444 CC
="$CC" CFLAGS_FOR_TARGET
="-O2" CFLAGS
="$HOSTCFLAGS" \
445 CFLAGS_FOR_BUILD
="$HOSTCFLAGS" ..
/gcc-
${GCC_VERSION}/configure \
446 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
447 --target=${TARGETARCH} --disable-werror --disable-shared \
448 --disable-libssp --disable-bootstrap --disable-nls \
449 --disable-libquadmath \
450 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
451 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
452 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
453 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
455 $MAKE $JOBS CFLAGS_FOR_BUILD
="$HOSTCFLAGS" ||
touch .failed
456 $MAKE install DESTDIR
=$DESTDIR ||
touch .failed
457 if [ ! -f .failed
]; then touch .success
; fi
458 ) > build-gcc
/crossgcc-build.log
2>&1
459 test -r build-gcc
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
460 test -r build-gcc
/.failed
&& exit 1
463 if [ -f build-gdb
/.success
]; then
464 printf "Skipping GDB as it is already built\n"
465 elif [ $SKIPGDB -eq 1 ]; then
466 printf "Skipping GDB as requested by command line\n"
468 printf "Building GDB ${GDB_VERSION} ... "
471 export PATH
=$PATH:$DESTDIR$TARGETDIR/bin
473 CFLAGS
="$HOSTCFLAGS" ..
/gdb-
${GDB_VERSION}/configure
--prefix=$TARGETDIR --target=${TARGETARCH} \
474 --without-python --disable-werror --disable-nls
475 $MAKE $JOBS ||
touch .failed
476 $MAKE install DESTDIR
=$DESTDIR ||
touch .failed
477 if [ ! -f .failed
]; then touch .success
; fi
478 ) > build-gdb
/crossgcc-build.log
2>&1
479 test -r build-gdb
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
480 test -r build-gdb
/.failed
&& exit 1
483 if [ -f $IASL_DIR/compiler
/.success
]; then
484 printf "Skipping IASL as it is already built\n"
486 printf "Building IASL ${IASL_VERSION} ... "
488 cd $IASL_DIR/compiler
489 export PATH
=$PATH:$DESTDIR$TARGETDIR/bin
492 $MAKE ||
touch .failed
493 rm -f $DESTDIR$TARGETDIR/bin
/iasl ||
touch .failed
494 cp iasl
$DESTDIR$TARGETDIR/bin ||
touch .failed
495 if [ ! -f .failed
]; then touch .success
; fi
496 ) > $IASL_DIR/compiler
/crossgcc-build.log
2>&1
497 test -r $IASL_DIR/compiler
/.failed
&& printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
498 test -r $IASL_DIR/compiler
/.failed
&& exit 1
501 if [ $SAVETEMPS -eq 0 ]; then
502 printf "Cleaning up... "
503 rm -rf ${GMP_DIR} build-gmp
504 rm -rf ${MPFR_DIR} build-mpfr
505 rm -rf ${MPC_DIR} build-mpc
506 rm -rf ${LIBELF_DIR} build-libelf
507 rm -rf ${BINUTILS_DIR} build-binutils
508 rm -rf ${GCC_DIR} build-gcc
509 rm -rf ${GDB_DIR} build-gdb
511 printf "${green}ok${NC}\n"
513 printf "Leaving temporary files around... ${green}ok${NC}\n"
516 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"