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="May 18th, 2010"
22 CROSSGCC_VERSION="1.01"
34 # GCC 4.5.0 is broken on some AMD boards:
35 # GCC_VERSION=4.5.0 # enable for Link Time Optimization & Co
37 BINUTILS_VERSION=2.20.1
43 GMP_ARCHIVE="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
44 MPFR_ARCHIVE="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
45 MPC_ARCHIVE="http://www.multiprecision.org/mpc/download/mpc-${MPC_VERSION}.tar.gz"
46 LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
47 GCC_ARCHIVE="ftp://ftp.gwdg.de/pub/gnu/ftp/gnu/gcc/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
48 BINUTILS_ARCHIVE="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
49 GDB_ARCHIVE="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
50 W32API_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW%20API%20for%20MS-Windows/w32api-${W32API_VERSION}/w32api-${W32API_VERSION}-mingw32-src.tar.gz"
51 MINGWRT_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW%20Runtime/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
53 GMP_DIR="gmp-${GMP_VERSION}"
54 MPFR_DIR="mpfr-${MPFR_VERSION}"
55 MPC_DIR="mpc-${MPC_VERSION}"
56 LIBELF_DIR="libelf-${LIBELF_VERSION}"
57 GCC_DIR="gcc-${GCC_VERSION}"
58 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
59 GDB_DIR="gdb-${GDB_VERSION}"
60 W32API_DIR="w32api-${W32API_VERSION}-mingw32"
61 MINGWRT_DIR="mingwrt-${MINGWRT_VERSION}-mingw32"
78 # result: GNU version of that tool on stdout
79 # or no output if no GNU version was found
80 for i in "$1" "g$1" "gnu$1"; do
81 if test -x "`which $i 2>/dev/null`"; then
82 if test `$i --version 2>/dev/null |grep -c GNU` -gt 0; then
88 printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
93 PATCH=`searchgnu patch`
98 printf "Cleaning up temporary files... "
99 rm -rf build-* combined gcc-* gmp-* mpfr-* mpc-* libelf-* binutils-* gdb-* w32api-* mingwrt-*
100 printf "${green}ok${NC}\n"
105 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>]\n"
106 printf " $0 [-V|--version]\n"
107 printf " $0 [-h|--help]\n\n"
110 printf " [-V|--version] print version number and exit\n"
111 printf " [-h|--help] print this help and exit\n"
112 printf " [-c|--clean] remove temporary files before build\n"
113 printf " [-t|--savetemps] don't remove temporary files after build\n"
114 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
115 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
116 printf " (defaults to $TARGETARCH)\n"
117 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
118 printf " (defaults to $TARGETDIR)\n\n"
119 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
120 printf " (for RPM builds, default unset)\n\n"
125 # version tag is always printed, so just print the license here
128 Copyright (C) 2008-2010 by coresystems GmbH
130 This program is free software; you can redistribute it and/or modify
131 it under the terms of the GNU General Public License as published by
132 the Free Software Foundation; version 2 of the License.
134 This program is distributed in the hope that it will be useful,
135 but WITHOUT ANY WARRANTY; without even the implied warranty of
136 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
137 GNU General Public License for more details.
142 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
144 # Look if we have getopt. If not, build it.
146 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
148 # parse parameters.. try to find out whether we're running GNU getopt
149 getoptbrand="`getopt -V`"
150 if [ "${getoptbrand:0:6}" == "getopt" ]; then
151 # Detected GNU getopt that supports long options.
152 args=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps Vhcd:p:j:D:t -- "$@"`
155 # Detected non-GNU getopt
156 args=`getopt Vhcd:p:j:D:t $*`
167 -V|--version) shift; myversion; exit 0;;
168 -h|--help) shift; myhelp; exit 0;;
169 -c|--clean) shift; cleanup;;
170 -t|--savetemps) shift; SAVETEMPS=1;;
171 -d|--directory) shift; TARGETDIR="$1"; shift;;
172 -p|--platform) shift; TARGETARCH="$1"; shift;;
173 -D|--destdir) shift; DESTDIR="$1"; shift;;
174 -j|--jobs) shift; JOBS="-j $1"; shift;;
176 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
182 if [ "$TARGETARCH" = "i386-mingw32" ]; then
183 MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
186 if [ ${GCC_VERSION} == "4.5.0" -o ${GCC_VERSION} == "4.6.0" ]; then
187 # coreboot does not like the GOLD linker
188 # USE_GOLD="--enable-gold"
190 GCC_OPTIONS="--enable-lto"
193 if [ ${GCC_VERSION} == "4.6.0" ]; then
194 if [ ! -r tarballs/gcc-core-${GCC_VERSION}.tar.bz2 ]; then
195 printf "Pre-Release GCC ${GCC_VERSION}, checking out subversion trunk\n"
196 mkdir -p tarballs/.tmp
198 rm -rf gcc-${GCC_VERSION}
199 svn export -q svn://gcc.gnu.org/svn/gcc/trunk gcc-${GCC_VERSION}
200 printf "done. Now creating tar ball...\n"
201 tar cjf ../gcc-core-${GCC_VERSION}.tar.bz2 gcc-${GCC_VERSION}
202 printf "done. Now cleaning up...\n"
210 printf "Downloading tar balls ... \n"
212 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
213 FILE=`basename $ARCHIVE`
215 test -f tarballs/$FILE && printf "(cached)" || (
216 printf "(downloading)"
220 test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
221 test -f tarballs/$FILE || exit 1
224 printf "Downloaded tar balls ... "
225 printf "${green}ok${NC}\n"
228 if [ "$TARGETARCH" = "i386-mingw32" ]; then
229 MINGW_PACKAGES="W32API MINGWRT"
232 printf "Unpacking and patching ... \n"
233 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS GDB $MINGW_PACKAGES; do
234 archive=$PACKAGE"_ARCHIVE"
238 printf " * `basename $archive`\n"
240 test ${archive:${#archive}-2:2} = "gz" && FLAGS=zxf
241 test ${archive:${#archive}-3:3} = "bz2" && FLAGS=jxf
242 $TAR $FLAGS tarballs/`basename $archive`
243 for patch in patches/${!dir}_*.patch; do
244 test -r $patch || continue
245 printf " o `basename $patch`\n"
246 $PATCH -s -N -p0 < `echo $patch`
250 printf "Unpacked and patched ... "
251 printf "${green}ok${NC}\n"
253 if [ "$TARGETARCH" = "i386-mingw32" ]; then
254 mkdir -p $TARGETDIR/i386-mingw32/sys-include
255 mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
258 if [ `uname` = "Darwin" ]; then
259 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
261 # generally the OS X compiler can create x64 binaries.
262 # Per default it generated i386 binaries in 10.5 and x64
263 # binaries in 10.6 (even if the kernel is 32bit)
264 # For some weird reason, 10.5 autodetects an ABI=64 though
265 # so we're setting the ABI explicitly here.
266 if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
273 #touch .architecture_check.c
274 #gcc .architecture_check.c -c -o .architecture_check.o
275 #ARCH=`file .architecture_check.o |cut -f5 -d\ `
276 #test "$ARCH" = "x86_64" && OPTIONS="ABI=64"
277 #rm .architecture_check.c .architecture_check.o
280 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc build-gdb
281 if [ -f build-gmp/.success ]; then
282 printf "Skipping GMP as it is already built\n"
284 printf "Building GMP ${GMP_VERSION} ... "
288 ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
290 $MAKE $JOBS || touch .failed
291 $MAKE install DESTDIR=$DESTDIR || touch .failed
292 if [ ! -f .failed ]; then touch .success; fi
293 ) &> build-gmp/crossgcc-build.log
294 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
295 test -r build-gmp/.failed && exit 1
298 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
299 # # create compat link
300 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
303 # Now set CFLAGS to match GMP CFLAGS.
304 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
306 if [ -f build-mpfr/.success ]; then
307 printf "Skipping MPFR as it is already built\n"
309 printf "Building MPFR ${MPFR_VERSION} ... "
311 test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
314 ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
315 --infodir=$TARGETDIR/info \
316 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
317 $MAKE $JOBS || touch .failed
318 $MAKE install DESTDIR=$DESTDIR || touch .failed
320 # work around build problem of libgmp.la
321 if [ "$DESTDIR" != "" ]; then
322 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
325 if [ ! -f .failed ]; then touch .success; fi
326 ) &> build-mpfr/crossgcc-build.log
327 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
328 test -r build-mpfr/.failed && exit 1
331 if [ -f build-mpc/.success ]; then
332 printf "Skipping MPC as it is already built\n"
334 printf "Building MPC ${MPC_VERSION} ... "
336 #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
339 ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
340 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
341 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
342 $MAKE $JOBS || touch .failed
343 $MAKE install DESTDIR=$DESTDIR || touch .failed
345 if [ ! -f .failed ]; then touch .success; fi
346 ) &> build-mpc/crossgcc-build.log
347 test -r build-mpc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
348 test -r build-mpc/.failed && exit 1
351 if [ -f build-libelf/.success ]; then
352 printf "Skipping libelf as it is already built\n"
354 printf "Building libelf ${LIBELF_VERSION} ... "
359 CFLAGS="$HOSTCFLAGS" ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
360 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
361 $MAKE $JOBS || touch .failed
362 $MAKE install DESTDIR=$DESTDIR || touch .failed
364 if [ ! -f .failed ]; then touch .success; fi
365 ) &> build-libelf/crossgcc-build.log
366 test -r build-libelf/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
367 test -r build-libelf/.failed && exit 1
370 if [ -f build-binutils/.success ]; then
371 printf "Skipping binutils as it is already built\n"
373 printf "Building binutils ${BINUTILS_VERSION} ... "
377 ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
378 --disable-werror --disable-nls $USE_GOLD \
379 CFLAGS="$HOSTCFLAGS" || touch .failed
380 $MAKE $JOBS || touch .failed
381 $MAKE install DESTDIR=$DESTDIR || touch .failed
382 if [ ! -f .failed ]; then touch .success; fi
383 ) &> build-binutils/crossgcc-build.log
384 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
385 test -r build-binutils/.failed && exit 1
388 if [ -f build-gcc/.success ]; then
389 printf "Skipping GCC as it is already built\n"
391 printf "Building GCC ${GCC_VERSION} ... "
394 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
396 # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
397 # both target and host object files. This is pretty misdesigned.
398 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
399 # but it does not seem to work properly. At least the host library
400 # libiberty is not compiled with CFLAGS_FOR_BUILD.
401 CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
402 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
403 --target=${TARGETARCH} --disable-werror --disable-shared \
404 --disable-libssp --disable-bootstrap --disable-nls \
405 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
406 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
407 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
408 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
410 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
411 $MAKE install DESTDIR=$DESTDIR || touch .failed
412 if [ ! -f .failed ]; then touch .success; fi
413 ) &> build-gcc/crossgcc-build.log
414 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
415 test -r build-gcc/.failed && exit 1
418 if [ -f build-gdb/.success ]; then
419 printf "Skipping GDB as it is already built\n"
421 printf "Building GDB ${GDB_VERSION} ... "
424 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
426 CFLAGS="$HOSTCFLAGS" ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
427 --without-python --disable-werror --disable-nls
428 $MAKE $JOBS || touch .failed
429 $MAKE install DESTDIR=$DESTDIR || touch .failed
430 if [ ! -f .failed ]; then touch .success; fi
431 ) &> build-gdb/crossgcc-build.log
432 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
433 test -r build-gdb/.failed && exit 1
436 if [ $SAVETEMPS -eq 0 ]; then
437 printf "Cleaning up... "
438 rm -rf ${GMP_DIR} build-gmp
439 rm -rf ${MPFR_DIR} build-mpfr
440 rm -rf ${MPC_DIR} build-mpc
441 rm -rf ${LIBELF_DIR} build-libelf
442 rm -rf ${BINUTILS_DIR} build-binutils
443 rm -rf ${GCC_DIR} build-gcc
444 rm -rf ${GDB_DIR} build-gdb
445 printf "${green}ok${NC}\n"
447 printf "Leaving temporary files around... ${green}ok${NC}\n"
450 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"