-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmake.py
More file actions
2017 lines (1744 loc) · 75.7 KB
/
Copy pathmake.py
File metadata and controls
2017 lines (1744 loc) · 75.7 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
make.py — single cross-platform entrypoint for the tableauio/loader repo.
Consolidates per-language `buf generate` / `cmake` / `go test` / `dotnet test`
recipes into one Python tool that works identically on
native Windows, macOS, Linux, and inside the devcontainer.
Usage (high level):
python3 make.py setup [--lang go|cpp|csharp|all] [--dry-run]
python3 make.py generate --lang go|cpp|csharp
python3 make.py build --lang go|cpp|csharp [build flags]
python3 make.py test --lang go|cpp|csharp [build flags] [-k FILTER] [--smoke]
python3 make.py clean [--lang ...] [--all]
python3 make.py env
python3 make.py --version
Standard flags (apply to every subcommand):
--verbose / -v echo every subprocess
--dry-run print but do not execute
--cwd <path> repo root (default: auto-detect from versions.env)
The Windows MSVC env trick: every command that needs cl.exe / vcpkg-protoc
runs through Platform.windows_msvc_wrap(), which transparently wraps the
command in `cmd /c "<vcvarsall.bat> x64 && <cmd>"`. The user's shell
PATH/INCLUDE/LIB is never mutated.
Stdlib only — no `pip install` required. Targets Python >= 3.10.
"""
import argparse
import json
import os
import platform as _stdlib_platform
import shlex # noqa: F401 (kept for potential future POSIX shell quoting)
import shutil
import subprocess
import sys
import urllib.request
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
MAKE_PY_VERSION = "0.1.0"
# ---------------------------------------------------------------------------
# Versions
# ---------------------------------------------------------------------------
@dataclass
class Versions:
"""Parsed view of .devcontainer/versions.env.
The format rules (documented in .devcontainer/README.md):
- One KEY=VALUE per line, no quotes, no spaces around `=`.
- Comments start with `#` at column 0.
- Blank lines are ignored.
- No shell expansion.
"""
raw: dict[str, str] = field(default_factory=dict)
@classmethod
def load(cls, repo_root: Path) -> "Versions":
path = repo_root / ".devcontainer" / "versions.env"
if not path.is_file():
raise FileNotFoundError(
f"Missing {path}; cannot resolve pinned tool versions."
)
raw: dict[str, str] = {}
for line in path.read_text(encoding="utf-8").splitlines():
if not line or line.startswith("#"):
continue
if "=" not in line:
continue
k, v = line.split("=", 1)
raw[k.strip()] = v.strip()
return cls(raw=raw)
def get(self, key: str, default: Optional[str] = None) -> Optional[str]:
return self.raw.get(key, default)
@property
def go_version(self) -> Optional[str]:
return self.raw.get("GO_VERSION")
@property
def buf_version(self) -> Optional[str]:
return self.raw.get("BUF_VERSION")
@property
def protobuf_version(self) -> Optional[str]:
"""Resolved protobuf version for the active (DEFAULT_VARIANT) row."""
return self._variant_value("PROTOBUF_VERSION")
@property
def vcpkg_baseline_commit(self) -> Optional[str]:
"""Resolved vcpkg baseline SHA for the active (DEFAULT_VARIANT) row."""
return self._variant_value("VCPKG_BASELINE_COMMIT")
@property
def default_variant(self) -> str:
"""Active variant label (e.g. ``modern`` / ``legacy_v3``).
Matched case-insensitively against variant-prefixed keys: a value of
``modern`` resolves keys with prefix ``MODERN_``; ``legacy-v3`` /
``legacy_v3`` both resolve ``LEGACY_V3_``. Defaults to ``modern`` if
the key is absent (preserves behaviour of older versions.env files).
"""
return (self.raw.get("DEFAULT_VARIANT") or "modern").strip().lower()
def variants(self) -> dict[str, dict[str, str]]:
"""Enumerate every (protobuf, vcpkg-baseline) variant defined.
Returns ``{variant_label: {"protobuf_version": ..., "vcpkg_baseline_commit": ...}}``
keyed by lowercase label. Labels that are missing one of the two keys
are still reported with the key they have, so callers can detect a
half-defined variant.
"""
suffixes = {
"_PROTOBUF_VERSION": "protobuf_version",
"_VCPKG_BASELINE_COMMIT": "vcpkg_baseline_commit",
}
out: dict[str, dict[str, str]] = {}
for k, v in self.raw.items():
for sfx, field_name in suffixes.items():
if k.endswith(sfx):
label = k[: -len(sfx)].lower()
out.setdefault(label, {})[field_name] = v
break
return out
def _variant_value(self, suffix: str) -> Optional[str]:
"""Resolve ``<DEFAULT_VARIANT>_<suffix>`` (uppercase). Falls back to
the unprefixed key for backward compat with old versions.env files
that used the flat ``PROTOBUF_VERSION`` / ``VCPKG_BASELINE_COMMIT``."""
prefix = self.default_variant.upper().replace("-", "_")
return self.raw.get(f"{prefix}_{suffix}") or self.raw.get(suffix)
@property
def dotnet_version(self) -> Optional[str]:
return self.raw.get("DOTNET_VERSION")
@property
def cmake_version(self) -> Optional[str]:
return self.raw.get("CMAKE_VERSION")
# ---------------------------------------------------------------------------
# Platform
# ---------------------------------------------------------------------------
@dataclass
class Platform:
"""Host OS / arch / devcontainer detection plus a few helpers.
The two helpers worth highlighting:
- cmake_toolchain_args(): returns the right `-DCMAKE_TOOLCHAIN_FILE=...
-DVCPKG_TARGET_TRIPLET=...` flags on Windows; returns [] inside the
devcontainer (CMAKE_PREFIX_PATH=/opt/vcpkg/active is preset by the
Dockerfile) and on macOS / Linux (system protobuf is on PATH).
- windows_msvc_wrap(cmd): on Windows wraps the command in
`cmd /c "<vcvarsall> x64 && <cmd>"` so MSVC env lives only in the
single child process. On all other OSes returns cmd unchanged.
"""
sys_platform: str
machine: str
in_devcontainer: bool
vcpkg_root: Optional[Path] = None
vcvarsall_path: Optional[Path] = None
protoc_tools_dir: Optional[Path] = None
vcpkg_installed_dir: Optional[Path] = None # manifest mode only
@classmethod
def detect(cls) -> "Platform":
sys_platform = sys.platform
machine = _stdlib_platform.machine().lower()
# Devcontainer signal: only the marker the .devcontainer/Dockerfile
# actually sets. /.dockerenv is created by Docker for EVERY container
# and is too broad — using it would silently no-op `setup` in any
# plain Docker container.
in_devcontainer = Path("/opt/vcpkg/active").exists()
return cls(
sys_platform=sys_platform,
machine=machine,
in_devcontainer=in_devcontainer,
)
@property
def is_windows(self) -> bool:
return self.sys_platform.startswith("win")
@property
def is_macos(self) -> bool:
return self.sys_platform == "darwin"
@property
def is_linux(self) -> bool:
return self.sys_platform.startswith("linux")
@property
def vcpkg_triplet(self) -> str:
"""Default vcpkg triplet for the host. Mirrors the Dockerfile lines 32-36."""
if self.is_windows:
return "x64-windows-static"
if self.is_macos:
if self.machine in ("arm64", "aarch64"):
return "arm64-osx"
return "x64-osx"
if self.is_linux:
if self.machine in ("arm64", "aarch64"):
return "arm64-linux"
return "x64-linux"
return "x64-linux"
def cmake_toolchain_args(
self, triplet: Optional[str] = None, force_vcpkg: bool = False
) -> list[str]:
"""Extra cmake -D flags to pick up vcpkg's protobuf, when applicable.
Default behaviour:
- devcontainer: [] (Dockerfile presets CMAKE_PREFIX_PATH).
- any host with VCPKG_ROOT: toolchain + triplet flags.
- host without vcpkg installed (Linux/macOS, no setup run yet):
[] (cmake will fall back to system
protobuf via find_package).
force_vcpkg=True forces the toolchain flags even inside the
devcontainer (used by manifest-mode invocations that point cmake
at a custom vcpkg_installed/ dir).
"""
if self.in_devcontainer and not force_vcpkg:
return [] # Dockerfile presets CMAKE_PREFIX_PATH=/opt/vcpkg/active.
# Locate VCPKG_ROOT (cached attr or env). If absent on macOS/Linux,
# we silently fall through to system protobuf (apt/brew). On Windows
# / manifest mode the cpp handler errors with an actionable message
# before we get here.
vcpkg_root = self.vcpkg_root or _env_path("VCPKG_ROOT")
if vcpkg_root is None:
return []
toolchain = vcpkg_root / "scripts" / "buildsystems" / "vcpkg.cmake"
return [
f"-DCMAKE_TOOLCHAIN_FILE={toolchain}",
f"-DVCPKG_TARGET_TRIPLET={triplet or self.vcpkg_triplet}",
]
def windows_msvc_wrap(self, cmd: list[str]) -> list[str]:
"""Wrap a command so it runs inside an MSVC-environment subshell.
On Windows, returns a single-element list containing one cmd-shell
command string of the form:
'call "<vcvarsall>" x64 >nul && <quoted cmd>'
Runner.run() detects the [windows-shell-string] pattern (via the
first element having no path separators but containing spaces) and
passes it to subprocess with shell=True so cmd parses it natively
— bypassing Python's Win32 CreateProcess quoting that otherwise
backslash-escapes the inner double quotes and breaks cmd's parser.
On every other OS this returns `cmd` unchanged.
"""
if not self.is_windows:
return cmd
vcvars = self.vcvarsall_path or locate_vcvarsall()
if vcvars is None:
# No MSVC available — return cmd unchanged so the caller's
# subprocess fails with a useful "cl.exe not found" error rather
# than a more confusing wrapping failure.
return cmd
self.vcvarsall_path = vcvars
inner = " ".join(_winquote(arg) for arg in cmd)
# Single string: `call "<vcvars>" x64 >nul && <inner>`.
# `call` is required so vcvarsall returns control to our `&&`.
# `>nul` swallows vcvarsall's banner (cosmetic).
line = f'call "{vcvars}" x64 >nul && {inner}'
return [_WIN_SHELL_MARKER + line]
# Sentinel prefix used to flag a Runner.run() argv as "single shell string,
# please run via cmd". Using a prefix instead of a separate kwarg keeps
# windows_msvc_wrap() composable with the rest of the runner pipeline.
_WIN_SHELL_MARKER = "\x00CMDSHELL\x00"
def _env_path(name: str) -> Optional[Path]:
v = os.environ.get(name)
return Path(v) if v else None
def _winquote(arg: str) -> str:
"""Quote an argument for the Windows cmd shell.
cmd's quoting is famously bad. Wrap in double quotes if there is any
whitespace, ampersand, or other shell metacharacter.
"""
if not arg:
return '""'
if any(c in arg for c in ' &|<>^"()'):
# Escape embedded quotes by doubling them (cmd convention).
return '"' + arg.replace('"', '""') + '"'
return arg
def locate_vcvarsall() -> Optional[Path]:
"""Find vcvarsall.bat on a Windows host.
Strategy:
1. Try vswhere.exe (the canonical method since VS 2017).
2. Fall back to a few well-known install paths.
Returns None if MSVC isn't installed.
"""
if sys.platform != "win32":
return None
pf86 = os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)")
pf = os.environ.get("ProgramFiles", r"C:\Program Files")
for base in (pf86, pf):
vswhere = Path(base) / "Microsoft Visual Studio" / "Installer" / "vswhere.exe"
if vswhere.is_file():
try:
out = subprocess.run(
[
str(vswhere),
"-latest",
"-products",
"*",
"-requires",
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"-property",
"installationPath",
],
capture_output=True,
text=True,
check=False,
)
inst = out.stdout.strip().splitlines()
if inst:
candidate = (
Path(inst[0]) / "VC" / "Auxiliary" / "Build" / "vcvarsall.bat"
)
if candidate.is_file():
return candidate
except OSError:
pass
# Hardcoded fallbacks for VS 2022 Build Tools / Community / Enterprise.
for base in (pf86, pf):
for edition in ("BuildTools", "Community", "Professional", "Enterprise"):
candidate = (
Path(base)
/ "Microsoft Visual Studio"
/ "2022"
/ edition
/ "VC"
/ "Auxiliary"
/ "Build"
/ "vcvarsall.bat"
)
if candidate.is_file():
return candidate
return None
# ---------------------------------------------------------------------------
# Runner — subprocess helper
# ---------------------------------------------------------------------------
@dataclass
class Runner:
"""Thin wrapper around subprocess.run with verbose / dry-run support."""
verbose: bool = False
dry_run: bool = False
def run(
self,
cmd: list[str],
cwd: Optional[Path] = None,
env: Optional[dict[str, str]] = None,
check: bool = True,
shell: bool = False,
) -> int:
# Detect the windows_msvc_wrap sentinel: a single-element argv whose
# value starts with _WIN_SHELL_MARKER. Strip the marker and run via
# cmd's native parser (shell=True) so Python's Win32 CreateProcess
# quoting doesn't mangle the embedded double quotes.
if (
len(cmd) == 1
and isinstance(cmd[0], str)
and cmd[0].startswith(_WIN_SHELL_MARKER)
):
line = cmd[0][len(_WIN_SHELL_MARKER) :]
location = f" (cwd={cwd})" if cwd else ""
if self.dry_run:
print(f"[dry-run] {line}{location}")
return 0
if self.verbose:
print(f"[run-shell] {line}{location}")
proc = subprocess.run(
line,
cwd=str(cwd) if cwd else None,
env=env,
check=False,
shell=True,
)
if check and proc.returncode != 0:
raise SystemExit(
f"[error] command failed with exit code {proc.returncode}: {line}"
)
return proc.returncode
printable = " ".join(_winquote(c) if " " in c else c for c in cmd)
location = f" (cwd={cwd})" if cwd else ""
if self.dry_run:
print(f"[dry-run] {printable}{location}")
return 0
if self.verbose:
print(f"[run] {printable}{location}")
proc = subprocess.run(
cmd,
cwd=str(cwd) if cwd else None,
env=env,
check=False,
shell=shell,
)
if check and proc.returncode != 0:
raise SystemExit(
f"[error] command failed with exit code {proc.returncode}: {printable}"
)
return proc.returncode
def rmtree(self, path: Path) -> None:
if self.dry_run:
# Always announce the wipe in dry-run, even if path is missing —
# it's the orchestrator's *intent* we want to capture.
print(f"[dry-run] rm -rf {path}")
return
if not path.exists():
return
if self.verbose:
print(f"[rm-rf] {path}")
shutil.rmtree(path, ignore_errors=True)
def mkdirp(self, path: Path) -> None:
if self.dry_run:
print(f"[dry-run] mkdir -p {path}")
return
if path.exists():
return
if self.verbose:
print(f"[mkdir-p] {path}")
path.mkdir(parents=True, exist_ok=True)
# ---------------------------------------------------------------------------
# Repo root discovery
# ---------------------------------------------------------------------------
def find_repo_root(start: Optional[Path] = None) -> Path:
"""Locate the repo root by walking upward to find .devcontainer/versions.env."""
here = (start or Path(__file__).resolve()).parent if start is None else start
here = here.resolve()
for candidate in [here, *here.parents]:
if (candidate / ".devcontainer" / "versions.env").is_file():
return candidate
raise SystemExit(
"[error] Could not locate repo root (no .devcontainer/versions.env found)."
)
# ---------------------------------------------------------------------------
# ~/.loader-env.json — Windows toolchain cache
# ---------------------------------------------------------------------------
LOADER_ENV_PATH = Path.home() / ".loader-env.json"
def load_loader_env() -> dict:
if not LOADER_ENV_PATH.is_file():
return {}
try:
return json.loads(LOADER_ENV_PATH.read_text(encoding="utf-8"))
except (OSError, ValueError):
return {}
def save_loader_env(data: dict, runner: Runner) -> None:
text = json.dumps(data, indent=2, sort_keys=True)
if runner.dry_run:
print(f"[dry-run] write {LOADER_ENV_PATH}: {text}")
return
LOADER_ENV_PATH.write_text(text, encoding="utf-8")
# ---------------------------------------------------------------------------
# vcpkg baseline lookup
# ---------------------------------------------------------------------------
#
# Pinning protobuf via `overrides` while leaving `builtin-baseline` on a newer
# vcpkg commit lets transitive deps (abseil/utf8-range/re2/...) drift forward
# and breaks ABI (e.g. missing `absl::if_constexpr`). So for protobuf versions
# vcpkg's baseline.json knows about (>= 3.14.0), we pin the baseline itself to
# the vcpkg commit whose `versions/baseline.json` had our target protobuf —
# that whole snapshot is by construction self-consistent.
#
# Resolution: `git log -S '"X.Y.Z"' -- versions/baseline.json` → for each hit,
# verify `default.protobuf.baseline == X.Y.Z` at that commit (guards against
# the literal appearing in unrelated ports). Results cached in
# ~/.loader-env.json under "vcpkg_baseline_for_protobuf".
#
# Exception — protobuf < 3.14.0 (predates baseline.json, so no snapshot can
# carry it): we pin `builtin-baseline` at the *floor* (3.14.0) commit and add
# an `overrides` entry for the older protobuf. The ABI-drift hazard above does
# NOT apply here because pre-3.14 protobuf is dependency-light (no abseil /
# utf8-range / re2), and 3.14.0 is the oldest baseline available, so its
# transitive deps are already as old as vcpkg knows about. See
# `_resolve_vcpkg_protobuf_pin`.
def _resolve_vcpkg_baseline_for_protobuf(
protobuf_version: str,
vcpkg_root: Path,
runner: Runner,
) -> str:
"""Find the vcpkg commit whose baseline.json has protobuf == protobuf_version.
Raises RuntimeError if no such commit can be found in the local vcpkg
checkout — caller should surface a clear error and suggest `git fetch`
or a different --protobuf-version.
"""
# Cache hit?
cache = load_loader_env()
cached_map = cache.get("vcpkg_baseline_for_protobuf", {}) or {}
cached = cached_map.get(protobuf_version)
if cached:
# Validate the cached commit still has the expected protobuf — the
# user might have re-cloned vcpkg or rewritten history.
if _vcpkg_baseline_has_protobuf(vcpkg_root, cached, protobuf_version):
return cached
# Cache is stale; fall through to re-resolve.
cached_map.pop(protobuf_version, None)
if runner.dry_run:
# Dry-run: don't shell out to git, return a placeholder so snapshot
# tests can still verify the command sequence.
return f"<baseline-for-protobuf-{protobuf_version}>"
if not (vcpkg_root / ".git").exists():
raise RuntimeError(
f"{vcpkg_root} is not a git checkout; cannot resolve a vcpkg "
f"baseline commit for protobuf {protobuf_version}."
)
# Step 1: candidate commits via pickaxe search on the literal version string.
try:
out = subprocess.check_output(
[
"git",
"-C",
str(vcpkg_root),
"log",
"-S",
f'"{protobuf_version}"',
"--reverse",
"--format=%H",
"--",
"versions/baseline.json",
],
text=True,
)
except subprocess.CalledProcessError as e:
raise RuntimeError(
f"git log failed while searching vcpkg history for protobuf "
f"{protobuf_version}: {e}"
) from e
candidates = [line.strip() for line in out.splitlines() if line.strip()]
if not candidates:
raise RuntimeError(
f"No commit in {vcpkg_root} touches versions/baseline.json with "
f'the literal "{protobuf_version}". Possible causes:\n'
f" - your local vcpkg checkout is shallow / out-of-date "
f"(`git -C {vcpkg_root} fetch --unshallow origin master` may help)\n"
f" - protobuf {protobuf_version} was never the vcpkg baseline "
f"(use --vcpkg-baseline=<commit> to point at a custom snapshot)"
f"{_format_known_protobuf_versions_hint(vcpkg_root)}"
)
# Step 2: validate each candidate by reading baseline.json at that commit.
for sha in candidates:
if _vcpkg_baseline_has_protobuf(vcpkg_root, sha, protobuf_version):
cached_map[protobuf_version] = sha
cache["vcpkg_baseline_for_protobuf"] = cached_map
save_loader_env(cache, runner)
print(
f"[info] resolved vcpkg baseline for protobuf "
f"{protobuf_version} -> {sha[:12]}",
file=sys.stderr,
)
return sha
raise RuntimeError(
f'Found {len(candidates)} commits mentioning "{protobuf_version}" in '
f"versions/baseline.json but none had default.protobuf.baseline set "
f"to that exact version (the literal likely appears in unrelated "
f"ports). Pass --vcpkg-baseline=<commit> manually."
f"{_format_known_protobuf_versions_hint(vcpkg_root)}"
)
# Earliest protobuf version usable as a vcpkg `builtin-baseline`.
# vcpkg's versions/baseline.json was introduced 2021-01-21, and its very
# first commit already pinned `default.protobuf.baseline = "3.14.0"`. Port
# versions older than this (3.0.2, 3.2.0, ..., 3.13.0) exist in
# versions/p-/protobuf.json but were never the global baseline and so can't
# serve as builtin-baseline. The user-facing error message uses a rounder
# "~3.18" wording on purpose; the precise floor lives only here.
_VCPKG_PROTOBUF_BASELINE_FLOOR = (3, 14, 0)
# Hard *support* floor — distinct from the vcpkg baseline floor above. Below
# 3.8.0 the C++ tableau-loader / generated code is known not to compile, even
# though vcpkg can still fetch those ancient ports via `overrides`. We refuse
# such versions up-front (before touching the user's trees) unless --force is
# given — a user may deliberately pick an unsupported version to investigate
# the compile breakage. Must stay <= _VCPKG_PROTOBUF_BASELINE_FLOOR so the
# below-baseline (overrides) range it gates is non-empty.
_PROTOBUF_MIN_SUPPORTED = (3, 8, 0)
def _parse_protobuf_version_tuple(v: str) -> Optional[tuple[int, ...]]:
"""Parse "3.14.0" / "3.21.12" / "5.29.5" into a numeric tuple for ordering.
Returns None for anything that isn't pure dotted-numeric (defensive: the
registry has historically been numeric-only for protobuf, but we'd
rather drop a weird entry than crash a hint formatter).
"""
parts = v.split(".")
if not all(p.isdigit() for p in parts) or not parts:
return None
return tuple(int(p) for p in parts)
def _ge_supported_floor(v: str) -> bool:
"""True iff dotted version `v` parses and is >= the 3.8.0 support floor.
Unparseable versions are treated as not-below-floor-worthy (returns False)
so they're dropped from below-floor listings rather than crashing.
"""
tup = _parse_protobuf_version_tuple(v)
return tup is not None and tup >= _PROTOBUF_MIN_SUPPORTED
def _read_protobuf_registry_versions(vcpkg_root: Path) -> list[str]:
"""All protobuf versions in versions/p-/protobuf.json at HEAD.
Reads the file with one cheap `git show` — vcpkg's authoritative list of
every protobuf version the registry has ever known. Returned in registry
order (newest first), de-duplicated, and *unfiltered* (includes pre-3.14
versions). On any failure returns [] so callers can degrade gracefully.
"""
try:
blob = subprocess.check_output(
[
"git",
"-C",
str(vcpkg_root),
"show",
"HEAD:versions/p-/protobuf.json",
],
text=True,
stderr=subprocess.DEVNULL,
)
except (subprocess.CalledProcessError, FileNotFoundError):
return []
try:
data = json.loads(blob)
except ValueError:
return []
seen: set[str] = set()
versions: list[str] = []
for entry in data.get("versions", []):
v = (
entry.get("version")
or entry.get("version-semver")
or entry.get("version-string")
)
if not v or v in seen:
continue
seen.add(v)
versions.append(v)
return versions
def _list_known_protobuf_versions(vcpkg_root: Path) -> list[str]:
"""Return protobuf versions usable as a vcpkg `builtin-baseline`.
Same source as `_read_protobuf_registry_versions`, filtered to
`>= _VCPKG_PROTOBUF_BASELINE_FLOOR` so the caller can blindly surface every
entry as something the user can pass to `--protobuf-version` and have pinned
*as the baseline* (older versions are still usable, but only via overrides).
"""
versions: list[str] = []
for v in _read_protobuf_registry_versions(vcpkg_root):
tup = _parse_protobuf_version_tuple(v)
if tup is None or tup < _VCPKG_PROTOBUF_BASELINE_FLOOR:
continue
versions.append(v)
return versions
def _protobuf_version_in_registry(vcpkg_root: Path, version: str) -> bool:
"""True iff `version` appears in versions/p-/protobuf.json at HEAD.
This is the precondition for pinning it via `overrides`: vcpkg resolves an
override's version through the registry's version DB at the current
checkout, so a version absent here can never be installed.
"""
return version in set(_read_protobuf_registry_versions(vcpkg_root))
def _format_known_protobuf_versions_hint(
vcpkg_root: Path, include_below_floor: bool = False
) -> str:
"""Render a human-readable bullet listing known protobuf versions.
Returns "" (caller can append unconditionally) when the version DB
can't be read. Otherwise returns a leading "\n - known versions: ..."
suitable for tacking onto the end of a RuntimeError message. When
`include_below_floor` is set, below-baseline versions are listed too —
down to the hard support floor (3.8.0) and *without* truncation — so a user
who asked for a missing older version can see every version they could pin
via `overrides`. Versions below the support floor are omitted (they won't
compile, so there's no point suggesting them).
"""
if include_below_floor:
versions = [
v
for v in _read_protobuf_registry_versions(vcpkg_root)
if _ge_supported_floor(v)
]
else:
versions = _list_known_protobuf_versions(vcpkg_root)
if not versions:
return ""
# Newest first (registry order). For the below-floor listing show the full
# range down to 3.8.0 (no truncation); for the baseline-only listing keep
# the compact head + "(+N older)" form.
if include_below_floor:
listed = ", ".join(versions)
else:
head = ", ".join(versions[:20])
tail = f" (+{len(versions) - 20} older)" if len(versions) > 20 else ""
listed = f"{head}{tail}"
return (
f"\n - known protobuf versions in {vcpkg_root}/versions/p-/protobuf.json "
f"(newest first, down to the {_PROTOBUF_MIN_SUPPORTED_STR} support "
f"floor): {listed}\n"
f" - note: versions older than 3.14.0 predate vcpkg's baseline.json, "
f"so they're pinned via `overrides` on the 3.14.0 baseline rather than "
f"as builtin-baseline (handled automatically by --protobuf-version)\n"
f" - note: {_PROTOBUF_MIN_SUPPORTED_STR} is the minimum supported "
f"version; anything older won't compile (pass --force to try anyway)"
)
def _vcpkg_baseline_has_protobuf(
vcpkg_root: Path, commit_sha: str, expected_version: str
) -> bool:
"""Read versions/baseline.json at commit_sha; return True iff protobuf matches."""
try:
blob = subprocess.check_output(
[
"git",
"-C",
str(vcpkg_root),
"show",
f"{commit_sha}:versions/baseline.json",
],
text=True,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError:
return False
try:
data = json.loads(blob)
except ValueError:
return False
entry = data.get("default", {}).get("protobuf", {})
return entry.get("baseline") == expected_version
# The floor expressed as a dotted string ("3.14.0"), derived once from the
# tuple so the two never drift apart. This is the baseline we pin when a
# requested protobuf version predates vcpkg's baseline.json.
_VCPKG_PROTOBUF_BASELINE_FLOOR_STR = ".".join(
str(n) for n in _VCPKG_PROTOBUF_BASELINE_FLOOR
)
# The hard support floor as a dotted string ("3.8.0"), derived from the tuple.
_PROTOBUF_MIN_SUPPORTED_STR = ".".join(str(n) for n in _PROTOBUF_MIN_SUPPORTED)
def _check_protobuf_min_supported(protobuf_version: str, force: bool) -> None:
"""Guard against protobuf versions below the hard support floor (3.8.0).
Below this floor the C++ loader / generated code is known not to compile.
Purely a version-number check (no vcpkg checkout needed), so it runs first
— before any baseline resolution or tree wipe.
- version >= floor (or unparseable): no-op.
- version < floor, force=False: raise RuntimeError (caller fails fast).
- version < floor, force=True: warn loudly and continue, so the user
can reproduce / investigate the break.
"""
tup = _parse_protobuf_version_tuple(protobuf_version)
if tup is None or tup >= _PROTOBUF_MIN_SUPPORTED:
return
if force:
print(
f"[warn] protobuf {protobuf_version} is below the supported floor "
f"{_PROTOBUF_MIN_SUPPORTED_STR} and is NOT expected to compile; "
f"proceeding anyway because --force was given",
file=sys.stderr,
)
return
raise RuntimeError(
f"protobuf {protobuf_version} is below the minimum supported version "
f"{_PROTOBUF_MIN_SUPPORTED_STR}; the C++ loader is not expected to "
f"compile against it, so the build is refused. Pass --force (-f) to "
f"attempt it anyway (e.g. to investigate the compile failure)."
)
@dataclass
class VcpkgProtobufPin:
"""How to pin protobuf into a generated vcpkg.json manifest.
baseline: value for the manifest's ``builtin-baseline``.
override_version: when set, the requested protobuf predates vcpkg's
baseline.json, so it is pinned via an ``overrides``
entry layered on top of the floor ``baseline``. ``None``
means the baseline snapshot already carries the exact
protobuf version (no ``overrides`` needed).
"""
baseline: str
override_version: Optional[str] = None
def _resolve_below_floor_override(
protobuf_version: str,
vcpkg_root: Path,
runner: Runner,
) -> Optional[str]:
"""Decide whether `protobuf_version` must be pinned via `overrides`.
Returns the version string when it predates the baseline.json floor (so the
caller should layer an `overrides` entry on the floor baseline), or None
when it is >= floor (pinned as the baseline itself).
Raises RuntimeError when the version is below the floor *and* absent from
the vcpkg versions registry — letting callers fail fast (before wiping
codegen/build trees) instead of rendering a manifest that `vcpkg install`
would only reject minutes later. Skipped in dry-run, where there is no real
checkout to consult.
"""
tup = _parse_protobuf_version_tuple(protobuf_version)
if tup is None or tup >= _VCPKG_PROTOBUF_BASELINE_FLOOR:
return None
if not runner.dry_run and not _protobuf_version_in_registry(
vcpkg_root, protobuf_version
):
raise RuntimeError(
f"protobuf {protobuf_version} predates vcpkg's baseline.json floor "
f"({_VCPKG_PROTOBUF_BASELINE_FLOOR_STR}) and is not present in "
f"{vcpkg_root}/versions/p-/protobuf.json, so it cannot be pinned via "
f"`overrides`. Possible causes:\n"
f" - typo in --protobuf-version\n"
f" - your local vcpkg checkout is shallow / out-of-date "
f"(`git -C {vcpkg_root} fetch origin master` may help)"
f"{_format_known_protobuf_versions_hint(vcpkg_root, include_below_floor=True)}"
)
return protobuf_version
def _resolve_vcpkg_protobuf_pin(
protobuf_version: str,
vcpkg_root: Path,
runner: Runner,
) -> VcpkgProtobufPin:
"""Resolve baseline (+ optional override) for a requested protobuf version.
protobuf >= floor (3.14.0): pin the self-consistent baseline whose
``versions/baseline.json`` carries that exact version — no ``overrides``
(see module comment on transitive-dep ABI drift).
protobuf < floor: pin the floor baseline and pin the older protobuf via
``overrides``. Safe because pre-3.14 protobuf has no abseil/utf8-range/re2
dependency, and the floor is the oldest baseline vcpkg has, so nothing
drifts forward. Raises RuntimeError early if the version doesn't exist.
"""
override = _resolve_below_floor_override(protobuf_version, vcpkg_root, runner)
if override is not None:
baseline = _resolve_vcpkg_baseline_for_protobuf(
_VCPKG_PROTOBUF_BASELINE_FLOOR_STR, vcpkg_root, runner
)
print(
f"[info] protobuf {protobuf_version} predates vcpkg's baseline.json "
f"(floor {_VCPKG_PROTOBUF_BASELINE_FLOOR_STR}); pinning baseline "
f"{baseline[:12]} and adding overrides protobuf={protobuf_version}",
file=sys.stderr,
)
return VcpkgProtobufPin(baseline=baseline, override_version=override)
baseline = _resolve_vcpkg_baseline_for_protobuf(protobuf_version, vcpkg_root, runner)
return VcpkgProtobufPin(baseline=baseline)
def hydrate_platform_from_env(plat: Platform) -> None:
"""Populate Platform from $VCPKG_ROOT and ~/.loader-env.json.
Cross-platform: macOS / Linux / Windows. Cache file is shared between
`make.py setup` (which writes it) and subsequent `make.py test`
invocations (which read it).
"""
if plat.vcpkg_root is None:
plat.vcpkg_root = _env_path("VCPKG_ROOT")
cache = load_loader_env()
if plat.vcpkg_root is None and cache.get("vcpkg_root"):
plat.vcpkg_root = Path(cache["vcpkg_root"])
if plat.protoc_tools_dir is None and cache.get("protoc_tools_dir"):
plat.protoc_tools_dir = Path(cache["protoc_tools_dir"])
if plat.vcpkg_installed_dir is None and cache.get("vcpkg_installed_dir"):
plat.vcpkg_installed_dir = Path(cache["vcpkg_installed_dir"])
# vcvarsall is Windows-only.
if plat.is_windows:
if plat.vcvarsall_path is None and cache.get("vcvarsall_path"):
candidate = Path(cache["vcvarsall_path"])
if candidate.is_file():
plat.vcvarsall_path = candidate
# ---------------------------------------------------------------------------
# Setup commands
# ---------------------------------------------------------------------------
LANGS_ALL = ("go", "cpp", "csharp")
def _which(name: str) -> Optional[str]:
return shutil.which(name)
def _prepend_path(directory: Path) -> None:
"""Prepend ``directory`` to ``os.environ['PATH']`` (idempotent, in-process).
This makes a freshly-installed tool visible to (a) subsequent ``_which``
probes inside the same ``setup`` run and (b) every child process spawned
by ``Runner`` afterwards (we never override ``env=``, so they inherit
``os.environ``). No-op if the directory is already on PATH.
"""
p = str(directory)
sep = os.pathsep
current = os.environ.get("PATH", "")
parts = current.split(sep) if current else []
# Case-insensitive comparison on Windows; exact match elsewhere.
norm = (lambda s: s.lower()) if os.name == "nt" else (lambda s: s)
already_on_path = any(norm(x) == norm(p) for x in parts if x)
# GitHub Actions: persist the new entry across subsequent steps so that
# tools installed by `make.py setup` are visible to later `run:` commands.
# This must run even when `directory` is already on the in-process PATH:
# being present on *this* process's PATH (e.g. because an earlier setup
# call already prepended it) says nothing about whether subsequent GitHub
# Actions steps — which are fresh processes — will see it. So append to
# GITHUB_PATH before the early-return that skips the in-process update.
gha_path = os.environ.get("GITHUB_PATH")
if gha_path:
with open(gha_path, "a", encoding="utf-8") as fh:
fh.write(p + "\n")
if already_on_path:
return
os.environ["PATH"] = p + (sep + current if current else "")
def cmd_setup(args, ctx: "Context") -> int:
"""Install host toolchains. OS-dispatched. Idempotent."""
if ctx.platform.in_devcontainer:
print("[info] Running inside devcontainer; toolchain already installed.")
return 0
langs = _resolve_langs(args.lang)
print(f"[info] Setting up host toolchain for: {', '.join(langs)}")
print(f"[info] Pinned versions: {ctx.versions.raw}")