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

_realname=Vulkan-ValidationLayers
pkgbase=mingw-w64-vulkan-validation-layers
pkgname=("${MINGW_PACKAGE_PREFIX}-vulkan-validation-layers")
pkgver=1.2.179
pkgrel=1
pkgdesc='Vulkan Validation Layers (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}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-tools")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/${_realname}/archive/v${pkgver}.tar.gz"
        001-fix-json-layers-install-dest.patch
        002-proper-def-files-for-32bit.patch
        003-install-layout.patch)
sha256sums=('3c9bd746c4f025674083d1d63bafc561149130be1a8e514532d43f5dcad45e9d'
            'aae34364773bbe39fa510713c48a5c1bf0fb7b5fe984770bc9e2fd4e1cd12f60'
            '07e12b905174d2b2cb44babe548fe521d913226000daf29e1d812a10ce67829a'
            '02dc645bdae0b7150766e1663cc9e675681201d7247d68375c307a5f75db4ce0')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/001-fix-json-layers-install-dest.patch
  patch -p1 -i ${srcdir}/002-proper-def-files-for-32bit.patch
  patch -p1 -i ${srcdir}/003-install-layout.patch
}

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

  if [[ "${MSYSTEM}" == "MINGW32" ]]; then
    # https://www.mail-archive.com/devel@lists.fedoraproject.org/msg167502.html
    extra_config+=("-DCMAKE_CXX_FLAGS_RELEASE=-O1 -DNDEBUG")
  fi

  CXXFLAGS+=" -Wno-unused-function" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DGLSLANG_INSTALL_DIR=${MINGW_PREFIX} \
    -DSPIRV_HEADERS_INSTALL_DIR=${MINGW_PREFIX} \
    -DBUILD_LAYER_SUPPORT_FILES=ON \
    -DBUILD_TESTS=OFF \
    -DBUILD_WERROR=OFF \
    -DUSE_ROBIN_HOOD_HASHING=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 -dm755 ${pkgdir}${MINGW_PREFIX}/share/doc/vulkan-validation-layers/
  cp -r ${srcdir}/${_realname}-${pkgver}/docs/* ${pkgdir}${MINGW_PREFIX}/share/doc/vulkan-validation-layers/
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.TXT ${pkgdir}${MINGW_PREFIX}/share/licenses/vulkan-validation-layers/LICENSE
}
