util/crossgcc: Add buildgcc -G and --skip-gdb options
[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 SKIPGDB=0
63
64 red='\e[0;31m'
65 RED='\e[1;31m'
66 green='\e[0;32m'
67 GREEN='\e[1;32m'
68 blue='\e[0;34m'
69 BLUE='\e[1;34m'
70 cyan='\e[0;36m'
71 CYAN='\e[1;36m'
72 NC='\e[0m' # No Color
73
74 searchgnu()
75 {
76         # $1 short name
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
82                                 echo $i
83                                 return
84                         fi
85                 fi
86         done
87         printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
88         exit 1
89 }
90
91 TAR=`searchgnu tar`
92 PATCH=`searchgnu patch`
93 MAKE=`searchgnu make`
94
95 cleanup()
96 {
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"
100 }
101
102 myhelp()
103 {
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"
107
108         printf "Options:\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"
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,skip-gdb Vhcd:p:j:D:tG -- "$@"`
153         eval set "$args"
154 else
155         # Detected non-GNU getopt
156         args=`getopt Vhcd:p:j:D:tG $*`
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                 -G|--skip-gdb)  shift; SKIPGDB=1;;
176                 --)             shift; break;;
177                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
178                 *)              break;;
179         esac
180 done
181
182 GDB_PACKAGE="GDB"
183 if [ $SKIPGDB -eq 1 ]; then
184         printf "Will skip GDB ... ${green}ok${NC}\n"
185         GDB_PACKAGE=""
186 fi
187
188 MINGW_ARCHIVES=""
189 if [ "$TARGETARCH" = "i386-mingw32" ]; then
190         MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
191 fi
192
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"
196   USE_GOLD=""
197   GCC_OPTIONS="--enable-lto"
198 fi
199
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
204     cd 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"
210     cd ..
211     rm -rf .tmp
212     cd ..
213     printf "done.\n"
214   fi
215 fi
216
217 printf "Downloading tar balls ... \n"
218 mkdir -p tarballs
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`
221         printf " * $FILE "
222         test -f tarballs/$FILE && printf "(cached)" || (
223                 printf "(downloading)"
224                 cd tarballs
225                 wget -q $ARCHIVE
226         )
227         test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
228         test -f tarballs/$FILE || exit 1
229         printf "\n"
230 done
231 printf "Downloaded tar balls ... "
232 printf "${green}ok${NC}\n"
233
234 MINGW_PACKAGES=""
235 if [ "$TARGETARCH" = "i386-mingw32" ]; then
236         MINGW_PACKAGES="W32API MINGWRT"
237 fi
238
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"
242         archive=${!archive}
243         dir=$PACKAGE"_DIR"
244         test -d ${!dir} || (
245                 printf " * `basename $archive`\n"
246                 FLAGS=zxf
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`
254                 done
255         )
256 done
257 printf "Unpacked and patched ... "
258 printf "${green}ok${NC}\n"
259
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
263 fi
264
265 if [ `uname` = "Darwin" ]; then
266         #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
267
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
274                 OPTIONS="ABI=64"
275         else
276                 OPTIONS="ABI=32"
277         fi
278         # old check:
279         #OPTIONS="ABI=32"
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
285 fi
286
287 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc
288 if [ $SKIPGDB -eq 0 ]; then
289         mkdir -p build-gdb
290 fi
291 if [ -f build-gmp/.success ]; then
292         printf "Skipping GMP as it is already built\n"
293 else
294 printf "Building GMP ${GMP_VERSION} ... "
295 (
296         cd build-gmp
297         rm -f .failed
298         ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
299                 || touch .failed
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
306 fi
307
308 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
309 #       # create compat link
310 #       ln -s $DESTDIR$TARGETDIR $TARGETDIR
311 #fi
312
313 # Now set CFLAGS to match GMP CFLAGS.
314 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
315
316 if [ -f build-mpfr/.success ]; then
317         printf "Skipping MPFR as it is already built\n"
318 else
319 printf "Building MPFR ${MPFR_VERSION} ... "
320 (
321         test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
322         cd build-mpfr
323         rm -f .failed
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
329
330         # work around build problem of libgmp.la
331         if [ "$DESTDIR" != "" ]; then
332             perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
333         fi
334
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
339 fi
340
341 if [ -f build-mpc/.success ]; then
342         printf "Skipping MPC as it is already built\n"
343 else
344 printf "Building MPC ${MPC_VERSION} ... "
345 (
346         #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
347         cd build-mpc
348         rm -f .failed
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
354
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
359 fi
360
361 if [ -f build-libelf/.success ]; then
362         printf "Skipping libelf as it is already built\n"
363 else
364 printf "Building libelf ${LIBELF_VERSION} ... "
365 (
366         cd build-libelf
367         rm -f .failed
368         echo "$HOSTCFLAGS"
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
373
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
378 fi
379
380 if [ -f build-binutils/.success ]; then
381         printf "Skipping binutils as it is already built\n"
382 else
383 printf "Building binutils ${BINUTILS_VERSION} ... "
384 (
385         cd build-binutils
386         rm -f .failed
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
396 fi
397
398 if [ -f build-gcc/.success ]; then
399         printf "Skipping GCC as it is already built\n"
400 else
401 printf "Building GCC ${GCC_VERSION} ... "
402 (
403         cd build-gcc
404         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
405         rm -f .failed
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" \
419                 || touch .failed
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
426 fi
427
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"
432 else
433 printf "Building GDB ${GDB_VERSION} ... "
434 (
435         cd build-gdb
436         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
437         rm -f .failed
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
446 fi
447
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"
458 else
459         printf "Leaving temporary files around... ${green}ok${NC}\n"
460 fi
461
462 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"
463
464