# Courtesy of qemu-3dfx <liewkj@yahoo.com>

_realname=virglrenderer
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=1.1.1
pkgrel=1
pkgdesc='A virtual 3D GPU library, that allows the guest operating system to use the host GPU to accelerate 3D rendering (mingw-w64)'
arch=('any')
# clang disabled until attribute gcc_struct is supported, see https://github.com/msys2/MINGW-packages/pull/21903#issue-2521206281
mingw_arch=('mingw64' 'ucrt64')
url='https://docs.mesa3d.org/drivers/virgl/'
msys2_repository_url="https://gitlab.freedesktop.org/virgl/virglrenderer"
msys2_references=(
  'archlinux: virglrenderer'
  "cpe: cpe:/a:virglrenderer_project:virglrenderer"
)
license=(spdx:MIT)
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python-yaml"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://gitlab.freedesktop.org/virgl/${_realname}/-/archive/${pkgver}/${_realname}-${pkgver}.tar.bz2")
sha256sums=('0fe489a812fbeb3d6718199c27b0f36f8d51bc5c54d8093aad53b00d107de2b3')

noextract=("${_realname}-${pkgver}.tar.bz2")

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.bz2 ..."
  tar -xjf "${srcdir}/${_realname}-${pkgver}.tar.bz2" || true
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    --wrap-mode=nodownload \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

check() {
  cd build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/meson test || true
}

package() {
  cd build-${MSYSTEM}

  DESTDIR="$pkgdir" ${MINGW_PREFIX}/bin/meson install

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"/${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
