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 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 CROSSGCC_DATE="November 16th, 2010"
22 CROSSGCC_VERSION="1.02"
35 BINUTILS_VERSION=2.20.1
41 GMP_ARCHIVE="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
42 MPFR_ARCHIVE="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
43 MPC_ARCHIVE="http://www.multiprecision.org/mpc/download/mpc-${MPC_VERSION}.tar.gz"
44 LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
45 GCC_ARCHIVE="ftp://ftp.gwdg.de/pub/gnu/ftp/gnu/gcc/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
46 BINUTILS_ARCHIVE="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
47 GDB_ARCHIVE="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
48 W32API_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/Win32-API/w32api-${W32API_VERSION}/w32api-${W32API_VERSION}-mingw32-src.tar.gz"
49 MINGWRT_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/MinGW-RT/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
51 GMP_DIR="gmp-${GMP_VERSION}"
52 MPFR_DIR="mpfr-${MPFR_VERSION}"
53 MPC_DIR="mpc-${MPC_VERSION}"
54 LIBELF_DIR="libelf-${LIBELF_VERSION}"
55 GCC_DIR="gcc-${GCC_VERSION}"
56 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
57 GDB_DIR="gdb-${GDB_VERSION}"
58 W32API_DIR="w32api-${W32API_VERSION}-mingw32"
59 MINGWRT_DIR="mingwrt-${MINGWRT_VERSION}-mingw32"
76 # result: GNU version of that tool on stdout
77 # or no output if no GNU version was found
78 for i in "$1" "g$1" "gnu$1"; do
79 if test -x "`which $i 2>/dev/null`"; then
80 if test `$i --version 2>/dev/null |grep -c GNU` -gt 0; then
86 printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
91 PATCH=`searchgnu patch`
96 printf "Cleaning up temporary files... "
97 rm -rf build-* combined gcc-* gmp-* mpfr-* mpc-* libelf-* binutils-* gdb-* w32api-* mingwrt-*
98 printf "${green}ok${NC}\n"
103 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>]\n"
104 printf " $0 [-V|--version]\n"
105 printf " $0 [-h|--help]\n\n"
108 printf " [-V|--version] print version number and exit\n"
109 printf " [-h|--help] print this help and exit\n"
110 printf " [-c|--clean] remove temporary files before build\n"
111 printf " [-t|--savetemps] don't remove temporary files after build\n"
112 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
113 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
114 printf " (defaults to $TARGETARCH)\n"
115 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
116 printf " (defaults to $TARGETDIR)\n\n"
117 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
118 printf " (for RPM builds, default unset)\n\n"
123 # version tag is always printed, so just print the license here
126 Copyright (C) 2008-2010 by coresystems GmbH
128 This program is free software; you can redistribute it and/or modify
129 it under the terms of the GNU General Public License as published by
130 the Free Software Foundation; version 2 of the License.
132 This program is distributed in the hope that it will be useful,
133 but WITHOUT ANY WARRANTY; without even the implied warranty of
134 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135 GNU General Public License for more details.
140 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
142 # Look if we have getopt. If not, build it.
144 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
146 # parse parameters.. try to find out whether we're running GNU getopt
147 getoptbrand="`getopt -V`"
148 if [ "${getoptbrand:0:6}" == "getopt" ]; then
149 # Detected GNU getopt that supports long options.
150 args=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps Vhcd:p:j:D:t -- "$@"`
153 # Detected non-GNU getopt
154 args=`getopt Vhcd:p:j:D:t $*`
165 -V|--version) shift; myversion; exit 0;;
166 -h|--help) shift; myhelp; exit 0;;
167 -c|--clean) shift; cleanup;;
168 -t|--savetemps) shift; SAVETEMPS=1;;
169 -d|--directory) shift; TARGETDIR="$1"; shift;;
170 -p|--platform) shift; TARGETARCH="$1"; shift;;
171 -D|--destdir) shift; DESTDIR="$1"; shift;;
172 -j|--jobs) shift; JOBS="-j $1"; shift;;
174 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
180 if [ "$TARGETARCH" = "i386-mingw32" ]; then
181 MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
184 if [ ${GCC_VERSION} == "4.5.0" -o ${GCC_VERSION} == "4.6.0" ]; then
185 # coreboot does not like the GOLD linker
186 # USE_GOLD="--enable-gold"
188 GCC_OPTIONS="--enable-lto"
191 if [ ${GCC_VERSION} == "4.6.0" ]; then
192 if [ ! -r tarballs/gcc-core-${GCC_VERSION}.tar.bz2 ]; then
193 printf "Pre-Release GCC ${GCC_VERSION}, checking out subversion trunk\n"
194 mkdir -p tarballs/.tmp
196 rm -rf gcc-${GCC_VERSION}
197 svn export -q svn://gcc.gnu.org/svn/gcc/trunk gcc-${GCC_VERSION}
198 printf "done. Now creating tar ball...\n"
199 tar cjf ../gcc-core-${GCC_VERSION}.tar.bz2 gcc-${GCC_VERSION}
200 printf "done. Now cleaning up...\n"
208 printf "Downloading tar balls ... \n"
210 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
211 FILE=`basename $ARCHIVE`
213 test -f tarballs/$FILE && printf "(cached)" || (
214 printf "(downloading)"
218 test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
219 test -f tarballs/$FILE || exit 1
222 printf "Downloaded tar balls ... "
223 printf "${green}ok${NC}\n"
226 if [ "$TARGETARCH" = "i386-mingw32" ]; then
227 MINGW_PACKAGES="W32API MINGWRT"
230 printf "Unpacking and patching ... \n"
231 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS GDB $MINGW_PACKAGES; do
232 archive=$PACKAGE"_ARCHIVE"
236 printf " * `basename $archive`\n"
238 test ${archive:${#archive}-2:2} = "gz" && FLAGS=zxf
239 test ${archive:${#archive}-3:3} = "bz2" && FLAGS=jxf
240 $TAR $FLAGS tarballs/`basename $archive`
241 for patch in patches/${!dir}_*.patch; do
242 test -r $patch || continue
243 printf " o `basename $patch`\n"
244 $PATCH -s -N -p0 < `echo $patch`
248 printf "Unpacked and patched ... "
249 printf "${green}ok${NC}\n"
251 if [ "$TARGETARCH" = "i386-mingw32" ]; then
252 mkdir -p $TARGETDIR/i386-mingw32/sys-include
253 mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
256 if [ `uname` = "Darwin" ]; then
257 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
259 # generally the OS X compiler can create x64 binaries.
260 # Per default it generated i386 binaries in 10.5 and x64
261 # binaries in 10.6 (even if the kernel is 32bit)
262 # For some weird reason, 10.5 autodetects an ABI=64 though
263 # so we're setting the ABI explicitly here.
264 if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
271 #touch .architecture_check.c
272 #gcc .architecture_check.c -c -o .architecture_check.o
273 #ARCH=`file .architecture_check.o |cut -f5 -d\ `
274 #test "$ARCH" = "x86_64" && OPTIONS="ABI=64"
275 #rm .architecture_check.c .architecture_check.o
278 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc build-gdb
279 if [ -f build-gmp/.success ]; then
280 printf "Skipping GMP as it is already built\n"
282 printf "Building GMP ${GMP_VERSION} ... "
286 ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
288 $MAKE $JOBS || touch .failed
289 $MAKE install DESTDIR=$DESTDIR || touch .failed
290 if [ ! -f .failed ]; then touch .success; fi
291 ) &> build-gmp/crossgcc-build.log
292 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
293 test -r build-gmp/.failed && exit 1
296 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
297 # # create compat link
298 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
301 # Now set CFLAGS to match GMP CFLAGS.
302 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
304 if [ -f build-mpfr/.success ]; then
305 printf "Skipping MPFR as it is already built\n"
307 printf "Building MPFR ${MPFR_VERSION} ... "
309 test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
312 ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
313 --infodir=$TARGETDIR/info \
314 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
315 $MAKE $JOBS || touch .failed
316 $MAKE install DESTDIR=$DESTDIR || touch .failed
318 # work around build problem of libgmp.la
319 if [ "$DESTDIR" != "" ]; then
320 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
323 if [ ! -f .failed ]; then touch .success; fi
324 ) &> build-mpfr/crossgcc-build.log
325 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
326 test -r build-mpfr/.failed && exit 1
329 if [ -f build-mpc/.success ]; then
330 printf "Skipping MPC as it is already built\n"
332 printf "Building MPC ${MPC_VERSION} ... "
334 #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
337 ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
338 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
339 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
340 $MAKE $JOBS || touch .failed
341 $MAKE install DESTDIR=$DESTDIR || touch .failed
343 if [ ! -f .failed ]; then touch .success; fi
344 ) &> build-mpc/crossgcc-build.log
345 test -r build-mpc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
346 test -r build-mpc/.failed && exit 1
349 if [ -f build-libelf/.success ]; then
350 printf "Skipping libelf as it is already built\n"
352 printf "Building libelf ${LIBELF_VERSION} ... "
357 CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
358 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
359 $MAKE $JOBS || touch .failed
360 $MAKE install DESTDIR=$DESTDIR || touch .failed
362 if [ ! -f .failed ]; then touch .success; fi
363 ) &> build-libelf/crossgcc-build.log
364 test -r build-libelf/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
365 test -r build-libelf/.failed && exit 1
368 if [ -f build-binutils/.success ]; then
369 printf "Skipping binutils as it is already built\n"
371 printf "Building binutils ${BINUTILS_VERSION} ... "
375 ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
376 --disable-werror --disable-nls $USE_GOLD \
377 CFLAGS="$HOSTCFLAGS" || touch .failed
378 $MAKE $JOBS || touch .failed
379 $MAKE install DESTDIR=$DESTDIR || touch .failed
380 if [ ! -f .failed ]; then touch .success; fi
381 ) &> build-binutils/crossgcc-build.log
382 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
383 test -r build-binutils/.failed && exit 1
386 if [ -f build-gcc/.success ]; then
387 printf "Skipping GCC as it is already built\n"
389 printf "Building GCC ${GCC_VERSION} ... "
392 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
394 # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
395 # both target and host object files. This is pretty misdesigned.
396 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
397 # but it does not seem to work properly. At least the host library
398 # libiberty is not compiled with CFLAGS_FOR_BUILD.
399 CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
400 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
401 --target=${TARGETARCH} --disable-werror --disable-shared \
402 --disable-libssp --disable-bootstrap --disable-nls \
403 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
404 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
405 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
406 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
408 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
409 $MAKE install DESTDIR=$DESTDIR || touch .failed
410 if [ ! -f .failed ]; then touch .success; fi
411 ) &> build-gcc/crossgcc-build.log
412 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
413 test -r build-gcc/.failed && exit 1
416 if [ -f build-gdb/.success ]; then
417 printf "Skipping GDB as it is already built\n"
419 printf "Building GDB ${GDB_VERSION} ... "
422 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
424 CFLAGS="$HOSTCFLAGS" ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
425 --without-python --disable-werror --disable-nls
426 $MAKE $JOBS || touch .failed
427 $MAKE install DESTDIR=$DESTDIR || touch .failed
428 if [ ! -f .failed ]; then touch .success; fi
429 ) &> build-gdb/crossgcc-build.log
430 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
431 test -r build-gdb/.failed && exit 1
434 if [ $SAVETEMPS -eq 0 ]; then
435 printf "Cleaning up... "
436 rm -rf ${GMP_DIR} build-gmp
437 rm -rf ${MPFR_DIR} build-mpfr
438 rm -rf ${MPC_DIR} build-mpc
439 rm -rf ${LIBELF_DIR} build-libelf
440 rm -rf ${BINUTILS_DIR} build-binutils
441 rm -rf ${GCC_DIR} build-gcc
442 rm -rf ${GDB_DIR} build-gdb
443 printf "${green}ok${NC}\n"
445 printf "Leaving temporary files around... ${green}ok${NC}\n"
448 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"