# Build script for binutils.
#
# Copyright (c) 2014-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=2.38

cd -- "$TMPDIR"
rm -rf binutils-${version}
unpack "${worktree}/sources/binutils-${version}.tar.lz"

# Build instructions
cd binutils-${version}

# Apply patches from Fedora (Nick Clifton)

patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-export-demangle.h.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-no-config-h-check.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-filename-in-error-messages.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-revert-PLT-elision.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-readelf-other-sym-info.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-2.27-aarch64-ifunc.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-link-with-static-libstdc++.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-special-sections-in-groups.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-mismatched-section-flags.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-CVE-2019-1010204.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-warn-unsupported.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-use-long-long.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-i386-gnu-property-notes.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-autoconf-version.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/gcc12-libtool-no-rpath.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-section-type.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gas-loc-view.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-use-debuginfod.patch"

patch -Np1 -i "${worktree}/patches/binutils/branch-updates.diff"

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

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

../binutils-${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-deterministic-archives \
 --enable-separate-code \
 --enable-relro \
 --enable-threads=auto \
 --enable-default-hash-style=gnu \
 --enable-64-bit-bfd \
 --enable-install-libiberty \
 --disable-compressed-debug-sections \
 --disable-generate-build-notes \
 --disable-nls \
 --disable-werror \
 --disable-ppl-version-check \
 --disable-cloog-version-check \
 --with-lib-path=/tools/lib \
 --with-pic \
 $multilib_options

make -j${jobs} V=1 MAKEINFO="true"
make -j${jobs} V=1 MAKEINFO="true" install

# Make a new ld(1) containing another search path,
# this is handled by "20-toolchain" (called from musl's recipe)
make -C ld clean
make -C ld LIB_PATH=/usr/lib${libSuffix}:/lib
cp -f ld/ld-new /tools/bin

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

cd -- "$TMPDIR"

cleanup()
{
    rm -rf binutils-${version} binutils-build
}

