# Description: The GNU Compiler Collection
# URL: http://gcc.gnu.org
# Maintainer: CRUX System Team, core-ports at crux dot nu
# Arch Maintainer: CRUX-ARM System Team, devel at crux-arm dot nu
# Depends on: zlib libmpc

name=gcc
version=7.3.0
release=1
source=(ftp://gcc.gnu.org/pub/gcc/releases/gcc-$version/$name-$version.tar.xz
        $name-nocheck-fixincludes.patch)


build() {
  cd $name-$version

  patch -p1 -i $SRC/$name-nocheck-fixincludes.patch
  sed -i 's|REVISION|REVISION " (CRUX-ARM)"|' $name/version.c
  # apply a sed substitution that will suppress the installation of libiberty.a
  sed 's|install_to_$(INSTALL_DEST) ||' -i libiberty/Makefile.in

  mkdir $SRC/build
  cd $SRC/build

  export CXXFLAGS="-O2 -pipe"
  LDFLAGS="-Wl,-rpath-link,$CROSSTOOLS/$CTARGET/lib" \
  ../$name-$version/configure --build=$CHOST \
                              --host=$CTARGET \
                              --target=$CTARGET \
                              --prefix=/usr \
                              --mandir=/usr/man \
                              --libexecdir=/usr/lib \
                              --enable-languages=c,c++ \
                              --disable-libstdcxx-pch \
                              --enable-threads=posix \
                              --enable-__cxa_atexit \
                              --enable-clocale=gnu \
                              --enable-shared \
                              --with-float=hard \
                              --disable-nls \
                              --with-x=no \
                              --with-system-zlib \
                              --with-pkgversion="CRUX-ARM"

  # prevent GCC from looking in the wrong directories for headers and libraries
  sed "/^HOST_\(GMP\|PPL\|CLOOG\)\(LIBS\|INC\)/s:-[IL]/\(lib\|include\)::" -i Makefile

  make 

  # prevent libtool from adding host lib paths for relink at install
  # based on patch submitted by Vincent Cadet for libtool bug #21455
  # see https://lists.gnu.org/archive/html/bug-libtool/2015-09/msg00012.html
  for lt_file in $(find $SRC/build/$CTARGET -name libtool) ; do
    sed -i 's/\(add_dir=\"\)\$add_dir \(-L\$inst_prefix_dir\$libdir\"\)/\1\2/' $lt_file
  done

  make -j1 DESTDIR=$PKG install

  mkdir $PKG/lib
  ln -sf ../usr/bin/cpp $PKG/lib/cpp
  ln -sf gcc $PKG/usr/bin/cc
  ln -sf g++ $PKG/usr/bin/c++

  mv $PKG/usr/lib/gcc/*/$version/include-fixed/{limits.h,syslimits.h} $PKG/usr/lib/gcc/*/$version/include/

  rm $PKG/usr/lib/libstdc++.so.6.0.24-gdb.py
  rm -r $PKG/usr/share
  rm -r $PKG/usr/lib/gcc/*/$version/{install-tools,include-fixed}

  sed -i "s|-L$SRC[^ ]* ||g" $PKG/usr/lib/{libstdc++.la,libsupc++.la}
}