# Build recipe for binutils.
#
# Copyright (c) 2015-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.

# Exit immediately on any error
set -e

program=binutils
version=2.38
release=2

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

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

# Remote source(s)
fetch=https://ftp.gnu.org/gnu/binutils/$tarname

description="
The GNU binary utilities.

Binutils is a collection of programming tools for the manipulation
of object code in various object file formats.  These tools consist
of the GNU linker (ld), the GNU assembler (as) and the profiler
(gprof).  There is also a collection of other binary tools like
the disassembler (objdump).
"

homepage=https://www.gnu.org/software/binutils
license="GPLv2+, GPLv3+, LGPLv2, LGPLv3"

# 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 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"

    # Create a separate build directory
    rm -rf BUILD
    mkdir BUILD
    cd BUILD

    # To pass extra options to binutils according to the architecture
    binutils_hash_style=gnu;
    case $arch in
    i?86 | amd64 | x32 )
        binutils_targets="--enable-targets=i386-efi-pe,x86_64-pep"
        ;;
    mips*)
        binutils_hash_style=sysv;
        ;;
    esac

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

    ../configure CPPFLAGS="$QICPPFLAGS" \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" \
    LDFLAGS="$(echo $QILDFLAGS | sed 's/-s//')" \
     $configure_args \
     $multilib_options \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --infodir=$infodir \
     --with-docdir=$docsdir \
     --enable-deterministic-archives \
     --enable-shared \
     --enable-gold=yes \
     --enable-ld=default \
     --enable-threads=auto \
     --enable-default-hash-style=$binutils_hash_style \
     --enable-relro \
     --enable-lto \
     --enable-plugins \
     --enable-64-bit-bfd \
     --enable-install-libiberty \
     --enable-separate-code \
     --disable-nls \
     --disable-werror \
     --disable-compressed-debug-sections \
     --disable-generate-build-notes \
     --with-system-zlib \
     --with-pic \
     --build=$MACHINE \
     $binutils_targets

    unset -v binutils_targets binutils_hash_style

    make -j${jobs} V=1 tooldir=/usr/${MACHINE}
    #make -j${jobs} V=1 check
    make -j${jobs} V=1 tooldir=/usr/${MACHINE} DESTDIR="$destdir" install

    find "$destdir" -type f -print0 | xargs -0 file | \
     awk '/ELF/ && /executable/ || /shared object/' | \
      cut -f 1 -d : | xargs -0 strip --remove-section=.comment \
       --remove-section=.note 2> /dev/null || true

    cd ../

    # Replace hard-links with relative soft-links for the package size
    (
        cd "${destdir}/usr/bin" && \
        rm -f ld ; ln -sf ld.bfd ld
    )
    (
        cd "${destdir}/usr/${MACHINE}/bin" && \
        for file in *
        do
            if test -x "../../bin/$file"
            then
                rm -f "$file" && ln -s "../../bin/$file" .
            fi
        done
    )

    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}"/*
    fi

    # Compress man pages
    if test -d "${destdir}/$mandir"
    then
        lzip -9 "${destdir}/${mandir}"/man?/*
    fi

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

