# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=spirv-cross
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2021.01.15
pkgrel=1
pkgdesc="A tool and library for parsing and converting SPIR-V to other shader languages (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/KhronosGroup/SPIRV-Cross/'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-nose"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-tools")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/SPIRV-Cross/archive/${pkgver//./-}.tar.gz")
sha512sums=('f934ef61602223f6fe6d9c826ed5beb129beb7a30b18b389625d4fc0b1efa1b8df930a2a2d2a0b4f377ef2899e8e034239819a4c6629a78c666f72004464da93')

prepare() {
  cd "${srcdir}/SPIRV-Cross-${pkgver//./-}"

  # Use system provided glslang and spirv-tools binaries.
  sed -e "s|NO_DEFAULT_PATH||g" -i CMakeLists.txt
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DSPIRV_CROSS_SHARED=ON \
    -DSPIRV_CROSS_ENABLE_TESTS=ON \
    "../SPIRV-Cross-${pkgver//./-}"

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

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

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

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