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>
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.
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.
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
21 CROSSGCC_DATE="August 18th, 2009"
22 CROSSGCC_VERSION="0.9"
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"
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"
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
80 printf "${RED}ERROR:${red} Missing toolchain: $1${NC}\n" >&2
85 PATCH=`searchgnu patch`
90 printf "Cleaning up temporary files... "
91 rm -rf build-* combined gcc-* gmp-* mpfr-* binutils-* gdb-* w32api-* mingwrt-*
92 printf "${green}ok${NC}\n"
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"
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"
116 # version tag is always printed, so just print the license here
119 Copyright (C) 2008-2009 by coresystems GmbH
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.
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.
133 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
135 # Look if we have getopt. If not, build it.
137 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
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 -- "$@"`
146 # Detected non-GNU getopt
147 args=`getopt Vhcd:p:D:t $*`
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;;
166 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
172 if [ "$TARGETARCH" = "i386-mingw32" ]; then
173 MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
176 printf "Downloading tar balls ... \n"
178 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
179 FILE=`basename $ARCHIVE`
181 test -f tarballs/$FILE && printf "(cached)" || (
182 printf "(downloading)"
186 test -f tarballs/$FILE || printf "Failed to download $FILE.\n"
187 test -f tarballs/$FILE || exit 1
190 printf "Downloaded tar balls ... "
191 printf "${green}ok${NC}\n"
194 if [ "$TARGETARCH" = "i386-mingw32" ]; then
195 MINGW_PACKAGES="W32API MINGWRT"
198 printf "Unpacking and patching ... \n"
199 for PACKAGE in GMP MPFR GCC BINUTILS GDB $MINGW_PACKAGES; do
200 archive=$PACKAGE"_ARCHIVE"
204 printf " * `basename $archive`\n"
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`
216 printf "Unpacked and patched ... "
217 printf "${green}ok${NC}\n"
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
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"
228 printf "Building GMP ${GMP_VERSION} ... "
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.
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
246 ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
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
256 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
257 # # create compat link
258 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
261 # Now set CFLAGS to match GMP CFLAGS.
262 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
264 if [ -f build-mpfr/.success ]; then
265 printf "Skipping MPFR as it is already built\n"
267 printf "Building MPFR ${MPFR_VERSION} ... "
269 test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
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
278 # work around build problem of libgmp.la
279 if [ "$DESTDIR" != "" ]; then
280 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
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
289 if [ -f build-binutils/.success ]; then
290 printf "Skipping binutils as it is already built\n"
292 printf "Building binutils ${BINUTILS_VERSION} ... "
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
307 if [ -f build-gcc/.success ]; then
308 printf "Skipping GCC as it is already built\n"
310 printf "Building GCC ${GCC_VERSION} ... "
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 \
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
333 if [ -f build-gdb/.success ]; then
334 printf "Skipping GDB as it is already built\n"
336 printf "Building GDB ${GDB_VERSION} ... "
339 export PATH=$PATH:$PREFIX/bin
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
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"
360 printf "Leaving temporary files around... ${green}ok${NC}\n"
363 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"