-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathbuild.py
More file actions
334 lines (281 loc) · 10.3 KB
/
Copy pathbuild.py
File metadata and controls
334 lines (281 loc) · 10.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""Windows build script for nanoprintf. Invokes cl.exe / link.exe directly."""
import argparse
import concurrent.futures
import json
import os
import pathlib
import subprocess
import sys
_SCRIPT_PATH = pathlib.Path(__file__).resolve().parent
_ENVY = _SCRIPT_PATH / "bin" / ("envy.bat" if os.name == "nt" else "envy")
# /WX is MSVC's -Werror, and it belongs on every target. Three of the compile-only
# ones built at the default warning level, so anything the unit and conformance
# builds would have refused went unnoticed there. The suppressions are the
# off-by-default level-4 notes any of these can trip -- a '..' in an include path,
# and the three inline-expansion remarks -- none of which say anything about the code.
_CL_WARN_FLAGS = ["/W4", "/WX", "/wd4464", "/wd4514", "/wd4710", "/wd4711"]
# Globbed, not listed: the Makefile compiles the same set, and a hand-maintained copy
# here silently missed every unit test added since it was written.
_UNIT_SRCS = sorted(
p.relative_to(_SCRIPT_PATH).as_posix()
for p in (_SCRIPT_PATH / "tests").glob("unit_*.cc")
)
def _parse_args() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--cfg",
choices=["Debug", "RelWithDebInfo", "Release"],
default="Release",
const="Release",
nargs="?",
help="Build configuration",
)
parser.add_argument(
"--arch",
type=int,
choices=(32, 64),
default=64,
const=64,
nargs="?",
help="Target architecture",
)
parser.add_argument("-v", "--verbose", action="store_true", help="verbose")
return parser.parse_args()
def _envy_product(name: str) -> str:
"""Resolve an envy product to its absolute path, installing it if it is missing."""
result = subprocess.run(
[str(_ENVY), "-q", "product", name], check=True, capture_output=True, text=True
)
return result.stdout.strip()
def _doctest_include_dir() -> pathlib.Path:
"""Include directory for doctest.h. NPF_DOCTEST_H names a host copy and skips envy."""
override = os.environ.get("NPF_DOCTEST_H")
if not override:
return pathlib.Path(_envy_product("doctest_cpp_h")).parent
doctest_h = pathlib.Path(override).resolve()
if not doctest_h.is_file():
msg = f"NPF_DOCTEST_H={override} does not exist"
raise ValueError(msg)
return doctest_h.parent
def _run(
args: list[str | pathlib.Path],
*,
verbose: bool,
cwd: pathlib.Path | None = None,
) -> None:
"""Run a subprocess, printing the command if verbose."""
if verbose:
print(f" {' '.join(str(a) for a in args)}")
subprocess.run(args, check=True, cwd=cwd)
def _compile_one(cmd: list[str], cwd: pathlib.Path) -> subprocess.CompletedProcess[bytes]:
"""Compile a single translation unit."""
result = subprocess.run(cmd, cwd=cwd, capture_output=True)
if result.returncode != 0:
sys.stdout.buffer.write(result.stdout)
sys.stderr.buffer.write(result.stderr)
result.check_returncode()
return result
def _compile_all(commands: list[list[str]], cwd: pathlib.Path, *, verbose: bool) -> bool:
"""Run compile commands concurrently. Returns False on the first failure."""
workers = os.cpu_count() or 1
if verbose:
print(f" Compiling {len(commands)} files with {workers} workers")
with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as pool:
futures = {pool.submit(_compile_one, cmd, cwd): cmd for cmd in commands}
for future in concurrent.futures.as_completed(futures):
try:
future.result()
except subprocess.CalledProcessError:
# Cancel remaining futures on first failure
for f in futures:
f.cancel()
return False
return True
def _cl_opt_flags(cfg: str) -> list[str]:
"""MSVC counterpart of the Makefile's per-configuration optimization flags.
/Z7 rather than /Zi: the debug info lands in each .obj, so parallel cl.exe
processes never race on one shared PDB.
"""
if cfg == "Debug":
return ["/Od", "/Z7"]
if cfg == "RelWithDebInfo":
return ["/Os", "/Z7"]
return ["/Os"]
def _build_conformance(args: argparse.Namespace) -> bool:
"""Generate, build, and run the conformance test suite."""
gen_script = _SCRIPT_PATH / "tests" / "gen_tests.py"
gen_dir = _SCRIPT_PATH / "build" / "generated"
gen_args: list[str | pathlib.Path] = [
sys.executable,
str(gen_script),
"--output",
str(gen_dir),
"--arch",
str(args.arch),
]
if args.verbose:
gen_args.append("--verbose")
try:
_run(gen_args, verbose=args.verbose)
except subprocess.CalledProcessError:
return False
# Read compile commands and run in parallel
with (gen_dir / "compile_commands.json").open() as f:
commands: list[list[str]] = json.load(f)
if not _compile_all(commands, gen_dir, verbose=args.verbose):
return False
# Link
try:
_run(
["link.exe", "/nologo", "/out:npf_conformance.exe", "@link.rsp"],
verbose=args.verbose,
cwd=gen_dir,
)
except subprocess.CalledProcessError:
return False
# Run
try:
_run([str(gen_dir / "npf_conformance.exe")], verbose=args.verbose)
except subprocess.CalledProcessError:
return False
return True
def _build_unit_tests(args: argparse.Namespace) -> bool:
"""Build and run both unit test variants with cl.exe."""
build_dir = _SCRIPT_PATH / "build"
build_dir.mkdir(parents=True, exist_ok=True)
cxx_flags = [
"/nologo",
*_cl_opt_flags(args.cfg),
f"/I{_doctest_include_dir()}",
"/std:c++20",
"/EHsc",
*_CL_WARN_FLAGS,
# doctest.h's own level-4 noise, not nanoprintf's.
"/wd4619",
"/wd4820",
"/wd5039",
"/wd5262",
"/wd5264",
"/wd5285",
]
# doctest_main.cc is compiled once and linked into both variants.
doctest_obj = build_dir / "doctest_main.obj"
commands = [
["cl.exe", *cxx_flags, "/c", f"/Fo{doctest_obj}", "tests/doctest_main.cc"]
]
variants: list[tuple[pathlib.Path, list[pathlib.Path]]] = []
for suffix, large_val in [("", "0"), ("_large", "1")]:
var_dir = build_dir / f"unit{suffix}"
var_dir.mkdir(parents=True, exist_ok=True)
objs: list[pathlib.Path] = []
for src in _UNIT_SRCS:
obj = var_dir / (pathlib.Path(src).stem + ".obj")
objs.append(obj)
commands.append(
[
"cl.exe",
*cxx_flags,
"/DNANOPRINTF_USE_ALT_FORM_FLAG=1",
"/DDOCTEST_CONFIG_SUPER_FAST_ASSERTS",
f"/DNANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS={large_val}",
*(["/DNANOPRINTF_32_BIT_TESTS"] if args.arch == 32 else []),
"/c",
f"/Fo{obj}",
src,
]
)
variants.append((build_dir / f"unit_tests{suffix}.exe", objs))
if not _compile_all(commands, _SCRIPT_PATH, verbose=args.verbose):
return False
for exe, objs in variants:
try:
_run(
[
"link.exe",
"/nologo",
f"/out:{exe}",
str(doctest_obj),
*(str(o) for o in objs),
],
verbose=args.verbose,
)
except subprocess.CalledProcessError:
return False
try:
_run([str(exe), "-m"], verbose=args.verbose)
except subprocess.CalledProcessError:
return False
return True
def _build_compile_only(args: argparse.Namespace) -> bool:
"""Build compile-only targets (verify compilation, not run)."""
build_dir = _SCRIPT_PATH / "build"
build_dir.mkdir(parents=True, exist_ok=True)
opt = _cl_opt_flags(args.cfg)
targets: list[tuple[str, list[str], list[str]]] = [
# (name, extra_cl_flags, source_files)
(
"npf_static",
["/nologo", *opt, *_CL_WARN_FLAGS],
["tests/static_nanoprintf.c", "tests/static_main.c"],
),
(
"npf_include_multiple",
["/nologo", *opt, *_CL_WARN_FLAGS],
["tests/include_multiple.c"],
),
(
"use_npf_directly",
["/nologo", *opt, *_CL_WARN_FLAGS, "/std:c++20", "/EHsc"],
[
"examples/use_npf_directly/your_project_nanoprintf.cc",
"examples/use_npf_directly/main.cc",
],
),
(
"wrap_npf",
["/nologo", *opt, *_CL_WARN_FLAGS, "/std:c++20", "/EHsc"],
[
"examples/wrap_npf/your_project_printf.cc",
"examples/wrap_npf/main.cc",
],
),
]
commands: list[list[str]] = []
for name, flags, srcs in targets:
# A private object directory per target: without /Fo cl.exe drops the .obj in
# the working directory, which puts build output in the repo root and makes
# the two targets that each compile a `main.cc` race for the same name.
obj_dir = build_dir / "compile_only" / name
obj_dir.mkdir(parents=True, exist_ok=True)
commands.append(
[
"cl.exe",
*flags,
f"/Fo{obj_dir}{os.sep}",
f"/Fe{build_dir / f'{name}.exe'}",
*srcs,
"/link",
"/nologo",
]
)
return _compile_all(commands, _SCRIPT_PATH, verbose=args.verbose)
def main() -> int:
"""Parse args, build conformance + unit tests + compile-only targets."""
os.chdir(_SCRIPT_PATH)
args = _parse_args()
print("=== Building conformance tests ===")
if not _build_conformance(args):
print("Conformance tests FAILED")
return 1
print("=== Building unit tests ===")
if not _build_unit_tests(args):
print("Unit tests FAILED")
return 1
print("=== Building compile-only targets ===")
if not _build_compile_only(args):
print("Compile-only targets FAILED")
return 1
return 0
if __name__ == "__main__":
sys.exit(main())