Files
metal2vulkan/scripts/moltenvk-msl-helper/build
T
Anees Iqbal 13b279df1b Squash 11203 commits of translator, runtime, and validation work
Rolls up everything since 43c46ac8 into a single commit. Its tree is
identical to the tree of private master at
b3c77f2d2b20a432be10591a2129547c10b1c24b.

- A Cargo workspace of five crates under crates/: metal2vulkan (the
  translator and the metal2vulkan command), metal2vulkan-runtime (the
  Metal API on Vulkan), metal2vulkan-air (the AIR front end),
  metal2vulkan-validation and metal2vulkan-devtools.
- Dual license: GPL-2.0-or-later OR LGPL-3.0-or-later.
- docs/: API, architecture, fast paths, how-to, Metal coverage,
  refusals, runtime porting and validation guides.

Private-Commit: b3c77f2d2b20a432be10591a2129547c10b1c24b
2026-10-11 20:09:13 +03:00

76 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env bash
# Build the pinned MoltenVK MSL helper, which converts SPIR-V to MSL as MoltenVK 1.4.1 does, from a
# module and the configuration `corpus-pipeline --keep-modules` writes beside it.
#
# scripts/moltenvk-msl-helper/build # prints the installed helper's path
#
# Fetches SPIRV-Cross at the commit MoltenVK 1.4.1 links (its ExternalRevisions pin, the value of
# `metal2vulkan_devtools::MOLTENVK_SPIRV_CROSS_REVISION`), refuses a checkout at any
# other commit or with modified files, and compiles SPIRV-Cross's MSL sources with helper.cpp (the
# validation crate's crates/metal2vulkan-validation/src/msl_portability/helper.cpp).
# The binary reports its identity (`--identity`): the SPIRV-Cross commit, the SHA-256 of the
# helper.cpp it was built from, and the compiler.
#
# Everything lives under ${XDG_CACHE_HOME:-$HOME/.cache}/metal2vulkan/moltenvk-msl-helper: the
# source checkout in src/, the helper in <commit12>-<helper12>/moltenvk-msl-helper. Validation tooling only; the published crate never builds C++.
set -euo pipefail
SPIRV_CROSS_COMMIT=adec7acbf41a988713cdb85f93f26c8ca5ea863e
SPIRV_CROSS_URL=https://github.com/KhronosGroup/SPIRV-Cross.git
SOURCES=(spirv_cross.cpp spirv_parser.cpp spirv_cross_parsed_ir.cpp spirv_cfg.cpp spirv_glsl.cpp spirv_msl.cpp)
die() { echo "moltenvk-msl-helper/build: $*" >&2; exit 1; }
[ "$#" -eq 0 ] || die "takes no arguments"
[ "$(uname -s)" = Darwin ] || die "the Metal compiler the lane pairs this with needs macOS"
here=$(cd "$(dirname "$0")" && pwd)
cache=${XDG_CACHE_HOME:-$HOME/.cache}/metal2vulkan/moltenvk-msl-helper
src=$cache/src/SPIRV-Cross-$SPIRV_CROSS_COMMIT
helper=$here/../../crates/metal2vulkan-validation/src/msl_portability/helper.cpp
helper_sha=$(shasum -a 256 "$helper" | cut -d' ' -f1)
out_dir=$cache/${SPIRV_CROSS_COMMIT:0:12}-${helper_sha:0:12}
out=$out_dir/moltenvk-msl-helper
if [ ! -d "$src/.git" ]; then
mkdir -p "$cache/src"
fetch=$(mktemp -d "$cache/src/fetch.XXXXXX")
trap 'rm -rf "$fetch"' EXIT
git -C "$fetch" init -q
git -C "$fetch" fetch -q --depth 1 "$SPIRV_CROSS_URL" "$SPIRV_CROSS_COMMIT"
git -C "$fetch" -c advice.detachedHead=false checkout -q FETCH_HEAD
mv "$fetch" "$src"
trap - EXIT
fi
head=$(git -C "$src" rev-parse HEAD)
[ "$head" = "$SPIRV_CROSS_COMMIT" ] || die "$src is at $head, not the pinned $SPIRV_CROSS_COMMIT"
[ -z "$(git -C "$src" status --porcelain --untracked-files=no)" ] || die "$src has modified files"
compiler=$(xcrun clang++ --version | head -n 1)
work=$(mktemp -d "${TMPDIR:-/tmp}/moltenvk-msl-helper.XXXXXX")
trap 'rm -rf "$work"' EXIT
cat >"$work/moltenvk_msl_helper_identity.h" <<EOF
#define M2V_SPIRV_CROSS_COMMIT "$SPIRV_CROSS_COMMIT"
#define M2V_HELPER_SOURCE_SHA256 "$helper_sha"
#define M2V_HELPER_COMPILER "${compiler//\"/}"
EOF
cp "$helper" "$work/helper.cpp"
# The copy is what compiles: hash it again so an edit racing the build cannot mislabel it.
[ "$(shasum -a 256 "$work/helper.cpp" | cut -d' ' -f1)" = "$helper_sha" ] || die "helper.cpp changed during the build"
flags=(-std=c++17 -O2 -DNDEBUG -I "$src" -I "$work")
pids=()
for source in "${SOURCES[@]}"; do
xcrun clang++ "${flags[@]}" -c "$src/$source" -o "$work/${source%.cpp}.o" &
pids+=($!)
done
xcrun clang++ "${flags[@]}" -c "$work/helper.cpp" -o "$work/helper.o" &
pids+=($!)
for pid in "${pids[@]}"; do wait "$pid" || die "compilation failed"; done
xcrun clang++ "$work"/*.o -o "$work/moltenvk-msl-helper"
mkdir -p "$out_dir"
mv "$work/moltenvk-msl-helper" "$out.tmp.$$"
mv "$out.tmp.$$" "$out"
"$out" --identity >/dev/null || die "the built helper does not run"
echo "$out"