# Maintainer: Lukas Jirkovsky <l.jirkovsky@gmail.com>
# Maintainer: Bruno Pagani <archange@archlinux.org>
# Maintainer: Torsten Keßler <tpkessler@archlinux.org>

pkgname=ispc
_commit=bcb2cf896c00f9a802a11cbf291ef6e44b205416 # refs/tags/v1.23.0
pkgver=1.23.0
pkgrel=1
pkgdesc="Compiler for high-performance SIMD programming on the CPU"
arch=(x86_64 aarch64)
url="https://ispc.github.io/"
license=(BSD-3-Clause)
depends=(clang gcc-libs glibc llvm-libs spirv-llvm-translator onetbb)
makedepends=(cmake git level-zero-headers llvm python vc-intrinsics)
makedepends_x86_64=(level-zero-loader openmp lib32-glibc)
checkdepends_x86_64=(intel-compute-runtime)
optdepends_x86_64=(
    'intel-compute-runtime: GPU support'
    'level-zero-loader: GPU support'
    'openmp: GPU support'
)
# NOTE: libispcrt_static.a is required by ospray during build time
options=(staticlibs)
source=(
  git+https://github.com/$pkgname/$pkgname#tag=$_commit
  $pkgname-benchmark::git+https://github.com/google/benchmark.git
  $pkgname-googletest::git+https://github.com/google/googletest.git
  pic.patch::https://github.com/$pkgname/$pkgname/commit/c2d80a6a4551f74c0f06ec444b8f054ef6cac20e.patch
)
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            '14eec56bc1abfdb2ae0102dae1d563f4fe87d61dc22c771b6565f0798746e810')

pkgver() {
  cd $pkgname
  git describe --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd $pkgname
  git submodule init
  git config submodule.benchmarks/vendor/google/benchmark.url "$srcdir/$pkgname-benchmark"
  git config submodule.ispcrt/tests/vendor/google/googletest.url "$srcdir/$pkgname-googletest"
  git -c protocol.file.allow=always submodule update
}

prepare() {
  cd $pkgname
  patch -Np1 < ../pic.patch
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=Release  # None not allowed :(
    -D CMAKE_INSTALL_PREFIX=/usr
    -D ISPC_INCLUDE_EXAMPLES=OFF
    -D ISPC_OPAQUE_PTR_MODE=OFF  # suggested in https://github.com/ispc/ispc/issues/2427
    -D ISPC_STATIC_LINK=OFF
    -W no-dev
    -S $pkgname
  )
  [ $CARCH = x86_64 ] && cmake_options+=(
    -D XE_ENABLED=ON
    -D XE_DEPS_DIR=/usr
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  # https://github.com/ispc/ispc/issues/2427
  # https://github.com/ispc/ispc/issues/2428
  # https://github.com/ispc/ispc/issues/2429
  make -C build check-all || echo "Tests failed"
}

package() {
  DESTDIR="$pkgdir" cmake --install build
  install -Dm644 $pkgname/LICENSE.txt -t "${pkgdir}"/usr/share/licenses/${pkgname}
  # Remove uneeded files: https://github.com/ispc/ispc/issues/2482
  if [ $CARCH = x86_64 ]; then
      rm -rv "${pkgdir}"/build
  fi
}
