git clone https://garage44.eu/jeroen/pinnacle.git.. /tools/macos-venus/build.sh
#!/bin/sh
# Builds, into ~/opt/venus on a Mac, a QEMU whose 3D virtio-gpu runs the
# guest's Vulkan (Venus) on the Mac's GPU through MoltenVK — with no Homebrew
# write access, for a build node's user. `cargo xtask` uses it when it is there.
#
# Needs Xcode's command-line tools, cmake, pkg-config, a Python 3.11+ at
# /opt/homebrew/bin/python3, and Homebrew's QEMU dependencies (glib, pixman,
# libslirp, capstone, dtc, zstd, libpng, jpeg-turbo). Everything else is
# fetched at the versions below. These are host tools: none of it is in
# Pinnacle.
set -eu
P=$HOME/opt/venus
S=$HOME/opt/venus-src
HERE=$(cd "$(dirname "$0")" && pwd)
export PATH=$HOME/Library/Python/3.9/bin:/usr/bin:/bin:/opt/homebrew/bin
export PKG_CONFIG_PATH=$P/lib/pkgconfig:$P/share/pkgconfig:/opt/homebrew/lib/pkgconfig
mkdir -p "$P/lib/pkgconfig" "$S"
cd "$S"
step() { echo "== $*"; }
# A repository at one commit, shallow.
fetch() { # dir url commit
[ -d "$1" ] && return
git init -q "$1"
git -C "$1" fetch -q --depth 1 "$2" "$3"
git -C "$1" checkout -q FETCH_HEAD
}
# Applies a patch to a tree once: one already in (it reverses cleanly) is left,
# so a Mac built before a patch was added gets it on the next run.
apply() { # dir patch
patch -d "$1" -p1 -R -s -f --dry-run < "$2" >/dev/null 2>&1 && return
patch -d "$1" -p1 -s < "$2"
}
step python tools
/usr/bin/python3 -m pip install --user -q meson ninja mako pyyaml
step vulkan headers and loader
fetch Vulkan-Headers https://github.com/KhronosGroup/Vulkan-Headers d277a0a3cda9c682708c93279f0c72cd6b0ae3f3
cmake -S Vulkan-Headers -B Vulkan-Headers/build -DCMAKE_INSTALL_PREFIX="$P" >/dev/null
cmake --install Vulkan-Headers/build >/dev/null
fetch Vulkan-Loader https://github.com/KhronosGroup/Vulkan-Loader 7d76ff3c3eac272ed37f606e13915b3bd288c30b
cmake -S Vulkan-Loader -B Vulkan-Loader/build -DCMAKE_INSTALL_PREFIX="$P" -DCMAKE_BUILD_TYPE=Release \
-DVULKAN_HEADERS_INSTALL_DIR="$P" -DCMAKE_PREFIX_PATH="$P" >/dev/null
cmake --build Vulkan-Loader/build -j8 >/dev/null
cmake --install Vulkan-Loader/build >/dev/null
step moltenvk
if [ ! -f "$P/lib/libMoltenVK.dylib" ]; then
curl -sfL https://github.com/KhronosGroup/MoltenVK/releases/download/v1.4.2/MoltenVK-macos.tar | tar x
cp MoltenVK/MoltenVK/dynamic/dylib/macOS/libMoltenVK.dylib "$P/lib/"
mkdir -p "$P/share/vulkan/icd.d"
sed 's#"library_path": *"[^"]*"#"library_path": "'"$P"'/lib/libMoltenVK.dylib"#' \
MoltenVK/MoltenVK/dynamic/dylib/macOS/MoltenVK_icd.json > "$P/share/vulkan/icd.d/MoltenVK_icd.json"
fi
# QEMU's OpenGL support (which its 3D device is built with) wants epoxy with
# EGL; epoxy loads EGL at run time, so the headers are enough. Nothing loads
# it here: Venus runs without a GL display.
step egl headers and epoxy
fetch EGL-Registry https://github.com/KhronosGroup/EGL-Registry aa39a34729e6d20b0b982e279c351976a5c91075
mkdir -p "$P/include" && cp -R EGL-Registry/api/EGL EGL-Registry/api/KHR "$P/include/"
cat > "$P/lib/pkgconfig/egl.pc" <<PC
prefix=$P
Name: egl
Description: Khronos EGL headers only
Version: 1.5
Cflags: -I\${prefix}/include
Libs:
PC
fetch libepoxy https://github.com/anholt/libepoxy d1f952c4565c389d82fd1657153e84e050f35b27
meson setup --reconfigure libepoxy/build libepoxy --prefix="$P" -Degl=yes -Dglx=no -Dx11=false -Dtests=false >/dev/null
ninja -C libepoxy/build install >/dev/null
# Venus only (no OpenGL renderer), its render server a thread of QEMU's.
step virglrenderer
fetch virglrenderer https://gitlab.freedesktop.org/virgl/virglrenderer 8f6c5eebee094f64ef1a126e5072481a414e6a9c
# Shared memory imported from an offset into its file: Pinnacle's guest
# blobs, its RAM's pages made the GPU's memory (qemu-guest-blobs-from-shm).
apply virglrenderer "$HERE/virglrenderer-shm-offset.patch"
meson setup --reconfigure virglrenderer/build virglrenderer --prefix="$P" \
-Dvenus=true -Dvrend=false -Drender-server-mode=thread -Dtests=false >/dev/null
ninja -C virglrenderer/build install >/dev/null
# QEMU 11.1.3, patched to allow Venus with no GL display. The Apple
# paravirtual GPU is left out: its framework is not in a macOS VM's SDK.
step qemu
if [ ! -d qemu-11.1.3 ]; then
curl -sfL https://download.qemu.org/qemu-11.1.3.tar.xz | tar xJ
fi
apply qemu-11.1.3 "$HERE/qemu-venus-without-gl.patch"
# A guest blob in one piece of shared-memory RAM is the GPU's memory
# without the hostmem window (256 MiB at most below 4 GiB).
apply qemu-11.1.3 "$HERE/qemu-guest-blobs-from-shm.patch"
mkdir -p qemu-11.1.3/build
cd qemu-11.1.3/build
../configure --python=/opt/homebrew/bin/python3 --prefix="$P" --target-list=aarch64-softmmu \
--enable-opengl --enable-virglrenderer --disable-pvg --disable-docs --disable-werror \
--extra-cflags=-DEGL_NO_PLATFORM_SPECIFIC_TYPES >/dev/null
make -j8 >/dev/null
make install >/dev/null
"$P/bin/qemu-system-aarch64" -device help | grep -q virtio-gpu-gl-pci
echo "== done: $P/bin/qemu-system-aarch64"