| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# Copyright 2025 The Fuchsia Authors |
| 4 |
# |
| 5 |
# Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 6 |
# <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 7 |
# license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 8 |
# This file may not be copied, modified, or distributed except according to |
| 9 |
# those terms. |
| 10 |
|
| 11 |
set -eo pipefail |
| 12 |
cd "$(dirname "$0")/.." |
| 13 |
|
| 14 |
read -r -d '' PYTHON_SCRIPT <<'EOF' || true |
| 15 |
import sys |
| 16 |
import json |
| 17 |
|
| 18 |
def parse_version(v): |
| 19 |
"""Converts a version string to a tuple of integers.""" |
| 20 |
return tuple(map(int, v.split("."))) |
| 21 |
|
| 22 |
def main(): |
| 23 |
""" |
| 24 |
Checks that the package's MSRV is strictly lower than any version |
| 25 |
specified in `package.metadata.build-rs`. |
| 26 |
""" |
| 27 |
try: |
| 28 |
data = json.load(sys.stdin) |
| 29 |
except json.JSONDecodeError as e: |
| 30 |
print(f"Error parsing JSON from cargo metadata: {e}", file=sys.stderr) |
| 31 |
sys.exit(1) |
| 32 |
|
| 33 |
# Find the zerocopy package |
| 34 |
try: |
| 35 |
pkg = next(p for p in data["packages"] if p["name"] == "zerocopy") |
| 36 |
except StopIteration: |
| 37 |
print("Error: zerocopy package not found in metadata", file=sys.stderr) |
| 38 |
sys.exit(1) |
| 39 |
|
| 40 |
msrv_str = pkg.get("rust_version") |
| 41 |
if not msrv_str: |
| 42 |
print("Error: rust-version not found in Cargo.toml", file=sys.stderr) |
| 43 |
sys.exit(1) |
| 44 |
|
| 45 |
try: |
| 46 |
msrv = parse_version(msrv_str) |
| 47 |
except ValueError: |
| 48 |
print(f"Error: Invalid MSRV format: {msrv_str}", file=sys.stderr) |
| 49 |
sys.exit(1) |
| 50 |
|
| 51 |
build_rs_versions = (pkg.get("metadata") or {}).get("build-rs", {}) |
| 52 |
|
| 53 |
failed = False |
| 54 |
for name, ver_str in build_rs_versions.items(): |
| 55 |
try: |
| 56 |
ver = parse_version(ver_str) |
| 57 |
except ValueError: |
| 58 |
print(f"Warning: Skipping invalid version format for {name}: {ver_str}", file=sys.stderr) |
| 59 |
continue |
| 60 |
|
| 61 |
# Check that MSRV < Version (strictly lower) |
| 62 |
if not (msrv < ver): |
| 63 |
print(f"Error: MSRV ({msrv_str}) is not strictly lower than {name} ({ver_str})", file=sys.stderr) |
| 64 |
failed = True |
| 65 |
|
| 66 |
if failed: |
| 67 |
sys.exit(1) |
| 68 |
|
| 69 |
if __name__ == "__main__": |
| 70 |
main() |
| 71 |
EOF |
| 72 |
|
| 73 |
cargo metadata --format-version 1 --no-deps | python3 -c "$PYTHON_SCRIPT" |