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

_realname=cffi
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.0.0
pkgrel=1
pkgdesc="Foreign Function Interface for Python calling C code (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://cffi.readthedocs.io/'
msys2_references=(
  'purl: pkg:pypi/cffi'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-python-pycparser")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://pypi.org/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "mingw-non-x86.patch"
        "0001-missing-exchange8.patch")
sha256sums=('44d1b5909021139fe36001ae048dbdde8214afa20200eda0f64c068cac5d5529'
            'c8a8ea2d65932ea0b62eae56dbc8afd6baacaadc7cb284ca0e4edf7ed1460c52'
            'fdb25cf730913cf6270e4cdd80faf62e685aea02ecbf84059f7d91861e1a3a95')

prepare() {
  cd "${srcdir}"/"${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}"/mingw-non-x86.patch

  # https://github.com/mingw-w64/mingw-w64/issues/118
  # https://github.com/python-cffi/cffi/pull/198
  patch -Np1 -i "${srcdir}"/0001-missing-exchange8.patch
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}"/python-build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -D -m644 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
}
