# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=libffi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3
pkgrel=4
pkgdesc="A portable, high level programming interface to various calling conventions (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
license=('MIT')
url="https://sourceware.org/libffi"
source=("https://sourceware.org/pub/libffi/${_realname}-${pkgver}.tar.gz"
        0001-Fix-building-for-aarch64-windows-with-mingw-toolchai.patch
        0002-Use-__builtin_ffs-instead-of-ffs-554.patch
        0003-win64_armasm-Fix-the-spelling-of-ALIGN-553.patch
        0004-arm-Fix-the-clang-specific-version-of-the-assembly-5.patch)
sha256sums=('72fba7922703ddfa7a028d513ac15a85c8d54c8d67f55fa5a4802885dc652056'
            '1417e8336a3953a0e542abb12723bf27187fb46eb1f2ba76af4e8746689e2e7f'
            '94f523f536784ced069d6735df4b8cbf4278f826bb48a0f0d91acd27f12cbab4'
            'cf7e9433538975965071a6b0a9127995d85fe5fcd1d8b1439a71466d9c43ef78'
            'f93dd4624668f87ffeb063b57a8f32dd9f41564620d4e0662c832ed775a8e8a6')

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

  # Upstream patches for aarch64 support, delete in next version
  patch -Np1 -i "${srcdir}/0001-Fix-building-for-aarch64-windows-with-mingw-toolchai.patch"
  patch -Np1 -i "${srcdir}/0002-Use-__builtin_ffs-instead-of-ffs-554.patch"
  patch -Np1 -i "${srcdir}/0003-win64_armasm-Fix-the-spelling-of-ALIGN-553.patch"
  patch -Np1 -i "${srcdir}/0004-arm-Fix-the-clang-specific-version-of-the-assembly-5.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --disable-symvers
  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
