# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: Biswapriyo Nath <nathbappai@gmail.com>

_realname=spirv-tools
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2021.1
pkgrel=1
pkgdesc="API and commands for processing SPIR-V modules (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.khronos.org/vulkan/"
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/KhronosGroup/SPIRV-Tools/archive/v${pkgver}.tar.gz"
        001-cmake-files-location.patch)
sha512sums=('e8478eacb86415f75a1e5b3f66a0508b01a9f7e9d8b070eb0329ca56be137f5543dd42125a1033cb8552c01f46e11affd7fda866231b3742c66de9b4341930d5'
            '8cbad8289e5f29c621cf225756e69a7784eba005998a041ae3d81cb017582956ec2d07d163ea7a3de38f3f7bd34900509511a8104d2a2d196da23d87c8e35a58')

prepare() {
  cd SPIRV-Tools-${pkgver}
  patch -p1 -i ${srcdir}/001-cmake-files-location.patch
}

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

  echo "Building static library"
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DSPIRV_WERROR=OFF \
      -DBUILD_SHARED_LIBS=OFF \
      -DSPIRV_TOOLS_BUILD_STATIC=ON \
      -DSPIRV-Headers_SOURCE_DIR=${MINGW_PREFIX} \
      ../SPIRV-Tools-${pkgver}

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

  echo "Building shared library"
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir -p "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DSPIRV_WERROR=OFF \
      -DBUILD_SHARED_LIBS=ON \
      -DSPIRV_TOOLS_BUILD_STATIC=OFF \
      -DSPIRV-Headers_SOURCE_DIR=${MINGW_PREFIX} \
      ../SPIRV-Tools-${pkgver}

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

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

  cd "${srcdir}"/build-${CARCH}-shared
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -dm755 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -m644 ${srcdir}/SPIRV-Tools-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"

  local _WIN_PREFIX=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -e "s|${_WIN_PREFIX}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
