#!/bin/sh

MAKEDEPS="grep make dejagnu flex"
DEPS="zstd musl binutils mpc mpfr gmp libstdc++ libgcc"


PKG_VER=11.2.0
SOURCE=https://ftp.gnu.org/gnu/gcc/gcc-$PKG_VER/gcc-$PKG_VER.tar.xz

ISL_VER=0.24
PATCH_SRC="https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/gcc-alpine"

ADDITIONAL="
    https://libisl.sourceforge.io/isl-$ISL_VER.tar.xz

    0001-posix_memalign.patch 
    0003-Turn-on-Wl-z-relro-z-now-by-default.patch 
    0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch 
    0006-Enable-Wformat-and-Wformat-security-by-default.patch 
    0007-Enable-Wtrampolines-by-default.patch 
    0009-Ensure-that-msgfmt-doesn-t-encounter-problems-during.patch 
    0010-Don-t-declare-asprintf-if-defined-as-a-macro.patch 
    0012-libitm-disable-FORTIFY.patch 
    0013-libgcc_s.patch 
    0015-libffi-use-__linux__-instead-of-__gnu_linux__-for-mu.patch 
    0016-dlang-update-zlib-binding.patch 
    0017-dlang-fix-fcntl-on-mips-add-libucontext-dep.patch 
    0018-ada-fix-shared-linking.patch 
    0019-build-fix-CXXFLAGS_FOR_BUILD-passing.patch 
    0020-add-fortify-headers-paths.patch 
    0023-Pure-64-bit-MIPS.patch 
    0024-use-pure-64-bit-configuration-where-appropriate.patch 
    0025-always-build-libgcc_eh.a.patch 
    0027-ada-musl-support-fixes.patch 
    0028-gcc-go-Use-_off_t-type-instead-of-_loff_t.patch 
    0029-gcc-go-Don-t-include-sys-user.h.patch 
    0030-gcc-go-Fix-ucontext_t-on-PPC64.patch 
    0031-gcc-go-Fix-handling-of-signal-34-on-musl.patch 
    0032-gcc-go-Use-int64-type-as-offset-argument-for-mmap.patch 
    0035-gcc-go-Prefer-_off_t-over-_off64_t.patch 
    0036-gcc-go-undef-SETCONTEXT_CLOBBERS_TLS-in-proc.c.patch 
    0037-gcc-go-link-to-libucontext.patch 
    0038-gcc-go-Disable-printing-of-unaccessible-ppc64-struct.patch 
    0041-Use-generic-errstr.go-implementation-on-musl.patch 
    0042-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch 
    0043-configure-Add-enable-autolink-libatomic-use-in-LINK_.patch 
    0021-Alpine-musl-package-provides-libssp_nonshared.a.-We-.patch
    getconf.1
    getconf.c
    getent.1
    getent.c
    iconv.c
"

DESC="The GNU Compiler Collection - C and C++ frontends"

prepare () {
    export languages="c,c++,lto"

    case $(uname -m) in
      x86_64) export TRUPLE="x86_64-linux-musl"
              export EXTRA_CONFIG=" --with-arch=x86-64"
              ;;
      i686)   export TRUPLE="i686-linux-musl"
              export EXTRA_CONFIG=" --with-arch=pentium3 --with-tune=pentium-m"
              ;;
      armv7l) export TRUPLE="armv7l-linux-musleabihf"
              export EXTRA_CONFIG=" --with-arch=armv7-a --with-tune=generic-armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-abi=aapcs-linux --with-mode=thumb"
              ;;
      armv6l) export TRUPLE="armv6l-linux-musleabihf"
              export EXTRA_CONFIG=" --with-arch=armv6zk --with-tune=arm1176jzf-s --with-fpu=vfp --with-float=hard --with-abi=aapcs-linux"
              ;;
      aarch64) export TRUPLE="aarch64-linux-musleabihf"
               export EXTRA_CONFIG=" --with-arch=armv8-a --with-abi=lp64 --enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419"
               sed -i '/m64=/s/lib64/lib/' gcc/config/aarch64/t-aarch64-linux
              ;;
    esac 

    tar xf isl-$ISL_VER.tar.xz
    mv isl-$ISL_VER isl
    
    apply_patches
}

