native_build.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Native compiler environment for dependency builds from a source checkout.
  2. Windows ARM64 has no wheel for parts of the locked closure (cryptography), so
  3. every sync there compiles from sdists and needs MSVC, Clang, Rust and static
  4. OpenSSL. PM owns the sync, so PM prepares that environment. Otherwise only
  5. callers that remembered to (source activation) could build, and
  6. install.ps1, `hermes update` and repair failed in openssl-sys.
  7. """
  8. from __future__ import annotations
  9. from collections.abc import Mapping
  10. import json
  11. import os
  12. from pathlib import Path
  13. import shutil
  14. import subprocess
  15. import tempfile
  16. from pm.progress import run_contained
  17. _PROVIDER = Path("scripts/build/windows-deps.ps1")
  18. def prepare_windows_environment(*, source: Path, state: Path, env: Mapping[str, str]) -> dict[str, str]:
  19. """The distribution adapter decides whether this target needs ARM64 tools."""
  20. shell = shutil.which("powershell", path=env.get("PATH")) or shutil.which("pwsh", path=env.get("PATH"))
  21. if shell is None:
  22. raise FileNotFoundError("PowerShell is required to prepare Windows ARM64 build dependencies")
  23. state.mkdir(parents=True, exist_ok=True)
  24. with tempfile.TemporaryDirectory(prefix="environment-", dir=state) as scratch:
  25. output = Path(scratch) / "environment.json"
  26. # A cold vcpkg clone and OpenSSL build print thousands of lines; the
  27. # user needs the step and its failure, not the patch log.
  28. run_contained(
  29. [shell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File",
  30. str(source / _PROVIDER), "-StateRoot", str(state),
  31. "-EnvironmentFile", str(output)],
  32. "Preparing Windows ARM64 build tools", indent=" ",
  33. cwd=source, env=dict(env), stdin=subprocess.DEVNULL,
  34. )
  35. prepared = json.loads(output.read_text(encoding="utf-8-sig"))
  36. if not isinstance(prepared, dict) or any(not isinstance(k, str) or not isinstance(v, str) for k, v in prepared.items()):
  37. raise ValueError("Windows build dependency provider returned an invalid environment")
  38. return prepared
  39. def source_build_environment(source: Path) -> dict[str, str] | None:
  40. """The environment a dependency build of ``source`` needs on this host.
  41. None means the ambient environment suffices. Only a checkout carries the
  42. provider; a payload's dependencies are prebuilt, so it needs no compiler.
  43. The state root is the store's parent, the one source setup has always
  44. used, so an existing vcpkg/OpenSSL build is reused rather than repeated.
  45. """
  46. from pm.paths import store_root
  47. from pm.store import current_target
  48. if current_target() != "win32-arm64" or not (source / _PROVIDER).is_file():
  49. return None
  50. from pm.index_config import bridged_index_settings
  51. prepared = prepare_windows_environment(source=source, state=store_root().parent, env=os.environ)
  52. # managed_environment translates pip's index knobs only for the ambient
  53. # environment; this one replaces it, so mirrors must ride along.
  54. prepared.update(bridged_index_settings(os.environ))
  55. return prepared