| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | # |
| 7 | # Brief: check_api_compat.py — build current sources using test/benchmark from the |
| 8 | # latest tagged release to detect API breakage. |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import os |
| 12 | import shlex |
| 13 | import shutil |
| 14 | import subprocess |
| 15 | from pathlib import Path |
| 16 | |
| 17 | import utils.git as git_utils |
| 18 | |
| 19 | |
| 20 | ROOT_DIR = git_utils.repo_root() |
| 21 | BUILD_DIR = "build-api-compat" |
| 22 | |
| 23 | |
| 24 | def run(cmd: list[str], *, check: bool = True) -> subprocess.CompletedProcess: |
| 25 | print(f"Running command: {shlex.join(cmd)}") |
| 26 | return subprocess.run( |
| 27 | cmd, |
| 28 | cwd=ROOT_DIR, |
| 29 | check=check, |
| 30 | text=True, |
| 31 | ) |
| 32 | |
| 33 | |
| 34 | def major_breaking_since(tag: str) -> bool: |
| 35 | headlines = git_utils.get_commits(tag, "HEAD", root_dir=ROOT_DIR) |
| 36 | return any(headline.lower().startswith("major:") for headline in headlines) |
| 37 | |
| 38 | |
| 39 | def build_jobs() -> str: |
| 40 | if os.environ.get("PARALLEL_JOBS"): |
| 41 | return os.environ["PARALLEL_JOBS"] |
| 42 | try: |
| 43 | return str(os.cpu_count() or 1) |
| 44 | except Exception: |
| 45 | return "1" |
| 46 | |
| 47 | |
| 48 | def gtest_args() -> list[str]: |
| 49 | args = [ |
| 50 | "--gtest_brief=1", |
| 51 | f"--gtest_random_seed={os.environ.get('GLOBAL_TEST_SEED', '42')}", |
| 52 | ] |
| 53 | gtest_filter = os.environ.get("GTEST_FILTER") |
| 54 | if gtest_filter: |
| 55 | args.append(f"--gtest_filter={gtest_filter}") |
| 56 | return args |
| 57 | |
| 58 | |
| 59 | def main() -> int: |
| 60 | fetch_return_code, _ = git_utils.fetch_tags(ROOT_DIR) |
| 61 | if fetch_return_code != 0: |
| 62 | print("Failed to fetch tags from origin; unable to run API compatibility test.") |
| 63 | return 1 |
| 64 | |
| 65 | latest_tag = git_utils.latest_tag_matching( |
| 66 | r"^v[0-9]+\.[0-9]+\.[0-9]+$", root_dir=ROOT_DIR |
| 67 | ) |
| 68 | if not latest_tag: |
| 69 | print("No semver tags found; unable to run API compatibility test.") |
| 70 | return 1 |
| 71 | |
| 72 | if major_breaking_since(latest_tag): |
| 73 | print( |
| 74 | "Major-breaking change detected since latest tag; " |
| 75 | f"API breakage allowed. Skipping API compatibility test against {latest_tag}." |
| 76 | ) |
| 77 | return 0 |
| 78 | |
| 79 | if git_utils.has_local_changes(["test", "benchmark"], root_dir=ROOT_DIR): |
| 80 | print( |
| 81 | "Repository has local changes in test/ or benchmark/. " |
| 82 | "Refusing to overwrite local work." |
| 83 | ) |
| 84 | return 1 |
| 85 | |
| 86 | print(f"Checking out test/ and benchmark/ from {latest_tag}.") |
| 87 | # CI job only: this intentionally mutates the workspace for the remainder of the script; |
| 88 | # the job workspace is ephemeral and cleaned up by CI. |
| 89 | for path in (ROOT_DIR / "test", ROOT_DIR / "benchmark"): |
| 90 | if path.exists(): |
| 91 | shutil.rmtree(path) |
| 92 | run( |
| 93 | [ |
| 94 | "git", |
| 95 | "checkout", |
| 96 | "-q", |
| 97 | latest_tag, |
| 98 | "--", |
| 99 | "test", |
| 100 | "benchmark", |
| 101 | ] |
| 102 | ) |
| 103 | |
| 104 | build_path = ROOT_DIR / BUILD_DIR |
| 105 | if build_path.exists(): |
| 106 | shutil.rmtree(build_path) |
| 107 | |
| 108 | if not (ROOT_DIR / "CMakePresets.json").is_file(): |
| 109 | raise SystemExit( |
| 110 | "CMakePresets.json is required for the API compatibility check." |
| 111 | ) |
| 112 | |
| 113 | # Configure the current library with released tests and benchmarks. |
| 114 | run(["cmake", "--preset", "default-config", "-B", BUILD_DIR], check=True) |
| 115 | |
| 116 | # Build the current library, released unit tests, and released benchmarks. |
| 117 | run( |
| 118 | [ |
| 119 | "cmake", |
| 120 | "--build", |
| 121 | BUILD_DIR, |
| 122 | f"-j{build_jobs()}", |
| 123 | "--verbose", |
| 124 | ], |
| 125 | check=True, |
| 126 | ) |
| 127 | |
| 128 | run([str(Path(BUILD_DIR) / "kleidiai_test"), *gtest_args()], check=True) |
| 129 | run( |
| 130 | [str(Path(BUILD_DIR) / "kleidiai_benchmark"), "--benchmark_list_tests"], |
| 131 | check=True, |
| 132 | ) |
| 133 | |
| 134 | return 0 |
| 135 | |
| 136 | |
| 137 | if __name__ == "__main__": |
| 138 | raise SystemExit(main()) |