# $Id: PKGBUILD 182460 2016-07-07 13:03:56Z spupykin $
# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Maintainer: rubenvb vanboxem <dottie> ruben <attie> gmail <dottie> com

# Same as official package but uses symlinks instead of hard links allowing
# installation when /usr/i686-w64-mingw32 and /usr/x86_64-w64-mingw32 are
# bound from another partition.

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

pkgname=mingw-w64-binutils-symlinks
pkgver=2.32
pkgrel=1
pkgdesc="Cross binutils for the MinGW-w64 cross-compiler (uses symlinks instead of hard links)"
arch=('i686' 'x86_64')
url="http://www.gnu.org/software/binutils"
license=('GPL')
groups=('mingw-w64-toolchain' 'mingw-w64')
depends=('zlib')
provides=('mingw-w64-binutils')
conflicts=('mingw-w64-binutils')
options=('!libtool' '!emptydirs')
validpgpkeys=('3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F')
source=("https://ftp.gnu.org/gnu/binutils/binutils-${pkgver}.tar.gz"{,.sig})
sha256sums=('9b0d97b3d30df184d302bced12f976aa1e5fbf4b0be696cdebc6cca30411a46e'
            'SKIP')

prepare() {
  cd ${srcdir}/binutils-${pkgver}
  #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
    msg "Building ${_target} cross binutils"
    mkdir -p ${srcdir}/binutils-${_target} && cd "${srcdir}/binutils-${_target}"
    $srcdir/binutils-${pkgver}/configure --prefix=/usr \
        --target=${_target} \
        --infodir=/usr/share/info/${_target} \
        --enable-lto --enable-plugins \
        --enable-deterministic-archives \
        --disable-multilib --disable-nls \
        --disable-werror
    make
  done
}

package() {
  for _target in ${_targets}; do
    msg "Installing ${_target} cross binutils"
    cd ${srcdir}/binutils-${_target}
    make DESTDIR=${pkgdir} install
  done
  cd "${pkgdir}/usr/bin"
  for file in *; do
    if [[ ! -L $file ]] && [[ ! -d $file ]]; then
      local _arch="${file%*-w64-mingw32*}"
      local _name="${file#*-w64-mingw32-}"
      local _target="/usr/${_arch}-w64-mingw32/bin/${_name}"
      if [[ -f ${pkgdir}/${_target} ]]; then
        ln -sf "${_target}" "${file}"
      fi
    fi
  done
}
