diff --git a/ci/test_tier2_macos_tail.py b/ci/test_tier2_macos_tail.py new file mode 100644 index 0000000000..077e844017 --- /dev/null +++ b/ci/test_tier2_macos_tail.py @@ -0,0 +1,129 @@ +#!/usr/bin/env -S uv run --script +"""macOS Tier 2 overlaps preparation, preserves every result and joins children. + + uv run python -m unittest ci/test_tier2_macos_tail.py +""" + +from __future__ import annotations + +import contextlib +import importlib.util +import io +import subprocess +import sys +import tempfile +import threading +import unittest +from pathlib import Path +from unittest import mock + +spec = importlib.util.spec_from_file_location("tier2_macos_tail", Path(__file__).with_name("tier2_macos_tail.py")) +tail = importlib.util.module_from_spec(spec) +spec.loader.exec_module(tail) + + +class MacosTailTest(unittest.TestCase): + def test_phase_reentry_selects_cmake_framework_among_native_outputs(self): + validator = Path(__file__).resolve().parent.parent / "rust/tools/tier2-validate/validate.sh" + discovery = next(line for line in validator.read_text().splitlines() if line.startswith("rust_lib=")) + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + expected = root / "libobs/RelWithDebInfo/libobs.framework/Versions/A/libobs" + for path in (root / "cargo-native-core/obs/libobs.framework/Versions/A/libobs", + root / "libobs-c-reference/RelWithDebInfo/libobs.framework/Versions/A/libobs", expected): + path.parent.mkdir(parents=True) + path.touch() + result = subprocess.run( + ["bash", "-c", 'set -eu\nbuild="$1"\n' + discovery + '\nprintf "%s" "$rust_lib"', "bash", str(root)], + capture_output=True, text=True, check=True, + ) + self.assertEqual(result.stdout, str(expected)) + + def run_fake(self, root: Path, failing: str = "") -> tuple[int, str]: + validator = root / "validate.sh" + validator.write_text( + '#!/bin/bash\nset -eu\nphase="$2"\n' + 'printf "%s\\n" "$@" > "$0.$phase.args"\n' + 'echo "stdout $phase"\necho "stderr $phase" >&2\n' + 'touch "$0.$phase.done"\n' + f'if [ "$phase" = "{failing}" ]; then exit 23; fi\n', + encoding="utf-8", + ) + output = io.StringIO() + with contextlib.redirect_stdout(output): + status = tail.run_tail(validator, ["--repeat", "2", "--tests", "with spaces"], root / "logs") + return status, output.getvalue() + + def test_success_keeps_logs_and_forwards_arguments(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + status, output = self.run_fake(root) + self.assertEqual(status, 0) + for phase in ("cmake-tests", "cargo-build", "cargo-tests"): + self.assertTrue((root / f"validate.sh.{phase}.done").is_file()) + self.assertEqual( + (root / f"validate.sh.{phase}.args").read_text().splitlines(), + ["--phase", phase, "--repeat", "2", "--tests", "with spaces"], + ) + self.assertIn(f"stdout {phase}", output) + self.assertIn(f"stderr {phase}", (root / "logs" / f"{phase}.log").read_text()) + + def test_any_failure_fails_without_abandoning_independent_work(self): + for failing in ("cmake-tests", "cargo-build", "cargo-tests"): + with self.subTest(failing=failing), tempfile.TemporaryDirectory() as directory: + root = Path(directory) + status, output = self.run_fake(root, failing) + self.assertNotEqual(status, 0) + self.assertIn(f"{failing}: exit 23", output) + for independent in ("cmake-tests", "cargo-build"): + self.assertTrue((root / f"validate.sh.{independent}.done").is_file()) + # Native suites require successful preparation; a failed CMake + # suite must still allow them to run and report their result. + self.assertEqual((root / "validate.sh.cargo-tests.done").exists(), failing != "cargo-build") + + def test_overlap_and_barrier_before_native_suites(self): + barrier = threading.Barrier(2, timeout=5) + finished: set[str] = set() + + def phase(name, command, log_dir): + if name == "cargo-tests": + self.assertEqual(finished, {"cmake-tests", "cargo-build"}) + else: + barrier.wait() + finished.add(name) + return 0 + + with tempfile.TemporaryDirectory() as directory, mock.patch.object(tail, "run_phase", side_effect=phase): + with mock.patch.object(tail, "show_log"): + self.assertEqual(tail.run_tail(Path("validate.sh"), [], Path(directory)), 0) + + def test_launch_error_still_runs_other_independent_phase(self): + def launch(command, **kwargs): + if "cargo-build" in command: + raise OSError("launch failed") + return mock.Mock(returncode=0) + + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + with mock.patch.object(tail.subprocess, "run", side_effect=launch) as run: + with contextlib.redirect_stdout(io.StringIO()) as output: + self.assertNotEqual(tail.run_tail(root / "validate.sh", [], root), 0) + self.assertEqual(run.call_count, 2) + self.assertIn("launch failed", output.getvalue()) + + def test_cli_propagates_failure_after_all_runnable_phases(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + self.run_fake(root, "cmake-tests") + result = subprocess.run( + [sys.executable, str(Path(tail.__file__)), "--validator", str(root / "validate.sh"), + "--log-dir", str(root / "cli-logs")], + capture_output=True, text=True, timeout=20, + ) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(len(list((root / "cli-logs").glob("*.log"))), 3) + self.assertIn("cargo-tests: exit 0", result.stdout) + + +if __name__ == "__main__": + unittest.main() diff --git a/ci/test_tier2_native_target.py b/ci/test_tier2_native_target.py index 4e924a3bb1..bc6043b13e 100644 --- a/ci/test_tier2_native_target.py +++ b/ci/test_tier2_native_target.py @@ -84,7 +84,7 @@ def test_validators_request_one_complete_build(self) -> None: with self.subTest(script=script): text = (HELPER.parent / script).read_text(encoding="utf-8") self.assertIn("-DOBS_BUILD_NATIVE_TIER2=ON", text) - command = re.search(r"^cmake --build .*?(?=\n[^ \t]|\Z)", text, re.M | re.S) + command = re.search(r"^[ \t]*cmake --build .*?(?=\n[^ \t]|\Z)", text, re.M | re.S) self.assertIsNotNone(command) self.assertRegex(command.group(0), r"--target tier2-native\s*(?:[\\>`\n]|$)") root = (ROOT / "CMakeLists.txt").read_text(encoding="utf-8") diff --git a/ci/tier2_macos_tail.py b/ci/tier2_macos_tail.py new file mode 100644 index 0000000000..9bffe0f9cb --- /dev/null +++ b/ci/tier2_macos_tail.py @@ -0,0 +1,79 @@ +#!/usr/bin/env -S uv run --script +"""Overlap independent macOS Tier 2 work on one runner (#5282). + +CTest's four passes share WorkingDirectory, fixtures, timing locks and +Testing/Temporary. Keep them serial. Cargo preparation only reads CMake's +artifacts and writes cargo-native-{rust,tests,core}, so it can overlap the +first two passes. Its Cargo invocations retain their existing target dirs +and run sequentially; no Cargo build lock or duplicate compilation is added. + +#5009 can reuse this boundary with its cross-build preparation command. +""" + +from __future__ import annotations + +import argparse +import shutil +import subprocess +import sys +import time +from concurrent.futures import ThreadPoolExecutor, as_completed +from pathlib import Path + + +def run_phase(name: str, command: list[str], log_dir: Path) -> int: + with (log_dir / f"{name}.log").open("w", encoding="utf-8") as log: + start = time.monotonic() + try: + status = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT, check=False).returncode + except OSError as error: + print(f"Unable to launch {name}: {error}", file=log) + status = 127 + print(f"\n{name}: exit {status}, elapsed {time.monotonic() - start:.1f}s", file=log) + return status + + +def show_log(name: str, log_dir: Path) -> None: + print(f"::group::macOS Tier 2 {name}", flush=True) + with (log_dir / f"{name}.log").open(encoding="utf-8", errors="replace") as log: + shutil.copyfileobj(log, sys.stdout) + print("::endgroup::", flush=True) + + +def run_tail(validator: Path, arguments: list[str], log_dir: Path) -> int: + log_dir.mkdir(parents=True, exist_ok=True) + results = {} + + def command(name: str) -> list[str]: + return ["bash", str(validator), "--phase", name, *arguments] + + # Executor shutdown joins every child even when an independent phase fails. + # Only the main thread prints completed logs, so groups cannot interleave. + with ThreadPoolExecutor(max_workers=2) as pool: + pending = {pool.submit(run_phase, name, command(name), log_dir): name + for name in ("cmake-tests", "cargo-build")} + for future in as_completed(pending): + name = pending[future] + results[name] = future.result() + show_log(name, log_dir) + + if results["cargo-build"] == 0: + results["cargo-tests"] = run_phase("cargo-tests", command("cargo-tests"), log_dir) + show_log("cargo-tests", log_dir) + else: + print("Skipping cargo-tests: cargo-build failed", flush=True) + return int(any(status != 0 for status in results.values())) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--validator", required=True, type=Path) + parser.add_argument("--log-dir", required=True, type=Path) + parser.add_argument("--repeat", default="0") + parser.add_argument("--tests", default="") + args = parser.parse_args() + return run_tail(args.validator, ["--repeat", args.repeat, "--tests", args.tests], args.log_dir) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/rust/tools/tier2-validate/validate.sh b/rust/tools/tier2-validate/validate.sh index a77eb3d3b7..7de5642ae3 100644 --- a/rust/tools/tier2-validate/validate.sh +++ b/rust/tools/tier2-validate/validate.sh @@ -43,8 +43,15 @@ show_crash_stacks() { repeat=0 tests= +phase= while [ $# -gt 0 ]; do case "$1" in + --phase) + # Internal entry points used by ci/tier2_macos_tail.py after the build. + phase="${2:-}" + case "$phase" in cmake-tests|cargo-build|cargo-tests) ;; *) exit 2 ;; esac + shift 2 + ;; --repeat) repeat="${2:-}" shift 2 || { echo "--repeat needs a count" >&2; exit 2; } @@ -74,38 +81,43 @@ cd "$(dirname "$0")/../../.." export XCODE_CAS_PATH="${XCODE_CAS_PATH:-$PWD/build_rust_xcode_cas}" mkdir -p "$XCODE_CAS_PATH" -cmocka_prefix="$(brew --prefix cmocka)" - # OBS_CTEST_JOBS: ctest parallelism for each suite (default 1); CI sets it # (#2138). Tests that must not overlap carry RUN_SERIAL or RESOURCE_LOCK. ctest_jobs=(-j "${OBS_CTEST_JOBS:-1}") build="build_rust" -echo "== configure (libobs and libobs-c-reference)" -cmake --preset macos-ci -B "$build" \ - -DENABLE_UNIT_TESTS=ON -DOBS_BUILD_C_REFERENCE=ON -DOBS_LIBOBS_BACKEND=HYBRID \ - -DOBS_BUILD_NATIVE_TIER2=ON \ - -DENABLE_FRONTEND=OFF -DENABLE_SCRIPTING=OFF -DENABLE_BROWSER=OFF \ - -DENABLE_WEBSOCKET=OFF \ - -DCMAKE_PREFIX_PATH="$cmocka_prefix" \ - >"$build.configure.log" 2>&1 || { tail -40 "$build.configure.log"; exit 1; } -echo "== build (complete native Tier 2 graph)" -cmake --build "$build" --config RelWithDebInfo --target tier2-native \ - >"$build.build.log" 2>&1 || { grep -E ": (fatal )?error|BUILD FAILED" "$build.build.log" | sort -u | head -40; exit 1; } +if [ -z "$phase" ]; then + cmocka_prefix="$(brew --prefix cmocka)" + echo "== configure (libobs and libobs-c-reference)" + cmake --preset macos-ci -B "$build" \ + -DENABLE_UNIT_TESTS=ON -DOBS_BUILD_C_REFERENCE=ON -DOBS_LIBOBS_BACKEND=HYBRID \ + -DOBS_BUILD_NATIVE_TIER2=ON \ + -DENABLE_FRONTEND=OFF -DENABLE_SCRIPTING=OFF -DENABLE_BROWSER=OFF \ + -DENABLE_WEBSOCKET=OFF \ + -DCMAKE_PREFIX_PATH="$cmocka_prefix" \ + >"$build.configure.log" 2>&1 || { tail -40 "$build.configure.log"; exit 1; } + echo "== build (complete native Tier 2 graph)" + cmake --build "$build" --config RelWithDebInfo --target tier2-native \ + >"$build.build.log" 2>&1 || { grep -E ": (fatal )?error|BUILD FAILED" "$build.build.log" | sort -u | head -40; exit 1; } +fi # libobs.framework twice: libobs (C, what the tests link) and its Rust twin # in $build/libobs-c-reference (#1065), same install name. c_lib=$(find "$build/libobs-c-reference" -path '*libobs.framework*' -name libobs -type f | head -1) -rust_lib=$(find "$build" -path "$build/libobs-c-reference" -prune -o -path '*libobs.framework*' -name libobs -type f -print | head -1) +# Phase reentry must select CMake's hybrid, never a Cargo framework left by +# this run or a previous one under cargo-native-core. +rust_lib=$(find "$build/libobs" -path '*libobs.framework*' -name libobs -type f | head -1) if [ -z "$c_lib" ] || [ -z "$rust_lib" ]; then echo "FAIL: libobs.framework not found (C: '${c_lib}', with Rust: '${rust_lib}') in $build" exit 1 fi -nm -gU "$c_lib" | awk '{print $3}' | sort >exports-OFF.txt -nm -gU "$rust_lib" | awk '{print $3}' | sort >exports-ON.txt -echo "== libobs exported symbols, C vs Rust" -diff exports-OFF.txt exports-ON.txt -echo "IDENTICAL ($(wc -l exports-OFF.txt + nm -gU "$rust_lib" | awk '{print $3}' | sort >exports-ON.txt + echo "== libobs exported symbols, C vs Rust" + diff exports-OFF.txt exports-ON.txt + echo "IDENTICAL ($(wc -l &1 | grep -qF "$framework_dir/libobs.framework/"; then - echo "FAIL: [$mode] tests did not load libobs from $framework_dir" - exit 1 - fi - echo "== [$mode] tests load $framework_dir/libobs.framework" +if [ "$phase" = cmake-tests ]; then + for mode in OFF ON; do + if [ "$mode" = ON ]; then lib=$rust_lib; else lib=$c_lib; fi + # The test executables load @rpath/libobs.framework, and their build rpath + # (@executable_path/../Frameworks) only holds it in an app bundle, so point + # dyld at the framework's directory (absolute: ctest runs each test from + # its own directory). That also picks which libobs the tests run against. + framework_dir="$PWD/${lib%%/libobs.framework/*}" + dyld=(env DYLD_FRAMEWORK_PATH="$framework_dir" DYLD_LIBRARY_PATH="$(dirname "$avcodec")") - echo "== [$mode] ctest" - if ! "${dyld[@]}" ctest --test-dir "$build" -C RelWithDebInfo --output-on-failure "${ctest_jobs[@]}"; then - DYLD_FRAMEWORK_PATH="$framework_dir" DYLD_LIBRARY_PATH="$(dirname "$avcodec")" \ - show_crash_stacks "$build" - exit 1 - fi + # Check the loader really took this libobs on one test binary. + if ! "${dyld[@]}" DYLD_PRINT_LIBRARIES=1 "$probe" 2>&1 | grep -qF "$framework_dir/libobs.framework/"; then + echo "FAIL: [$mode] tests did not load libobs from $framework_dir" + exit 1 + fi + echo "== [$mode] tests load $framework_dir/libobs.framework" - if [ "$repeat" -gt 0 ]; then - echo "== [$mode] ctest --repeat until-fail:$repeat${tests:+ -R $tests}" - "${dyld[@]}" ctest --test-dir "$build" -C RelWithDebInfo --output-on-failure \ - --repeat "until-fail:$repeat" ${tests:+-R "$tests"} - fi -done + echo "== [$mode] ctest" + if ! "${dyld[@]}" ctest --test-dir "$build" -C RelWithDebInfo --output-on-failure "${ctest_jobs[@]}"; then + DYLD_FRAMEWORK_PATH="$framework_dir" DYLD_LIBRARY_PATH="$(dirname "$avcodec")" \ + show_crash_stacks "$build" + exit 1 + fi + + if [ "$repeat" -gt 0 ]; then + echo "== [$mode] ctest --repeat until-fail:$repeat${tests:+ -R $tests}" + "${dyld[@]}" ctest --test-dir "$build" -C RelWithDebInfo --output-on-failure \ + --repeat "until-fail:$repeat" ${tests:+-R "$tests"} + fi + done + exit 0 +fi # Exercise the Cargo native graph on the same macOS machine and dependency # bundle as the reference. Build the Rust archive outside build.rs, into an @@ -177,28 +201,31 @@ capture_reference() { reference_capture=$(capture_reference mac-capture mac-capture) reference_camera=$(capture_reference mac-avcapture mac-avcapture) reference_legacy_camera=$(capture_reference mac-avcapture-legacy mac-avcapture) -soldr cargo build -p libobs-rust --release --target "$native_target" --target-dir "$native_rust" -OBS_BUILD_MACOS_DEPENDENCIES="$(dirname "$(dirname "$avcodec")")" \ - OBS_BUILD_MACOS_TARGET="$native_target" \ - OBS_BUILD_MACOS_OUTPUT="$native_output" \ - OBS_BUILD_MACOS_RUST_ARCHIVE="$native_rust/$native_target/release/liblibobs_rust.a" \ - OBS_BUILD_MACOS_REFERENCE="$PWD/$c_lib" \ - OBS_BUILD_MACOS_GRAPHICS=1 \ - OBS_BUILD_MACOS_OPENGL_REFERENCE="$reference_opengl" \ - OBS_BUILD_MACOS_METAL_REFERENCE="$reference_metal" \ - OBS_BUILD_MACOS_CAPTURE=1 \ - OBS_BUILD_MACOS_MAC_CAPTURE_REFERENCE="$reference_capture" \ - OBS_BUILD_MACOS_MAC_AVCAPTURE_REFERENCE="$reference_camera" \ - OBS_BUILD_MACOS_MAC_AVCAPTURE_LEGACY_REFERENCE="$reference_legacy_camera" \ - SWIFTC="$(xcrun --find swiftc)" \ - SDKROOT="$(xcrun --sdk macosx --show-sdk-path)" \ - soldr cargo test -p obs-studio-rust --features libobs-integration \ - --target "$native_target" --target-dir "$native_tests" \ - --test obs-tests-app native_libobs::macos_core_builds_from_the_canonical_manifest -- --exact +if [ "$phase" = cargo-build ]; then + soldr cargo build -p libobs-rust --release --target "$native_target" --target-dir "$native_rust" + OBS_BUILD_MACOS_DEPENDENCIES="$(dirname "$(dirname "$avcodec")")" \ + OBS_BUILD_MACOS_TARGET="$native_target" \ + OBS_BUILD_MACOS_OUTPUT="$native_output" \ + OBS_BUILD_MACOS_RUST_ARCHIVE="$native_rust/$native_target/release/liblibobs_rust.a" \ + OBS_BUILD_MACOS_REFERENCE="$PWD/$c_lib" \ + OBS_BUILD_MACOS_GRAPHICS=1 \ + OBS_BUILD_MACOS_OPENGL_REFERENCE="$reference_opengl" \ + OBS_BUILD_MACOS_METAL_REFERENCE="$reference_metal" \ + OBS_BUILD_MACOS_CAPTURE=1 \ + OBS_BUILD_MACOS_MAC_CAPTURE_REFERENCE="$reference_capture" \ + OBS_BUILD_MACOS_MAC_AVCAPTURE_REFERENCE="$reference_camera" \ + OBS_BUILD_MACOS_MAC_AVCAPTURE_LEGACY_REFERENCE="$reference_legacy_camera" \ + SWIFTC="$(xcrun --find swiftc)" \ + SDKROOT="$(xcrun --sdk macosx --show-sdk-path)" \ + soldr cargo test -p obs-studio-rust --features libobs-integration \ + --target "$native_target" --target-dir "$native_tests" \ + --test obs-tests-app native_libobs::macos_core_builds_from_the_canonical_manifest -- --exact -OBS_SYS_NATIVE_DEPENDENCIES="$(dirname "$(dirname "$avcodec")")" \ - OBS_BUILD_RUST_ARCHIVE="$native_rust/$native_target/release/liblibobs_rust.a" \ - soldr cargo check -p obs-sys --features bundled --target "$native_target" --target-dir "$native_tests" + OBS_SYS_NATIVE_DEPENDENCIES="$(dirname "$(dirname "$avcodec")")" \ + OBS_BUILD_RUST_ARCHIVE="$native_rust/$native_target/release/liblibobs_rust.a" \ + soldr cargo check -p obs-sys --features bundled --target "$native_target" --target-dir "$native_tests" + exit 0 +fi # Check each generated development framework contains the newly linked core. # This is not release packaging. Existing test binaries and their