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

_realname=openxr-sdk
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=1.1.50
pkgrel=1
pkgdesc='OpenXR is a royalty-free, open standard that provides high-performance access to Augmented Reality (AR) and Virtual Reality (VR) (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.khronos.org/openxr/'
msys2_repository_url="https://github.com/KhronosGroup/OpenXR-SDK-Source"
msys2_references=(
  'archlinux: openxr'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-shaderc")
source=("https://github.com/KhronosGroup/OpenXR-SDK-Source/releases/download/release-${pkgver}/OpenXR-SDK-Source-release-${pkgver}.tar.gz"{,.asc}
        001-use-def-file-on-mingw-w64.patch
        002-libname-def-file.patch
        003-disable-version-script.patch
        004-fix-cmake-install-dir.patch)
sha256sums=('999bd93316f0bf935425439f1ca8c137ebf177f21d09e0c241426bbfb6848f78'
            'SKIP'
            'eba681cdb04e735166d6d651e58cd34907304ac3399534fb9d45f8bd94205cde'
            'a3e8aacb197336ac707f4871757ad56758a1753d2ca630678ed24b2df7d33bbf'
            'ac2f0940e3151706a52a033afec12ef5371d09604bf0057d6dc5132d3c38a5d1'
            '67bd694785dfee54e52e9139b9597b5d0ddbe218ad80c63c4b6fb8817d2d507f')
validpgpkeys=(45207B2B1E53E1F2755FF63CC5A2D593A61DBC9D) # Ryan Andrew Pavlik <ryan.pavlik@collabora.com>

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/OpenXR-SDK-Source-release-${pkgver}

  apply_patch_with_msg \
    001-use-def-file-on-mingw-w64.patch \
    002-libname-def-file.patch \
    003-disable-version-script.patch \
    004-fix-cmake-install-dir.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

  export VULKAN_SDK=${MINGW_PREFIX}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DDYNAMIC_LOADER=ON \
    -DBUILD_WITH_SYSTEM_JSONCPP=ON \
    -DBUILD_TESTS=OFF \
    ../OpenXR-SDK-Source-release-${pkgver}

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

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/OpenXR-SDK-Source-release-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
