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

_realname=openimagedenoise
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.3
pkgrel=3
pkgdesc="Intel Open Image Denoise library of high-performance, high-quality denoising filters for images rendered with ray tracing (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://openimagedenoise.github.io/"
msys2_repository_url="https://github.com/OpenImageDenoise/oidn"
license=("spdx:Apache-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-ispc")
source=(https://github.com/OpenImageDenoise/oidn/releases/download/v${pkgver}/oidn-${pkgver}.src.tar.gz
        001-fix-build-on-aarch64.patch
        004-find-tbb.patch)
sha256sums=('ccf221535b4007607fb53d3ff5afa74de25413bb8ef5d03d215f46c7cc2f96cf'
            '865b649315737771d80879b71655dfcafeb1aa72d89e07b4e70927f551d713af'
            '411506b1081e2b061e025b284815aeeb3efd1fa07fc3e7bb1a0763f89bfc65db')

prepare() {
  cd "${srcdir}"/oidn-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-build-on-aarch64.patch
  patch -p1 -i ${srcdir}/004-find-tbb.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 build-${MSYSTEM} && cd build-${MSYSTEM}

  LDFLAGS+=" -lntdllcrt" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ${extra_config} \
    ../oidn-${pkgver}

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

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

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

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