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/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"
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 " [-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"
117 # version tag is always printed, so just print the license here
120 Copyright (C) 2008-2009 by coresystems GmbH
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.
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.
134 printf "${blue}Welcome to the ${red}coresystems${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
136 # Look if we have getopt. If not, build it.
138 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
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 -- "$@"`
147 # Detected non-GNU getopt
148 args=`getopt Vhcd:p:j:D:t $*`
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;;
168 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
174 if [ "$TARGETARCH" = "i386-mingw32" ]; then
175 MINGW_ARCHIVES="$W32API_ARCHIVE $MINGWRT_ARCHIVE"
178 printf "Downloading tar balls ... \n"
180 for ARCHIVE in $GMP_ARCHIVE $MPFR_ARCHIVE $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $MINGW_ARCHIVES; do
181 FILE=`basename $ARCHIVE`
183 test -f tarballs/$FILE && printf "(cached)" || (
184 printf "(downloading)"
188 test -f tarballs/$FILE || printf "Failed to download $FILE.\n"
189 test -f tarballs/$FILE || exit 1
192 printf "Downloaded tar balls ... "
193 printf "${green}ok${NC}\n"
196 if [ "$TARGETARCH" = "i386-mingw32" ]; then
197 MINGW_PACKAGES="W32API MINGWRT"
200 printf "Unpacking and patching ... \n"
201 for PACKAGE in GMP MPFR GCC BINUTILS GDB $MINGW_PACKAGES; do
202 archive=$PACKAGE"_ARCHIVE"
206 printf " * `basename $archive`\n"
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`
218 printf "Unpacked and patched ... "
219 printf "${green}ok${NC}\n"
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
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"
230 printf "Building GMP ${GMP_VERSION} ... "
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.
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
248 ../${GMP_DIR}/configure --disable-shared --prefix=$TARGETDIR $OPTIONS \
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
258 #if [ "$DESTDIR" != "" -a ! -x $TARGETDIR ]; then
259 # # create compat link
260 # ln -s $DESTDIR$TARGETDIR $TARGETDIR
263 # Now set CFLAGS to match GMP CFLAGS.
264 HOSTCFLAGS=`grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2`
266 if [ -f build-mpfr/.success ]; then
267 printf "Skipping MPFR as it is already built\n"
269 printf "Building MPFR ${MPFR_VERSION} ... "
271 test `uname` = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
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
280 # work around build problem of libgmp.la
281 if [ "$DESTDIR" != "" ]; then
282 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
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
291 if [ -f build-binutils/.success ]; then
292 printf "Skipping binutils as it is already built\n"
294 printf "Building binutils ${BINUTILS_VERSION} ... "
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
309 if [ -f build-gcc/.success ]; then
310 printf "Skipping GCC as it is already built\n"
312 printf "Building GCC ${GCC_VERSION} ... "
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 \
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
335 if [ -f build-gdb/.success ]; then
336 printf "Skipping GDB as it is already built\n"
338 printf "Building GDB ${GDB_VERSION} ... "
341 export PATH=$PATH:$PREFIX/bin
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
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"
362 printf "Leaving temporary files around... ${green}ok${NC}\n"
365 printf "\n${green}You can now run your $TARGETARCH cross toolchain from $TARGETDIR.${NC}\n"