f25685121ce0f646b18cf006702c679e64efb0a3
[coreboot.git] / util / crossgcc / buildgcc
1 #!/bin/bash
2 #
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>
6 #
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.
10 #
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.
15 #
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
19 #
20
21 CROSSGCC_DATE="May 18th, 2010"
22 CROSSGCC_VERSION="1.01"
23
24 # default settings
25 TARGETDIR=`pwd`/xgcc
26 TARGETARCH=i386-elf
27 DESTDIR=
28
29 # version numbers
30 GMP_VERSION=5.0.1
31 MPFR_VERSION=2.4.2
32 MPC_VERSION=0.8.2
33 LIBELF_VERSION=0.8.13
34 # GCC 4.5.0 is broken on some AMD boards:
35 # GCC_VERSION=4.5.0 # enable for Link Time Optimization & Co
36 GCC_VERSION=4.4.4
37 BINUTILS_VERSION=2.20.1
38 GDB_VERSION=7.1
39 W32API_VERSION=3.14
40 MINGWRT_VERSION=3.18
41
42 # archive locations
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"
52
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"
62
63 SAVETEMPS=0
64
65 red='\e[0;31m'
66 RED='\e[1;31m'
67 green='\e[0;32m'
68 GREEN='\e[1;32m'
69 blue='\e[0;34m'
70 BLUE='\e[1;34m'
71 cyan='\e[0;36m'
72 CYAN='\e[1;36m'
73 NC='\e[0m' # No Color
74
75 searchgnu()
76 {
77         # $1 short name
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
83                                 echo $i
84                                 return
85                         fi
86                 fi
87         done
88         printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
89         exit 1
90 }
91
92 TAR=`searchgnu tar`
93 PATCH=`searchgnu patch`
94 MAKE=`searchgnu make`
95
96 cleanup()
97 {
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"
101 }
102
103 myhelp()
104 {
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"
108
109         printf "Options:\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"
121 }
122
123 myversion()
124 {
125         # version tag is always printed, so just print the license here
126
127         cat << EOF
128 Copyright (C) 2008-2010 by coresystems GmbH
129
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.
133
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.
138
139 EOF
140 }
141
142 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
143
144 # Look if we have getopt. If not, build it.
145 export PATH=$PATH:.
146 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
147
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 -- "$@"`
153         eval set "$args"
154 else
155         # Detected non-GNU getopt
156         args=`getopt Vhcd:p:j:D:t $*`
157         set -- $args
158 fi
159
160 if [ $? != 0 ]; then
161         myhelp
162         exit 1
163 fi
164
165 while true ; do
166         case "$1" in
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                 --)             shift; break;;
176                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
177                 *)              break;;
178         esac
179 done
180
181 MINGW_ARCHIVES=""
182 if [ "$TARGETARCH" = "i386-mingw32" ]; then
183         MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
184 fi
185
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"
189   USE_GOLD=""
190   GCC_OPTIONS="--enable-lto"
191 fi
192
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
197     cd 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"
203     cd ..
204     rm -rf .tmp
205     cd ..
206     printf "done.\n"
207   fi
208 fi
209
210 printf "Downloading tar balls ... \n"
211 mkdir -p tarballs
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`
214         printf " * $FILE "
215         test -f tarballs/$FILE && printf "(cached)" || (
216                 printf "(downloading)"
217                 cd tarballs
218                 wget -q $ARCHIVE
219         )
220         test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
221         test -f tarballs/$FILE || exit 1
222         printf "\n"
223 done
224 printf "Downloaded tar balls ... "
225 printf "${green}ok${NC}\n"
226
227 MINGW_PACKAGES=""
228 if [ "$TARGETARCH" = "i386-mingw32" ]; then
229         MINGW_PACKAGES="W32API MINGWRT"
230 fi
231
232 printf "Unpacking and patching ... \n"
233 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS GDB $MINGW_PACKAGES; do
234         archive=$PACKAGE"_ARCHIVE"
235         archive=${!archive}
236         dir=$PACKAGE"_DIR"
237         test -d ${!dir} || (
238                 printf " * `basename $archive`\n"
239                 FLAGS=zxf
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`
247                 done
248         )
249 done
250 printf "Unpacked and patched ... "
251 printf "${green}ok${NC}\n"
252
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
256 fi
257
258 if [ `uname` = "Darwin" ]; then
259         #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
260
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
267                 OPTIONS="ABI=64"
268         else
269                 OPTIONS="ABI=32"
270         fi
271         # old check:
272         #OPTIONS="ABI=32"
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
278 fi
279
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"
283 else
284 printf "Building GMP ${GMP_VERSION} ... "
285 (
286         cd build-gmp
287         rm -f .failed
288         ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
289                 || touch .failed
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
296 fi
297
298 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
299 #       # create compat link
300 #       ln -s $DESTDIR$TARGETDIR $TARGETDIR
301 #fi
302
303 # Now set CFLAGS to match GMP CFLAGS.
304 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
305
306 if [ -f build-mpfr/.success ]; then
307         printf "Skipping MPFR as it is already built\n"
308 else
309 printf "Building MPFR ${MPFR_VERSION} ... "
310 (
311         test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
312         cd build-mpfr
313         rm -f .failed
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
319
320         # work around build problem of libgmp.la
321         if [ "$DESTDIR" != "" ]; then
322             perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
323         fi
324
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
329 fi
330
331 if [ -f build-mpc/.success ]; then
332         printf "Skipping MPC as it is already built\n"
333 else
334 printf "Building MPC ${MPC_VERSION} ... "
335 (
336         #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
337         cd build-mpc
338         rm -f .failed
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
344
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
349 fi
350
351 if [ -f build-libelf/.success ]; then
352         printf "Skipping libelf as it is already built\n"
353 else
354 printf "Building libelf ${LIBELF_VERSION} ... "
355 (
356         cd build-libelf
357         rm -f .failed
358         echo "$HOSTCFLAGS"
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
363
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
368 fi
369
370 if [ -f build-binutils/.success ]; then
371         printf "Skipping binutils as it is already built\n"
372 else
373 printf "Building binutils ${BINUTILS_VERSION} ... "
374 (
375         cd build-binutils
376         rm -f .failed
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
386 fi
387
388 if [ -f build-gcc/.success ]; then
389         printf "Skipping GCC as it is already built\n"
390 else
391 printf "Building GCC ${GCC_VERSION} ... "
392 (
393         cd build-gcc
394         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
395         rm -f .failed
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" \
409                 || touch .failed
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
416 fi
417
418 if [ -f build-gdb/.success ]; then
419         printf "Skipping GDB as it is already built\n"
420 else
421 printf "Building GDB ${GDB_VERSION} ... "
422 (
423         cd build-gdb
424         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
425         rm -f .failed
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
434 fi
435
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"
446 else
447         printf "Leaving temporary files around... ${green}ok${NC}\n"
448 fi
449
450 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"
451
452