Refactor the code generation process

This commit is contained in:
JCW
2026-03-31 16:22:50 +01:00
parent 5c8dfe5456
commit a1344b91c3
14 changed files with 193 additions and 173 deletions

View File

@@ -0,0 +1,4 @@
# Auto-generated by protocol autogen - do not edit manually.
# This file tracks input hashes to avoid unnecessary code regeneration.
# It should be checked into version control alongside the generated files.
COMBINED_HASH=24a9168ac6a450f09fa4e2ab288d06624a368041e91fbc7741101d3565d1e601

View File

@@ -138,28 +138,11 @@ def main():
"--sfields-macro",
help="Path to sfields.macro (default: auto-detect from macro_path)",
)
parser.add_argument(
"--list-outputs",
action="store_true",
help="List output files without generating (one per line)",
)
args = parser.parse_args()
# Parse the macro file to get ledger entry names
entries = parse_macro_file(args.macro_path)
# If --list-outputs, just print the output file paths and exit
if args.list_outputs:
header_dir = Path(args.header_dir)
for entry in entries:
print(header_dir / f"{entry['name']}.h")
if args.test_dir:
test_dir = Path(args.test_dir)
for entry in entries:
print(test_dir / f"{entry['name']}Tests.cpp")
return
# Auto-detect sfields.macro path if not provided
if args.sfields_macro:
sfields_path = Path(args.sfields_macro)

View File

@@ -147,28 +147,11 @@ def main():
"--sfields-macro",
help="Path to sfields.macro (default: auto-detect from macro_path)",
)
parser.add_argument(
"--list-outputs",
action="store_true",
help="List output files without generating (one per line)",
)
args = parser.parse_args()
# Parse the macro file to get transaction names
transactions = parse_macro_file(args.macro_path)
# If --list-outputs, just print the output file paths and exit
if args.list_outputs:
header_dir = Path(args.header_dir)
for tx in transactions:
print(header_dir / f"{tx['name']}.h")
if args.test_dir:
test_dir = Path(args.test_dir)
for tx in transactions:
print(test_dir / f"{tx['name']}Tests.cpp")
return
# Auto-detect sfields.macro path if not provided
if args.sfields_macro:
sfields_path = Path(args.sfields_macro)

View File

@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""
Check or update the codegen stamp file.
Uses only the Python standard library (hashlib, pathlib, sys) so it can
run without a virtual environment.
Modes:
--check Exit 0 if stamp is up-to-date, exit 1 if stale/missing.
--update Recompute the hash and write it to the stamp file.
Usage:
python update_codegen_stamp.py --check <stamp_file> <input_files...>
python update_codegen_stamp.py --update <stamp_file> <input_files...>
"""
import hashlib
import sys
from pathlib import Path
def compute_combined_hash(input_files: list[str]) -> str:
"""Compute a combined SHA-256 hash of all input files.
Algorithm: compute each file's SHA-256 hex digest, concatenate them
all, then SHA-256 the concatenation.
"""
parts = []
for filepath in input_files:
file_hash = hashlib.sha256(Path(filepath).read_bytes()).hexdigest()
parts.append(file_hash)
combined = "".join(parts)
return hashlib.sha256(combined.encode()).hexdigest()
def read_stamp_hash(stamp_file: str) -> str:
"""Read the COMBINED_HASH from an existing stamp file, or '' if missing."""
path = Path(stamp_file)
if not path.exists():
return ""
for line in path.read_text().splitlines():
if line.startswith("COMBINED_HASH="):
return line.split("=", 1)[1]
return ""
def main():
if len(sys.argv) < 4 or sys.argv[1] not in ("--check", "--update"):
print(
f"Usage: {sys.argv[0]} --check|--update <stamp_file> <input_files...>",
file=sys.stderr,
)
sys.exit(2)
mode = sys.argv[1]
stamp_file = sys.argv[2]
input_files = sys.argv[3:]
current_hash = compute_combined_hash(input_files)
if mode == "--check":
stamp_hash = read_stamp_hash(stamp_file)
if current_hash == stamp_hash:
sys.exit(0)
else:
sys.exit(1)
# --update
with open(stamp_file, "w") as fp:
fp.write(
"# Auto-generated by protocol autogen - do not edit manually.\n"
"# This file tracks input hashes to avoid unnecessary code regeneration.\n"
"# It should be checked into version control alongside the generated files.\n"
)
fp.write(f"COMBINED_HASH={current_hash}\n")
if __name__ == "__main__":
main()