# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=Vulkan-Loader
pkgbase=mingw-w64-vulkan-loader
pkgname=("${MINGW_PACKAGE_PREFIX}-vulkan-loader")
provides=("${MINGW_PACKAGE_PREFIX}-vulkan")
replaces=("${MINGW_PACKAGE_PREFIX}-vulkan")
pkgver=1.2.179
pkgrel=2
pkgdesc='Vulkan Installable Client Driver (ICD) Loader (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.khronos.org/vulkan/"
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-vulkan-headers")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/${_realname}/archive/v${pkgver}.tar.gz"
        002-proper-def-files-for-32bit.patch
        003-generate-pkgconfig-files.patch
        004-disable-suffix-in-static-lib.patch
        libvulkan-32.def)
sha256sums=('bc035c3735cc5147b98aed4856446ebe7e2512394b8c86193891bb08baf15f66'
            'e02c140457301960c81d7941e01675d5676ca29e5894525c10fb17097d1ced69'
            '97c5ff3d945d2ed5d5a8b9cc8b6199ff55d4e6b210e59143228c4e82d0252cb4'
            'ee0def2718356a2ed441a786e99a0136b9c425274f660cd4447ed0aff4b0de6a'
            '70a65c93c9bd4fece5ea50cdbf77dad615996d9264813c840b19b1c14186b73b')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/002-proper-def-files-for-32bit.patch
  patch -p1 -i ${srcdir}/003-generate-pkgconfig-files.patch
  patch -p1 -i ${srcdir}/004-disable-suffix-in-static-lib.patch

  # Fix stdcall export symbols for 32bit
  if [ "${CARCH}" = "i686" ]; then
    cp -v ${srcdir}/libvulkan-32.def loader/vulkan-1.def
  fi
}

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

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -D__STDC_FORMAT_MACROS -Wno-unused-function" \
  CPPFLAGS+=" -D__STDC_FORMAT_MACROS" \
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO -D__STDC_FORMAT_MACROS -fpermissive -Wno-unused-function" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUSE_MASM=OFF \
    -DBUILD_TESTS=OFF \
    ../${_realname}-${pkgver}

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

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

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.TXT ${pkgdir}${MINGW_PREFIX}/share/licenses/vulkan-loader/LICENSE
}
