# Build recipe for GCC.
#
# Copyright (c) 2015-2020 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=gcc
version=10-20200829
release=1

# Define a category for the output of the package name
pkgcategory=devel

tarname=${program}-${version}.tar.xz

# Remote source(s)
fetch=http://mirror.marwan.ma/gcc/snapshots/${version}/$tarname
#fetch="
#  http://www.netgull.com/gcc/snapshots/${version}/$tarname
#  http://gnu.c3sl.ufpr.br/ftp/gcc/gcc-${version}/$tarname
#  ftp://gcc.gnu.org/pub/gcc/snapshots/${version}/$tarname
#"

homepage=http://www.gnu.org/software/gcc
license="GPLv2+, GPLv3+, LGPLv2.1, LGPLv3 + GCC runtime library exception"

description="
The GNU C Compiler Collection.

The GNU Compiler Collection (GCC) is a compiler system produced by the
GNU Project supporting various programming languages.  GCC is a key
component of the GNU toolchain.  The Free Software Foundation (FSF)
distributes GCC under the GNU General Public License (GNU GPL).

For more information about GCC, visit:  $homepage
"

# Source documentation
docs="COPYING* README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Apply specific patches for the support in musl.
    # http://port70.net/~nsz/musl/gcc-10.1.0/

    patch -Np1 -i "${worktree}/patches/gcc/10/0002-posix_memalign.patch"
    patch -Np1 -i "${worktree}/patches/gcc/10/0003-j2.patch"
    patch -Np1 -i "${worktree}/patches/gcc/10/0004-static-pie.patch"

    # Apply extra patches to increment the security
    patch -Np1 -i "${worktree}/patches/gcc/10/extra-relro-in-dragora.patch"
    patch -Np1 -i "${worktree}/patches/gcc/10/extra-musl_libssp.patch"

    # Create a separate build directory
    rm -rf ../gcc-build
    mkdir ../gcc-build
    cd ../gcc-build

    # System-dependent
    MACHINE="$(gcc -dumpmachine)"

    ../${program}-${version}/configure SED=sed \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --infodir=$infodir \
     --enable-bootstrap \
     --enable-languages=c,c++,fortran,objc,obj-c++ \
     --enable-clocale=generic \
     --enable-threads=posix \
     --enable-cet=auto \
     --enable-tls \
     --enable-nls \
     --enable-lto \
     --enable-objc-gc \
     --enable-libstdcxx-time \
     --enable-checking=release \
     --enable-fully-dynamic-string \
     --enable-default-ssp \
     --enable-default-pie \
     --enable-libssp \
     --disable-symvers \
     --disable-libitm \
     --disable-gnu-indirect-function \
     --disable-libstdcxx-pch \
     --disable-libmudflap \
     --disable-libsanitizer \
     --disable-libcilkrts \
     --with-system-zlib \
     --with-linker-hash-style=gnu \
     --build=$MACHINE \
     --host=$MACHINE \
     --target=$MACHINE \
     $multilib_options \
     $gcc_options

    make -j${jobs} V=1 \
     BOOT_LDFLAGS="$QILDFLAGS" \
     BOOT_CFLAGS="$QICFLAGS" \
     BOOT_CXXFLAGS="$QICXXFLAGS"

    make -j${jobs} V=1 DESTDIR="$destdir" install

    # Delete non-shared version of libssp (provided on the musl package)
    rm -f "${destdir}/usr/lib${libSuffix}"/libssp_nonshared.a

    # Strip ELF executables only excluding shared objects
    find "$destdir" -type f | xargs file | awk '/ELF/ && /executable/' | \
     cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

    # Strip static libraries in order to reduce the package size
    find "$destdir" -type f | xargs file | awk '/current ar archive/' | \
     cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null || true

    # Back to source dir
    cd .. && cd "$srcdir"

    # Move misplaced file(s) for GDB
    mkdir -p "${destdir}/usr/share/gdb/auto-load/usr/lib${libSuffix}"
    mv "${destdir}/usr/lib${libSuffix}"/*-gdb.py \
       "${destdir}/usr/share/gdb/auto-load/usr/lib${libSuffix}"

    # Provide compatibility and make soft links for package size
    (
        cd "${destdir}/usr/bin" || exit 1
        mv gcc gcc-${version}
        ln -sf gcc-${version} gcc
        ln -sf gcc cc
        ln -sf gcc-${version} ${MACHINE}-gcc
        ln -sf gcc-${version} ${MACHINE}-gcc-${version}

        mv g++ g++-gcc-${version}
        ln -sf g++-gcc-${version} g++
        ln -sf g++ c++
        ln -sf g++-gcc-${version} ${MACHINE}-g++
        ln -sf g++-gcc-${version} ${MACHINE}-c++

        mv gfortran gfortran-${version}
        ln -sf gfortran-${version} gfortran
        ln -sf gfortran-${version} ${MACHINE}-gfortran
        # Extra symlinks for fortran
        ln -sf gfortran g77
        ln -sf g77 f77
        ln -sf gfortran g95
        ln -sf g95 f95

        mv gcc-ar gcc-ar-${version}
        ln -sf gcc-ar-${version} gcc-ar
        ln -sf gcc-ar-${version} ${MACHINE}-gcc-ar

        mv gcc-nm gcc-nm-${version}
        ln -sf gcc-nm-${version} gcc-nm
        ln -sf gcc-nm-${version} ${MACHINE}-gcc-nm

        mv gcc-ranlib gcc-ranlib-${version}
        ln -sf gcc-ranlib-${version} gcc-ranlib
        ln -sf gcc-ranlib-${version} ${MACHINE}-gcc-ranlib
    )
    (
        mkdir -p "${destdir}/lib" && \
        cd "${destdir}/lib" && ln -sf ../usr/bin/cpp .
    )

    unset -v MACHINE

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/* || true
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}/"

    # Delete temporary build directory
    cd -- "$TMPDIR"
    if test -d ./gcc-build
    then
        rm -rf ./gcc-build
    fi
}

