#! /bin/bash

tar xf ${SRC_PKGS}/gcc* &&
cd gcc* &&

export CC="${MLFS_TARGET}-gcc"             &&
export CXX="${MLFS_TARGET}-g++"            &&
export AR="${MLFS_TARGET}-ar"              &&
export AS="${MLFS_TARGET}-as"              &&
export RANLIB="${MLFS_TARGET}-ranlib"      &&
export LD="${MLFS_TARGET}-ld"              &&
export STRIP="${MLFS_TARGET}-strip"        &&

tar xf ${SRC_PKGS}/mpfr*  &&
tar xf ${SRC_PKGS}/gmp*   &&
tar xf ${SRC_PKGS}/mpc*   &&
mv -v mpfr* mpfr &&
mv -v gmp*  gmp  &&
mv -v mpc*  mpc  &&
# Apply alpine patches, except patch for libssp.
bash ../patches/gcc-10.3.1_git20210424-alpine/apply_patches_ct.sh

cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
  `dirname $($MLFS_TARGET-gcc -print-libgcc-file-name)`/include-fixed/limits.h &&
for file in gcc/config/{linux,i386/linux{,64}}.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done &&

for file in gcc/config/arm/linux-eabi.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done &&
for file in gcc/config/aarch64/aarch64-linux.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done &&
mkdir -v build && cd build                         &&
CFLAGS='-g0 -O0' \
CXXFLAGS='-g0 -O0' \
../configure                                       \
    --target=${MLFS_TARGET}                        \
    --build=${MLFS_HOST}                           \
    --host=${MLFS_TARGET}                          \
    --prefix=/tools                                \
    --with-local-prefix=/tools                     \
    --with-native-system-header-dir=/tools/include \
    --enable-languages=c,c++                       \
    --disable-libstdcxx-pch                        \
    --disable-multilib                             \
    --disable-bootstrap                            \
    --disable-libgomp                              \
    --disable-libquadmath                          \
    --disable-libssp                               \
    --disable-libvtv                               \
    --disable-symvers                              \
    --disable-libitm                               \
    --disable-libsanitizer \
    --with-arch=${MLFS_CPU} ${CSPEC} &&
PATH=/bin:/usr/bin:/cross-tools/bin:/tools/bin make ${MJ} &&
make install                                              &&
ln -sv gcc /tools/bin/cc                                                       &&
GCC_INCLUDEDIR=`dirname $(${MLFS_TARGET}-gcc -print-libgcc-file-name)`/include &&
find ${GCC_INCLUDEDIR}/* -maxdepth 0 -xtype d -exec rm -rvf '{}' \;            &&
rm -vf `grep -l "DO NOT EDIT THIS FILE" ${GCC_INCLUDEDIR}/*`                   &&
unset GCC_INCLUDEDIR                                                           &&

# Test toolchain
echo 'int main(){}' > dummy.c       &&
cc dummy.c                          &&
readelf -l a.out | grep ': /tools' >> ${PRGRSS}/tc.progress &&
rm -v dummy.c a.out                 &&
cd ${SRC_ROOT} && rm -rf gcc*       
