# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=anari-sdk
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.13.1
pkgrel=1
pkgdesc="ANARI Software Development Kit (SDK) (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/KhronosGroup/ANARI-SDK'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/KhronosGroup/ANARI-SDK/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('b8979ab0dea22cf71c2eacf9421b0cf3fe5807224147c63686d6ed07e65873f4')

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_HELIDE_DEVICE=OFF \
      -DBUILD_SCENES_LIB=OFF \
      -DBUILD_EXAMPLES=OFF \
      -DBUILD_TESTING=OFF \
      -DINSTALL_VIEWER_LIBRARY=OFF \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -S "ANARI-SDK-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake -DBUILD_EXAMPLES=ON -DBUILD_TESTING=ON -S "ANARI-SDK-${pkgver}" -B "build-${MSYSTEM}"
  cmake --build "build-${MSYSTEM}"
  ctest --test-dir "build-${MSYSTEM}" --output-on-failure || true
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-p" \
    python -m compileall -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}"${MINGW_PREFIX}

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