# Build script for gcc.
#
# Copyright (c) 2016-2022 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.

version=11-20220429

# Prerequisites
gmp_version=6.2.1
mpfr_version=4.1.0
mpc_version=1.2.1
isl_version=0.24

cd -- "$TMPDIR"

rm -rf gcc-${version} gmp-${gmp_version} \
       mpfr-${mpfr_version} mpc-${mpc_version} isl-${isl_version}
unpack "${worktree}/sources/gcc-${version}.tar.xz" \
       "${worktree}/sources/gmp-${gmp_version}.tar.lz" \
       "${worktree}/sources/mpfr-${mpfr_version}.tar.bz2" \
       "${worktree}/sources/mpc-${mpc_version}.tar.gz" \
       "${worktree}/sources/isl-${isl_version}.tar.bz2"

# Build instructions
cd gcc-${version}

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

patch -Np1 -i "${worktree}/patches/gcc/11/0002-posix_memalign.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0003-j2.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0004-static-pie.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0005-m68k-sqrt.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/extra-musl_libssp.patch"

# Apply patches from "Alpine Linux" in order to improve the security (Thanks!)
echo "patch1:"
patch -Np1 -i "${worktree}/patches/gcc/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch"
echo "patch2:"
patch -Np1 -i "${worktree}/patches/gcc/0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch"
echo "patch3:"
patch -Np1 -i "${worktree}/patches/gcc/0006-Enable-Wformat-and-Wformat-security-by-default.patch"
echo "patch4:"
patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
echo "patch5:"
patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
echo "patch6:"
patch -Np1 -i "${worktree}/patches/gcc/0021-add-fortify-headers-paths.patch"

# Hard code default ld path to look at /tools

find . -type f -name '*.h' -exec grep -l "/lib/ld-" {} + | while read -r file
do
    test -f "$file" || continue;
    cp -f "$file" "${file}.orig"

    sed -e 's@/lib\(64\)\{0,1\}\(32\)\{0,1\}/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"
done

# Make symlinks for requisites
ln -s ../gmp-${gmp_version} gmp
ln -s ../mpfr-${mpfr_version} mpfr
ln -s ../mpc-${mpc_version} mpc
ln -s ../isl-${isl_version} isl

# Apply patches for MPFR
(
    cd mpfr || exit 1

    for file in "${worktree}"/patches/mpfr/*
    do
        if test -f "$file"
        then
            rm -f PATCHES
            patch -p1 < "$file"
        fi
    done
)

# Update detection for hosts based on musl

cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
cp -f "${worktree}/archive/common/config.sub" gmp/config.sub

cp -f "${worktree}/archive/common/config.guess" isl/config.guess
cp -f "${worktree}/archive/common/config.sub" isl/config.sub

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

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

../gcc-${version}/configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --libdir=/tools/lib${libSuffix} \
 --build=$host \
 --host=$target \
 --target=$target \
 --enable-languages=c,c++ \
 --enable-clocale=generic \
 --enable-cet=auto \
 --enable-initfini-array \
 --enable-tls \
 --enable-libstdcxx-time \
 --enable-fully-dynamic-string \
 --enable-default-ssp \
 --enable-default-pie \
 --enable-libssp \
 --disable-symvers \
 --disable-bootstrap \
 --disable-libstdcxx-pch \
 --disable-gnu-indirect-function \
 --disable-libsanitizer \
 --disable-nls \
 --disable-install-libiberty \
 --with-linker-hash-style=gnu \
 --with-local-prefix=/tools \
 --with-native-system-header-dir=/tools/include \
 $multilib_options \
 $gcc_options

make -j${jobs} all \
 AS_FOR_TARGET="$AS" \
 LD_FOR_TARGET="$LD"

make -j${jobs} install

# Provide minimal libssp_nonshared.a

$BTCC -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
      -o __stack_chk_fail_local.o
$AR rc libssp_nonshared.a __stack_chk_fail_local.o
$RANLIB libssp_nonshared.a
cp -p libssp_nonshared.a /tools/lib${libSuffix}

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cd -- "$TMPDIR"

cleanup()
{
    rm -rf gcc-${version} gcc-build \
     gmp-${gmp_version} mpfr-${mpfr_version} \
     mpc-${mpc_version} isl-${isl_version}
}

