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="December 16th, 2010"
22 CROSSGCC_VERSION="1.03"
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.fu-berlin.de/unix/languages/gcc/releases/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"
77 # result: GNU version of that tool on stdout
78 # or no output if no GNU version was found
79 for i in "$1" "g$1" "gnu$1"; do
80 if test -x "`which $i 2>/dev/null`"; then
81 if test `$i --version 2>/dev/null |grep -c GNU` -gt 0; then
87 printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
92 PATCH=`searchgnu patch`
97 printf "Cleaning up temporary files... "
98 rm -rf build-* combined gcc-* gmp-* mpfr-* mpc-* libelf-* binutils-* gdb-* w32api-* mingwrt-*
99 printf "${green}ok${NC}\n"
104 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-G]\n"
105 printf " $0 [-V|--version]\n"
106 printf " $0 [-h|--help]\n\n"
109 printf " [-V|--version] print version number and exit\n"
110 printf " [-h|--help] print this help and exit\n"
111 printf " [-c|--clean] remove temporary files before build\n"
112 printf " [-t|--savetemps] don't remove temporary files after build\n"
113 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
114 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
115 printf " (defaults to $TARGETARCH)\n"
116 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
117 printf " (defaults to $TARGETDIR)\n\n"
118 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
119 printf " (for RPM builds, default unset)\n\n"
120 printf " [-G|--skip-gdb] don't build GNU debugger\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,skip-gdb Vhcd:p:j:D:tG -- "$@"`
155 # Detected non-GNU getopt
156 args=`getopt Vhcd:p:j:D:tG $*`
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;;
175 -G|--skip-gdb) shift; SKIPGDB=1;;
177 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
183 if [ $SKIPGDB -eq 1 ]; then
184 printf "Will skip GDB ... ${green}ok${NC}\n"
189 if [ "$TARGETARCH" = "i386-mingw32" ]; then
190 MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
193 if [ ${GCC_VERSION} == "4.5.0" -o ${GCC_VERSION} == "4.6.0" ]; then
194 # coreboot does not like the GOLD linker
195 # USE_GOLD="--enable-gold"
197 GCC_OPTIONS="--enable-lto"
200 if [ ${GCC_VERSION} == "4.6.0" ]; then
201 if [ ! -r tarballs/gcc-core-${GCC_VERSION}.tar.bz2 ]; then
202 printf "Pre-Release GCC ${GCC_VERSION}, checking out subversion trunk\n"
203 mkdir -p tarballs/.tmp
205 rm -rf gcc-${GCC_VERSION}
206 svn export -q svn://gcc.gnu.org/svn/gcc/trunk gcc-${GCC_VERSION}
207 printf "done. Now creating tar ball...\n"
208 tar cjf ../gcc-core-${GCC_VERSION}.tar.bz2 gcc-${GCC_VERSION}
209 printf "done. Now cleaning up...\n"
217 printf "Downloading tar balls ... \n"
219 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
220 FILE=`basename $ARCHIVE`
222 test -f tarballs/$FILE && printf "(cached)" || (
223 printf "(downloading)"
227 test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
228 test -f tarballs/$FILE || exit 1
231 printf "Downloaded tar balls ... "
232 printf "${green}ok${NC}\n"
235 if [ "$TARGETARCH" = "i386-mingw32" ]; then
236 MINGW_PACKAGES="W32API MINGWRT"
239 printf "Unpacking and patching ... \n"
240 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS $GDB_PACKAGE $MINGW_PACKAGES; do
241 archive=$PACKAGE"_ARCHIVE"
245 printf " * `basename $archive`\n"
247 test ${archive:${#archive}-2:2} = "gz" && FLAGS=zxf
248 test ${archive:${#archive}-3:3} = "bz2" && FLAGS=jxf
249 $TAR $FLAGS tarballs/`basename $archive`
250 for patch in patches/${!dir}_*.patch; do
251 test -r $patch || continue
252 printf " o `basename $patch`\n"
253 $PATCH -s -N -p0 < `echo $patch`
257 printf "Unpacked and patched ... "
258 printf "${green}ok${NC}\n"
260 if [ "$TARGETARCH" = "i386-mingw32" ]; then
261 mkdir -p $TARGETDIR/i386-mingw32/sys-include
262 mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
265 if [ `uname` = "Darwin" ]; then
266 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
268 # generally the OS X compiler can create x64 binaries.
269 # Per default it generated i386 binaries in 10.5 and x64
270 # binaries in 10.6 (even if the kernel is 32bit)
271 # For some weird reason, 10.5 autodetects an ABI=64 though
272 # so we're setting the ABI explicitly here.
273 if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
280 #touch .architecture_check.c
281 #gcc .architecture_check.c -c -o .architecture_check.o
282 #ARCH=`file .architecture_check.o |cut -f5 -d\ `
283 #test "$ARCH" = "x86_64" && OPTIONS="ABI=64"
284 #rm .architecture_check.c .architecture_check.o
287 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc
288 if [ $SKIPGDB -eq 0 ]; then
291 if [ -f build-gmp/.success ]; then
292 printf "Skipping GMP as it is already built\n"
294 printf "Building GMP ${GMP_VERSION} ... "
298 ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
300 $MAKE $JOBS || touch .failed
301 $MAKE install DESTDIR=$DESTDIR || touch .failed
302 if [ ! -f .failed ]; then touch .success; fi
303 ) &> build-gmp/crossgcc-build.log
304 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
305 test -r build-gmp/.failed && exit 1
308 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
309 # # create compat link
310 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
313 # Now set CFLAGS to match GMP CFLAGS.
314 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
316 if [ -f build-mpfr/.success ]; then
317 printf "Skipping MPFR as it is already built\n"
319 printf "Building MPFR ${MPFR_VERSION} ... "
321 test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
324 ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
325 --infodir=$TARGETDIR/info \
326 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
327 $MAKE $JOBS || touch .failed
328 $MAKE install DESTDIR=$DESTDIR || touch .failed
330 # work around build problem of libgmp.la
331 if [ "$DESTDIR" != "" ]; then
332 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
335 if [ ! -f .failed ]; then touch .success; fi
336 ) &> build-mpfr/crossgcc-build.log
337 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
338 test -r build-mpfr/.failed && exit 1
341 if [ -f build-mpc/.success ]; then
342 printf "Skipping MPC as it is already built\n"
344 printf "Building MPC ${MPC_VERSION} ... "
346 #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
349 ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
350 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
351 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
352 $MAKE $JOBS || touch .failed
353 $MAKE install DESTDIR=$DESTDIR || touch .failed
355 if [ ! -f .failed ]; then touch .success; fi
356 ) &> build-mpc/crossgcc-build.log
357 test -r build-mpc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
358 test -r build-mpc/.failed && exit 1
361 if [ -f build-libelf/.success ]; then
362 printf "Skipping libelf as it is already built\n"
364 printf "Building libelf ${LIBELF_VERSION} ... "
369 CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
370 --infodir=$TARGETDIR/info 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-libelf/crossgcc-build.log
376 test -r build-libelf/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
377 test -r build-libelf/.failed && exit 1
380 if [ -f build-binutils/.success ]; then
381 printf "Skipping binutils as it is already built\n"
383 printf "Building binutils ${BINUTILS_VERSION} ... "
387 ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
388 --disable-werror --disable-nls $USE_GOLD \
389 CFLAGS="$HOSTCFLAGS" || touch .failed
390 $MAKE $JOBS || touch .failed
391 $MAKE install DESTDIR=$DESTDIR || touch .failed
392 if [ ! -f .failed ]; then touch .success; fi
393 ) &> build-binutils/crossgcc-build.log
394 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
395 test -r build-binutils/.failed && exit 1
398 if [ -f build-gcc/.success ]; then
399 printf "Skipping GCC as it is already built\n"
401 printf "Building GCC ${GCC_VERSION} ... "
404 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
406 # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
407 # both target and host object files. This is pretty misdesigned.
408 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
409 # but it does not seem to work properly. At least the host library
410 # libiberty is not compiled with CFLAGS_FOR_BUILD.
411 CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
412 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
413 --target=${TARGETARCH} --disable-werror --disable-shared \
414 --disable-libssp --disable-bootstrap --disable-nls \
415 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
416 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
417 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
418 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
420 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
421 $MAKE install DESTDIR=$DESTDIR || touch .failed
422 if [ ! -f .failed ]; then touch .success; fi
423 ) &> build-gcc/crossgcc-build.log
424 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
425 test -r build-gcc/.failed && exit 1
428 if [ -f build-gdb/.success ]; then
429 printf "Skipping GDB as it is already built\n"
430 elif [ $SKIPGDB -eq 1 ]; then
431 printf "Skipping GDB as requested by command line\n"
433 printf "Building GDB ${GDB_VERSION} ... "
436 export PATH=$PATH:$DESTDIR$TARGETDIR/bin
438 CFLAGS="$HOSTCFLAGS" ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
439 --without-python --disable-werror --disable-nls
440 $MAKE $JOBS || touch .failed
441 $MAKE install DESTDIR=$DESTDIR || touch .failed
442 if [ ! -f .failed ]; then touch .success; fi
443 ) &> build-gdb/crossgcc-build.log
444 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
445 test -r build-gdb/.failed && exit 1
448 if [ $SAVETEMPS -eq 0 ]; then
449 printf "Cleaning up... "
450 rm -rf ${GMP_DIR} build-gmp
451 rm -rf ${MPFR_DIR} build-mpfr
452 rm -rf ${MPC_DIR} build-mpc
453 rm -rf ${LIBELF_DIR} build-libelf
454 rm -rf ${BINUTILS_DIR} build-binutils
455 rm -rf ${GCC_DIR} build-gcc
456 rm -rf ${GDB_DIR} build-gdb
457 printf "${green}ok${NC}\n"
459 printf "Leaving temporary files around... ${green}ok${NC}\n"
462 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"