Since some people disapprove of white space cleanups mixed in regular commits
[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.14
36 MINGWRT_VERSION=3.18
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/w32api-${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 "    [-j|--jobs <num>]             run <num> jobs in parallel in make\n"
107         printf "    [-p|--platform <platform>]    target platform to build cross compiler for\n"
108         printf "                                  (defaults to $TARGETARCH)\n"
109         printf "    [-d|--directory <target dir>] target directory to install cross compiler to\n"
110         printf "                                  (defaults to $TARGETDIR)\n\n"
111         printf "    [-D|--destdir <dest dir>]     destination directory to install cross compiler to\n"
112         printf "                                  (for RPM builds, default unset)\n\n"
113 }
114
115 myversion()
116 {
117         # version tag is always printed, so just print the license here
118
119         cat << EOF
120 Copyright (C) 2008-2009 by coresystems GmbH
121
122 This program is free software; you can redistribute it and/or modify
123 it under the terms of the GNU General Public License as published by
124 the Free Software Foundation; version 2 of the License.
125
126 This program is distributed in the hope that it will be useful,
127 but WITHOUT ANY WARRANTY; without even the implied warranty of
128 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
129 GNU General Public License for more details.
130
131 EOF
132 }
133
134 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
135
136 # Look if we have getopt. If not, build it.
137 export PATH=$PATH:.
138 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
139
140 # parse parameters.. try to find out whether we're running GNU getopt
141 getoptbrand="`getopt -V`"
142 if [ "${getoptbrand:0:6}" == "getopt" ]; then
143         # Detected GNU getopt that supports long options.
144         args=`getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps Vhcd:p:j:D:t -- "$@"`
145         eval set "$args"
146 else
147         # Detected non-GNU getopt
148         args=`getopt Vhcd:p:j:D:t $*`
149         set -- $args
150 fi
151
152 if [ $? != 0 ]; then
153         myhelp
154         exit 1
155 fi
156
157 while true ; do
158         case "$1" in
159                 -V|--version)   shift; myversion; exit 0;;
160                 -h|--help)      shift; myhelp; exit 0;;
161                 -c|--clean)     shift; cleanup;;
162                 -t|--savetemps) shift; SAVETEMPS=1;;
163                 -d|--directory) shift; TARGETDIR="$1"; shift;;
164                 -p|--platform)  shift; TARGETARCH="$1"; shift;;
165                 -D|--destdir)   shift; DESTDIR="$1"; shift;;
166                 -j|--jobs)      shift; JOBS="-j $1"; shift;;
167                 --)             shift; break;;
168                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
169                 *)              break;;
170         esac
171 done
172
173 MINGW_ARCHIVES=""
174 if [ "$TARGETARCH" = "i386-mingw32" ]; then
175         MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
176 fi
177
178 printf "Downloading tar balls ... \n"
179 mkdir -p tarballs
180 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
181         FILE=`basename $ARCHIVE`
182         printf " * $FILE "
183         test -f tarballs/$FILE && printf "(cached)" || (
184                 printf "(downloading)"
185                 cd tarballs
186                 wget -q $ARCHIVE
187         )
188         test -f tarballs/$FILE || printf "Failed to download $FILE.\n"
189         test -f tarballs/$FILE || exit 1
190         printf "\n"
191 done
192 printf "Downloaded tar balls ... "
193 printf "${green}ok${NC}\n"
194
195 MINGW_PACKAGES=""
196 if [ "$TARGETARCH" = "i386-mingw32" ]; then
197         MINGW_PACKAGES="W32API MINGWRT"
198 fi
199
200 printf "Unpacking and patching ... \n"
201 for PACKAGE in GMP MPFR GCC BINUTILS GDB $MINGW_PACKAGES; do
202         archive=$PACKAGE"_ARCHIVE"
203         archive=${!archive}
204         dir=$PACKAGE"_DIR"
205         test -d ${!dir} || (
206                 printf " * `basename $archive`\n"
207                 FLAGS=zxf
208                 test ${archive:${#archive}-2:2} = "gz" && FLAGS=zxf
209                 test ${archive:${#archive}-3:3} = "bz2" && FLAGS=jxf
210                 $TAR $FLAGS tarballs/`basename $archive`
211                 for patch in patches/${!dir}_*.patch; do
212                         test -r $patch || continue
213                         printf "   o `basename $patch`\n"
214                         $PATCH -s -N -p0 < `echo $patch`
215                 done
216         )
217 done
218 printf "Unpacked and patched ... "
219 printf "${green}ok${NC}\n"
220
221 if [ "$TARGETARCH" = "i386-mingw32" ]; then
222         mkdir -p $TARGETDIR/i386-mingw32/sys-include
223         mv $MINGWRT_DIR/include/* $W32API_DIR/include/* $TARGETDIR/i386-mingw32/sys-include
224 fi
225
226 mkdir -p build-gmp build-mpfr build-binutils build-gcc build-gdb
227 if [ -f build-gmp/.success ]; then
228         printf "Skipping GMP as it is already built\n"
229 else
230 printf "Building GMP ${GMP_VERSION} ... "
231 (
232         cd build-gmp
233         rm -f .failed
234         if [ `uname` = "Darwin" ]; then
235                 # generally the OS X compiler can create x64 binaries.
236                 # Per default it generated i386 binaries in 10.5 and x64
237                 # binaries in 10.6 (even if the kernel is 32bit)
238                 # For some weird reason, 10.5 autodetects an ABI=64 though
239                 # so we're setting the ABI explicitly here.
240                 OPTIONS="ABI=32"
241                 touch .architecture_check.c
242                 gcc .architecture_check.c -c -o .architecture_check.o
243                 ARCH=`file .architecture_check.o |cut -f5 -d\ `
244                 test  "$ARCH" = "x86_64" && OPTIONS="ABI=64"
245                 rm .architecture_check.c .architecture_check.o
246         fi
247
248         ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
249                 || touch .failed
250         $MAKE $JOBS || touch .failed
251         $MAKE install DESTDIR=$DESTDIR || touch .failed
252         if [ ! -f .failed ]; then touch .success; fi
253 ) &> build-gmp/crossgcc-build.log
254 test -r build-gmp/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
255 test -r build-gmp/.failed && exit 1
256 fi
257
258 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
259 #       # create compat link
260 #       ln -s $DESTDIR$TARGETDIR $TARGETDIR
261 #fi
262
263 # Now set CFLAGS to match GMP CFLAGS.
264 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
265
266 if [ -f build-mpfr/.success ]; then
267         printf "Skipping MPFR as it is already built\n"
268 else
269 printf "Building MPFR ${MPFR_VERSION} ... "
270 (
271         test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
272         cd build-mpfr
273         rm -f .failed
274         ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
275                 --infodir=$TARGETDIR/info \
276                 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || touch .failed
277         $MAKE $JOBS || touch .failed
278         $MAKE install DESTDIR=$DESTDIR || touch .failed
279
280         # work around build problem of libgmp.la
281         if [ "$DESTDIR" != "" ]; then
282             perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
283         fi
284
285         if [ ! -f .failed ]; then touch .success; fi
286 ) &> build-mpfr/crossgcc-build.log
287 test -r build-mpfr/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
288 test -r build-mpfr/.failed && exit 1
289 fi
290
291 if [ -f build-binutils/.success ]; then
292         printf "Skipping binutils as it is already built\n"
293 else
294 printf "Building binutils ${BINUTILS_VERSION} ... "
295 (
296         cd build-binutils
297         rm -f .failed
298         ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
299                 --disable-werror --disable-nls \
300                 CFLAGS="$HOSTCFLAGS" || touch .failed
301         $MAKE $JOBS || touch .failed
302         $MAKE install DESTDIR=$DESTDIR || touch .failed
303         if [ ! -f .failed ]; then touch .success; fi
304 ) &> build-binutils/crossgcc-build.log
305 test -r build-binutils/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
306 test -r build-binutils/.failed && exit 1
307 fi
308
309 if [ -f build-gcc/.success ]; then
310         printf "Skipping GCC as it is already built\n"
311 else
312 printf "Building GCC ${GCC_VERSION} ... "
313 (
314         cd build-gcc
315         rm -f .failed
316         # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
317         # both target and host object files. This is pretty misdesigned.
318         # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
319         # but it does not seem to work properly. At least the host library
320         # libiberty is not compiled with CFLAGS_FOR_BUILD.
321         CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
322                 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
323                 --target=${TARGETARCH} --disable-werror --disable-shared \
324                 --disable-libssp --disable-bootstrap --disable-nls \
325                 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
326                 || touch .failed
327         $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" || touch .failed
328         $MAKE install DESTDIR=$DESTDIR || touch .failed
329         if [ ! -f .failed ]; then touch .success; fi
330 ) &> build-gcc/crossgcc-build.log
331 test -r build-gcc/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
332 test -r build-gcc/.failed && exit 1
333 fi
334
335 if [ -f build-gdb/.success ]; then
336         printf "Skipping GDB as it is already built\n"
337 else
338 printf "Building GDB ${GDB_VERSION} ... "
339 (
340         cd build-gdb
341         export PATH=$PATH:$PREFIX/bin
342         rm -f .failed
343         ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR --target=${TARGETARCH} \
344                 --disable-werror --disable-nls
345         $MAKE $JOBS || touch .failed
346         $MAKE install DESTDIR=$DESTDIR || touch .failed
347         if [ ! -f .failed ]; then touch .success; fi
348 ) &> build-gdb/crossgcc-build.log
349 test -r build-gdb/.failed && printf "${RED}failed${NC}\n" || printf "${green}ok${NC}\n"
350 test -r build-gdb/.failed && exit 1
351 fi
352
353 if [ $SAVETEMPS -eq 0 ]; then
354         printf "Cleaning up... "
355         rm -rf ${GMP_DIR} build-gmp
356         rm -rf ${MPFR_DIR} build-mpfr
357         rm -rf ${BINUTILS_DIR} build-binutils
358         rm -rf ${GCC_DIR} build-gcc
359         rm -rf ${GDB_DIR} build-gdb
360         printf "${green}ok${NC}\n"
361 else
362         printf "Leaving temporary files around... ${green}ok${NC}\n"
363 fi
364
365 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"
366
367