135 lines
4.4 KiB
Python
135 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate or verify Kryon's checked-in public API identifier snapshot."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import difflib
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SNAPSHOT = ROOT / "docs" / "PUBLIC_API_SNAPSHOT.txt"
|
|
EXCLUDED_HEADERS = {"kryon_compat.generated.h", "kryon_plan9.h"}
|
|
|
|
|
|
def strip_comments(text: str) -> str:
|
|
text = re.sub(r"/\*.*?\*/", "", text, flags=re.S)
|
|
return re.sub(r"//.*", "", text)
|
|
|
|
|
|
def header_guard_name(path: Path) -> str:
|
|
return re.sub(r"[^A-Za-z0-9]", "_", path.name).upper()
|
|
|
|
|
|
def logical_lines(text: str) -> list[str]:
|
|
lines: list[str] = []
|
|
current = ""
|
|
for raw in text.splitlines():
|
|
line = raw.strip()
|
|
if not line:
|
|
continue
|
|
current = f"{current} {line}".strip()
|
|
if line.endswith(";") or line.startswith("#"):
|
|
lines.append(current)
|
|
current = ""
|
|
if current:
|
|
lines.append(current)
|
|
return lines
|
|
|
|
|
|
def collect() -> list[str]:
|
|
entries: set[str] = set()
|
|
for path in sorted((ROOT / "include").glob("*.h")):
|
|
if path.name in EXCLUDED_HEADERS:
|
|
continue
|
|
guard = header_guard_name(path)
|
|
text = strip_comments(path.read_text(encoding="utf-8"))
|
|
header = path.relative_to(ROOT).as_posix()
|
|
|
|
for match in re.finditer(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z0-9_]*)", text, flags=re.M):
|
|
name = match.group(1)
|
|
if name == guard or name.startswith("__") or name.endswith("_H"):
|
|
continue
|
|
entries.add(f"{header} macro {name}")
|
|
|
|
for match in re.finditer(
|
|
r"\btypedef\s+[^;]*\(\s*\*\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)\s*\([^;]*\)\s*;",
|
|
text,
|
|
flags=re.S,
|
|
):
|
|
entries.add(f"{header} typedef {match.group(1)}")
|
|
|
|
for match in re.finditer(
|
|
r"\btypedef\s+(?:struct|enum|union)?[^;{]*[}\w\s,*]*\s+([A-Za-z_][A-Za-z0-9_]*)\s*;",
|
|
text,
|
|
flags=re.S,
|
|
):
|
|
entries.add(f"{header} typedef {match.group(1)}")
|
|
|
|
for enum_body in re.findall(r"\benum(?:\s+[A-Za-z_][A-Za-z0-9_]*)?\s*{(.*?)}", text, flags=re.S):
|
|
for item in enum_body.split(","):
|
|
name = item.split("=", 1)[0].strip()
|
|
if re.match(r"^[A-Z][A-Z0-9_]*$", name):
|
|
entries.add(f"{header} enum {name}")
|
|
|
|
for line in logical_lines(text):
|
|
if line.startswith("#") or line.startswith("typedef ") or "(*" in line:
|
|
continue
|
|
match = re.match(
|
|
r"^(?:extern\s+)?[A-Za-z_][A-Za-z0-9_\s*]*\s+([A-Za-z_][A-Za-z0-9_]*)\s*\([^;]*\)\s*;",
|
|
line,
|
|
)
|
|
if match:
|
|
entries.add(f"{header} function {match.group(1)}")
|
|
|
|
return sorted(entries)
|
|
|
|
|
|
def snapshot_text() -> str:
|
|
lines = [
|
|
"# Generated by scripts/public-api-snapshot.py. DO NOT EDIT MANUALLY.",
|
|
"# Tracks public Kryon identifiers from include/*.h, excluding generated compatibility and platform shim headers.",
|
|
"",
|
|
]
|
|
lines.extend(collect())
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--check", action="store_true", help="verify docs/PUBLIC_API_SNAPSHOT.txt is current")
|
|
parser.add_argument("--write", action="store_true", help="rewrite docs/PUBLIC_API_SNAPSHOT.txt")
|
|
args = parser.parse_args()
|
|
|
|
generated = snapshot_text()
|
|
if args.write:
|
|
SNAPSHOT.write_text(generated, encoding="utf-8")
|
|
print(f"wrote {SNAPSHOT.relative_to(ROOT)}")
|
|
return 0
|
|
|
|
if args.check:
|
|
current = SNAPSHOT.read_text(encoding="utf-8") if SNAPSHOT.exists() else ""
|
|
if current != generated:
|
|
diff = difflib.unified_diff(
|
|
current.splitlines(),
|
|
generated.splitlines(),
|
|
fromfile=str(SNAPSHOT.relative_to(ROOT)),
|
|
tofile="generated public API snapshot",
|
|
lineterm="",
|
|
)
|
|
print("\n".join(diff), file=sys.stderr)
|
|
print("public API snapshot is stale; run scripts/public-api-snapshot.py --write", file=sys.stderr)
|
|
return 1
|
|
print("public API snapshot ok")
|
|
return 0
|
|
|
|
print(generated, end="")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|