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

_realname=openpgl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.0
pkgrel=1
pkgdesc="Intel(R) Open Path Guiding Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://www.openpgl.org/'
msys2_repository_url="https://github.com/RenderKit/openpgl"
msys2_references=(
  'archlinux: openpgl'
)
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}-ninja")
source=("https://github.com/RenderKit/openpgl/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-add-compile-flags.patch
        002-Fix-finding-TBB.patch
        003-Fix-building-on-mingw-w64-aarch64.patch)
sha256sums=('58be6ac86e3bcf8a787e9c1332d1527e6d18f4b1403b96bb02c909e20af2ca94'
            'eab367b079ca1489c08b46bef6f441d52630dece7c3bf8ac5b703ba2bc30b0a0'
            '6703e4dd3f06dfd2d9f28a9d7cb727c9476d231f662fd676883d42a61873aa0f'
            'cca09ab79db692f91789cd643b8e3787abd53fe567da86afcf526dfa25108032')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-add-compile-flags.patch
  patch -p1 -i "${srcdir}"/002-Fix-finding-TBB.patch
  patch -p1 -i "${srcdir}"/003-Fix-building-on-mingw-w64-aarch64.patch
}

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

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

  if [[ ${CARCH} == aarch64 ]]; then
    _extra_config+=("-DOPENPGL_ISA_NEON2X=OFF")
  fi

  CXXFLAGS+=" -D_USE_MATH_DEFINES -w" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      ../${_realname}-${pkgver}

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

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

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

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