Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Pick a C++ compiler that has <format>

alan.kungf9dd4f6db091
diff --git a/scripts/cxx.sh b/scripts/cxx.sh
new file mode 100644
index 0000000..2286bdb
--- /dev/null
+++ b/scripts/cxx.sh
@@ -0,0 +1,24 @@
+# Sourced by setup.sh and port.sh: sets cxx_flags to the CMake arguments for a
+# C++ compiler that has <format>. $CXX wins when it works; otherwise the newest
+# clang++ (with its own libc++ if the system libstdc++ predates GCC 13), then g++.
+cxx_flags=()
+cxx_probe=$(mktemp -d)
+printf '#include <format>\nint main() { return std::format("{}", 1).size() != 1; }\n' > "$cxx_probe/t.cpp"
+cxx_candidates=()
+[ -n "${CXX:-}" ] && cxx_candidates+=("$CXX")
+cxx_candidates+=($(compgen -c clang++- | grep -E '^clang\+\+-[0-9]+$' | sort -t- -k2 -rn | uniq) clang++ g++)
+for cxx in "${cxx_candidates[@]}"; do
+ command -v "$cxx" > /dev/null || continue
+ for stdlib in "" -stdlib=libc++; do
+ if "$cxx" -std=c++20 $stdlib "$cxx_probe/t.cpp" -o "$cxx_probe/t" 2> /dev/null; then
+ cxx_flags=(-DCMAKE_CXX_COMPILER="$(command -v "$cxx")")
+ [ -z "$stdlib" ] || cxx_flags+=(-DCMAKE_CXX_FLAGS="$stdlib")
+ break 2
+ fi
+ done
+done
+rm -rf "$cxx_probe"
+if [ ${#cxx_flags[@]} -eq 0 ]; then
+ echo "no C++ compiler here has <format>: install clang 17+ with libc++ (e.g. clang-19 libc++-19-dev) or GCC 13+, or point CXX at one" >&2
+ exit 1
+fi
diff --git a/scripts/port.sh b/scripts/port.sh
index 19920eb..161d3bb 100755
--- a/scripts/port.sh
+++ b/scripts/port.sh
@@ -106,9 +106,8 @@ translate
build_native() {
if [ ! -f "$native/build.ninja" ]; then
- compilers=()
- if command -v clang++ > /dev/null; then compilers=(-DCMAKE_CXX_COMPILER=clang++); fi
- cmake -S "$ROOT" -B "$native" -G Ninja -DCMAKE_BUILD_TYPE=Release "${compilers[@]}" -DPS2_GENERATED="$generated" > /dev/null
+ . "$here/cxx.sh"
+ cmake -S "$ROOT" -B "$native" -G Ninja -DCMAKE_BUILD_TYPE=Release "${cxx_flags[@]}" -DPS2_GENERATED="$generated" > /dev/null
fi
ninja -C "$native" ps2run > "$native/ninja.log" 2>&1 || { tail -30 "$native/ninja.log"; exit 1; }
}
diff --git a/scripts/setup.sh b/scripts/setup.sh
index f467308..b2221d1 100755
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -19,8 +19,7 @@ if [ ! -f "$ROOT/third_party/wgpu-native/lib/libwgpu_native.a" ]; then
"$ROOT/scripts/fetch_wgpu.sh"
fi
-compilers=()
-if command -v clang++ > /dev/null; then compilers=(-DCMAKE_CXX_COMPILER=clang++); fi
-cmake -S "$ROOT" -B "$ROOT/build/tools" -G Ninja -DCMAKE_BUILD_TYPE=Release "${compilers[@]}" > /dev/null
+. "$ROOT/scripts/cxx.sh"
+cmake -S "$ROOT" -B "$ROOT/build/tools" -G Ninja -DCMAKE_BUILD_TYPE=Release "${cxx_flags[@]}" > /dev/null
ninja -C "$ROOT/build/tools" eerecomp vurecomp
echo "== ready"