# 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.45
pkgrel=1
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/"
msys2_references=(
  "cpe: cpe:/a:gnu: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}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${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
        libiberty-unlink-handle-windows-nul.patch
        3001-hack-libiberty-link-order.patch
        4000-fix-DEBUG_S_INLINEELINES.patch
)
sha256sums=('1393f90db70c2ebd785fb434d6127f8888c559d5eeb9c006c354b203bab3473e'
            'SKIP'
            '2c99345fc575c3a060d6677537f636c6c4154fac0fde508070f3b6296c1060d4'
            '4e8ac055df61b1b5d6ae29dc87e1154737c2e87c7b244b44866702cabf1a5d18'
            '79eab27a3b0aeaddcdac49c93b5e3ee1bfac7e9782fc4d17d64061a52362e76a'
            '9945635f4a67712616202f09cbb66cf70df01be168c2c8054c455bb58bf334dd'
            'd584f1cd9e94cba0e9b27625c4acc8ad5242cd625c9b44839d42fc116072568c'
            'a094660ec95996c00b598429843b7869037732146442af567ada9f539bd40480'
            '7ccbd418695733c50966068fa9755a6abb156f53af23701d2bc097c63e9e0030'
            '604628156c08f3e361de60329af250fab6839e23e61e289f8369a7e18a04e277'
            '5ed35a728b956ad49a5e196ae59f681666f2754f0a4903b9280a430d7a312ea8')
validpgpkeys=('EAF1C276A747E9ED86210CBAC3126D3B4AE55E93'  # Tristan Gingold <gingold@adacore.com>
              '3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F') # Nick Clifton <nickc@redhat.com>

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
  # confirmed with 2.42 that it's still required
  patch -p2 -i "${srcdir}/reproducible-import-libraries.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"

  # XXX: make sure we link against the just built libiberty, not the system one
  # to avoid a linker error. All the ld deps contain system deps and system
  # search paths, so imho if things link against the system lib or the just
  # built one is just luck, and I don't know how that is supposed to work.
  patch -p1 -i "${srcdir}/3001-hack-libiberty-link-order.patch"

  # https://github.com/msys2/MINGW-packages/issues/24148
  # https://sourceware.org/bugzilla/show_bug.cgi?id=32942
  patch -p1 -i "${srcdir}/4000-fix-DEBUG_S_INLINEELINES.patch"
}

build() {
  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 \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-multilib \
    --disable-rpath \
    --disable-shared \
    --disable-werror \
    --enable-deterministic-archives \
    --enable-install-libiberty \
    --enable-lto \
    --enable-nls \
    --enable-plugins \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-sysroot=${MINGW_PREFIX} \
    --with-system-zlib \
    "${extra_config[@]}"

  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

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING"* -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

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

  # Remove files shipped with gdb
  find "${pkgdir}"${MINGW_PREFIX}/share -type f -iname "opcodes.mo" -o -iname "bfd.mo" | xargs -rtl1 rm
}
