26fbc662c351ffa573c52cbd63ffd0c371e3ddcd
[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="December 16th, 2010"
22 CROSSGCC_VERSION="1.03"
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=3.0.0
32 MPC_VERSION=0.8.2
33 LIBELF_VERSION=0.8.13
34 GCC_VERSION=4.5.2
35 BINUTILS_VERSION=2.20.1
36 GDB_VERSION=7.2
37 W32API_VERSION=3.15
38 MINGWRT_VERSION=3.18
39
40 # archive locations
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"
50
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"
60
61 SAVETEMPS=0
62
63 red='\e[0;31m'
64 RED='\e[1;31m'
65 green='\e[0;32m'
66 GREEN='\e[1;32m'
67 blue='\e[0;34m'
68 BLUE='\e[1;34m'
69 cyan='\e[0;36m'
70 CYAN='\e[1;36m'
71 NC='\e[0m' # No Color
72
73 searchgnu()
74 {
75         # $1 short name
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
81                                 echo $i
82                                 return
83                         fi
84                 fi
85         done
86         printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
87         exit 1
88 }
89
90 TAR=`searchgnu tar`
91 PATCH=`searchgnu patch`
92 MAKE=`searchgnu make`
93
94 cleanup()
95 {
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"
99 }
100
101 myhelp()
102 {
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"
106
107         printf "Options:\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"
119 }
120
121 myversion()
122 {
123         # version tag is always printed, so just print the license here
124
125         cat << EOF
126 Copyright (C) 2008-2010 by coresystems GmbH
127
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.
131
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.
136
137 EOF
138 }
139
140 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
141
142 # Look if we have getopt. If not, build it.
143 export PATH=$PATH:.
144 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
145
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 -- "$@"`
151         eval set "$args"
152 else
153         # Detected non-GNU getopt
154         args=`getopt Vhcd:p:j:D:t $*`
155         set -- $args
156 fi
157
158 if [ $? != 0 ]; then
159         myhelp
160         exit 1
161 fi
162
163 while true ; do
164         case "$1" in
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;;
173                 --)             shift; break;;
174                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
175                 *)              break;;
176         esac
177 done
178
179 MINGW_ARCHIVES=""
180 if [ "$TARGETARCH" = "i386-mingw32" ]; then
181         MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
182 fi
183
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"
187   USE_GOLD=""
188   GCC_OPTIONS="--enable-lto"
189 fi
190
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
195     cd 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"
201     cd ..
202     rm -rf .tmp
203     cd ..
204     printf "done.\n"
205   fi
206 fi
207
208 printf "Downloading tar balls ... \n"
209 mkdir -p tarballs
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`
212         printf " * $FILE "
213         test -f tarballs/$FILE && printf "(cached)" || (
214                 printf "(downloading)"
215                 cd tarballs
216                 wget -q $ARCHIVE
217         )
218         test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
219         test -f tarballs/$FILE || exit 1
220         printf "\n"
221 done
222 printf "Downloaded tar balls ... "
223 printf "${green}ok${NC}\n"
224
225 MINGW_PACKAGES=""
226 if [ "$TARGETARCH" = "i386-mingw32" ]; then
227         MINGW_PACKAGES="W32API MINGWRT"
228 fi
229
230 printf "Unpacking and patching ... \n"
231 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS GDB $MINGW_PACKAGES; do
232         archive=$PACKAGE"_ARCHIVE"
233         archive=${!archive}
234         dir=$PACKAGE"_DIR"
235         test -d ${!dir} || (
236                 printf " * `basename $archive`\n"
237                 FLAGS=zxf
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`
245                 done
246         )
247 done
248 printf "Unpacked and patched ... "
249 printf "${green}ok${NC}\n"
250
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
254 fi
255
256 if [ `uname` = "Darwin" ]; then
257         #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
258
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
265                 OPTIONS="ABI=64"
266         else
267                 OPTIONS="ABI=32"
268         fi
269         # old check:
270         #OPTIONS="ABI=32"
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
276 fi
277
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"
281 else
282 printf "Building GMP ${GMP_VERSION} ... "
283 (
284         cd build-gmp
285         rm -f .failed
286         ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
287                 || touch .failed
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
294 fi
295
296 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
297 #       # create compat link
298 #       ln -s $DESTDIR$TARGETDIR $TARGETDIR
299 #fi
300
301 # Now set CFLAGS to match GMP CFLAGS.
302 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
303
304 if [ -f build-mpfr/.success ]; then
305         printf "Skipping MPFR as it is already built\n"
306 else
307 printf "Building MPFR ${MPFR_VERSION} ... "
308 (
309         test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
310         cd build-mpfr
311         rm -f .failed
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
317
318         # work around build problem of libgmp.la
319         if [ "$DESTDIR" != "" ]; then
320             perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
321         fi
322
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
327 fi
328
329 if [ -f build-mpc/.success ]; then
330         printf "Skipping MPC as it is already built\n"
331 else
332 printf "Building MPC ${MPC_VERSION} ... "
333 (
334         #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
335         cd build-mpc
336         rm -f .failed
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
342
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
347 fi
348
349 if [ -f build-libelf/.success ]; then
350         printf "Skipping libelf as it is already built\n"
351 else
352 printf "Building libelf ${LIBELF_VERSION} ... "
353 (
354         cd build-libelf
355         rm -f .failed
356         echo "$HOSTCFLAGS"
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
361
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
366 fi
367
368 if [ -f build-binutils/.success ]; then
369         printf "Skipping binutils as it is already built\n"
370 else
371 printf "Building binutils ${BINUTILS_VERSION} ... "
372 (
373         cd build-binutils
374         rm -f .failed
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
384 fi
385
386 if [ -f build-gcc/.success ]; then
387         printf "Skipping GCC as it is already built\n"
388 else
389 printf "Building GCC ${GCC_VERSION} ... "
390 (
391         cd build-gcc
392         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
393         rm -f .failed
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" \
407                 || touch .failed
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
414 fi
415
416 if [ -f build-gdb/.success ]; then
417         printf "Skipping GDB as it is already built\n"
418 else
419 printf "Building GDB ${GDB_VERSION} ... "
420 (
421         cd build-gdb
422         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
423         rm -f .failed
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
432 fi
433
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"
444 else
445         printf "Leaving temporary files around... ${green}ok${NC}\n"
446 fi
447
448 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"
449
450