# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Mateusz Mikula <mati865@gmail.com>

_realname=binutils
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.41
pkgrel=2
pkgdesc="A set of programs to assemble and manipulate binary and object files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url="https://www.gnu.org/software/binutils/"
license=('spdx:GPL-3.0-or-later AND GPL-2.0-or-later AND LGPL-3.0-or-later AND LGPL-2.0-or-later')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=(https://ftp.gnu.org/gnu/binutils/${_realname}-${pkgver}.tar.bz2{,.sig}
        0002-check-for-unusual-file-harder.patch
        0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
        0110-binutils-mingw-gnu-print.patch
        2001-ld-option-to-move-default-bases-under-4GB.patch
        2003-Restore-old-behaviour-of-windres-so-that-options-con.patch
        reproducible-import-libraries.patch
        specify-timestamp.patch
        libiberty-unlink-handle-windows-nul.patch
        "6aadf8a04d162feb2afe3c41f5b36534d661d447.patch::https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff_plain;h=6aadf8a04d162feb2afe3c41f5b36534d661d447"
        "398f1ddf5e89e066aeee242ea854dcbaa8eb9539.patch::https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff_plain;h=398f1ddf5e89e066aeee242ea854dcbaa8eb9539"
        "26d0081b52dc482c59abba23ca495304e698ce4b.patch::https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff_plain;h=26d0081b52dc482c59abba23ca495304e698ce4b"
        "8606b47e94078e77a53f3cd714272c853d2add22.patch::https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff_plain;h=8606b47e94078e77a53f3cd714272c853d2add22"
        "54d57acf610e5db2e70afa234fd4018207606774.patch::https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff_plain;h=54d57acf610e5db2e70afa234fd4018207606774"
)
sha256sums=('a4c4bec052f7b8370024e60389e194377f3f48b56618418ea51067f67aaab30b'
            'SKIP'
            '2c99345fc575c3a060d6677537f636c6c4154fac0fde508070f3b6296c1060d4'
            '4e8ac055df61b1b5d6ae29dc87e1154737c2e87c7b244b44866702cabf1a5d18'
            '5f3fc3949172d2d6e7cd595f9359077e012391a8c3c2aebc02e30fa656ded833'
            '328ed91bdc9c729ab19f2a566d2d1cbb5ac1beb4775bd71d85e9777b4ed4c17a'
            'd584f1cd9e94cba0e9b27625c4acc8ad5242cd625c9b44839d42fc116072568c'
            'a094660ec95996c00b598429843b7869037732146442af567ada9f539bd40480'
            '27696da8ecfff307537a461b205fad44d6abc1fa648fbf839e72a1d3ea71c40a'
            '7ccbd418695733c50966068fa9755a6abb156f53af23701d2bc097c63e9e0030'
            '6da34bb31674dd06b6224cec7d3cda45f689fdfe8cdb8a4fc5e7f93694abf124'
            'e524ca344af401d9ea513931dae70daf62014e1efcd706a681bdb9dd0deffaa9'
            '8388ea27dc4d0d477fb1a6cbe96f4697c81a714c7b8a6923bd8df29d4aef0321'
            '0c300b88823d0a8ee23b570f1f7f4d32a08530910175117ea60d559b1e412b3e'
            '70f639d4c66a261ff94a9c6bc4669efcefe669cdf6a45edce83c158e7d4fa306')
validpgpkeys=('EAF1C276A747E9ED86210CBAC3126D3B4AE55E93'
              '3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  apply_patch_with_msg \
    0002-check-for-unusual-file-harder.patch \
    0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch \
    0110-binutils-mingw-gnu-print.patch

  # Add an option to change default bases back below 4GB to ease transition
  # https://github.com/msys2/MINGW-packages/issues/7027
  # https://github.com/msys2/MINGW-packages/issues/7023
  apply_patch_with_msg 2001-ld-option-to-move-default-bases-under-4GB.patch

  # https://github.com/msys2/MINGW-packages/pull/9233#issuecomment-889439433
  patch -R -p1 -i "${srcdir}/2003-Restore-old-behaviour-of-windres-so-that-options-con.patch"

  # patches for reproducibility from Debian:
  # https://salsa.debian.org/mingw-w64-team/binutils-mingw-w64/-/tree/master/debian/patches
  patch -p2 -i "${srcdir}/reproducible-import-libraries.patch"
  patch -p2 -i "${srcdir}/specify-timestamp.patch"

  # Handle Windows nul device
  # https://github.com/msys2/MINGW-packages/issues/1840
  # https://github.com/msys2/MINGW-packages/issues/10520
  # https://github.com/msys2/MINGW-packages/issues/14725

  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108276
  # https://gcc.gnu.org/pipermail/gcc-patches/2023-January/609487.html
  patch -p1 -i "${srcdir}/libiberty-unlink-handle-windows-nul.patch"

  apply_patch_with_msg \
    6aadf8a04d162feb2afe3c41f5b36534d661d447.patch \
    398f1ddf5e89e066aeee242ea854dcbaa8eb9539.patch \
    26d0081b52dc482c59abba23ca495304e698ce4b.patch \
    8606b47e94078e77a53f3cd714272c853d2add22.patch \
    54d57acf610e5db2e70afa234fd4018207606774.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
  fi

  declare -a extra_config=""
  if [ "${CARCH}" = "x86_64" ]; then
    extra_config+=('--enable-64-bit-bfd')
  else
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-werror \
    --enable-lto \
    --with-sysroot=${MINGW_PREFIX} \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    ${extra_config[@]} \
    --enable-nls \
    --disable-rpath \
    --disable-multilib \
    --enable-install-libiberty \
    --enable-plugins \
    --disable-shared \
    --enable-deterministic-archives

  make
}

check() {
  cd ${srcdir}/build-${MSYSTEM}

  # unset LDFLAGS as testsuite makes assumptions about which ones are active
  # do not abort on errors - manually check log files
  make LDFLAGS="" -k check || true
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  make DESTDIR=${pkgdir} install

  # https://github.com/msys2/MINGW-packages/issues/7890
  rm "${pkgdir}${MINGW_PREFIX}/lib/bfd-plugins/libdep.a"

  find ${pkgdir}${MINGW_PREFIX}/share -type f -iname "opcodes.mo" -o -iname "bfd.mo" | xargs -rtl1 rm

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING      ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING.LIB  ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING3     ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING3
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING3.LIB ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING3.LIB
}
