Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

gpu: build scripts keep only whole downloads and take a relative W; Venus aligns a copy of the allocation info; the compute test within dispatch limits; CPU Metal builds from a fresh clone as the README says

Fred Nix48dfd61702d7
diff --git a/.github/workflows/metal-plugin.yml b/.github/workflows/metal-plugin.yml
index 9614a13..8c324d5 100644
--- a/.github/workflows/metal-plugin.yml
+++ b/.github/workflows/metal-plugin.yml
@@ -33,23 +33,15 @@ jobs:
brew unlink spirv-tools
[ -e /opt/homebrew/opt/llvm@22 ] || ln -s "$(brew --prefix llvm)" /opt/homebrew/opt/llvm@22
/opt/homebrew/opt/llvm@22/bin/llvm-config --version
- mkdir -p build/vk-deps/bin
- "$(brew --prefix python3)/bin/python3" -m venv build/pyvenv
- build/pyvenv/bin/pip -q install mako pyyaml packaging
- ln -sf "$(brew --prefix bison)/bin/bison" "$(brew --prefix flex)/bin/flex" build/vk-deps/bin/
rustup toolchain install 1.100.0 --profile minimal
- rustup toolchain install nightly-aarch64-apple-darwin --profile minimal --component rust-src
+ rustup toolchain install nightly-2026-11-03-aarch64-apple-darwin --profile minimal --component rust-src
+ # from here on the steps are the README's, in its order
- name: Sources
run: scripts/fetch-sources.sh
- - name: Vulkan headers and loader (arm64, arm64e)
- run: |
- export PATH="$PWD/build/pyvenv/bin:/opt/homebrew/bin:$PATH"
- git clone -q --depth 1 --branch vulkan-sdk-1.4.363.0 https://github.com/KhronosGroup/Vulkan-Headers.git build/Vulkan-Headers
- cmake -G Ninja -S build/Vulkan-Headers -B build/Vulkan-Headers/build -DCMAKE_INSTALL_PREFIX="$PWD/build/vk-deps/vulkan-headers/1.4.363.0"
- ninja -C build/Vulkan-Headers/build install
- scripts/build-vkloader-arm64e.sh
- mkdir -p build/vk-deps/vulkan-loader/1.4.363.0/lib
- cp -f build/vkloader-arm64-install/lib/libvulkan.1.4.363.dylib build/vk-deps/vulkan-loader/1.4.363.0/lib/
+ - name: Python, bison, flex and the Vulkan headers under build/
+ run: scripts/setup-deps.sh
+ - name: Vulkan loader (arm64, arm64e)
+ run: scripts/build-vkloader-arm64e.sh
- name: lavapipe (arm64)
run: scripts/build-lavapipe.sh
- name: metal2vulkan bridge (arm64)
@@ -78,6 +70,8 @@ jobs:
scripts/build-bundle-universal.sh
for f in dist/XnuCpuMetal.bundle/Contents/Frameworks/*.dylib dist/XnuCpuMetal.bundle/Contents/MacOS/XnuCpuMetal; do
for a in arm64 arm64e; do
+ # otool and nm print nothing for a slice that is not there, which would pass both checks
+ case " $(lipo -archs "$f") " in *" $a "*) ;; *) echo "$f: no $a slice"; exit 1;; esac
if otool -arch $a -L "$f" | tail -n +2 | grep -v -E '^\s*(/usr/lib/|/System/Library/|@rpath/)' | grep -v "$(basename "$f")"; then echo "$f ($a): a dependency outside the system"; exit 1; fi
if nm -arch $a -u "$f" | grep -E '^_spv'; then echo "$f ($a): undefined SPIRV-Tools symbols"; exit 1; fi
done
diff --git a/.github/workflows/scripts.yml b/.github/workflows/scripts.yml
new file mode 100644
index 0000000..4b9753d
--- /dev/null
+++ b/.github/workflows/scripts.yml
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Run the tests of the repository's shell scripts (tests/scripts): each script as it is, in a sandbox of stand-in
+# commands, so the job needs no root, no VM and no Mac. A test added there is run without a change to this file.
+name: scripts
+on:
+ push:
+ pull_request:
+jobs:
+ test:
+ runs-on: ubuntu-24.04
+ steps:
+ - uses: actions/checkout@v4
+ - name: The shell scripts' tests
+ run: bash tests/scripts/run.sh
diff --git a/gpu/venus/build-darwin.sh b/gpu/venus/build-darwin.sh
index ae36185..49a850e 100755
--- a/gpu/venus/build-darwin.sh
+++ b/gpu/venus/build-darwin.sh
@@ -4,20 +4,22 @@
# DRM bridge (gpu/xnu-drm: libxnudrm in the driver, kexts/XnuDrm in the hosted kernel, xnudrmd on Linux). Patches as for
# the Linux guest (patches/), plus libxnudrm copied into the tree. Also builds test/compute.c against it.
# Output: $OUT/lib/libvulkan_virtio.dylib (universal arm64 + arm64e: the Metal plugin loads it into system processes and
-# into ordinary arm64 ones), $OUT/bin/venus-compute (arm64).
+# into ordinary arm64 ones), $OUT/bin/venus-compute (arm64) and its shader $OUT/bin/compute.spv.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
MESA=26.3.1
ARCHS=${ARCHS:-arm64 arm64e}
-W=${W:-$HOME/w/venus-darwin}; OUT=${OUT:-$W/out}; mkdir -p "$W"; cd "$W"
+W=${W:-$HOME/w/venus-darwin}; mkdir -p "$W"; cd "$W"; W=$PWD; OUT=${OUT:-$W/out} # W absolute: PATH and meson's --prefix below
+UV=$(command -v uv || true) # before PATH is narrowed: uv's own installer puts it in ~/.local/bin
export PATH=$W/venv/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin
# Mesa wants Python >= 3.10 (macOS ships 3.9): uv's own when there is uv, else Homebrew's
if ! { [ -x venv/bin/meson ] && venv/bin/python3 -c 'import sys; sys.exit(sys.version_info < (3, 10))'; }; then
rm -rf venv
- if command -v uv >/dev/null; then uv venv -q -p 3.13 venv && VIRTUAL_ENV=$W/venv uv pip install -q meson ninja mako pyyaml packaging
+ if [ -n "$UV" ]; then "$UV" venv -q -p 3.13 venv && VIRTUAL_ENV=$W/venv "$UV" pip install -q meson ninja mako pyyaml packaging
else /opt/homebrew/bin/python3 -m venv venv && venv/bin/pip -q install meson ninja mako pyyaml packaging; fi
fi
-[ -f mesa-$MESA.tar.xz ] || curl -sSLo mesa-$MESA.tar.xz https://archive.mesa3d.org/mesa-$MESA.tar.xz
+# (under another name until it is whole: an error page or a transfer cut short is not the tarball of the next run)
+[ -f mesa-$MESA.tar.xz ] || { curl -fsSLo mesa-$MESA.tar.xz.part https://archive.mesa3d.org/mesa-$MESA.tar.xz; mv mesa-$MESA.tar.xz.part mesa-$MESA.tar.xz; }
rm -rf mesa && mkdir mesa && tar -C mesa --strip-components=1 -xf mesa-$MESA.tar.xz
for p in "$HERE"/patches/*.patch; do patch -d mesa -p1 --no-backup-if-mismatch -s < "$p"; done
mkdir -p mesa/src/virtio/vulkan/xnudrm
@@ -48,3 +50,6 @@ lipo -info "$OUT"/lib/libvulkan_virtio.dylib
mkdir -p "$OUT"/bin
# the test talks to the driver directly (no loader in the hosted system): vk_icdGetInstanceProcAddr
/usr/bin/clang -arch arm64 -O2 -I mesa/include "$HERE"/test/compute.c -o "$OUT"/bin/venus-compute
+# and its shader, which it takes as its argument (glslang: brew install glslang)
+if command -v glslangValidator >/dev/null; then glslangValidator -V "$HERE"/test/compute.comp -o "$OUT"/bin/compute.spv >/dev/null
+else echo "no glslangValidator: $OUT/bin/venus-compute needs compute.spv, made from $HERE/test/compute.comp (build-linux.sh makes one too)" >&2; fi
diff --git a/gpu/venus/build-linux.sh b/gpu/venus/build-linux.sh
index d555a70..7a78e6e 100755
--- a/gpu/venus/build-linux.sh
+++ b/gpu/venus/build-linux.sh
@@ -6,12 +6,13 @@
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
MESA=26.3.1
-W=${W:-$HOME/w/venus}; OUT=${OUT:-$W/out}; mkdir -p "$W"; cd "$W"
+W=${W:-$HOME/w/venus}; mkdir -p "$W"; cd "$W"; W=$PWD; OUT=${OUT:-$W/out} # W absolute: PATH and meson's --prefix below
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq ninja-build libdrm-dev glslang-tools bison flex pkg-config \
libexpat1-dev zlib1g-dev libudev-dev libzstd-dev libvulkan-dev vulkan-tools python3-venv >/dev/null
[ -x venv/bin/meson ] || { python3 -m venv venv && venv/bin/pip -q install meson mako pyyaml packaging; }
export PATH=$W/venv/bin:$PATH
-[ -f mesa-$MESA.tar.xz ] || curl -sSLo mesa-$MESA.tar.xz https://archive.mesa3d.org/mesa-$MESA.tar.xz
+# (under another name until it is whole: an error page or a transfer cut short is not the tarball of the next run)
+[ -f mesa-$MESA.tar.xz ] || { curl -fsSLo mesa-$MESA.tar.xz.part https://archive.mesa3d.org/mesa-$MESA.tar.xz; mv mesa-$MESA.tar.xz.part mesa-$MESA.tar.xz; }
rm -rf mesa && mkdir mesa && tar -C mesa --strip-components=1 -xf mesa-$MESA.tar.xz
for p in "$HERE"/patches/*.patch; do patch -d mesa -p1 --no-backup-if-mismatch -s < "$p"; done
meson setup mesa/build mesa --prefix="$OUT" --buildtype=release -Dvulkan-drivers=virtio -Dgallium-drivers= \
diff --git a/gpu/venus/patches/0001-venus-16k-host-page-alignment.patch b/gpu/venus/patches/0001-venus-16k-host-page-alignment.patch
index bce65db..c0a1470 100644
--- a/gpu/venus/patches/0001-venus-16k-host-page-alignment.patch
+++ b/gpu/venus/patches/0001-venus-16k-host-page-alignment.patch
@@ -10,13 +10,15 @@ alignment (VIRTIO_GPU_F_BLOB_ALIGNMENT); until Mesa reads it, align everything t
--- a/src/virtio/vulkan/vn_device_memory.c
+++ b/src/virtio/vulkan/vn_device_memory.c
-@@ -31,6 +31,10 @@ vn_device_memory_alloc_simple(struct vn_device *dev,
+@@ -31,6 +31,12 @@ vn_device_memory_alloc_simple(struct vn_device *dev,
{
VkDevice dev_handle = vn_device_to_handle(dev);
VkDeviceMemory mem_handle = vn_device_memory_to_handle(mem);
+
-+ ((VkMemoryAllocateInfo *)alloc_info)->allocationSize =
-+ align64(alloc_info->allocationSize, 16384);
++ /* in a copy: the info is the application's own, const and possibly in read-only memory */
++ VkMemoryAllocateInfo aligned_info = *alloc_info;
++ aligned_info.allocationSize = align64(alloc_info->allocationSize, 16384);
++ alloc_info = &aligned_info;
+
if (VN_PERF(NO_ASYNC_MEM_ALLOC)) {
return vn_call_vkAllocateMemory(dev->primary_ring, dev_handle,
diff --git a/gpu/venus/test/compute.c b/gpu/venus/test/compute.c
index 365a8eb..b3373bb 100644
--- a/gpu/venus/test/compute.c
+++ b/gpu/venus/test/compute.c
@@ -81,6 +81,7 @@ static void load_instance(VkInstance inst) {
#define CK(x) do{VkResult r=(x); if(r){printf("FAIL %s = %d\n",#x,r); exit(1);}}while(0)
int main(int argc,char**argv){
const uint32_t N=1u<<22; /* 4M uints = 16 MB */
+ if(argc<2){ printf("usage: %s compute.spv (glslangValidator -V compute.comp -o compute.spv; VT_ICD=driver, VT_DEVICE=n)\n",argv[0]); return 2; }
VkInstance inst; VkApplicationInfo ai={VK_STRUCTURE_TYPE_APPLICATION_INFO,0,"vt",1,0,0,VK_API_VERSION_1_1};
VkInstanceCreateInfo ic={VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,0,0,&ai};
load_global();
@@ -106,7 +107,7 @@ int main(int argc,char**argv){
VkMemoryAllocateInfo ma={VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,0,mr.size,mt}; VkDeviceMemory mem; CK(vkAllocateMemory(dev,&ma,0,&mem));
CK(vkBindBufferMemory(dev,buf,mem,0)); uint32_t*p; CK(vkMapMemory(dev,mem,0,N*4,0,(void**)&p));
for(uint32_t i=0;i<N;i++) p[i]=i;
- FILE*f=fopen(argv[1],"rb"); fseek(f,0,2); long sz=ftell(f); rewind(f); uint32_t*code=malloc(sz); if(fread(code,1,sz,f)!=(size_t)sz){printf("short read\n");return 1;} fclose(f);
+ FILE*f=fopen(argv[1],"rb"); if(!f){ perror(argv[1]); return 1; } fseek(f,0,2); long sz=ftell(f); rewind(f); uint32_t*code=malloc(sz); if(fread(code,1,sz,f)!=(size_t)sz){printf("short read\n");return 1;} fclose(f);
VkShaderModuleCreateInfo sc={VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,0,0,sz,code}; VkShaderModule sm; CK(vkCreateShaderModule(dev,&sc,0,&sm));
VkDescriptorSetLayoutBinding lb={0,VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,1,VK_SHADER_STAGE_COMPUTE_BIT};
VkDescriptorSetLayoutCreateInfo lc={VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,0,0,1,&lb}; VkDescriptorSetLayout dl; CK(vkCreateDescriptorSetLayout(dev,&lc,0,&dl));
@@ -120,7 +121,7 @@ int main(int argc,char**argv){
VkCommandBufferAllocateInfo cba={VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,0,cp,0,1}; VkCommandBuffer cb; CK(vkAllocateCommandBuffers(dev,&cba,&cb));
VkCommandBufferBeginInfo bi={VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; CK(vkBeginCommandBuffer(cb,&bi));
vkCmdBindPipeline(cb,VK_PIPELINE_BIND_POINT_COMPUTE,pipe); vkCmdBindDescriptorSets(cb,VK_PIPELINE_BIND_POINT_COMPUTE,pl,0,1,&ds,0,0);
- vkCmdDispatch(cb,N/64,1,1);
+ vkCmdDispatch(cb,1024,N/65536,1); /* 1024 x 64 groups of 64 (compute.comp): N/64 in one row is 65536, and 65535 is all a device has to take (lavapipe's limit) */
VkMemoryBarrier mb={VK_STRUCTURE_TYPE_MEMORY_BARRIER,0,VK_ACCESS_SHADER_WRITE_BIT,VK_ACCESS_HOST_READ_BIT};
vkCmdPipelineBarrier(cb,VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,VK_PIPELINE_STAGE_HOST_BIT,0,1,&mb,0,0,0,0);
CK(vkEndCommandBuffer(cb));
diff --git a/gpu/venus/test/compute.comp b/gpu/venus/test/compute.comp
index a52821d..badc29a 100644
--- a/gpu/venus/test/compute.comp
+++ b/gpu/venus/test/compute.comp
@@ -2,4 +2,5 @@
#version 450
layout(local_size_x=64) in;
layout(std430, binding=0) buffer B { uint d[]; };
-void main(){ uint i=gl_GlobalInvocationID.x; d[i]=d[i]*2u+1u; }
+// groups in two dimensions (compute.c dispatches 1024 x 64 of them): a row is 1024 * 64 = 65536 elements
+void main(){ uint i=gl_GlobalInvocationID.y*65536u+gl_GlobalInvocationID.x; d[i]=d[i]*2u+1u; }
diff --git a/gpu/xnu-cpu-metal/README.md b/gpu/xnu-cpu-metal/README.md
index 3068644..fccbe8b 100644
--- a/gpu/xnu-cpu-metal/README.md
+++ b/gpu/xnu-cpu-metal/README.md
@@ -13,11 +13,23 @@ build-time renderers (`ictool`/Icon Composer, `actool`, CoreImage).
- `scripts/fetch-sources.sh` fetches, at pinned revisions and **never committed**: metal2vulkan (GPL-2.0-or-later / LGPL-3.0-or-later),
Vulkan-Loader (Apache-2.0), Mesa 26.3.1 (lavapipe, MIT), LLVM 22.1.8. None of it goes into XNU or the Linux kernel.
-## Build (on a Mac with Xcode and a Rust toolchain)
+## Build (on an Apple silicon Mac with Xcode, Homebrew and rustup)
+Once, the tools (what `.github/workflows/metal-plugin.yml` installs):
+
+ brew install meson ninja cmake pkgconf bison flex glslang zstd python3 llvm@22
+ brew unlink spirv-tools # it comes with glslang; Mesa must not link Homebrew's SPIRV-Tools into the bundle
+ rustup toolchain install 1.100.0 --profile minimal
+ rustup toolchain install nightly-2026-11-03-aarch64-apple-darwin --profile minimal --component rust-src
+
+Then, in this order (each step uses what the ones before it left under `build/` and `dist/`):
+
scripts/fetch-sources.sh
- scripts/build-lavapipe.sh && scripts/build-lavapipe-arm64e.sh # needs llvm-arm64e: scripts/build-llvm-arm64e.sh
- scripts/build-m2v.sh && scripts/build-m2v-arm64e.sh
- scripts/build-vkloader-arm64e.sh
+ scripts/setup-deps.sh # build/pyvenv, build/vk-deps: Python, bison, flex, Vulkan headers
+ scripts/build-vkloader-arm64e.sh # the Vulkan loader, arm64 and arm64e
+ scripts/build-lavapipe.sh && scripts/build-m2v.sh # arm64
+ scripts/build-llvm-arm64e.sh # LLVM for the arm64e llvmpipe JIT: about 40 minutes
+ scripts/build-lavapipe-arm64e.sh && scripts/build-m2v-arm64e.sh # arm64e
+ ../venus/build-darwin.sh # optional: the Venus driver, taken into the bundle when built
scripts/build-bundle-universal.sh # -> dist/XnuCpuMetal.bundle
Install: `scripts/mk-macvm-disk.sh` (repo root) puts the bundle on the system disk; the hosted kernel needs the boot-arg `lp-sip0=2`.
diff --git a/gpu/xnu-cpu-metal/scripts/build-m2v-arm64e.sh b/gpu/xnu-cpu-metal/scripts/build-m2v-arm64e.sh
index 34ef851..c0c607f 100755
--- a/gpu/xnu-cpu-metal/scripts/build-m2v-arm64e.sh
+++ b/gpu/xnu-cpu-metal/scripts/build-m2v-arm64e.sh
@@ -12,8 +12,9 @@ OBJCOPY="/opt/homebrew/opt/llvm@22/bin/llvm-objcopy"
mkdir -p "$DIST_DIR/lib" "$DIST_DIR/include"
rm -rf "$WORK" && mkdir -p "$WORK/objs"
-# 1. Build Rust staticlib for arm64e-apple-darwin with -Zbuild-std
-export RUSTUP_TOOLCHAIN=nightly-aarch64-apple-darwin
+# 1. Build Rust staticlib for arm64e-apple-darwin with -Zbuild-std: the nightly the pinned metal2vulkan names in its own
+# rust-toolchain.toml (RUST_NIGHTLY to try another), not whichever nightly rustup has today
+export RUSTUP_TOOLCHAIN="${RUST_NIGHTLY:-nightly-2026-11-03}-aarch64-apple-darwin"
cargo build --manifest-path "$BRIDGE_DIR/Cargo.toml" \
-Zbuild-std=std,panic_unwind \
--target arm64e-apple-darwin \
diff --git a/gpu/xnu-cpu-metal/scripts/build-vkloader-arm64e.sh b/gpu/xnu-cpu-metal/scripts/build-vkloader-arm64e.sh
index a70de8a..951199d 100755
--- a/gpu/xnu-cpu-metal/scripts/build-vkloader-arm64e.sh
+++ b/gpu/xnu-cpu-metal/scripts/build-vkloader-arm64e.sh
@@ -32,3 +32,7 @@ build_arch() {
build_arch arm64
build_arch arm64e
+
+# the arm64 loader where scripts/build-m2v.sh takes it from (it goes beside lavapipe's driver)
+mkdir -p "$ROOT/build/vk-deps/vulkan-loader/1.4.363.0/lib"
+cp -f "$ROOT/build/vkloader-arm64-install/lib/libvulkan.1.4.363.dylib" "$ROOT/build/vk-deps/vulkan-loader/1.4.363.0/lib/"
diff --git a/gpu/xnu-cpu-metal/scripts/fetch-sources.sh b/gpu/xnu-cpu-metal/scripts/fetch-sources.sh
index 49095b0..9610de1 100755
--- a/gpu/xnu-cpu-metal/scripts/fetch-sources.sh
+++ b/gpu/xnu-cpu-metal/scripts/fetch-sources.sh
@@ -36,8 +36,11 @@ git_at Vulkan-Loader https://github.com/KhronosGroup/Vulkan-Loader.git fa218055f
git_at llvm-project https://github.com/llvm/llvm-project.git ca7933e47d3a3451d81e72ac174dcb5aa28b59d1 # llvmorg-22.1.8 (arm64e llvmpipe JIT)
git_at AdrenoMetal https://github.com/UniXNU/AdrenoMetal.git cc0a4e8e57d20f4a4698753c2530875f2b10c0ca # MIT; only bridge/RustPacBoundary.c (arm64e PAC trampolines) is built
mkdir -p "$ROOT/build"
-[ -f "$ROOT/build/mesa-26.3.1.tar.xz" ] || curl -sSLo "$ROOT/build/mesa-26.3.1.tar.xz" https://archive.mesa3d.org/mesa-26.3.1.tar.xz
-[ -d mesa ] || { mkdir mesa && tar -C mesa --strip-components=1 -xf "$ROOT/build/mesa-26.3.1.tar.xz"; } # MIT (lavapipe)
+# the tarball and the tree under another name until they are whole: an error page, a transfer cut short or a failed
+# unpacking is not what the next run finds there
+M="$ROOT/build/mesa-26.3.1.tar.xz"
+[ -f "$M" ] || { curl -fsSLo "$M.part" https://archive.mesa3d.org/mesa-26.3.1.tar.xz; mv "$M.part" "$M"; }
+[ -d mesa ] || { rm -rf "$ROOT/build/mesa.part"; mkdir "$ROOT/build/mesa.part"; tar -C "$ROOT/build/mesa.part" --strip-components=1 -xf "$M"; mv "$ROOT/build/mesa.part" mesa; } # MIT (lavapipe)
apply_patches metal2vulkan metal2vulkan
apply_patches Vulkan-Loader Vulkan-Loader
diff --git a/gpu/xnu-cpu-metal/scripts/setup-deps.sh b/gpu/xnu-cpu-metal/scripts/setup-deps.sh
new file mode 100755
index 0000000..7a6e67b
--- /dev/null
+++ b/gpu/xnu-cpu-metal/scripts/setup-deps.sh
@@ -0,0 +1,27 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0-or-later
+# What the build scripts expect under build/ that none of them makes. Once, before the first build:
+# build/pyvenv the Python the Mesa and LLVM builds run (mako, pyyaml, packaging: Mesa's generators)
+# build/vk-deps/bin Homebrew's bison and flex, ahead of macOS's own (Mesa needs bison 3)
+# build/vk-deps/vulkan-headers/$VK the Vulkan headers of the loader's version (scripts/fetch-sources.sh fetches the loader)
+# The loader itself goes to build/vk-deps/vulkan-loader by scripts/build-vkloader-arm64e.sh.
+# Needs Homebrew's python3, bison, flex, cmake and ninja (the README's brew line); it installs nothing outside build/.
+set -euo pipefail
+ROOT="$(cd "$(dirname "$0")/.." && pwd)"; cd "$ROOT"
+VK=1.4.363.0
+
+for tool in python3 bison flex; do
+ [ -x "$(brew --prefix $tool 2>/dev/null)/bin/$tool" ] || { echo "setup-deps.sh: Homebrew's $tool is not installed (brew install $tool)" >&2; exit 1; }
+done
+mkdir -p build/vk-deps/bin
+[ -x build/pyvenv/bin/python3 ] || "$(brew --prefix python3)/bin/python3" -m venv build/pyvenv
+build/pyvenv/bin/pip -q install mako pyyaml packaging
+ln -sf "$(brew --prefix bison)/bin/bison" "$(brew --prefix flex)/bin/flex" build/vk-deps/bin/
+
+if [ ! -f "build/vk-deps/vulkan-headers/$VK/include/vulkan/vulkan.h" ]; then
+ export PATH="$ROOT/build/pyvenv/bin:/opt/homebrew/bin:$PATH"
+ [ -d build/Vulkan-Headers/.git ] || git clone -q --depth 1 --branch "vulkan-sdk-$VK" https://github.com/KhronosGroup/Vulkan-Headers.git build/Vulkan-Headers
+ cmake -G Ninja -S build/Vulkan-Headers -B build/Vulkan-Headers/build -DCMAKE_INSTALL_PREFIX="$ROOT/build/vk-deps/vulkan-headers/$VK" >/dev/null
+ ninja -C build/Vulkan-Headers/build install >/dev/null
+fi
+echo "build/pyvenv, build/vk-deps/bin and build/vk-deps/vulkan-headers/$VK are ready"
diff --git a/tests/scripts/gpu-build-scripts.test.sh b/tests/scripts/gpu-build-scripts.test.sh
new file mode 100755
index 0000000..9332eb1
--- /dev/null
+++ b/tests/scripts/gpu-build-scripts.test.sh
@@ -0,0 +1,113 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+# The GPU build scripts up to where they hand over to the real build: gpu/venus/build-linux.sh and build-darwin.sh,
+# gpu/xnu-cpu-metal/scripts/fetch-sources.sh, tools/dgx-spark/gpu/build-virglrenderer.sh. The scripts run as they are;
+# curl, tar, meson, ninja, git, uv and the rest are stand-ins. curl "downloads" a recognisable file, or stops half way
+# as a transfer that is cut does; tar takes only what a whole download left; meson refuses a prefix that is not an
+# absolute path, as the real one does.
+. "$(dirname "$0")/lib.sh"
+sandbox_repo gpu/venus gpu/xnu-cpu-metal/scripts tools/dgx-spark/gpu gpu/xnu-drm
+P=$T/repo; export T
+PY=$(command -v python3)
+for c in apt-get ldconfig pip patch; do stub $c < /dev/null; done
+stub vulkaninfo <<'EOF'
+echo "deviceName = stand-in"
+EOF
+stub sudo <<'EOF'
+while [ $# -gt 0 ]; do case $1 in *=*) export "$1"; shift;; *) break;; esac; done
+exec "$@"
+EOF
+stub curl <<'EOF'
+while [ $# -gt 1 ]; do case $1 in -*o) out=$2; shift;; esac; shift; done # -fsSLo FILE URL
+case $(cat "$T/curl") in
+ok) echo "TARBALL of $1" > "$out";;
+cut) echo "half of it" > "$out"; echo "curl: (18) transfer closed with outstanding read data remaining" >&2; exit 18;;
+esac
+EOF
+stub tar <<'EOF'
+while [ $# -gt 0 ]; do case $1 in -C) d=$2; shift;; -xf) f=$2; shift;; esac; shift; done
+grep -q '^TARBALL' "$f" || { echo "tar: This does not look like a tar archive" >&2; exit 2; }
+[ ! -e "$T/tar-fails" ] || { : > "$d/half-unpacked"; echo "tar: Unexpected EOF in archive" >&2; exit 2; }
+: > "$d/meson.build"
+EOF
+stub meson <<'EOF'
+for a in "$@"; do case $a in --prefix=*) p=${a#--prefix=}
+ case $p in /*) echo "$p" > "$T/prefix";; *) echo "ERROR: prefix value '$p' must be an absolute path" >&2; exit 1;; esac;; esac; done
+EOF
+stub ninja <<'EOF'
+p=$(cat "$T/prefix")
+case " $* $p" in *" install $T"/*) mkdir -p "$p/share/vulkan/icd.d"; : > "$p/share/vulkan/icd.d/virtio_icd.aarch64.json";; esac # (never outside the sandbox)
+EOF
+stub glslangValidator <<'EOF'
+while [ $# -gt 1 ]; do case $1 in -o) : > "$2";; esac; shift; done
+EOF
+stub cc <<'EOF'
+while [ $# -gt 1 ]; do case $1 in -o) printf '#!/bin/sh\necho "compute: ok"\n' > "$2"; chmod +x "$2";; esac; shift; done
+EOF
+stub pkg-config <<'EOF'
+echo 1.2.0
+EOF
+stub git <<'EOF'
+[ "$1" != -C ] || { [ "$3" != init ] || mkdir -p "$2/.git"; }
+EOF
+export PY
+cat > "$T/bin/python3" <<'EOF' # -m venv DIR: a venv with the stand-ins in it; anything else is the real python3
+#!/bin/bash
+echo "python3 $*" >> "$T/calls"
+if [ "$1 $2" = "-m venv" ]; then mkdir -p "$3/bin"; cp "$T/bin/meson" "$T/bin/pip" "$3/bin/"; exit 0; fi
+exec "$PY" "$@"
+EOF
+chmod +x "$T/bin/python3"
+tarballs() { ls "$1" 2>/dev/null | grep -c -E '\.tar\.(xz|gz)$'; } # files there that pass for a whole tarball
+
+# ---- gpu/venus/build-linux.sh
+mkdir -p "$T/cwd"; echo ok > "$T/curl"
+(cd "$T/cwd" && W=rel/w /bin/bash "$P/gpu/venus/build-linux.sh" > "$T/out" 2>&1); s=$?
+check 'build-linux.sh with a W relative to where it is run from: it builds' [ $s = 0 ]
+check "... meson's prefix is the absolute W/out" called "^meson setup .* --prefix=$T/cwd/rel/w/out "
+check '... and the test it ends with ran' grep -q '^compute: ok' "$T/out"
+echo cut > "$T/curl"; : > "$T/calls"
+W=$T/w1 /bin/bash "$P/gpu/venus/build-linux.sh" > "$T/out" 2>&1; s=$?
+check 'build-linux.sh, the download cut short: it stops' [ $s != 0 ]
+check '... and leaves nothing that the next run would take for the tarball' [ "$(tarballs "$T/w1")" = 0 ]
+echo ok > "$T/curl"
+W=$T/w1 /bin/bash "$P/gpu/venus/build-linux.sh" > "$T/out" 2>&1; s=$?
+check '... so the next run downloads again and builds' [ $s = 0 -a "$(grep -c '^curl ' "$T/calls")" = 2 ]
+
+# ---- tools/dgx-spark/gpu/build-virglrenderer.sh
+echo cut > "$T/curl"; : > "$T/calls"
+W=$T/w2 /bin/bash "$P/tools/dgx-spark/gpu/build-virglrenderer.sh" > "$T/out" 2>&1; s=$?
+check 'build-virglrenderer.sh, the download cut short: it stops, nothing kept as the tarball' [ $s != 0 -a "$(tarballs "$T/w2")" = 0 ]
+echo ok > "$T/curl"
+W=$T/w2 /bin/bash "$P/tools/dgx-spark/gpu/build-virglrenderer.sh" > "$T/out" 2>&1; s=$?
+check '... and the next run downloads again and builds' [ $s = 0 -a "$(grep -c '^curl ' "$T/calls")" = 2 ]
+
+# ---- gpu/xnu-cpu-metal/scripts/fetch-sources.sh
+M=$P/gpu/xnu-cpu-metal; mkdir -p "$M/src"
+echo cut > "$T/curl"
+/bin/bash "$M/scripts/fetch-sources.sh" > "$T/out" 2>&1; s=$?
+check 'fetch-sources.sh, the download cut short: it stops, nothing kept as the tarball' [ $s != 0 -a "$(tarballs "$M/build")" = 0 ]
+echo ok > "$T/curl"; : > "$T/tar-fails"
+/bin/bash "$M/scripts/fetch-sources.sh" > "$T/out" 2>&1; s=$?
+check 'fetch-sources.sh, the unpacking failing half way: it stops, and src/mesa is not there for the next run to take' [ $s != 0 -a ! -e "$M/src/mesa" ]
+rm "$T/tar-fails"
+/bin/bash "$M/scripts/fetch-sources.sh" > "$T/out" 2>&1; s=$?
+check '... the next run unpacks it whole' [ $s = 0 -a -e "$M/src/mesa/meson.build" -a ! -e "$M/src/mesa/half-unpacked" ]
+
+# ---- gpu/venus/build-darwin.sh: uv where its installer puts it (~/.local/bin), which the script's own PATH leaves out
+if [ -x /opt/homebrew/bin/python3 ]; then
+ echo "skipped: build-darwin.sh and uv (this machine has Homebrew's python3, which the script would use without the fix)"
+else
+ mkdir -p "$HOME/.local/bin"
+ printf '#!/bin/sh\necho "reached the download" >> "$T/calls"; exit 7\n' > "$T/curl-stop"; chmod +x "$T/curl-stop"
+ cat > "$HOME/.local/bin/uv" <<'EOF' # uv venv ... DIR: a venv with the stand-ins, whose curl ends the script at the download
+#!/bin/bash
+echo "uv $*" >> "$T/calls"
+if [ "$1" = venv ]; then for d; do :; done; mkdir -p "$d/bin"; cp "$T/bin/meson" "$d/bin/"; ln -s "$PY" "$d/bin/python3"; cp "$T/curl-stop" "$d/bin/curl"; fi
+EOF
+ chmod +x "$HOME/.local/bin/uv"; : > "$T/calls"
+ PATH=$HOME/.local/bin:$PATH W=$T/w3 /bin/bash "$P/gpu/venus/build-darwin.sh" > "$T/out" 2>&1; s=$?
+ check "build-darwin.sh makes its Python environment with the user's uv" called '^uv venv .* venv$'
+ check '... installs into it with that uv, and goes on to the download' [ "$(grep -c '^uv pip install' "$T/calls")$(grep -c '^reached the download' "$T/calls")" = 11 ]
+fi
+finish
diff --git a/tests/scripts/lib.sh b/tests/scripts/lib.sh
new file mode 100644
index 0000000..7b74395
--- /dev/null
+++ b/tests/scripts/lib.sh
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Sourced by tests/scripts/*.test.sh: a sandbox to run the repository's shell scripts in as they are, off the machines
+# they are written for (no root, no VM, no Mac, nothing written outside a temporary directory).
+# $T the temporary directory; at exit every process still running out of it is ended and it is removed
+# $HOME $T/home
+# $T/repo sandbox_repo: a copy of scripts/ (and what else is named), so a script's own "../" is $T
+# $T/bin first in PATH. `stub NAME <<'EOF' ... EOF` puts a stand-in for a command there (bash; "$@" are the
+# caller's arguments, $T and $VM are set); each call of one is a line "NAME ARGUMENTS" in $T/calls
+# $VM the stand-in VM or host: stub_ssh makes `ssh [OPTIONS] HOST COMMAND` run COMMAND there, its /var/tmp
+# being $VM/var/tmp and $T/vmbin first in its PATH (`vmstub NAME <<'EOF' ... EOF`)
+# check 'what must hold' COMMAND... one check; `finish` prints the count and ends the test with its status
+# Written for /bin/bash as macOS has it (3.2) as well as Linux's.
+set -u
+REPO=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
+T=$(mktemp -d) && T=$(cd "$T" && pwd -P) && [ -n "$T" ] || { echo "tests/scripts/lib.sh: no temporary directory" >&2; exit 1; }
+VM=$T/vm; HOME=$T/home; PATH=$T/bin:$PATH; export HOME PATH
+mkdir -p "$T/bin" "$T/vmbin" "$VM/var/tmp" "$HOME"; : > "$T/calls"
+trap 'pkill -f "$T/" 2>/dev/null; rm -rf "$T"' EXIT
+CHECKS=0; FAILED=0
+
+stub() { # stub NAME [DIRECTORY] < its body
+ { printf '#!/bin/bash\nexport T=%q VM=%q\necho "%s $*" >> "$T/calls"\n' "$T" "$VM" "$1"; cat; } > "${2:-$T/bin}/$1"
+ chmod +x "${2:-$T/bin}/$1"
+}
+vmstub() { stub "$1" "$T/vmbin"; }
+stub_ssh() {
+ stub ssh <<'EOF'
+while [ $# -gt 0 ]; do case $1 in -[oipF]) shift 2;; -*) shift;; *) break;; esac; done
+shift # the host
+cd "$VM" && PATH=$T/vmbin:$PATH exec /bin/bash -c "${*//\/var\/tmp\//$VM/var/tmp/}"
+EOF
+}
+sandbox_repo() { # sandbox_repo [PATH...]: $T/repo with scripts/ and the paths named, as the repository has them
+ local p; mkdir -p "$T/repo"
+ for p in scripts "$@"; do mkdir -p "$T/repo/$(dirname "$p")"; cp -Rp "$REPO/$p" "$T/repo/$p"; done
+}
+called() { grep -E -- "$1" "$T/calls" >/dev/null; } # called 'PATTERN': a stand-in was called so
+not() { ! "$@"; }
+wait_for() { # wait_for SECONDS COMMAND...: until COMMAND succeeds; fails when it has not after SECONDS
+ local n=$(($1 * 10)); shift
+ while ! "$@" >/dev/null 2>&1; do n=$((n - 1)); [ $n -gt 0 ] || return 1; sleep 0.1; done
+}
+within() { # within SECONDS COMMAND...: COMMAND's status, or 124 when it had to be stopped after SECONDS
+ local n=$(($1 * 10)) p; shift
+ "$@" & p=$!
+ while kill -0 $p 2>/dev/null; do n=$((n - 1)); [ $n -gt 0 ] || { kill $p 2>/dev/null; wait $p 2>/dev/null; return 124; }; sleep 0.1; done
+ wait $p
+}
+check() {
+ local what=$1; shift; CHECKS=$((CHECKS + 1))
+ if "$@"; then echo "ok $what"; else echo "FAIL $what"; FAILED=$((FAILED + 1)); fi
+}
+finish() { echo "$((CHECKS - FAILED)) of $CHECKS checks passed"; [ $FAILED = 0 ]; exit; }
diff --git a/tests/scripts/run.sh b/tests/scripts/run.sh
new file mode 100755
index 0000000..9711e92
--- /dev/null
+++ b/tests/scripts/run.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+# The tests of the repository's shell scripts: every tests/scripts/*.test.sh (each can be run by itself). A test runs
+# the script as it is in a sandbox of stand-in commands (lib.sh), on Linux or macOS, as any user, in a few seconds.
+cd "$(dirname "$0")" || exit 1
+s=0
+for t in *.test.sh; do echo "== $t"; /bin/bash "$t" || s=1; done
+exit $s
diff --git a/tools/dgx-spark/gpu/build-virglrenderer.sh b/tools/dgx-spark/gpu/build-virglrenderer.sh
index a1fbe3b..9f85b9e 100755
--- a/tools/dgx-spark/gpu/build-virglrenderer.sh
+++ b/tools/dgx-spark/gpu/build-virglrenderer.sh
@@ -7,8 +7,9 @@ V=1.2.0
W=${W:-$HOME/w/virglrenderer}; mkdir -p "$W"; cd "$W"
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq build-essential meson ninja-build pkg-config libdrm-dev \
libepoxy-dev libvulkan-dev libgbm-dev libegl-dev python3-yaml >/dev/null
-[ -f virglrenderer-$V.tar.gz ] || curl -sSLo virglrenderer-$V.tar.gz \
- https://gitlab.freedesktop.org/virgl/virglrenderer/-/archive/$V/virglrenderer-$V.tar.gz
+# (under another name until it is whole: an error page or a transfer cut short is not the tarball of the next run)
+[ -f virglrenderer-$V.tar.gz ] || { curl -fsSLo virglrenderer-$V.tar.gz.part \
+ https://gitlab.freedesktop.org/virgl/virglrenderer/-/archive/$V/virglrenderer-$V.tar.gz; mv virglrenderer-$V.tar.gz.part virglrenderer-$V.tar.gz; }
rm -rf src && mkdir src && tar -C src --strip-components=1 -xf virglrenderer-$V.tar.gz
# A process for each context (the default), not a thread: NVIDIA's driver gives one process about 512 open device files,
# a context with a device holds 10 to 75 of them, and past that vkCreateDevice fails (VK_ERROR_INITIALIZATION_FAILED):