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

_realname=openxr-sdk
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=1.0.12
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=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.khronos.org/openxr/'
license=('Apache 2.0')
depends=("${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
options=('strip' '!debug' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/KhronosGroup/OpenXR-SDK/archive/release-${pkgver}.tar.gz
        001-generate-pkgconfig-files.patch
        002-libname-def-file.patch
        003-disable-version-script.patch
        004-fix-cmake-install-dir.patch
        libopenxr_loader_32.def)
sha256sums=('66ec1d325b820fff4dac62183c271c05de16835b97b1ad2416a01105f94407da'
            '8af59e72237185982c3234ceebf98f61b57bc4ac39f51ec619c04b2120bceb51'
            'f564371aed6dc7bfc2aaad3745791fa00ccb8f7a346da2aca82e3ee780fd7a9c'
            'd2974e5e3e69be52cf83e57aa19ab32114f0b9fd3a1d9dedf78235c5ce3bfeda'
            'b8037431a695b747179bee352d5ac0c662d26cc29f3fa160503b1d66925ce092'
            'c268c94efe7ec8cfb83eccc506a8035869dd42eebfb5037537ecf642b08f25cc')

prepare() {
  cd ${srcdir}/OpenXR-SDK-release-${pkgver}
  patch -p1 -i ${srcdir}/001-generate-pkgconfig-files.patch
  patch -p1 -i ${srcdir}/002-libname-def-file.patch
  patch -p1 -i ${srcdir}/003-disable-version-script.patch
  patch -p1 -i ${srcdir}/004-fix-cmake-install-dir.patch

  # For stdcall fixup, generated from upstream released DLLs
  if [ "${CARCH}" = "i686" ]; then
    cp ${srcdir}/libopenxr_loader_32.def src/loader/openxr-loader.def
  fi
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  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.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DDYNAMIC_LOADER=ON \
    -DBUILD_WITH_SYSTEM_JSONCPP=ON \
    ../OpenXR-SDK-release-${pkgver}

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

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

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