# Maintainer: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>

pkgbase=omarchy-mlx
pkgname=(omarchy-mlx omarchy-mlx-vulkan)
pkgver=0.7.28
pkgrel=1
arch=('aarch64')
url='https://github.com/joshuaswarren/omarchy-mlx'
# The two packages publish together, so the tested wheel and the Vulkan driver
# it was qualified on can never skew.
_wheel=mlx_omarchy-0.32.4.dev202610050725+5c15fba-cp314-cp314-linux_aarch64.whl
_vendor=omarchy-mlx-vendor-wheels-v$pkgver-cp314-aarch64.tar
# The Honeykrisp Vulkan driver: joshuaswarren/mesa-1 honeykrisp-omarchy-v3.
_mesa_commit=e7631595df6281748ea5e643d74db59c5f783b01
# The driver reports this in driverInfo as "git-<sha>"; the runtime compares it.
_mesa_sha=${_mesa_commit:0:10}
makedepends=('clang' 'elfutils' 'expat' 'glslang' 'libclc' 'libdrm' 'llvm' 'meson'
             'python' 'python-mako' 'python-packaging' 'python-yaml'
             'spirv-llvm-translator' 'spirv-tools' 'zstd')
# check() imports the staged venv's mlx.core, which loads these shared
# libraries at import. Per-package depends are not installed at check() time
# in a fresh makepkg container, so they must come from pkgbase-level checkdepends.
checkdepends=('openblas' 'lapack' 'blas' 'vulkan-icd-loader')
options=('!debug' '!lto')
source=("omarchy-mlx-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz"
        "$_wheel::$url/releases/download/v$pkgver/${_wheel//+/%2B}"
        "$_vendor::$url/releases/download/v$pkgver/$_vendor"
        "mesa-$_mesa_commit.tar.gz::https://github.com/joshuaswarren/mesa-1/archive/$_mesa_commit.tar.gz")
noextract=("$_wheel" "$_vendor")
sha256sums=(
            'b46a8c766c4ac8b60a85e085213f0e3ca7936ec83209887bfe8038b1e5c5912a'
            '68bb536fa4879ff6367b35617e800e9a3cfe1d35563458474ebdf33e2ce4c92d'
            'defbc78d3abe4f799213f416768915dc3176abe7bd18a3465073c610f47a7f32'
            'ae00b9568f1c57a7be7518b6f508354bc86c4494d83a82ef79bd0a799835c4b2'
            )

prepare() {
  rm -rf vendor
  mkdir vendor
  tar -xf "$_vendor" -C vendor --strip-components=1
  [[ -f vendor/$_wheel ]] || cp "$_wheel" vendor/
}

build() {
  # Vulkan driver only: no GL, no gallium drivers, no window systems, no layers.
  # The tarball has no .git; git_sha1.h is generated at compile time from this.
  export MESA_GIT_SHA1_OVERRIDE=$_mesa_sha
  meson setup "mesa-1-$_mesa_commit" build-mesa \
    --prefix=/usr \
    --libdir=lib \
    --buildtype=release \
    -Db_ndebug=true \
    -Dvulkan-drivers=asahi \
    -Dgallium-drivers= \
    -Dplatforms= \
    -Dvulkan-layers= \
    -Dopengl=false \
    -Dgles1=disabled \
    -Dgles2=disabled \
    -Degl=disabled \
    -Dgbm=disabled \
    -Dglx=disabled \
    -Dllvm=enabled \
    -Dshared-llvm=enabled \
    -Dvalgrind=disabled \
    -Dbuild-tests=false
  meson compile -C build-mesa
  DESTDIR="$srcdir/mesa-stage" meson install -C build-mesa

  # The system venv and launchers, built offline from the hash-locked wheels.
  cd "omarchy-mlx-$pkgver"
  MLX_OMARCHY_CONV_RING=0 bash install.sh --system \
    --dest-root "$srcdir/stage" \
    --vendor "$srcdir/vendor" \
    --lock "$srcdir/vendor/requirements-lock.txt"
}

check() {
  grep -aq "(git-$_mesa_sha)" "$srcdir/mesa-stage/usr/lib/libvulkan_asahi.so"
  "$srcdir/stage/usr/lib/omarchy-mlx/venv/bin/python" \
    -c 'import mlx.core, mlx_omarchy_paths, mlx_omarchy_serve'
  # Refuse a stage whose ANE bytes disagree with the wheel's runtime pin.
  python3 "$srcdir/omarchy-mlx-$pkgver/scripts/verify_runtime_assets.py" \
    "$srcdir/stage/usr/lib/omarchy-mlx/venv/lib/python3.14/site-packages/mlx/share/mlx-omarchy/parakeet-1"
}

package_omarchy-mlx() {
  pkgdesc='MLX for Apple Silicon Macs: GPU inference through Vulkan, with ANE support'
  license=('MIT')
  groups=('omarchy-platform-apple-silicon')
  depends=("omarchy-mlx-vulkan=$pkgver-$pkgrel" 'python>=3.14' 'python<3.15'
           'blas' 'gcc-libs' 'glibc' 'lapack' 'openblas' 'vulkan-icd-loader')
  optdepends=('omarchy-ane-abi: Apple Neural Engine execution on qualified chips (omarchy-ane-dkms)')
  cp -a "$srcdir/stage/usr" "$pkgdir/usr"
}

package_omarchy-mlx-vulkan() {
  pkgdesc='Honeykrisp Vulkan driver for omarchy-mlx only (private ICD, not used by other programs)'
  license=('MIT AND BSD-3-Clause AND SGI-B-2.0')
  groups=('omarchy-platform-apple-silicon')
  depends=('expat' 'gcc-libs' 'glibc' 'libdrm' 'libelf' 'llvm-libs' 'spirv-tools' 'zstd')
  conflicts=('mesa-honeykrisp-omarchy')

  local dir=/usr/lib/omarchy-mlx/vulkan
  install -Dm755 "$srcdir/mesa-stage/usr/lib/libvulkan_asahi.so" "$pkgdir$dir/libvulkan_asahi.so"
  # The ICD manifest stays out of the loader's search path; only omarchy-mlx
  # names it. library_path is absolute.
  python - "$srcdir/mesa-stage/usr/share/vulkan/icd.d/asahi_icd.aarch64.json" \
    "$pkgdir$dir/honeykrisp_icd.aarch64.json" "$dir/libvulkan_asahi.so" <<'PY'
import json, sys
src, dst, lib = sys.argv[1:]
manifest = json.load(open(src))
manifest["ICD"]["library_path"] = lib
with open(dst, "w") as f:
    json.dump(manifest, f, indent=4)
    f.write("\n")
PY
  chmod 644 "$pkgdir$dir/honeykrisp_icd.aarch64.json"
  printf '%s\n' "$_mesa_sha" | install -Dm644 /dev/stdin "$pkgdir$dir/mesa-git-sha"
  install -Dm644 "$srcdir/mesa-1-$_mesa_commit/docs/license.rst" \
    "$pkgdir/usr/share/licenses/$pkgname/license.rst"
}
