#!/usr/bin/env bash
name='gcc'
release='1'
version='12.2.0'
url='https://ftp.gnu.org/gnu/gcc/'
description='GNU Compiler Collection'
email=' parduscix@yandex.ru'
maintainer=' sulincix'
license=('GPLv3')
source=("https://ftp.gnu.org/gnu/gcc/gcc-12.2.0/gcc-${version}.tar.xz")
depends=(binutils libmpc zlib)
makedepends=( )
md5sums=('73bafd0af874439dcdb9fc063b6fb069')
arch=('x86_64')
group=(sys.devel)
uses=(multilib)
uses_extra=(langs)

cd $name-$version
export CFLAGS="-O2 -s"
export CXXFLAGS="-O2 -s"
unset LDFLAGS

prepare(){
    cd ..
    # tar permissions and owners is not supported by ymp sandbox.
    # we remove broken source and extract again without permission and owners
    rm -rf $name-$version
    tar --no-same-owner --no-same-permissions -xvf $name-$version.tar.xz
}

setup(){
    options=(
      --prefix=/usr
      --libdir=/usr/lib64
      --libexecdir=/usr/libexec
      --mandir=/usr/share/man
      --infodir=/usr/share/info
      --with-pkgversion="Sulix"
      --with-bugurl=https://gitlab.com/groups/sulix/-/issues
      --disable-checking
      --disable-fixed-point
      --disable-libstdcxx-pch
      --enable-plugin
      --enable-lto
      --enable-languages=c,c++$(use_opt langs ",fortran,go,objc,obj-c++" "")
      $(use_opt multilib "--enable-multilib" "")
      $(use_opt multilib "--enable-multiarch" "")
      --enable-shared
      --without-zstd
      --with-system-zlib
      --disable-nls
    )
    mkdir build
    cd build
    ../configure ${options[@]}
}

build(){
    cd build
    make -j`nproc`
}

package(){
    cd build
    make install -j`nproc`
    mkdir -p ${DESTDIR}/usr/lib64/
    if use multilib ; then
        # move lib to lib64
        if [ -d ${DESTDIR}/usr/lib && ! -d ${DESTDIR}/usr/lib32 ] ; then
            mv ${DESTDIR}/usr/{lib,lib32}
        fi
    fi
}

