-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·234 lines (193 loc) · 8.05 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·234 lines (193 loc) · 8.05 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
#!/usr/bin/env python3
from __future__ import annotations
import json
import os
import re
import shutil
import subprocess
import sys
import time
from pathlib import Path
USAGE = """Usage:
build.py [configure] [test] [release] [clean] [target=<cmake-target>] [jobs=<n>]
[build_dir=<dir>] [static_schema_path=<path>] [static_ipc_schema_path=<path>]
[timesync=0|1] [discovery=0|1] [ctest_filter=<regex>]
Examples:
build.py
build.py test
build.py release test
build.py test ctest_filter=sedsprintf_rust_router_interop
build.py configure build_dir=build/overlay static_ipc_schema_path=tests/schemas/ipc_link_local_overlay.json
build.py target=sedsprintf_cpp_overlay_tests
"""
RUST_INTEROP_TEST = "sedsprintf_rust_router_interop"
RUST_INTEROP_TARGET = "sedsprintf_rust_interop_cpp_peer"
def run(cmd: list[str], cwd: Path) -> None:
print("+", " ".join(cmd))
subprocess.run(cmd, cwd=cwd, check=True)
def format_seconds(seconds: float) -> str:
if seconds < 1:
return f"{seconds * 1000:.0f}ms"
if seconds < 60:
return f"{seconds:.2f}s"
minutes = int(seconds // 60)
return f"{minutes}m{seconds - minutes * 60:.0f}s"
def print_banner(title: str) -> None:
line = "-" * 60
print(f"\n{line}\n{title}\n{line}")
def print_step(index: int, total: int, title: str) -> None:
print_banner(f"{index}/{total} {title}")
def run_timed(cmd: list[str], cwd: Path) -> None:
start = time.perf_counter()
run(cmd, cwd)
print(f"info: finished in {format_seconds(time.perf_counter() - start)}")
def configure_args(root: Path, build_dir: Path, options: dict[str, object], *, export_compile_commands: bool) -> list[str]:
cmake_args = [
"cmake",
"-S",
str(root),
"-B",
str(build_dir),
f"-DCMAKE_BUILD_TYPE={'Release' if options['release'] else 'Debug'}",
]
if export_compile_commands:
cmake_args.append("-DCMAKE_EXPORT_COMPILE_COMMANDS=ON")
cmake_args.append(f"-DSEDSNET_ENABLE_TESTS={'ON' if options['test'] else 'OFF'}")
if options["static_schema_path"]:
cmake_args.append(f"-DSEDSNET_SCHEMA_PATH={(root / str(options['static_schema_path'])).resolve()}")
if options["static_ipc_schema_path"]:
cmake_args.append(f"-DSEDSNET_IPC_SCHEMA_PATH={(root / str(options['static_ipc_schema_path'])).resolve()}")
if options["timesync"] is not None:
cmake_args.append(f"-DSEDSPRINTF_ENABLE_TIMESYNC={'ON' if options['timesync'] == '1' else 'OFF'}")
if options["discovery"] is not None:
cmake_args.append(f"-DSEDSPRINTF_ENABLE_DISCOVERY={'ON' if options['discovery'] == '1' else 'OFF'}")
return cmake_args
def run_test_mode(root: Path, build_dir: Path, *, jobs: str | int | None, target: object,
ctest_filter: object) -> None:
total_steps = 4
print_banner("TEST MODE")
print_step(1, total_steps, "codegen")
codegen_cmd = ["cmake", "--build", str(build_dir), "--target", "sedsprintf_codegen", "sedsprintf_codegen_overlay"]
if jobs:
codegen_cmd.extend(["-j", str(jobs)])
run_timed(codegen_cmd, root)
print_step(2, total_steps, "cmake build")
build_cmd = ["cmake", "--build", str(build_dir)]
build_targets: list[str] = []
if target:
build_targets.append(str(target))
selected_tests_can_include_interop = ctest_filter is None or re.search(str(ctest_filter), RUST_INTEROP_TEST)
if selected_tests_can_include_interop and RUST_INTEROP_TARGET not in build_targets:
build_targets.append(RUST_INTEROP_TARGET)
if build_targets:
build_cmd.extend(["--target", *build_targets])
if jobs:
build_cmd.extend(["-j", str(jobs)])
run_timed(build_cmd, root)
print_step(3, total_steps, "ctest")
test_cmd = ["ctest", "--test-dir", str(build_dir), "--output-on-failure"]
if ctest_filter:
test_cmd.extend(["-R", str(ctest_filter)])
run_timed(test_cmd, root)
print_step(4, total_steps, "codegen check")
run_timed([sys.executable, str(root / "tests" / "check_codegen.py")], root)
def help_and_exit(code: int = 0) -> int:
stream = sys.stderr if code else sys.stdout
print(USAGE, file=stream)
return code
def parse_args(argv: list[str]) -> dict[str, object]:
options: dict[str, object] = {
"release": False,
"test": False,
"clean": False,
"configure_only": False,
"build_dir": None,
"static_schema_path": None,
"static_ipc_schema_path": None,
"timesync": None,
"discovery": None,
"target": None,
"jobs": None,
"ctest_filter": None,
}
for arg in argv:
if arg in {"-h", "--help", "help"}:
raise SystemExit(help_and_exit())
if arg == "release":
options["release"] = True
elif arg == "test":
options["test"] = True
elif arg == "clean":
options["clean"] = True
elif arg == "configure":
options["configure_only"] = True
elif arg.startswith("build_dir="):
options["build_dir"] = arg.split("=", 1)[1]
elif arg.startswith("static_schema_path="):
options["static_schema_path"] = arg.split("=", 1)[1]
elif arg.startswith("static_ipc_schema_path="):
options["static_ipc_schema_path"] = arg.split("=", 1)[1]
elif arg.startswith("schema_path="):
options["static_schema_path"] = arg.split("=", 1)[1]
elif arg.startswith("ipc_schema_path="):
options["static_ipc_schema_path"] = arg.split("=", 1)[1]
elif arg.startswith("timesync="):
value = arg.split("=", 1)[1]
if value not in {"0", "1"}:
raise SystemExit(help_and_exit(1))
options["timesync"] = value
elif arg.startswith("discovery="):
value = arg.split("=", 1)[1]
if value not in {"0", "1"}:
raise SystemExit(help_and_exit(1))
options["discovery"] = value
elif arg.startswith("target="):
options["target"] = arg.split("=", 1)[1]
elif arg.startswith("jobs="):
options["jobs"] = arg.split("=", 1)[1]
elif arg.startswith("ctest_filter="):
options["ctest_filter"] = arg.split("=", 1)[1]
else:
print(f"error: unknown argument {arg!r}", file=sys.stderr)
raise SystemExit(help_and_exit(1))
return options
def remove_build_dir(build_dir: Path) -> None:
if not build_dir.exists():
return
print(f"+ rm -rf {build_dir}")
shutil.rmtree(build_dir)
def main(argv: list[str]) -> int:
options = parse_args(argv)
root = Path(__file__).resolve().parent
build_dir = root / (options["build_dir"] or "build")
build_dir.parent.mkdir(parents=True, exist_ok=True)
if options["clean"]:
remove_build_dir(build_dir)
cmake_args = configure_args(root, build_dir, options, export_compile_commands=bool(options["test"]))
start = time.perf_counter()
run(cmake_args, root)
print(f"info: configure finished in {format_seconds(time.perf_counter() - start)}")
if options["configure_only"]:
return 0
jobs = options["jobs"] or os.cpu_count()
if options["test"]:
run_test_mode(root, build_dir, jobs=jobs, target=options["target"], ctest_filter=options["ctest_filter"])
return 0
build_cmd = ["cmake", "--build", str(build_dir)]
if options["target"]:
build_cmd.extend(["--target", str(options["target"])])
if jobs:
build_cmd.extend(["-j", str(jobs)])
start = time.perf_counter()
run(build_cmd, root)
print(f"info: build finished in {format_seconds(time.perf_counter() - start)}")
if options["test"]:
test_cmd = ["ctest", "--test-dir", str(build_dir), "--output-on-failure"]
if options["ctest_filter"]:
test_cmd.extend(["-R", str(options["ctest_filter"])])
start = time.perf_counter()
run(test_cmd, root)
print(f"info: tests finished in {format_seconds(time.perf_counter() - start)}")
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))