# Maintainer: Felix Xing (Felixaszx) <felixaszx@outlook.com>

_realname=ktx-software
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.4.0
pkgrel=1
pkgdesc="KTX (Khronos Texture) Library and Tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.khronos.org/ktx/"
msys2_repository_url="https://github.com/KhronosGroup/KTX-Software/"
msys2_references=()
license=("spdx:Apache-2.0")
depends=(
  "${MINGW_PACKAGE_PREFIX}-opencl-icd"
)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-opencl-headers")
source=("https://github.com/KhronosGroup/KTX-Software/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('3585d76edcdcbe3a671479686f8c81c1c10339f419e4b02a9a6f19cc6e4e0612')

prepare() {
  cd "${_realname}-${pkgver}"

  # make sure we don't use prebuilt stuff
  rm -rf "external/basisu/OpenCL/lib"
}

build() {
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_TESTING=OFF \
      -DASTCENC_ISA_NATIVE=ON \
      -DBASISU_SUPPORT_OPENCL=ON \
      -DKTX_FEATURE_TESTS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  cd "${srcdir}"

  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.md" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  cp -r "${srcdir}/${_realname}-${pkgver}/LICENSES" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
