Debian Codegraph @

debian/rust-vendor/codspeed/instrument-hooks/scripts/release.py

raw
1 import re
2 from pathlib import Path
3
4 # Zig's C backend mangles struct names (e.g. instrument_hooks_InstrumentHooks__547),
5 # and there's no way to control this from Zig source. Replace with the stable
6 # name from core.h so that core.c can be included alongside core.h without
7 # type conflicts.
8 REPLACEMENTS = [
9 (
10 re.compile(r"struct instrument_hooks_InstrumentHooks__\d+"),
11 "struct InstrumentHooks",
12 ),
13 ]
14
15
16 def load_transpiled(path: Path) -> str:
17 content = path.read_text()
18 print(f"Read {path} ({len(content.splitlines())} lines)")
19 for pattern, replacement in REPLACEMENTS:
20 match = pattern.search(content)
21 if not match:
22 raise RuntimeError(f"Pattern not found in {path}: {pattern.pattern}")
23 count = len(pattern.findall(content))
24 print(f" {match.group(0):>50s} -> {replacement} ({count} occurrences)")
25 content = pattern.sub(replacement, content)
26 # Add core.h include at the top so consumers get the constants
27 # (MARKER_TYPE_*) and inline helpers from core.h automatically.
28 return '#include "core.h"\n' + content
29
30
31 linux_c = load_transpiled(Path("zig-out/lib/core.linux.c"))
32 macos_c = load_transpiled(Path("zig-out/lib/core.macos.c"))
33 valgrind_wrapper = Path("src/helpers/valgrind_wrapper.c").read_text()
34 windows_stub = Path("scripts/stub.c").read_text()
35
36 TEMPLATE = f"""\
37 // This file is generated by scripts/release.py
38 // Do not edit it manually.
39
40 // Include compatibility layer for cross-platform support
41 #include "compat.h"
42
43 #if defined(_WIN32)
44 {windows_stub}
45 #elif defined(__APPLE__)
46 {macos_c}
47 #else
48 {linux_c}
49 #endif
50 {valgrind_wrapper}
51 """
52
53 Path("dist/core.c").write_text(TEMPLATE)
54 print(f"\nWrote dist/core.c ({len(TEMPLATE.splitlines())} lines)")