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