Update reference toolchain to
[coreboot.git] / util / crossgcc / buildgcc
1 #!/bin/bash
2 #
3 # Copyright (C) 2008-2009 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="August 18th, 2009"
22 CROSSGCC_VERSION="0.9"
23
24 # default settings
25 TARGETDIR=`pwd`/xgcc
26 TARGETARCH=i386-elf
27 DESTDIR=
28
29 # version numbers
30 GMP_VERSION=4.3.1
31 MPFR_VERSION=2.4.2
32 GCC_VERSION=4.4.2
33 BINUTILS_VERSION=2.20
34 GDB_VERSION=7.0
35 W32API_VERSION=3.13
36 MINGWRT_VERSION=3.16
37
38 # archive locations
39 GMP_ARCHIVE="ftp://ftp.gmplib.org/pub/gmp-${GMP_VERSION}/gmp-${GMP_VERSION}.tar.bz2"
40 MPFR_ARCHIVE="http://www.mpfr.org/mpfr-${MPFR_VERSION}/mpfr-${MPFR_VERSION}.tar.bz2"
41 GCC_ARCHIVE="ftp://ftp.gwdg.de/pub/gnu/ftp/gnu/gcc/gcc-${GCC_VERSION}/gcc-core-${GCC_VERSION}.tar.bz2"
42 BINUTILS_ARCHIVE="http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
43 GDB_ARCHIVE="http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.bz2"
44 W32API_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW%20API%20for%20MS-Windows/Current%20Release_%20w32api-${W32API_VERSION}/w32api-${W32API_VERSION}-mingw32-src.tar.gz"
45 MINGWRT_ARCHIVE="http://downloads.sourceforge.net/project/mingw/MinGW%20Runtime/mingwrt-${MINGWRT_VERSION}/mingwrt-${MINGWRT_VERSION}-mingw32-src.tar.gz"
46
47 GMP_DIR="gmp-${GMP_VERSION}"
48 MPFR_DIR="mpfr-${MPFR_VERSION}"
49 GCC_DIR="gcc-${GCC_VERSION}"
50 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
51 GDB_DIR="gdb-${GDB_VERSION}"
52 W32API_DIR="w32api-${W32API_VERSION}-mingw32"
53 MINGWRT_DIR="mingwrt-${MINGWRT_VERSION}-mingw32"
54
55 SAVETEMPS=0
56
57 red='\e[0;31m'
58 RED='\e[1;31m'
59 green='\e[0;32m'
60 GREEN='\e[1;32m'
61 blue='\e[0;34m'
62 BLUE='\e[1;34m'
63 cyan='\e[0;36m'
64 CYAN='\e[1;36m'
65 NC='\e[0m' # No Color
66
67 searchgnu()
68 {
69         # $1 short name
70         # result: GNU version of that tool on stdout
71         #         or no output if no GNU version was found
72         for i in "$1" "g$1" "gnu$1"; do
73                 if test -x "`which $i 2>/dev/null`"; then
74                         if test `$i --version 2>/dev/null |grep -c GNU` -gt 0; then
75                                 echo $i
76                                 return
77                         fi
78                 fi
79         done
80         printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
81         exit 1
82 }
83
84 TAR=`searchgnu tar`
85 PATCH=`searchgnu patch`
86 MAKE=`searchgnu make`
87
88 cleanup()
89 {
90         printf "Cleaning up temporary files... "
91         rm -rf build-* combined gcc-* gmp-* mpfr-* binutils-* gdb-* w32api-* mingwrt-*
92         printf "${green}ok${NC}\n"
93 }
94
95 myhelp()
96 {
97         printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>]\n"
98         printf "       $0 [-V|--version]\n"
99         printf "       $0 [-h|--help]\n\n"
100
101         printf "Options:\n"
102         printf "    [-V|--version]                print version number and exit\n"
103         printf "    [-h|--help]                   print this help and exit\n"
104         printf "    [-c|--clean]                  remove temporary files before build\n"
105         printf "    [-t|--savetemps]              don't remove temporary files after build\n"
106         printf "    [-p|--platform <platform>]    target platform to build cross compiler for\n"
107         printf "                                  (defaults to $TARGETARCH)\n"
108         printf "    [-d|--directory <target dir>] target directory to install cross compiler to\n"
109         printf "                                  (defaults to $TARGETDIR)\n\n"
110         printf "    [-D|--destdir <dest dir>]     destination directory to install cross compiler to\n"
111         printf "                                  (for RPM builds, default unset)\n\n"
112 }
113
114 myversion()
115 {
116         # version tag is always printed, so just print the license here
117
118         cat << EOF
119 Copyright (C) 2008-2009 by coresystems GmbH
120
121 This program is free software; you can redistribute it and/or modify
122 it under the terms of the GNU General Public License as published by
123 the Free Software Foundation; version 2 of the License.
124
125 This program is distributed in the hope that it will be useful,
126 but WITHOUT ANY WARRANTY; without even the implied warranty of
127 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
128 GNU General Public License for more details.
129
130 EOF
131 }
132
133 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
134
135 # parse parameters.. try to find out whether we're running GNU getopt
136 getoptbrand="`getopt -V`"
137 if [ "${getoptbrand:0:6}" == "getopt" ]; then
138         # Detected GNU getopt that supports long options.
139         args=`getopt -l version,help,clean,directory:,platform:,destdir:,savetemps Vhcd:p:D:t -- "$@"`
140         eval set "$args"
141 else
142         # Detected non-GNU getopt
143         args=`getopt Vhcd:p:D:t $*`
144         set -- $args
145 fi
146
147 if [ $? != 0 ]; then
148         myhelp
149         exit 1
150 fi
151
152 while true ; do
153         case "$1" in
154                 -V|--version)   shift; myversion; exit 0;;
155                 -h|--help)      shift; myhelp; exit 0;;
156                 -c|--clean)     shift; cleanup;;
157                 -t|--savetemps) shift; SAVETEMPS=1;;
158                 -d|--directory) shift; TARGETDIR="$1"; shift;;
159                 -p|--platform)  shift; TARGETARCH="$1"; shift;;
160                 -D|--destdir)   shift; DESTDIR="$1"; shift;;
161                 --)             shift; break;;
162                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
163                 *)              break;;
164         esac
165 done
166
167 MINGW_ARCHIVES=""
168 if [ "$TARGETARCH" = "i386-mingw32" ]; then
169         MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
170 fi
171
172 printf "Downloading tar balls ... \n"
173 mkdir -p tarballs
174 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
175         FILE=`basename $ARCHIVE`
176         printf " * $FILE "
177         test -f tarballs/$FILE && printf "(cached)" || (
178                 printf "(downloading)"
179                 cd tarballs
180                 wget -q $ARCHIVE
181         )
182         test -f tarballs/$FILE || printf "Failed to download $FILE.\n"
183         test -f tarballs/$FILE || exit 1
184         printf "\n"
185 done
186 printf "Downloaded tar balls ... "
187 printf "${green}ok${NC}\n"
188
189 MINGW_PACKAGES=""
190 if [ "$TARGETARCH" = "i386-mingw32" ]; then
191         MINGW_PACKAGES="W32API MINGWRT"
192 fi
193
194 printf "Unpacking and patching ... \n"
195 for PACKAGE in GMP MPFR GCC BINUTILS GDB $MINGW_PACKAGES; do
196         archive=$PACKAGE"_ARCHIVE"
197         archive=${!archive}
198         dir=$PACKAGE"_DIR"
199         test -d ${!dir} || ( 
200                 printf " * `basename $archive`\n"
201                 FLAGS=zxf
202                 test ${archive:${#archive}-2:2} = "gz" && FLAGS=zxf
203                 test ${archive:${#archive}-3:3} = "bz2" && FLAGS=jxf
204                 $TAR $FLAGS tarballs/`basename $archive`
205                 for patch in patches/${!dir}_*.patch; do
206                         test -r $patch || continue
207                         printf "   o `basename $patch`\n"
208                         patch -s -N -p0 < `echo $patch`
209                 done
210         )
211 done
212 printf "Unpacked and patched ... "
213 printf "${green}ok${NC}\n"
214
215 if [ "$TARGETARCH" = "i386-mingw32" ]; then
216         mkdir -p $TARGETDIR/i386-mingw32/sys-include
217         mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
218 fi
219
220 mkdir -p build-gmp build-mpfr build-binutils build-gcc build-gdb
221 if [ -f build-gmp/.success ]; then
222         printf "Skipping GMP as it is already built\n"
223 else
224 printf "Building GMP ${GMP_VERSION} ... "
225 (
226         cd build-gmp
227         rm -f .failed
228         if [ `uname` = "Darwin" ]; then
229                 # generally the OS X compiler can create x64 binaries.
230                 # Per default it generated i386 binaries in 10.5 and x64
231                 # binaries in 10.6 (even if the kernel is 32bit)
232                 # For some weird reason, 10.5 autodetects an ABI=64 though
233                 # so we're setting the ABI explicitly here.
234                 OPTIONS="ABI=32"
235                 touch .architecture_check.c
236                 gcc .architecture_check.c -c -o .architecture_check.o
237                 ARCH=`file .architecture_check.o |cut -f5 -d\ `
238                 test  "$ARCH" = "x86_64" && OPTIONS="ABI=64"
239                 rm .architecture_check.c .architecture_check.o
240         fi
241
242         ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
243                 || touch .failed
244         $MAKE || touch .failed
245         $MAKE install DESTDIR=$DESTDIR || touch .failed
246         if [ ! -f .failed ]; then touch .success; fi
247 ) &> build-gmp/crossgcc-build.log
248 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
249 test -r build-gmp/.failed && exit 1
250 fi
251
252 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
253 #       # create compat link
254 #       ln -s $DESTDIR$TARGETDIR $TARGETDIR
255 #fi
256
257 # Now set CFLAGS to match GMP CFLAGS.
258 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
259
260 if [ -f build-mpfr/.success ]; then
261         printf "Skipping MPFR as it is already built\n"
262 else
263 printf "Building MPFR ${MPFR_VERSION} ... "
264 (
265         test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
266         cd build-mpfr
267         rm -f .failed
268         ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
269                 --infodir=$TARGETDIR/info \
270                 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
271         $MAKE || touch .failed
272         $MAKE install DESTDIR=$DESTDIR || touch .failed
273
274         # work around build problem of libgmp.la
275         if [ "$DESTDIR" != "" ]; then
276             perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
277         fi
278
279         if [ ! -f .failed ]; then touch .success; fi
280 ) &> build-mpfr/crossgcc-build.log
281 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
282 test -r build-mpfr/.failed && exit 1
283 fi
284
285 if [ -f build-binutils/.success ]; then
286         printf "Skipping binutils as it is already built\n"
287 else
288 printf "Building binutils ${BINUTILS_VERSION} ... "
289 (
290         cd build-binutils
291         rm -f .failed
292         ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
293                 --disable-werror --disable-nls \
294                 CFLAGS="$HOSTCFLAGS" || touch .failed
295         $MAKE || touch .failed
296         $MAKE install DESTDIR=$DESTDIR || touch .failed
297         if [ ! -f .failed ]; then touch .success; fi
298 ) &> build-binutils/crossgcc-build.log
299 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
300 test -r build-binutils/.failed && exit 1
301 fi
302
303 if [ -f build-gcc/.success ]; then
304         printf "Skipping GCC as it is already built\n"
305 else
306 printf "Building GCC ${GCC_VERSION} ... "
307 (
308         cd build-gcc
309         rm -f .failed
310         # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
311         # both target and host object files. This is pretty misdesigned.
312         # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
313         # but it does not seem to work properly. At least the host library
314         # libiberty is not compiled with CFLAGS_FOR_BUILD.
315         CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
316                 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
317                 --target=${TARGETARCH} --disable-werror --disable-shared \
318                 --disable-libssp --disable-bootstrap --disable-nls \
319                 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
320                 || touch .failed
321         $MAKE CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
322         $MAKE install DESTDIR=$DESTDIR || touch .failed
323         if [ ! -f .failed ]; then touch .success; fi
324 ) &> build-gcc/crossgcc-build.log
325 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
326 test -r build-gcc/.failed && exit 1
327 fi
328
329 if [ -f build-gdb/.success ]; then
330         printf "Skipping GDB as it is already built\n"
331 else
332 printf "Building GDB ${GDB_VERSION} ... "
333 (
334         cd build-gdb
335         export PATH=$PATH:$PREFIX/bin
336         rm -f .failed
337         ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
338                 --disable-werror --disable-nls
339         $MAKE || touch .failed
340         $MAKE install DESTDIR=$DESTDIR || touch .failed
341         if [ ! -f .failed ]; then touch .success; fi
342 ) &> build-gdb/crossgcc-build.log
343 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
344 test -r build-gdb/.failed && exit 1
345 fi
346
347 if [ $SAVETEMPS -eq 0 ]; then
348         printf "Cleaning up... "
349         rm -rf ${GMP_DIR} build-gmp
350         rm -rf ${MPFR_DIR} build-mpfr
351         rm -rf ${BINUTILS_DIR} build-binutils
352         rm -rf ${GCC_DIR} build-gcc
353         rm -rf ${GDB_DIR} build-gdb
354         printf "${green}ok${NC}\n"
355 else
356         printf "Leaving temporary files around... ${green}ok${NC}\n"
357 fi
358
359 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"
360
361