import re
from pathlib import Path
content = Path("zig-out/lib/core.c").read_text()
print(f"Read zig-out/lib/core.c ({len(content.splitlines())} lines)")
valgrind_wrapper = Path("src/helpers/valgrind_wrapper.c").read_text()
STUB = Path("scripts/stub.c").read_text()
REPLACEMENTS = [
(
re.compile(r"struct instrument_hooks_InstrumentHooks__\d+"),
"struct InstrumentHooks",
),
]
print("\nPost-processing replacements:")
for pattern, replacement in REPLACEMENTS:
match = pattern.search(content)
if not match:
raise RuntimeError(f"Pattern not found: {pattern.pattern}")
count = len(pattern.findall(content))
print(f" {match.group(0):>50s} -> {replacement} ({count} occurrences)")
content = pattern.sub(replacement, content)
content = '#include "core.h"\n' + content
TEMPLATE = f"""\
// This file is generated by scripts/release.py
// Do not edit it manually.
// Include compatibility layer for cross-platform support
#include "compat.h"
#if defined(_WIN32) || defined(__APPLE__)
{STUB}
#else
{content}
#endif
{valgrind_wrapper}
"""
Path("dist/core.c").write_text(TEMPLATE)
print(f"\nWrote dist/core.c ({len(TEMPLATE.splitlines())} lines)")