-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen
More file actions
executable file
·310 lines (257 loc) · 11.3 KB
/
Copy pathcodegen
File metadata and controls
executable file
·310 lines (257 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
"""Run the nativeapi code generators.
Orchestrates the Rust generators in tools/codegen against the workspace layout:
./codegen # full run: C ABI, then all bindings
./codegen capi # C ABI + umbrella header only
./codegen bindings # bindings only (parses headers, writes no C ABI)
./codegen bindings --lang rust,swift
./codegen check # verify everything is up to date (CI mode)
./codegen sync # after a core change: regenerate everything,
# update every binding's embedded core
# submodule, rerun bindgen/ffigen, and commit
# core, bindings, and workspace pointers
./codegen sync -m "Add Foo API" --push
./codegen --dump-ir ir.json # also keep the intermediate IR at this path
The C ABI generator parses the C++ headers in core/ with libclang and emits an
IR JSON; the bindings generator consumes that IR. Binding repos whose submodule
is not initialized are skipped automatically.
"""
import argparse
import shutil
import subprocess
import sys
from pathlib import Path
WORKSPACE = Path(__file__).resolve().parent
CORE = WORKSPACE / "core"
CODEGEN = WORKSPACE / "tools" / "codegen"
DEFAULT_IR = CODEGEN / "target" / "ir.json"
# lang -> (cli flag of codegen-bindings, binding repo path)
BINDINGS = {
"rust": ("--rust", WORKSPACE / "bindings" / "rust"),
"dart": ("--dart", WORKSPACE / "bindings" / "flutter"),
"csharp": ("--csharp", WORKSPACE / "bindings" / "csharp"),
}
# Binding repos without a generator that still embed core and need dependency
# bumps during sync.
SYNC_ONLY: list[Path] = []
def run(binary: str, *extra: str) -> None:
cmd = [
"cargo", "run", "--quiet", "--release",
"--manifest-path", str(CODEGEN / "Cargo.toml"),
"-p", binary, "--", *extra,
]
result = subprocess.run(cmd)
if result.returncode != 0:
sys.exit(result.returncode)
def initialized(repo: Path) -> bool:
"""An uninitialized submodule leaves an empty directory behind."""
return (repo / ".git").exists()
def binding_flags(langs: list[str]) -> list[str]:
flags = []
for lang in langs:
flag, repo = BINDINGS[lang]
if initialized(repo):
flags += [flag, str(repo)]
else:
print(f" {lang:<11} (skipped, submodule {repo.name} not initialized)",
file=sys.stderr)
return flags
def run_capi(ir: Path, check: bool, parse_only: bool = False) -> None:
extra = ["--repo", str(CORE), "--emit-ir", str(ir)]
if parse_only:
extra.append("--parse-only")
if check:
extra.append("--check")
run("codegen-capi", *extra)
def run_bindings(ir: Path, langs: list[str], check: bool) -> None:
extra = ["--ir", str(ir), "--repo", str(CORE), *binding_flags(langs)]
if check:
extra.append("--check")
run("codegen-bindings", *extra)
# ---------------------------------------------------------------------------
# sync: propagate a core change through every binding repo
# ---------------------------------------------------------------------------
def git(repo: Path, *args: str, capture: bool = False, check: bool = True) -> str:
result = subprocess.run(
["git", "-C", str(repo), *args],
capture_output=capture, text=True,
)
if check and result.returncode != 0:
if capture and result.stderr:
print(result.stderr, file=sys.stderr, end="")
sys.exit(f"error: git {' '.join(args)} failed in {repo}")
return (result.stdout or "").strip() if capture else ""
def dirty(repo: Path) -> bool:
return bool(git(repo, "status", "--porcelain", capture=True))
def current_branch(repo: Path) -> str:
branch = git(repo, "branch", "--show-current", capture=True)
if not branch:
sys.exit(f"error: {repo.relative_to(WORKSPACE)} is on a detached HEAD; "
"check out a branch first")
return branch
def core_dep_paths(repo: Path) -> list[str]:
"""Paths of submodules inside `repo` that point at the nativeapi repo."""
if not (repo / ".gitmodules").exists():
return []
out = git(repo, "config", "-f", ".gitmodules", "--get-regexp",
r"submodule\..*\.(path|url)", capture=True)
entries: dict[str, dict[str, str]] = {}
for line in out.splitlines():
key, _, value = line.partition(" ")
name, _, attr = key.rpartition(".")
entries.setdefault(name, {})[attr] = value
return [
entry["path"]
for entry in entries.values()
if "path" in entry
and entry.get("url", "").rstrip("/").removesuffix(".git").endswith("/nativeapi")
]
def is_gitlink(repo: Path, path: str) -> bool:
entry = git(repo, "ls-files", "-s", "--", path, capture=True)
return entry.startswith("160000")
def update_core_dep(repo: Path, path: str, sha: str, branch: str) -> None:
git(repo, "submodule", "update", "--init", "--", path)
sub = repo / path
# Fetch from the local core checkout so sync works before core is pushed.
git(sub, "fetch", "--quiet", str(CORE), branch)
git(sub, "checkout", "--quiet", "--detach", sha)
def regen_rust_raw_ffi(repo: Path) -> bool:
if shutil.which("bindgen") is None:
print("warning: bindgen not installed, skipped regenerating "
"crates/cnativeapi/src/bindings.rs", file=sys.stderr)
return False
sysroot = subprocess.run(
["xcrun", "--show-sdk-path"], capture_output=True, text=True,
).stdout.strip()
cmd = [
"bindgen", str(CORE / "include" / "nativeapi.h"),
"--allowlist-function", "native_.*|free_c_str",
"--allowlist-type", "native_.*",
"--allowlist-var", "NATIVE_.*",
"--with-derive-default", "--no-layout-tests", "--no-prepend-enum-name",
"--raw-line", "#![allow(non_upper_case_globals)]",
"--raw-line", "#![allow(non_camel_case_types)]",
"--raw-line", "#![allow(non_snake_case)]",
"-o", str(repo / "crates" / "cnativeapi" / "src" / "bindings.rs"),
"--", "-x", "c", "-isysroot", sysroot,
f"-I{CORE / 'src'}", f"-I{CORE / 'include'}",
]
if subprocess.run(cmd).returncode != 0:
print("warning: bindgen failed; bindings.rs may be stale", file=sys.stderr)
return False
return True
def regen_flutter_ffi(repo: Path) -> bool:
"""Run the flutter repo's own codegen.py (mm includes, umbrella headers,
ffigen.yaml, dart ffigen)."""
package = repo / "packages" / "cnativeapi"
if shutil.which("dart") is None:
print("warning: dart not installed, skipped flutter ffigen regeneration",
file=sys.stderr)
return False
if subprocess.run(["dart", "pub", "get"], cwd=package).returncode != 0:
print("warning: dart pub get failed; skipped flutter ffigen regeneration",
file=sys.stderr)
return False
result = subprocess.run(
[sys.executable, "codegen.py", "--no-submodule-update"], cwd=package,
)
if result.returncode != 0:
print("warning: flutter codegen.py failed; its outputs may be stale",
file=sys.stderr)
return False
return True
def commit_if_dirty(repo: Path, message: str) -> bool:
if not dirty(repo):
return False
git(repo, "add", "-A")
git(repo, "commit", "-m", message)
return True
def cmd_sync(args, langs: list[str], ir: Path) -> None:
repos = [repo for _, repo in (BINDINGS[lang] for lang in langs)]
repos += [repo for repo in SYNC_ONLY if repo not in repos]
repos = [repo for repo in repos if initialized(repo)]
# Fail early on detached HEADs: sync commits into every repo.
core_branch = current_branch(CORE)
for repo in repos:
current_branch(repo)
print("==> regenerating C ABI and bindings")
run_capi(ir, check=False)
run_bindings(ir, langs, check=False)
print("==> committing core")
committed = []
if commit_if_dirty(CORE, args.message):
committed.append(CORE)
core_sha = git(CORE, "rev-parse", "HEAD", capture=True)
short = core_sha[:9]
sync_message = f"Sync with core {short}"
for repo in repos:
name = str(repo.relative_to(WORKSPACE))
print(f"==> syncing {name}")
deps = [p for p in core_dep_paths(repo) if is_gitlink(repo, p)]
if not deps:
print(f"warning: no core submodule gitlink found in {name}",
file=sys.stderr)
for path in deps:
update_core_dep(repo, path, core_sha, core_branch)
if repo == BINDINGS["rust"][1]:
regen_rust_raw_ffi(repo)
if repo == BINDINGS["dart"][1]:
regen_flutter_ffi(repo)
if commit_if_dirty(repo, sync_message):
committed.append(repo)
print("==> committing workspace submodule pointers")
pointers = [str(CORE.relative_to(WORKSPACE))] + [
str(repo.relative_to(WORKSPACE)) for repo in repos
]
git(WORKSPACE, "add", "--", *pointers)
if git(WORKSPACE, "diff", "--cached", "--name-only", capture=True):
git(WORKSPACE, "commit", "-m", sync_message)
committed.append(WORKSPACE)
if not committed:
print("nothing to commit; everything already in sync")
return
names = ", ".join(
"workspace" if repo == WORKSPACE else str(repo.relative_to(WORKSPACE))
for repo in committed
)
print(f"committed: {names}")
if args.push:
# Core first so binding submodule pointers never dangle remotely.
for repo in committed:
name = "workspace" if repo == WORKSPACE else str(repo.relative_to(WORKSPACE))
print(f"==> pushing {name}")
git(repo, "push")
else:
print("not pushed; rerun with --push, or push manually (core first, "
"then bindings, then the workspace repo)")
def main() -> None:
cli = argparse.ArgumentParser(description=__doc__.split("\n", 1)[0])
cli.add_argument("command", nargs="?", default="all",
choices=["all", "capi", "bindings", "check", "sync"])
cli.add_argument("--lang", default=",".join(BINDINGS),
help="comma-separated subset of: " + ",".join(BINDINGS))
cli.add_argument("--dump-ir", type=Path, metavar="PATH",
help="keep the intermediate IR JSON at this path")
cli.add_argument("-m", "--message", default="Update API",
metavar="MSG", help="sync: commit message for the core repo")
cli.add_argument("--push", action="store_true",
help="sync: also push every repo that received a commit")
args = cli.parse_args()
langs = [lang.strip() for lang in args.lang.split(",") if lang.strip()]
unknown = [lang for lang in langs if lang not in BINDINGS]
if unknown:
cli.error(f"unknown --lang value(s): {', '.join(unknown)}")
ir = args.dump_ir or DEFAULT_IR
check = args.command == "check"
if args.command in ("all", "check"):
run_capi(ir, check)
run_bindings(ir, langs, check)
elif args.command == "capi":
run_capi(ir, check=False)
elif args.command == "bindings":
run_capi(ir, check=False, parse_only=True)
run_bindings(ir, langs, check=False)
elif args.command == "sync":
cmd_sync(args, langs, ir)
if __name__ == "__main__":
main()