From d3741102e4760cdff7ebf9ca91510b5d8f5c96f7 Mon Sep 17 00:00:00 2001 From: Madhyn Date: Tue, 24 Nov 2026 22:43:53 -0300 Subject: [PATCH 1/2] runtime: accept prebuilt XMA2 decoder via -DLEONIDA_XMA2_DECODER_LIB (BUILD-XMA2-PREBUILT-LIB) --- docs/linux.md | 5 ++ runtime/CMakeLists.txt | 78 +++++++++++-------- runtime/cmake/xma2_decoder.cmake | 25 ++++++ runtime/xma_decoder/README.md | 5 ++ tests/test_cmake_xma2_prebuilt.py | 122 ++++++++++++++++++++++++++++++ 5 files changed, 202 insertions(+), 33 deletions(-) create mode 100644 runtime/cmake/xma2_decoder.cmake create mode 100644 tests/test_cmake_xma2_prebuilt.py diff --git a/docs/linux.md b/docs/linux.md index e7ca27455f..4660f8d539 100644 --- a/docs/linux.md +++ b/docs/linux.md @@ -83,6 +83,11 @@ Runtime workflow. The stage writes `/gta6rt.sh`, which starts `gta6rt.exe` [testing.md](testing.md#the-windows-runtime-on-linux-under-wine-or-proton) has the test runs under Wine and Proton and what fails there. +For XMA2 audio, `runtime/CMakeLists.txt` builds `runtime/xma_decoder` for `x86_64-pc-windows-msvc` when `cargo` is on +`PATH`. Distribution Rust packages typically omit the Windows MSVC standard library, which leaves XMA voices silent +unless you add the `x86_64-pc-windows-msvc` Rust target or pass a prebuilt static library with +`-DLEONIDA_XMA2_DECODER_LIB=` ([runtime/xma_decoder/README.md](../runtime/xma_decoder/README.md)). + ### Why there is no native Linux `gta6rt` A native (ELF) loader is not a matter of build flags: diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 1544671726..2eb6039f9d 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -106,41 +106,53 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" ) - # Rust XMA2 decoder (runtime/xma_decoder), built at configure time when cargo is on PATH. - # Without it (or if that build fails) the runtime still builds and XMA voices output silence. + # Rust XMA2 decoder (runtime/xma_decoder), built at configure time when cargo is on PATH, + # or supplied prebuilt via -DLEONIDA_XMA2_DECODER_LIB= (skipping cargo at configure time). + # Without it (or if the cargo build fails) the runtime still builds and XMA voices output silence. option(LEONIDA_REQUIRE_XMA2_DECODER "Fail configure if the Rust XMA2 decoder is unavailable" OFF) - find_program(LEONIDA_CARGO cargo) - if(LEONIDA_CARGO) - # Cargo's host (or configured default target) can differ from the runtime's MSVC ABI. - set(_xma_target x86_64-pc-windows-msvc) - set(_xma_target_dir ${CMAKE_BINARY_DIR}/xma_decoder) - set(_xma_lib ${_xma_target_dir}/${_xma_target}/release/leonida_xma2_decoder.lib) - set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS - ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.toml - ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.lock - ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/src/lib.rs) - execute_process( - COMMAND ${LEONIDA_CARGO} build --release --locked --target ${_xma_target} - --manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.toml - --target-dir ${_xma_target_dir} - TIMEOUT 120 - RESULT_VARIABLE _xma_result) - if(_xma_result STREQUAL "0" AND EXISTS ${_xma_lib}) - add_library(leonida_xma2_decoder STATIC IMPORTED) - # Link Rust's Windows SDK dependencies. CMake's C++ target selects its own CRT - # (/MD or /MDd); the C ABI keeps allocation and deallocation on the same side. - # synchronization: Rust's std imports WaitOnAddress and WakeByAddress*, which MSVC's - # default libraries cover but clang-cl with an xwin SDK does not (lld-link: undefined symbol). - set_target_properties(leonida_xma2_decoder PROPERTIES - IMPORTED_LOCATION ${_xma_lib} - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder - INTERFACE_COMPILE_DEFINITIONS LEONIDA_HAVE_XMA2_DECODER=1 - INTERFACE_LINK_LIBRARIES "kernel32;ntdll;userenv;ws2_32;dbghelp;synchronization") - else() - message(WARNING "cargo build of runtime/xma_decoder failed: XMA voices will be silent") - endif() + set(LEONIDA_XMA2_DECODER_LIB "" CACHE FILEPATH + "Prebuilt leonida_xma2_decoder.lib (skips running cargo at configure time)") + include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/xma2_decoder.cmake") + leonida_resolve_prebuilt_xma2_decoder("${LEONIDA_XMA2_DECODER_LIB}" "${CMAKE_BINARY_DIR}" _xma_lib_resolved) + if(_xma_lib_resolved) + message(STATUS "Using prebuilt Rust XMA2 decoder: ${_xma_lib_resolved}") else() - message(STATUS "cargo not found: runtime/xma_decoder not built, XMA voices will be silent") + find_program(LEONIDA_CARGO cargo) + if(LEONIDA_CARGO) + # Cargo's host (or configured default target) can differ from the runtime's MSVC ABI. + set(_xma_target x86_64-pc-windows-msvc) + set(_xma_target_dir ${CMAKE_BINARY_DIR}/xma_decoder) + set(_xma_lib ${_xma_target_dir}/${_xma_target}/release/leonida_xma2_decoder.lib) + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.toml + ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.lock + ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/src/lib.rs) + execute_process( + COMMAND ${LEONIDA_CARGO} build --release --locked --target ${_xma_target} + --manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.toml + --target-dir ${_xma_target_dir} + TIMEOUT 120 + RESULT_VARIABLE _xma_result) + if(_xma_result STREQUAL "0" AND EXISTS "${_xma_lib}") + set(_xma_lib_resolved "${_xma_lib}") + else() + message(WARNING "cargo build of runtime/xma_decoder failed: XMA voices will be silent") + endif() + else() + message(STATUS "cargo not found: runtime/xma_decoder not built, XMA voices will be silent") + endif() + endif() + if(_xma_lib_resolved) + add_library(leonida_xma2_decoder STATIC IMPORTED) + # Link Rust's Windows SDK dependencies. CMake's C++ target selects its own CRT + # (/MD or /MDd); the C ABI keeps allocation and deallocation on the same side. + # synchronization: Rust's std imports WaitOnAddress and WakeByAddress*, which MSVC's + # default libraries cover but clang-cl with an xwin SDK does not (lld-link: undefined symbol). + set_target_properties(leonida_xma2_decoder PROPERTIES + IMPORTED_LOCATION "${_xma_lib_resolved}" + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder" + INTERFACE_COMPILE_DEFINITIONS LEONIDA_HAVE_XMA2_DECODER=1 + INTERFACE_LINK_LIBRARIES "kernel32;ntdll;userenv;ws2_32;dbghelp;synchronization") endif() if(LEONIDA_REQUIRE_XMA2_DECODER AND NOT TARGET leonida_xma2_decoder) message(FATAL_ERROR "LEONIDA_REQUIRE_XMA2_DECODER is enabled, but the Rust XMA2 decoder is unavailable") diff --git a/runtime/cmake/xma2_decoder.cmake b/runtime/cmake/xma2_decoder.cmake new file mode 100644 index 0000000000..64f39976e9 --- /dev/null +++ b/runtime/cmake/xma2_decoder.cmake @@ -0,0 +1,25 @@ +# Resolves -DLEONIDA_XMA2_DECODER_LIB= for runtime/CMakeLists.txt. +# Kept in its own file so tests/test_cmake_xma2_prebuilt.py can run it with `cmake -P`. +# +# When LEONIDA_XMA2_DECODER_LIB is set, the runtime links that prebuilt static library directly and skips +# running cargo at configure time (useful for offline builds and Linux cross-builds where the distribution's +# rustc does not ship the x86_64-pc-windows-msvc standard library). A value that does not name an existing +# file is a configure error rather than a silent fallback to muted XMA voices. +function(leonida_resolve_prebuilt_xma2_decoder lib_input base_dir out_var) + if(NOT lib_input) + set(${out_var} "" PARENT_SCOPE) + return() + endif() + if(IS_ABSOLUTE "${lib_input}") + set(_resolved "${lib_input}") + else() + cmake_path(ABSOLUTE_PATH lib_input BASE_DIRECTORY "${base_dir}" NORMALIZE OUTPUT_VARIABLE _resolved) + endif() + if(EXISTS "${_resolved}" AND NOT IS_DIRECTORY "${_resolved}") + set(${out_var} "${_resolved}" PARENT_SCOPE) + else() + message(FATAL_ERROR + "LEONIDA_XMA2_DECODER_LIB does not point to an existing file: ${_resolved} " + "(pass the path to leonida_xma2_decoder.lib or leave it empty to build with cargo)") + endif() +endfunction() diff --git a/runtime/xma_decoder/README.md b/runtime/xma_decoder/README.md index 67341e60aa..e279057be2 100644 --- a/runtime/xma_decoder/README.md +++ b/runtime/xma_decoder/README.md @@ -26,3 +26,8 @@ The `xma2-decoder` workflow builds and runs the C++ smoke test with MSVC. It lin Without `cargo`, when that build fails, or with the cl.exe fallback of `scripts/build_runtime.ps1`, XMA voices output silence. Use `-DLEONIDA_REQUIRE_XMA2_DECODER=ON` to require the decoder in a CMake build, as the Windows runtime CI does. + +To link an already-built `leonida_xma2_decoder.lib` without invoking `cargo` during CMake configure (for offline builds +or Linux cross-builds whose distribution `rustc` omits the `x86_64-pc-windows-msvc` standard library), pass +`-DLEONIDA_XMA2_DECODER_LIB=`. A value that does not name an existing file fails configure instead of falling +back silently to muted XMA voices. diff --git a/tests/test_cmake_xma2_prebuilt.py b/tests/test_cmake_xma2_prebuilt.py new file mode 100644 index 0000000000..e042cc5e36 --- /dev/null +++ b/tests/test_cmake_xma2_prebuilt.py @@ -0,0 +1,122 @@ +"""-DLEONIDA_XMA2_DECODER_LIB= links a prebuilt Rust XMA2 decoder without running cargo. + +On Linux cross-builds, a distribution rustc package normally lacks rust-std for x86_64-pc-windows-msvc, so configure +falls back to muted XMA voices unless the user supplies a prebuilt leonida_xma2_decoder.lib or installs that target. +Passing a missing path or directory must fail configure loudly rather than silently producing muted audio. + +Runs runtime/cmake/xma2_decoder.cmake with ``cmake -P`` (no compiler or cargo needed); skipped without cmake. +""" +import re +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +MODULE = ROOT / "runtime" / "cmake" / "xma2_decoder.cmake" +CMAKE_LISTS = ROOT / "runtime" / "CMakeLists.txt" +CMAKE = shutil.which("cmake") + + +def run_resolver(lib_input, base_dir): + with tempfile.TemporaryDirectory() as tmp: + script = Path(tmp) / "driver.cmake" + script.write_text( + f'include("{MODULE.as_posix()}")\n' + 'leonida_resolve_prebuilt_xma2_decoder("${TEST_LIB}" "${TEST_BASE}" _out)\n' + 'message(STATUS "RESOLVED=${_out}")\n', + encoding="utf-8", + newline="\n", + ) + args = [ + CMAKE, + f"-DTEST_LIB={lib_input}", + f"-DTEST_BASE={Path(base_dir).as_posix()}", + "-P", + str(script), + ] + return subprocess.run(args, capture_output=True, text=True, timeout=60) + + +@unittest.skipUnless(CMAKE, "cmake not on PATH") +class PrebuiltXma2ResolverTest(unittest.TestCase): + def test_empty_value_leaves_output_empty(self): + with tempfile.TemporaryDirectory() as tmp: + r = run_resolver("", tmp) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn("-- RESOLVED=\n", r.stdout) + + def test_existing_absolute_file_resolves(self): + with tempfile.TemporaryDirectory() as tmp: + lib = Path(tmp) / "leonida_xma2_decoder.lib" + lib.write_bytes(b"!\n") + r = run_resolver(lib.as_posix(), tmp) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn(f"-- RESOLVED={lib.as_posix()}\n", r.stdout) + + def test_existing_relative_file_resolves_against_base_dir(self): + with tempfile.TemporaryDirectory() as tmp: + sub = Path(tmp) / "prebuilt" + sub.mkdir() + lib = sub / "leonida_xma2_decoder.lib" + lib.write_bytes(b"!\n") + r = run_resolver("prebuilt/leonida_xma2_decoder.lib", tmp) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn(f"-- RESOLVED={lib.as_posix()}\n", r.stdout) + + def test_existing_path_with_spaces_resolves(self): + with tempfile.TemporaryDirectory() as tmp: + sub = Path(tmp) / "xma prebuilt dir" + sub.mkdir() + lib = sub / "leonida xma2.lib" + lib.write_bytes(b"!\n") + r = run_resolver(lib.as_posix(), tmp) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn(f"-- RESOLVED={lib.as_posix()}\n", r.stdout) + + def test_missing_file_fails_configure(self): + with tempfile.TemporaryDirectory() as tmp: + missing = Path(tmp) / "does_not_exist.lib" + r = run_resolver(missing.as_posix(), tmp) + self.assertNotEqual(r.returncode, 0) + self.assertIn("LEONIDA_XMA2_DECODER_LIB", r.stderr) + self.assertIn("does_not_exist.lib", r.stderr) + + def test_directory_path_fails_configure(self): + with tempfile.TemporaryDirectory() as tmp: + folder = Path(tmp) / "not_a_file" + folder.mkdir() + r = run_resolver(folder.as_posix(), tmp) + self.assertNotEqual(r.returncode, 0) + self.assertIn("LEONIDA_XMA2_DECODER_LIB", r.stderr) + + +class CMakeListsXma2PrebuiltWiringTest(unittest.TestCase): + def test_cmakelists_resolves_prebuilt_before_cargo_and_preserves_target_contract(self): + text = CMAKE_LISTS.read_text(encoding="utf-8") + self.assertIn('set(LEONIDA_XMA2_DECODER_LIB "" CACHE FILEPATH', text) + self.assertIn('include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/xma2_decoder.cmake")', text) + self.assertIn("leonida_resolve_prebuilt_xma2_decoder(", text) + + resolve_pos = text.index("leonida_resolve_prebuilt_xma2_decoder(") + cargo_pos = text.index("find_program(LEONIDA_CARGO cargo)") + imported_pos = text.index("add_library(leonida_xma2_decoder STATIC IMPORTED)") + require_pos = text.index("if(LEONIDA_REQUIRE_XMA2_DECODER AND NOT TARGET leonida_xma2_decoder)") + + self.assertLess(resolve_pos, cargo_pos) + self.assertLess(cargo_pos, imported_pos) + self.assertLess(imported_pos, require_pos) + + block = text[resolve_pos:require_pos] + self.assertRegex( + block, + re.compile(r"if\(_xma_lib_resolved\).*?else\(\).*?find_program\(LEONIDA_CARGO cargo\)", re.S), + ) + self.assertIn('IMPORTED_LOCATION "${_xma_lib_resolved}"', block) + self.assertIn("LEONIDA_HAVE_XMA2_DECODER=1", block) + self.assertIn("synchronization", block) + + +if __name__ == "__main__": + unittest.main() From 46e078824d2b7a6ae8a8051d6adc7bf8011595cc Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Tue, 24 Nov 2026 20:59:12 -0500 Subject: [PATCH 2/2] docs: build the prebuilt XMA2 decoder yourself, from this checkout Configure checks only that the file passed with -DLEONIDA_XMA2_DECODER_LIB exists, not which source, version or target it was built from. Say so, and give the command that builds the library from this checkout's runtime/xma_decoder for x86_64-pc-windows-msvc, where it lands, that it needs only the Rust target (so it works on Linux), that the option stops CMake from rebuilding the decoder after source changes, and that a relative path given with -D is resolved against the directory CMake runs in. Co-Authored-By: Claude Opus 5.5 --- docs/linux.md | 3 ++- runtime/xma_decoder/README.md | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/linux.md b/docs/linux.md index 4660f8d539..f051ca84a7 100644 --- a/docs/linux.md +++ b/docs/linux.md @@ -86,7 +86,8 @@ and what fails there. For XMA2 audio, `runtime/CMakeLists.txt` builds `runtime/xma_decoder` for `x86_64-pc-windows-msvc` when `cargo` is on `PATH`. Distribution Rust packages typically omit the Windows MSVC standard library, which leaves XMA voices silent unless you add the `x86_64-pc-windows-msvc` Rust target or pass a prebuilt static library with -`-DLEONIDA_XMA2_DECODER_LIB=` ([runtime/xma_decoder/README.md](../runtime/xma_decoder/README.md)). +`-DLEONIDA_XMA2_DECODER_LIB=`, built yourself from this checkout's `runtime/xma_decoder` with the command in +[runtime/xma_decoder/README.md](../runtime/xma_decoder/README.md). ### Why there is no native Linux `gta6rt` diff --git a/runtime/xma_decoder/README.md b/runtime/xma_decoder/README.md index e279057be2..f02bdcb0cb 100644 --- a/runtime/xma_decoder/README.md +++ b/runtime/xma_decoder/README.md @@ -31,3 +31,19 @@ To link an already-built `leonida_xma2_decoder.lib` without invoking `cargo` dur or Linux cross-builds whose distribution `rustc` omits the `x86_64-pc-windows-msvc` standard library), pass `-DLEONIDA_XMA2_DECODER_LIB=`. A value that does not name an existing file fails configure instead of falling back silently to muted XMA voices. + +Build that library yourself, from this checkout's `runtime/xma_decoder`, so that it matches the C ABI the runtime is +compiled against. Configure checks only that the file exists, not which source, version or target it was built from, +so don't use a library from another checkout, another project or a download: + +```sh +rustup target add x86_64-pc-windows-msvc +cargo build --manifest-path runtime/xma_decoder/Cargo.toml --release --locked --target x86_64-pc-windows-msvc +``` + +The library is then `runtime/xma_decoder/target/x86_64-pc-windows-msvc/release/leonida_xma2_decoder.lib`. Building a +static library for that target needs only the Rust target, not the MSVC linker, so this also works on Linux. + +With the option set, CMake no longer builds the decoder: after a change under `runtime/xma_decoder/`, run the command +again before building the runtime. A relative path given with `-D` on the command line is made absolute against the +directory CMake runs in, so passing an absolute path avoids surprises.