# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=binutils
pkgver=2.36.1
pkgrel=4
pkgdesc="A set of programs to assemble and manipulate binary and object files"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
groups=('msys2-devel')
depends=('libiconv' 'libintl' 'zlib')
checkdepends=('dejagnu' 'bc')
makedepends=('libiconv-devel' 'gettext-devel' 'zlib-devel')
options=('staticlibs' '!distcc' '!ccache')
source=(https://ftp.gnu.org/gnu/binutils/binutils-${pkgver}.tar.xz{,.sig}
        0050-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
        0100-binutils-2.36-msys2.patch
        2000-Temporarily-revert-default-dll-characteristics.patch
        2002-Allow-spaces-in-the-name-of-the-external-preprocesso.patch
        3000-more-base-entropy.patch)
sha256sums=('e81d9edf373f193af428a0f256674aea62a9d74dfe93f65192d4eae030b0f3b0'
            'SKIP'
            '604e76e0f702ced493ee22aa3c1768b4776b2008a7d70ae0dd35fe5be3522141'
            'fd67c34cbe827bd1a720de3e5ee2029c5788a02fe76f63221b6d20507be90a85'
            '29c1aab1eb96bc188c1ad29422f611e7c946cca901aed29ee2d0b9e861c75a23'
            '57478b9971183d430c93701b1d533e3724dab5334bbf44db924777e4a93c1063'
            'ce3cda1ad8ff8379869142226178b5f7fdb5b93f85a43d3437475ea0314dbf1b')
validpgpkeys=('EAF1C276A747E9ED86210CBAC3126D3B4AE55E93'
              '3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F')

prepare() {
  cd "${srcdir}"/binutils-${pkgver}
  patch -p1 -i "${srcdir}"/0050-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
  patch -p1 -i "${srcdir}"/0100-binutils-2.36-msys2.patch

  # Until we get a response here: https://cygwin.com/pipermail/cygwin/2021-February/247922.html
  patch -p1 -i "${srcdir}"/2000-Temporarily-revert-default-dll-characteristics.patch

  # https://github.com/msys2/MSYS2-packages/issues/2379
  patch -R -p1 -i "${srcdir}"/2002-Allow-spaces-in-the-name-of-the-external-preprocesso.patch

  # https://github.com/msys2/MSYS2-packages/issues/2487
  patch -p1 -i "${srcdir}"/3000-more-base-entropy.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

  mkdir "${srcdir}"/binutils-${CARCH}
}

build() {
  cd "${srcdir}"/binutils-${CARCH}
  ../binutils-${pkgver}/configure \
    --prefix=/usr \
    --{build,host,target}=${CHOST} \
    --disable-werror \
    --disable-sim \
    --enable-64-bit-bfd \
    --enable-install-libiberty \
    --without-libiconv-prefix \
    --without-libintl-prefix

  make
}

check() {
  # cd "${srcdir}"/binutils-${CARCH}

  # 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
  return 0
}

package() {
  cd "${srcdir}"/binutils-${CARCH}
  make DESTDIR=${pkgdir} install
  cp libiberty/libiberty.a ${pkgdir}/usr/lib

  # Remove unwanted files
  #rm ${pkgdir}/usr/share/info/{configure,standards}.info

  # Add some useful headers
  #install -m644 "${srcdir}"/binutils-${pkgver}/include/libiberty.h ${pkgdir}/usr/include
  #install -m644 "${srcdir}"/binutils-${pkgver}/include/demangle.h ${pkgdir}/usr/include
}
