Add mcTVM mctvm cmake cache audit#34
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new Python script, tools/cmake_cache_audit.py, designed to audit CMakeCache.txt files for required MACA build configuration keys. The review feedback suggests several robustness improvements: stripping whitespace during parsing to prevent key/value mismatches, using the tempfile module in the self-test to avoid workspace pollution, and gracefully handling missing cache files in the main entry point to prevent raw tracebacks.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| def parse_cache(path: Path) -> dict[str, str]: | ||
| values: dict[str, str] = {} | ||
| for line in path.read_text(encoding="utf-8", errors="replace").splitlines(): | ||
| if not line or line.startswith("//") or line.startswith("#") or "=" not in line: | ||
| continue | ||
| key_type, value = line.split("=", 1) | ||
| key = key_type.split(":", 1)[0] | ||
| values[key] = value | ||
| return values |
There was a problem hiding this comment.
The current parser does not strip leading or trailing whitespace from lines, keys, or values. If there are leading spaces or spaces around the = delimiter, the parser might fail to identify keys or include unwanted whitespace in the parsed values. Stripping whitespace improves parsing robustness.
| def parse_cache(path: Path) -> dict[str, str]: | |
| values: dict[str, str] = {} | |
| for line in path.read_text(encoding="utf-8", errors="replace").splitlines(): | |
| if not line or line.startswith("//") or line.startswith("#") or "=" not in line: | |
| continue | |
| key_type, value = line.split("=", 1) | |
| key = key_type.split(":", 1)[0] | |
| values[key] = value | |
| return values | |
| def parse_cache(path: Path) -> dict[str, str]: | |
| values: dict[str, str] = {} | |
| for line in path.read_text(encoding="utf-8", errors="replace").splitlines(): | |
| line = line.strip() | |
| if not line or line.startswith("//") or line.startswith("#") or "=" not in line: | |
| continue | |
| key_type, value = line.split("=", 1) | |
| key = key_type.split(":", 1)[0].strip() | |
| values[key] = value.strip() | |
| return values |
| def self_test() -> None: | ||
| sample = Path("_CMakeCache_sample.txt") | ||
| sample.write_text("MACA_HOME:PATH=/opt/maca\n", encoding="utf-8") | ||
| try: | ||
| data = audit(sample) | ||
| assert "missing" in data | ||
| print(json.dumps({"ok": True, "missing": len(data["missing"])}, ensure_ascii=False)) | ||
| finally: | ||
| sample.unlink(missing_ok=True) |
There was a problem hiding this comment.
Writing a temporary file _CMakeCache_sample.txt directly to the current working directory can lead to permission issues, workspace pollution if the script is interrupted, or race conditions during concurrent test execution. Using Python's built-in tempfile module to create a temporary directory is a safer and cleaner approach.
| def self_test() -> None: | |
| sample = Path("_CMakeCache_sample.txt") | |
| sample.write_text("MACA_HOME:PATH=/opt/maca\n", encoding="utf-8") | |
| try: | |
| data = audit(sample) | |
| assert "missing" in data | |
| print(json.dumps({"ok": True, "missing": len(data["missing"])}, ensure_ascii=False)) | |
| finally: | |
| sample.unlink(missing_ok=True) | |
| def self_test() -> None: | |
| import tempfile | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| sample = Path(tmpdir) / "CMakeCache.txt" | |
| sample.write_text("MACA_HOME:PATH=/opt/maca\n", encoding="utf-8") | |
| data = audit(sample) | |
| assert "missing" in data | |
| print(json.dumps({"ok": True, "missing": len(data["missing"])}, ensure_ascii=False)) |
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("cache", nargs="?", default="CMakeCache.txt") | ||
| parser.add_argument("--self-test", action="store_true") | ||
| args = parser.parse_args() | ||
| if args.self_test: | ||
| self_test() | ||
| return 0 | ||
| print(json.dumps(audit(Path(args.cache)), ensure_ascii=False, indent=2)) | ||
| return 0 |
There was a problem hiding this comment.
If the specified CMake cache file (defaulting to CMakeCache.txt) does not exist, the script will crash with a raw FileNotFoundError traceback. It is better to check if the file exists and print a clean error message to sys.stderr before returning a non-zero exit code.
| def main() -> int: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("cache", nargs="?", default="CMakeCache.txt") | |
| parser.add_argument("--self-test", action="store_true") | |
| args = parser.parse_args() | |
| if args.self_test: | |
| self_test() | |
| return 0 | |
| print(json.dumps(audit(Path(args.cache)), ensure_ascii=False, indent=2)) | |
| return 0 | |
| def main() -> int: | |
| import sys | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("cache", nargs="?", default="CMakeCache.txt") | |
| parser.add_argument("--self-test", action="store_true") | |
| args = parser.parse_args() | |
| if args.self_test: | |
| self_test() | |
| return 0 | |
| cache_path = Path(args.cache) | |
| if not cache_path.is_file(): | |
| print(f"Error: CMake cache file not found at '{cache_path}'", file=sys.stderr) | |
| return 1 | |
| print(json.dumps(audit(cache_path), ensure_ascii=False, indent=2)) | |
| return 0 |
Summary
Validation
Review notes