# Maintainer: Markus Fritsche <mfritsche@reauktion.de>
#
# vulkan-panfrost — Mesa's panvk Vulkan driver, packaged as a
# standalone ICD on Arch Linux ARM. Stock ALARM `mesa` does not build
# with -Dvulkan-drivers=panfrost, so panvk doesn't ship.
#
# Targets both Mali kernel drivers:
#   - panfrost (Bifrost: Mali-G31 / G52 / G57) on RK3566 / RK3568 etc.
#   - panthor  (Valhall: Mali-G610+)            on RK3588 / RK3588S etc.
#
# panvk on Mali-G52 r1 (Bifrost-gen2) currently returns
# VK_ERROR_INCOMPATIBLE_DRIVER on probe — that's an upstream mesa
# issue, not a packaging one. The driver lights up cleanly on
# Mali-G610 Valhall (RK3588) which is the immediate target. Install on
# Bifrost boards anyway; future mesa releases may unblock G52 r1
# without re-packaging.

pkgname=vulkan-panfrost-fourier
pkgver=26.0.5
pkgrel=2
epoch=1
pkgdesc='Mesa Vulkan ICD for Mali Bifrost / Valhall (panvk)'
arch=('aarch64')
url='https://gitlab.freedesktop.org/mesa/mesa'
license=('MIT')
depends=(
  vulkan-icd-loader
  libdrm
  zlib
  zstd
  expat
  libelf
  wayland
)
makedepends=(
  meson
  ninja
  python-mako
  glslang
  libxrandr
  libxshmfence
  libxxf86vm
  vulkan-headers
  wayland-protocols
  rust-bindgen
  rust
  llvm
  llvm-libs
  libclc
  spirv-tools
  spirv-llvm-translator
)
provides=('vulkan-driver')
options=('!lto')
source=("https://archive.mesa3d.org/mesa-${pkgver}.tar.xz")
sha256sums=('SKIP')

build() {
  cd "${srcdir}/mesa-${pkgver}"

  # Strip mesa down to just the panvk Vulkan driver — no Gallium
  # drivers, no GL/GLES, no GLX, no EGL, no VAAPI/VDPAU. The host's
  # stock `mesa` package keeps providing all of those; this PKGBUILD
  # only adds the missing Vulkan ICD next to it.
  #
  # --auto-features=disabled means features have to be opt-in. Avoids
  # mesa's default "enable everything we can find headers for" pulling
  # in xlib-lease / gallium-va / etc. that we don't want here.
  meson setup . build \
    --prefix=/usr \
    --libexecdir=lib \
    --sbindir=bin \
    --buildtype=release \
    --auto-features=disabled \
    --wrap-mode=nodownload \
    -Db_lto=false \
    -Db_pie=true \
    -Dvulkan-drivers=panfrost \
    -Dgallium-drivers= \
    -Dplatforms=wayland \
    -Dshared-glapi=disabled \
    -Dgallium-rusticl=false \
    -Dmicrosoft-clc=disabled \
    -Dvideo-codecs= \
    -Dllvm=enabled \
    -Dshared-llvm=enabled \
    -Dspirv-tools=enabled \
    -Dvulkan-icd-dir=/usr/share/vulkan/icd.d

  meson compile -C build
}

package() {
  cd "${srcdir}/mesa-${pkgver}"

  # Mesa's install rules drop a lot of files we don't want in this
  # narrow package — stage to a temp dir, then cherry-pick.
  DESTDIR="${srcdir}/staging" meson install -C build --no-rebuild

  # The ICD shared object
  install -Dm755 "${srcdir}/staging/usr/lib/libvulkan_panfrost.so" \
    "${pkgdir}/usr/lib/libvulkan_panfrost.so"

  # The Vulkan loader manifest. Mesa installs it as
  # `panfrost_icd.<arch>.json` (the userspace driver name) — that
  # filename is what the Vulkan loader expects, no need to rename.
  install -dm755 "${pkgdir}/usr/share/vulkan/icd.d"
  cp -av "${srcdir}/staging/usr/share/vulkan/icd.d/"panfrost_icd*.json \
    "${pkgdir}/usr/share/vulkan/icd.d/"
}
