# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=binutils
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=2.35.1
pkgrel=1
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=("${_mingw_suff}-toolchain" "${_mingw_suff}")
depends=("libiconv" "zlib")
checkdepends=('dejagnu' 'bc')
makedepends=("libiconv" "zlib")
options=('staticlibs' '!distcc' '!ccache' '!buildflags')
source=(https://ftp.gnu.org/gnu/binutils/binutils-${pkgver}.tar.xz{,.sig}
        0001-enable-gold-on.mingw32.patch
        0002-check-for-unusual-file-harder.patch
        0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
        0020-binutils_2.31_mkdtemp_impl.patch
        0100-binutils-2.30-msys2.patch
        0110-binutils-mingw-gnu-print.patch
        0120-windres-handle-spaces.patch)
sha256sums=('3ced91db9bf01182b7e420eab68039f2083aed0a214c0424e257eae3ddee8607'
            'SKIP'
            '93296b909e1a4f9d8a4bbe2437aafa17ca565ef6642a9812b0360c05be228c9d'
            '2c99345fc575c3a060d6677537f636c6c4154fac0fde508070f3b6296c1060d4'
            '604e76e0f702ced493ee22aa3c1768b4776b2008a7d70ae0dd35fe5be3522141'
            '34ba6c001ff7f95ae1da0619c73130112b76d0d2a31bb8a00602eb22f1f84cb8'
            '21a5f835d8e9c1d7daba1ffb8369fce8e38be05949997c4039b5cb10d8589082'
            '76658ef1bb8c5fc3fe6c26e2b5dd9ee0f1d12661988c0c65562b0a3e2d32ae1f'
            '86ae90d997e986a54aaebb5251f3a71800b0c5c3f5b57b9094a42995e9f5c478')
validpgpkeys=('EAF1C276A747E9ED86210CBAC3126D3B4AE55E93'
              '3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F')

_targets="i686-w64-mingw32 x86_64-w64-mingw32" # armv7-w64-mingw32

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

  patch -p1 -i "${srcdir}"/0001-enable-gold-on.mingw32.patch
  patch -p1 -i "${srcdir}"/0002-check-for-unusual-file-harder.patch
  patch -p1 -i "${srcdir}"/0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
  patch -p1 -i "${srcdir}"/0020-binutils_2.31_mkdtemp_impl.patch

  patch -p1 -i "${srcdir}"/0100-binutils-2.30-msys2.patch
  patch -p1 -i "${srcdir}"/0110-binutils-mingw-gnu-print.patch
  patch -p1 -i "${srcdir}"/0120-windres-handle-spaces.patch

  #do not install libiberty
  #sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

}

build() {
  for _target in ${_targets}; do
    mkdir -p ${srcdir}/binutils-build-${_target} && cd ${srcdir}/binutils-build-${_target}

    if [ "${_target}" != "armv7-w64-mingw32" ]; then
      local _conf='--enable-lto'
    else
      local _conf=''
    fi

    if [ "${_target}" = "x86_64-w64-mingw32" ]; then
     _conf+=' --enable-64-bit-bfd'
    fi

    ${srcdir}/binutils-${pkgver}/configure \
      --build=${CHOST} \
      --host=${CHOST} \
      --target=${_target} \
      --prefix=/opt \
      --disable-werror \
      --with-libiconv-prefix=/usr \
      $_conf

    make
  done
}

check() {
  for _target in ${_targets}; do
    cd ${srcdir}/binutils-build-${_target}

    # 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
  done
}

package() {
  for _target in ${_targets}; do
    cd ${srcdir}/binutils-build-${_target}
    make DESTDIR=${pkgdir} install

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

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