ingot-zig / tools /buildgraph.py
ego0op's picture
Ingot v1.1: verified pool 175M -> 187M tokens (+49 repos now build after dependency-fetch fixes; build.zig modules wired into the per-file check); ore/slag re-graded; tools updated
306baca verified
Raw History Blame Contribute Delete
2.68 kB
# Static module graph from a repo's build.zig text (Opus · Smelter, 2026-10-27). Works when build.zig itself no longer
# compiles (stale build API): name -> repo-local root file, for modules declared with addModule/createModule and wired
# with addImport / .imports = .{ .name, .module } / addAnonymousImport. External (b.dependency) modules are not resolved.
import re, os
PATH = r'(?:b\.path\(\s*"([^"]+)"\s*\)|\.\{\s*\.(?:src_)?path\s*=\s*"([^"]+)"\s*\}|"([^"]+\.zig)")'
def _p(m):
return next((g for g in m.groups() if g), None)
def modules(root):
txt = ''
for f in ('build.zig',):
try: txt += open(os.path.join(root, f), encoding='utf-8', errors='replace').read()
except OSError: pass
if not txt: return {}
var, named = {}, {}
# const V = b.createModule(.{ ... root_source_file = PATH ... }) / b.addModule("N", .{ ... })
for m in re.finditer(r'(?:const|var)\s+(\w+)\s*=\s*b\.(createModule|addModule)\(\s*(?:"([^"]+)"\s*,\s*)?\.\{', txt):
body = txt[m.end():m.end() + 1500]
pm = re.search(r'\.(?:root_source_file|source_file)\s*=\s*' + PATH, body)
if pm: var[m.group(1)] = _p(pm); named[m.group(3)] = _p(pm) if m.group(3) else None
for m in re.finditer(r'b\.addModule\(\s*"([^"]+)"\s*,\s*\.\{', txt):
pm = re.search(r'\.(?:root_source_file|source_file)\s*=\s*' + PATH, txt[m.end():m.end() + 1500])
if pm: named[m.group(1)] = _p(pm)
# X.addImport("N", V) / addModule("N", V) / .{ .name = "N", .module = V }
for m in re.finditer(r'\.(?:addImport|addModule)\(\s*"([^"]+)"\s*,\s*(\w+)\s*\)', txt):
if m.group(2) in var: named.setdefault(m.group(1), var[m.group(2)])
for m in re.finditer(r'\.name\s*=\s*"([^"]+)"\s*,\s*\.module\s*=\s*(\w+)\b', txt):
if m.group(2) in var: named.setdefault(m.group(1), var[m.group(2)])
# inline: .{ .name = "N", .module = b.createModule(.{ .root_source_file = PATH }) } and addAnonymousImport("N", .{ ...PATH })
for m in re.finditer(r'\.name\s*=\s*"([^"]+)"\s*,\s*\.module\s*=\s*b\.createModule\(\s*\.\{', txt):
pm = re.search(r'\.root_source_file\s*=\s*' + PATH, txt[m.end():m.end() + 600])
if pm: named.setdefault(m.group(1), _p(pm))
for m in re.finditer(r'\.(?:addAnonymousImport|addAnonymousModule)\(\s*"([^"]+)"\s*,\s*\.\{', txt):
pm = re.search(r'\.(?:root_source_file|source_file)\s*=\s*' + PATH, txt[m.end():m.end() + 600])
if pm: named.setdefault(m.group(1), _p(pm))
out = {}
for n, p in named.items():
if not n or not p: continue
p = os.path.normpath(p)
if not p.startswith('..') and os.path.isfile(os.path.join(root, p)): out[n] = p
return out