# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=llvmlite
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.36.0
pkgrel=1
pkgdesc='Lightweight LLVM python binding for writing JIT compilers (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/numba/llvmlite"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-polly>=10.0.1")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('!emptydirs' 'staticlibs' 'strip')
source=(llvmlite-$pkgver.tar.gz::"https://github.com/numba/llvmlite/archive/v$pkgver.tar.gz"
       "multi-defs.patch"
       "path_fix.patch"
       "llvm-11.0.patch")
sha256sums=('084dcde3831def454b938e4c28eaaa65162e9f7b28110bdcb1727010f6b8f532'
            'b4610934ac8fd7e614d9ea920856ff6da2fbeb146028a664ada8543b8b33ec56'
            '813ecc48f18543f0d36b03c7596a1a6a26be31b9cfa44f1111ac232821844a79'
            '5313db3353a18b08fdaa2032c244a8848a1945e4c88f26fcc37ce1f30b96f173')

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

  patch -p1 -i "${srcdir}/multi-defs.patch"
  patch -p1 -i "${srcdir}/path_fix.patch"
  # https://github.com/msys2/MINGW-packages/discussions/8386
  patch -p1 -i "${srcdir}/llvm-11.0.patch"
  sed -i "s,_MSC_VER,_WIN32," ffi/core.h
}

build() {
  cd "$srcdir"
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"

  msg "Python build for ${CARCH}"
  export CMAKE_GENERATOR="MSYS Makefiles"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  export CMAKE_GENERATOR="MSYS Makefiles"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -Dm644 ffi/build/libllvmlite.dll "${pkgdir}${MINGW_PREFIX}/bin/llvmlite.dll"
  install -Dm644 ffi/build/libllvmlite.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libllvmlite.dll.a"
}
