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

_srcname=SPIRV-LLVM-Translator
_realname=spirv-llvm-translator
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=21.1.0
pkgrel=1
pkgdesc='Tool and a library for bi-directional translation between SPIR-V and LLVM IR (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/KhronosGroup/SPIRV-LLVM-Translator'
msys2_references=(
  'archlinux: spirv-llvm-translator'
)
license=(spdx:NCSA)
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-spirv-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers")
source=("https://github.com/KhronosGroup/SPIRV-LLVM-Translator/archive/v${pkgver}/${_srcname}-${pkgver}.tar.gz")
sha256sums=('4f7019a06c731daebbc18080db338964002493ead4cfb440fef95d120c50a170')

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=${MINGW_PREFIX}/include/spirv \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DLLVM_SPIRV_INCLUDE_TESTS=OFF \
      -B build-${MSYSTEM}-static \
      -S ${_srcname}-${pkgver}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=${MINGW_PREFIX}/include/spirv \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DLLVM_SPIRV_INCLUDE_TESTS=OFF \
      -B build-${MSYSTEM}-shared \
      -S ${_srcname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build build-${MSYSTEM}-shared
  ${MINGW_PREFIX}/bin/cmake.exe --build build-${MSYSTEM}-shared --target llvm-spirv
}

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

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-shared
  install -Dm755 build-${MSYSTEM}-shared/tools/llvm-spirv/llvm-spirv.exe "${pkgdir}"${MINGW_PREFIX}/bin

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