#!/bin/sh
# c8-xr: the OpenXR side of cont8nuum's XR, built into a prefix:
# - the OpenXR loader (Khronos' OpenXR-SDK-Source, not packaged for
# Tumbleweed), with hello_xr kept in the build for tests
# - Monado, the OpenXR runtime: our fork (monado/, a submodule on
# its branch c8-xreal-one-pro-plus), whose commits add cont8nuum's
# driver for the XREAL glasses (driver/) and change its compositor
# - xrizer, OpenVR on OpenXR, so SteamVR's games run on Monado without
# SteamVR (not packaged for Tumbleweed; built with cargo)
#
# xr/build.sh PREFIX [BUILDDIR-SUFFIX]
#
# Run by the top build.sh with --xr. All three are git submodules
# (openxr-sdk/, xrizer/: upstream's, at the tags they pin; monado/:
# ours), checked out here if they aren't yet.
# Optional: the desktop builds and runs without any of this.
set -eu
here=$(cd "$(dirname "$0")" && pwd)
top=$(cd "$here/.." && pwd)
prefix=$1
suffix=${2:-}
sudo=${C8_SUDO:-}
if [ ! -f "$here/openxr-sdk/CMakeLists.txt" ] ||
[ ! -f "$here/xrizer/Cargo.toml" ] ||
[ ! -f "$here/monado/CMakeLists.txt" ]; then
git -C "$top" submodule update --init --depth 1 -- xr
fi
monado="$here/monado"
if [ ! -f "$monado/CMakeLists.txt" ]; then
echo "c8-xr: no Monado in xr/monado (git submodule update --init -- xr)" >&2
exit 1
fi
# A build dir made from the old clone (subprojects-src/, before the fork
# was a submodule): cmake won't change a cache's source dir
cache="$here/build-monado$suffix/CMakeCache.txt"
if [ -f "$cache" ] && ! grep -q "^CMAKE_HOME_DIRECTORY:INTERNAL=$monado\$" "$cache"; then
rm -rf "$here/build-monado$suffix"
fi
# The SDK's Wayland parts include wayland-client.h by its bare name
flags="$(pkg-config --cflags wayland-client wayland-egl egl)"
type=${C8_BUILDTYPE:-debugoptimized}
case "$type" in
debug) ctype=Debug ;;
*) ctype=RelWithDebInfo ;;
esac
echo "==> c8-xr: OpenXR loader"
cmake -S "$here/openxr-sdk" -B "$here/build-openxr$suffix" -G Ninja \
-DCMAKE_BUILD_TYPE=$ctype -DCMAKE_INSTALL_PREFIX="$prefix" \
-DCMAKE_C_FLAGS="$flags" -DCMAKE_CXX_FLAGS="$flags" \
-DBUILD_TESTS=ON -DBUILD_API_LAYERS=OFF -DDYNAMIC_LOADER=ON \
-DPRESENTATION_BACKEND=wayland >/dev/null
ninja -C "$here/build-openxr$suffix"
$sudo ninja -C "$here/build-openxr$suffix" install >/dev/null
echo "==> c8-xr: Monado"
cmake -S "$monado" -B "$here/build-monado$suffix" -G Ninja \
-DCMAKE_BUILD_TYPE=$ctype -DCMAKE_INSTALL_PREFIX="$prefix" \
-DC8_GLASSES_DIR="$here/driver" -DC8_SRC_DIR="$top/compositor/src" \
-DXRT_FEATURE_SERVICE=ON -DXRT_BUILD_SAMPLES=OFF >/dev/null
# c8's overlay draws with GLES: without it Monado still builds, quietly,
# and c8 can't join it (Mesa-libGLESv3-devel has the headers it wants)
if ! grep -q '^XRT_HAVE_OPENGLES:BOOL=ON' "$here/build-monado$suffix/CMakeCache.txt"; then
echo "c8-xr: Monado found no OpenGL ES 3 headers (GLES3/gl3.h)" >&2
exit 1
fi
ninja -C "$here/build-monado$suffix"
$sudo ninja -C "$here/build-monado$suffix" install >/dev/null
# xrizer: its own `cargo xbuild` (xbuild/ in its tree) builds the library
# and lays it out as an OpenVR runtime, bin/linux64/vrclient.so (a
# symlink to it: copied as a file here) and bin/version.txt. Steam runs
# games in a container that sees the home folder but /usr only as
# /run/host/usr (xrizer's README), so with a prefix outside home it goes
# to ~/.local/share/xrizer, where c8 looks first (xrruntime.c), as the
# user: no sudo
echo "==> c8-xr: xrizer"
(cd "$here/xrizer" && CARGO_TARGET_DIR="$here/build-xrizer$suffix" \
cargo xbuild --release)
case "$prefix" in
"$HOME"/*) dest="$prefix/share/xrizer" ;;
*) dest="${XDG_DATA_HOME:-$HOME/.local/share}/xrizer" ;;
esac
built="$here/build-xrizer$suffix/release"
mkdir -p "$dest.new/bin/linux64"
cp -L "$built/bin/linux64/vrclient.so" "$dest.new/bin/linux64/"
cp "$built/bin/version.txt" "$dest.new/bin/"
rm -rf "$dest"
mv "$dest.new" "$dest"
echo "c8-xr: xrizer in $dest"