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

_realname=ispc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.29.0
pkgrel=1
pkgdesc="Compiler for high-performance SIMD programming on the CPU (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://ispc.github.io/"
msys2_repository_url="https://github.com/ispc/ispc"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-clang")
         #"${MINGW_PACKAGE_PREFIX}-spirv-llvm-translator"
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-python")
             #"${MINGW_PACKAGE_PREFIX}-level-zero-headers"
             #"${MINGW_PACKAGE_PREFIX}-level-zero-loader"
source=("https://github.com/ispc/ispc/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-mingw-build.patch
        003-mingw-build-examples.patch)
sha256sums=('416774690e27d67f76e8a224fbb6641ddcfb7733a1a220e4cef4edca90440ae4'
            '4e5425f2066a95303467dd12dbe8515f2caa4d3d44f58e72bf54af0020524511'
            '7c718f30d96e307d050cb88e8ae75fc13e1e2bf4caadfcfc45f8f15a39a86129')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-mingw-build.patch
  patch -p1 -i ${srcdir}/003-mingw-build-examples.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DISPC_STATIC_LINK=OFF \
    -DISPC_INCLUDE_EXAMPLES=OFF \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ${extra_config[@]} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

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

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