build () {
    mkdir build
    cd build

    export  NOFF="--disable-libstdcxx-pch 
                  --disable-nls 
                  --disable-multilib 
                  --disable-bootstrap 
                  --disable-symvers 
                  --disable-libsanitizer 
                  --disable-libmpx 
                  --disable-libmudflap 
                  --disable-fixed-point 
                  --disable-sjlj-exceptions 
                  --disable-werror "

    # Enable graphite
    export XCONFIG="--with-ppl=yes --with-cloog=yes "

    # Enable features for x86_64
    export XTRA86="--with-arch=x86-64 "

    # Enable features
    export  FON="--enable-threads=posix 
                 --enable-clocale=generic 
                 --enable-tls 
                 --enable-libstdcxx-time 
                 --enable-fully-dynamic-string 
                 --enable-linker-build-id 
                 --enable-checking=release 
                 --enable-cloog-backend 
                 --enable-__cxa_atexit 
                 --enable-lto 
                 --enable-plugins 
                 --with-system-zlib 
                 --with-linker-hash-style=gnu "
    #export FON+="--with-isl "

    # Configure source
    SED=sed  libat_cv_have_ifunc=no  \
    ../configure --prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-pkgversion="XiLinux $PKG_VER" \
		--enable-checking=release \
		--disable-fixed-point \
		--disable-libstdcxx-pch \
		--disable-multilib \
		--disable-nls \
		--disable-werror \
		--enable-__cxa_atexit \
		--enable-default-pie \
		--enable-cloog-backend \
		--enable-languages=$languages \
		--with-system-zlib \
        --target=${TRUPLE}          \
        $NOFF $XTRA86 $XCONFIG $FON &&

    make &&

    cd .. &&
    cc getent.c -o getent &&
    cc getconf.c -o getconf &&
    cc iconv.c -o iconv 
}

check () {
    cd build
    if id -u tester; then
        chown -R tester .
        su tester -c "PATH=$PATH make $MAKEFLAGS -k check" || true
        ../contrib/test_summary | grep -A7 Summ || true
    fi
    cd ..
}


package () {

    # create specsfile
    export SPECFILE=$PKG_DEST`dirname $(gcc -print-libgcc-file-name)`/specs
gcc -dumpspecs | sed -e 's@/tools@@g'                   \
    -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
    -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' >  tempspecfile
    mv -f tempspecfile $SPECFILE &&
    unset SPECFILE  MLFS_TARGET



    install -dm755 $PKG_DEST/usr/bin
    install -dm755 $PKG_DEST/usr/share/man
    install -dm755 $PKG_DEST/usr/lib/bfd-plugins

    cd build
    make DESTDIR=$PKG_DEST install
    cd ..

    cp getent  $PKG_DEST/usr/bin/
    cp getconf $PKG_DEST/usr/bin/
    cp iconv   $PKG_DEST/usr/bin/
    cp getent.1   $PKG_DEST/usr/share/man/man1/
    cp getconf.1  $PKG_DEST/usr/share/man/man1/

    ln -s ../bin/cpp $PKG_DEST/usr/lib
    ln -s gcc $PKG_DEST/usr/bin/cc

    ln -sf ../../libexec/gcc/$(gcc -dumpmachine)/$PKG_VER/liblto_plugin.so \
            $PKG_VER/usr/lib/bfd-plugins/

    mkdir -p $PKG_DEST/usr/share/gdb/auto-load/usr/lib
    #mv $PKG_DEST/usr/lib/*gdb.py $PKG_DEST/usr/share/gdb/auto-load/usr/lib

    for p in gcov gcc-ar gcc gcc-ranlib c++ g++ gcc-nm lto-dump gcov-tools gcov-dump cpp; do
        ln -s $TRUPLE-$p $PKG_DEST/usr/bin/$p
    done


}