crossgcc: update w32api
[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.17-2
38 W32API_VERSION_SHORT=3.17
39 MINGWRT_VERSION=3.18
40
41 # archive locations
42 GMP_ARCHIVE="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
43 MPFR_ARCHIVE="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
44 MPC_ARCHIVE="http://www.multiprecision.org/mpc/download/mpc-${MPC_VERSION}.tar.gz"
45 LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
46 GCC_ARCHIVE="ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
47 BINUTILS_ARCHIVE="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
48 GDB_ARCHIVE="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
49 W32API_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/Win32-API/w32api-${W32API_VERSION_SHORT}/w32api-${W32API_VERSION}-mingw32-src.tar.lzma"
50 MINGWRT_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW/BaseSystem/RuntimeLibrary/MinGW-RT/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
51
52 GMP_DIR="gmp-${GMP_VERSION}"
53 MPFR_DIR="mpfr-${MPFR_VERSION}"
54 MPC_DIR="mpc-${MPC_VERSION}"
55 LIBELF_DIR="libelf-${LIBELF_VERSION}"
56 GCC_DIR="gcc-${GCC_VERSION}"
57 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
58 GDB_DIR="gdb-${GDB_VERSION}"
59 W32API_DIR="w32api-${W32API_VERSION}-mingw32"
60 MINGWRT_DIR="mingwrt-${MINGWRT_VERSION}-mingw32"
61
62 SAVETEMPS=0
63 SKIPGDB=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` || exit $?
93 PATCH=`searchgnu patch` || exit $?
94 MAKE=`searchgnu make` || exit $?
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>] [-G]\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         printf "    [-G|--skip-gdb]               don't build GNU debugger\n"
122 }
123
124 myversion()
125 {
126         # version tag is always printed, so just print the license here
127
128         cat << EOF
129 Copyright (C) 2008-2010 by coresystems GmbH
130
131 This program is free software; you can redistribute it and/or modify
132 it under the terms of the GNU General Public License as published by
133 the Free Software Foundation; version 2 of the License.
134
135 This program is distributed in the hope that it will be useful,
136 but WITHOUT ANY WARRANTY; without even the implied warranty of
137 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
138 GNU General Public License for more details.
139
140 EOF
141 }
142
143 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
144
145 # Look if we have getopt. If not, build it.
146 export PATH=$PATH:.
147 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
148
149 # parse parameters.. try to find out whether we're running GNU getopt
150 getoptbrand="`getopt -V`"
151 if [ "${getoptbrand:0:6}" == "getopt" ]; then
152         # Detected GNU getopt that supports long options.
153         args=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps,skip-gdb Vhcd:p:j:D:tG -- "$@"`
154         eval set "$args"
155 else
156         # Detected non-GNU getopt
157         args=`getopt Vhcd:p:j:D:tG $*`
158         set -- $args
159 fi
160
161 if [ $? != 0 ]; then
162         myhelp
163         exit 1
164 fi
165
166 while true ; do
167         case "$1" in
168                 -V|--version)   shift; myversion; exit 0;;
169                 -h|--help)      shift; myhelp; exit 0;;
170                 -c|--clean)     shift; cleanup;;
171                 -t|--savetemps) shift; SAVETEMPS=1;;
172                 -d|--directory) shift; TARGETDIR="$1"; shift;;
173                 -p|--platform)  shift; TARGETARCH="$1"; shift;;
174                 -D|--destdir)   shift; DESTDIR="$1"; shift;;
175                 -j|--jobs)      shift; JOBS="-j $1"; shift;;
176                 -G|--skip-gdb)  shift; SKIPGDB=1;;
177                 --)             shift; break;;
178                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
179                 *)              break;;
180         esac
181 done
182
183 GDB_PACKAGE="GDB"
184 if [ $SKIPGDB -eq 1 ]; then
185         printf "Will skip GDB ... ${green}ok${NC}\n"
186         GDB_PACKAGE=""
187 fi
188
189 MINGW_ARCHIVES=""
190 if [ "$TARGETARCH" = "i386-mingw32" ]; then
191         MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
192 fi
193
194 if [ ${GCC_VERSION} == "4.5.0" -o ${GCC_VERSION} == "4.6.0" ]; then
195   # coreboot does not like the GOLD linker
196   # USE_GOLD="--enable-gold"
197   USE_GOLD=""
198   GCC_OPTIONS="--enable-lto"
199 fi
200
201 if [ ${GCC_VERSION} == "4.6.0" ]; then
202   if [ ! -r tarballs/gcc-core-${GCC_VERSION}.tar.bz2 ]; then
203     printf "Pre-Release GCC ${GCC_VERSION}, checking out subversion trunk\n"
204     mkdir -p tarballs/.tmp
205     cd tarballs/.tmp
206     rm -rf gcc-${GCC_VERSION}
207     svn export -q svn://gcc.gnu.org/svn/gcc/trunk gcc-${GCC_VERSION}
208     printf "done. Now creating tar ball...\n"
209     tar cjf ../gcc-core-${GCC_VERSION}.tar.bz2 gcc-${GCC_VERSION}
210     printf "done. Now cleaning up...\n"
211     cd ..
212     rm -rf .tmp
213     cd ..
214     printf "done.\n"
215   fi
216 fi
217
218 printf "Downloading tar balls ... \n"
219 mkdir -p tarballs
220 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
221         FILE=`basename $ARCHIVE`
222         printf " * $FILE "
223         test -f tarballs/$FILE && printf "(cached)" || (
224                 printf "(downloading)"
225                 cd tarballs
226                 wget -q $ARCHIVE
227         )
228         test -f tarballs/$FILE || printf "\n${RED}Failed to download $FILE.${red}\n"
229         test -f tarballs/$FILE || exit 1
230         printf "\n"
231 done
232 printf "Downloaded tar balls ... "
233 printf "${green}ok${NC}\n"
234
235 MINGW_PACKAGES=""
236 if [ "$TARGETARCH" = "i386-mingw32" ]; then
237         MINGW_PACKAGES="W32API MINGWRT"
238 fi
239
240 printf "Unpacking and patching ... \n"
241 for PACKAGE in GMP MPFR MPC LIBELF GCC BINUTILS $GDB_PACKAGE $MINGW_PACKAGES; do
242         archive=$PACKAGE"_ARCHIVE"
243         archive=${!archive}
244         dir=$PACKAGE"_DIR"
245         test -d ${!dir} || (
246                 printf " * `basename $archive`\n"
247                 FLAGS=zxf
248                 test ${archive:${#archive}-2:2} = "gz" && FLAGS=zxf
249                 test ${archive:${#archive}-3:3} = "bz2" && FLAGS=jxf
250                 test ${archive:${#archive}-4:4} = "lzma" && FLAGS="--lzma -xf"
251                 $TAR $FLAGS tarballs/`basename $archive`
252                 for patch in patches/${!dir}_*.patch; do
253                         test -r $patch || continue
254                         printf "   o `basename $patch`\n"
255                         $PATCH -s -N -p0 < `echo $patch`
256                 done
257         )
258 done
259 printf "Unpacked and patched ... "
260 printf "${green}ok${NC}\n"
261
262 if [ "$TARGETARCH" = "i386-mingw32" ]; then
263         mkdir -p $TARGETDIR/i386-mingw32/sys-include
264         mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
265 fi
266
267 if [ `uname` = "Darwin" ]; then
268         #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
269
270         # generally the OS X compiler can create x64 binaries.
271         # Per default it generated i386 binaries in 10.5 and x64
272         # binaries in 10.6 (even if the kernel is 32bit)
273         # For some weird reason, 10.5 autodetects an ABI=64 though
274         # so we're setting the ABI explicitly here.
275         if [ `sysctl -n hw.optional.x86_64` -eq 1 ]; then
276                 OPTIONS="ABI=64"
277         else
278                 OPTIONS="ABI=32"
279         fi
280         # old check:
281         #OPTIONS="ABI=32"
282         #touch .architecture_check.c
283         #gcc .architecture_check.c -c -o .architecture_check.o
284         #ARCH=`file .architecture_check.o |cut -f5 -d\ `
285         #test  "$ARCH" = "x86_64" && OPTIONS="ABI=64"
286         #rm .architecture_check.c .architecture_check.o
287 fi
288
289 mkdir -p build-gmp build-mpfr build-mpc build-libelf build-binutils build-gcc
290 if [ $SKIPGDB -eq 0 ]; then
291         mkdir -p build-gdb
292 fi
293 if [ -f build-gmp/.success ]; then
294         printf "Skipping GMP as it is already built\n"
295 else
296 printf "Building GMP ${GMP_VERSION} ... "
297 (
298         cd build-gmp
299         rm -f .failed
300         ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
301                 || touch .failed
302         $MAKE $JOBS || touch .failed
303         $MAKE install DESTDIR=$DESTDIR || touch .failed
304         if [ ! -f .failed ]; then touch .success; fi
305 ) &> build-gmp/crossgcc-build.log
306 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
307 test -r build-gmp/.failed && exit 1
308 fi
309
310 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
311 #       # create compat link
312 #       ln -s $DESTDIR$TARGETDIR $TARGETDIR
313 #fi
314
315 # Now set CFLAGS to match GMP CFLAGS.
316 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
317
318 if [ -f build-mpfr/.success ]; then
319         printf "Skipping MPFR as it is already built\n"
320 else
321 printf "Building MPFR ${MPFR_VERSION} ... "
322 (
323         test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
324         cd build-mpfr
325         rm -f .failed
326         ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
327                 --infodir=$TARGETDIR/info \
328                 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
329         $MAKE $JOBS || touch .failed
330         $MAKE install DESTDIR=$DESTDIR || touch .failed
331
332         # work around build problem of libgmp.la
333         if [ "$DESTDIR" != "" ]; then
334             perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
335         fi
336
337         if [ ! -f .failed ]; then touch .success; fi
338 ) &> build-mpfr/crossgcc-build.log
339 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
340 test -r build-mpfr/.failed && exit 1
341 fi
342
343 if [ -f build-mpc/.success ]; then
344         printf "Skipping MPC as it is already built\n"
345 else
346 printf "Building MPC ${MPC_VERSION} ... "
347 (
348         #test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
349         cd build-mpc
350         rm -f .failed
351         ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
352                 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
353                 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
354         $MAKE $JOBS || touch .failed
355         $MAKE install DESTDIR=$DESTDIR || touch .failed
356
357         if [ ! -f .failed ]; then touch .success; fi
358 ) &> build-mpc/crossgcc-build.log
359 test -r build-mpc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
360 test -r build-mpc/.failed && exit 1
361 fi
362
363 if [ -f build-libelf/.success ]; then
364         printf "Skipping libelf as it is already built\n"
365 else
366 printf "Building libelf ${LIBELF_VERSION} ... "
367 (
368         cd build-libelf
369         rm -f .failed
370         echo "$HOSTCFLAGS"
371         CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
372                 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
373         $MAKE $JOBS || touch .failed
374         $MAKE install DESTDIR=$DESTDIR || touch .failed
375
376         if [ ! -f .failed ]; then touch .success; fi
377 ) &> build-libelf/crossgcc-build.log
378 test -r build-libelf/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
379 test -r build-libelf/.failed && exit 1
380 fi
381
382 if [ -f build-binutils/.success ]; then
383         printf "Skipping binutils as it is already built\n"
384 else
385 printf "Building binutils ${BINUTILS_VERSION} ... "
386 (
387         cd build-binutils
388         rm -f .failed
389         ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
390                 --disable-werror --disable-nls $USE_GOLD \
391                 CFLAGS="$HOSTCFLAGS" || touch .failed
392         $MAKE $JOBS || touch .failed
393         $MAKE install DESTDIR=$DESTDIR || touch .failed
394         if [ ! -f .failed ]; then touch .success; fi
395 ) &> build-binutils/crossgcc-build.log
396 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
397 test -r build-binutils/.failed && exit 1
398 fi
399
400 if [ -f build-gcc/.success ]; then
401         printf "Skipping GCC as it is already built\n"
402 else
403 printf "Building GCC ${GCC_VERSION} ... "
404 (
405         cd build-gcc
406         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
407         rm -f .failed
408         # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
409         # both target and host object files. This is pretty misdesigned.
410         # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
411         # but it does not seem to work properly. At least the host library
412         # libiberty is not compiled with CFLAGS_FOR_BUILD.
413         CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
414                 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
415                 --target=${TARGETARCH} --disable-werror --disable-shared \
416                 --disable-libssp --disable-bootstrap --disable-nls \
417                 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
418                 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
419                 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
420                 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
421                 || touch .failed
422         $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
423         $MAKE install DESTDIR=$DESTDIR || touch .failed
424         if [ ! -f .failed ]; then touch .success; fi
425 ) &> build-gcc/crossgcc-build.log
426 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
427 test -r build-gcc/.failed && exit 1
428 fi
429
430 if [ -f build-gdb/.success ]; then
431         printf "Skipping GDB as it is already built\n"
432 elif [ $SKIPGDB -eq 1 ]; then
433         printf "Skipping GDB as requested by command line\n"
434 else
435 printf "Building GDB ${GDB_VERSION} ... "
436 (
437         cd build-gdb
438         export PATH=$PATH:$DESTDIR$TARGETDIR/bin
439         rm -f .failed
440         CFLAGS="$HOSTCFLAGS" ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
441                 --without-python --disable-werror --disable-nls
442         $MAKE $JOBS || touch .failed
443         $MAKE install DESTDIR=$DESTDIR || touch .failed
444         if [ ! -f .failed ]; then touch .success; fi
445 ) &> build-gdb/crossgcc-build.log
446 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
447 test -r build-gdb/.failed && exit 1
448 fi
449
450 if [ $SAVETEMPS -eq 0 ]; then
451         printf "Cleaning up... "
452         rm -rf ${GMP_DIR} build-gmp
453         rm -rf ${MPFR_DIR} build-mpfr
454         rm -rf ${MPC_DIR} build-mpc
455         rm -rf ${LIBELF_DIR} build-libelf
456         rm -rf ${BINUTILS_DIR} build-binutils
457         rm -rf ${GCC_DIR} build-gcc
458         rm -rf ${GDB_DIR} build-gdb
459         printf "${green}ok${NC}\n"
460 else
461         printf "Leaving temporary files around... ${green}ok${NC}\n"
462 fi
463
464 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"
465
466