# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=binutils
_mingw_suff=midipix-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=2.24.51
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" "make" "patch" "diffutils" "texinfo")
options=('staticlibs' '!distcc' '!ccache' '!buildflags')
source=("https://mirrors.kernel.org/sources.redhat.com/binutils/snapshots/binutils-${pkgver}.tar.bz2"
        'binutils-2.24.51.midipix.patch'
        'binutils-2.24.51.msys2-post.midipix.patch')
sha256sums=('250d3b2925c6b211fb16173b0b25bc091c58829fbcad3eb849645e0af52cf7fa'
            'ac18ffe13f50d18fe640be1517fe804039c0ccb6fd94d5ceac3bd6375a49e14c'
            'b168607e0a57d887d0ba8558d2c24fe1c7a6e1747e3847d7c42bd88c787c78fe')

#not supported yet
#i686-nt32-midipix
_targets="x86_64-nt64-midipix"

prepare() {
  cd ${srcdir}/binutils-${pkgver}
  patch -p1 -i ${srcdir}/binutils-2.24.51.midipix.patch
  #patch -p1 -i ${srcdir}/binutils-2.24.51.msys2.patch
  patch -p1 -i ${srcdir}/binutils-2.24.51.msys2-post.midipix.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

  mkdir ${srcdir}/binutils-build
}

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

  if [ "${_target}" = "x86_64-w64-mingw32" ]; then
    local _conf='--enable-64-bit-bfd'
  else
    local _conf=''
  fi
  ${srcdir}/binutils-${pkgver}/configure \
  --build=${CHOST} \
  --host=${CHOST} \
  --target=${_target} \
  --with-sysroot=/opt/${_target} \
  --prefix=/opt \
  --disable-werror \
  --with-libiconv-prefix=/usr \
  $_conf

  #not supported yet
  #--enable-lto \

  make
  done
}

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

  # install clashes with mingw-cross without this
  rm -rf ${pkgdir}/opt/share
  done
}
