#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

$SCRIPTS/unpack gcc

setup_toolchain host

cd $BUILD/gcc*
mkdir -p objdir-$1 && cd objdir-$1
../configure --host=$HOST_NAME \
             --build=$HOST_NAME \
             --target=$TARGET_NAME \
             --prefix=$ROOT/$TOOLCHAIN \
             --with-sysroot=$SYSROOT_PREFIX \
             --with-gmp=$ROOT/$TOOLCHAIN \
             --with-mpfr=$ROOT/$TOOLCHAIN \
             --with-mpc=$ROOT/$TOOLCHAIN \
             --with-ppl=$ROOT/$TOOLCHAIN \
             --disable-ppl-version-check \
             --with-cloog=$ROOT/$TOOLCHAIN \
             --enable-languages=${TOOLCHAIN_LANGUAGES} \
             --with-gnu-as \
             --with-gnu-ld \
             --enable-__cxa_atexit \
             --disable-libada \
             --enable-decimal-float \
             --disable-libmudflap \
             --disable-libssp \
             --disable-multilib \
             --disable-gold \
             --enable-ld \
             --enable-plugin \
             --enable-lto \
             --disable-libquadmath \
             --enable-cloog-backend=isl \
             --enable-tls \
             --enable-shared \
             --enable-c99 \
             --enable-long-long \
             --enable-threads=posix \
             --disable-libstdcxx-pch \
             --enable-clocale=gnu \
             $GCC_OPTS \
             --disable-nls

make

# fix wrong link
  rm -rf $TARGET_NAME/libgcc/libgcc_s.so
  ln -sf libgcc_s.so.1 $TARGET_NAME/libgcc/libgcc_s.so

if [ ! "$DEBUG" = yes ]; then
  $TARGET_STRIP $TARGET_NAME/libgcc/libgcc_s.so*
  $TARGET_STRIP $TARGET_NAME/libgomp/.libs/libgomp.so*
  $TARGET_STRIP $TARGET_NAME/libitm/.libs/libitm.so*
  $TARGET_STRIP $TARGET_NAME/libstdc++-v3/src/.libs/libstdc++.so*
fi

make install
cp -PR $TARGET_NAME/libstdc++-v3/src/.libs/libstdc++.so* $SYSROOT_PREFIX/usr/lib

GCC_VERSION=`$ROOT/$TOOLCHAIN/$TARGET_NAME/bin/gcc -dumpversion`
DATE="0501`echo $GCC_VERSION | sed 's/\([0-9]\)/0\1/g' | sed 's/\.//g'`"
CROSS_CC=$TARGET_CC-$GCC_VERSION
CROSS_CXX=$TARGET_CXX-$GCC_VERSION

rm -f $TARGET_CC
cat > $TARGET_CC <<EOF
#!/bin/sh
$ROOT/$TOOLCHAIN/bin/ccache $CROSS_CC "\$@"
EOF
chmod +x $TARGET_CC

# To avoid cache trashing
  touch -c -t $DATE $CROSS_CC

[ ! -f "$CROSS_CXX" ] && mv $TARGET_CXX $CROSS_CXX
cat > $TARGET_CXX <<EOF
#!/bin/sh
$ROOT/$TOOLCHAIN/bin/ccache $CROSS_CXX "\$@"
EOF
chmod +x $TARGET_CXX

# To avoid cache trashing
  touch -c -t $DATE $CROSS_CXX
