Build Windows wheels locally and document release builds

This commit is contained in:
Jan Petykiewicz 2026-11-17 15:07:51 -08:00
commit 58adf0cc99

View file

@ -1,5 +1,6 @@
"""Bundle Cargo's C ABI library in a platform wheel, without a Python ABI tie."""
import json
import os
from pathlib import Path
import shutil
import subprocess
@ -13,6 +14,11 @@ from wheel.bdist_wheel import bdist_wheel
ROOT = Path(__file__).resolve().parent
VERSION = tomllib.loads((ROOT / "Cargo.toml").read_text())["package"]["version"]
BUILD_TARGET = os.environ.get("PHEON_BUILD_TARGET")
WINDOWS_TARGET = "x86_64-pc-windows-gnu"
if BUILD_TARGET is not None and BUILD_TARGET != WINDOWS_TARGET:
raise ValueError(f"PHEON_BUILD_TARGET must be {WINDOWS_TARGET}, got {BUILD_TARGET!r}")
if not (ROOT / "Cargo.lock").is_file():
@ -22,8 +28,11 @@ if not (ROOT / "Cargo.lock").is_file():
class CargoBuild(build_py):
def run(self):
super().run()
command = ["cargo", "build", "--release", "--lib", "--locked", "--message-format=json"]
if BUILD_TARGET:
command.extend(["--target", BUILD_TARGET])
result = subprocess.run(
["cargo", "build", "--release", "--lib", "--locked", "--message-format=json"],
command,
cwd=ROOT, capture_output=True, text=True,
)
sys.stderr.write(result.stderr)
@ -43,8 +52,8 @@ class CargoBuild(build_py):
if Path(filename).suffix in (".so", ".dylib", ".dll"):
library = Path(filename)
result.check_returncode()
if library is None:
raise RuntimeError("Cargo did not produce the pheon shared library")
if library is None or (BUILD_TARGET and library.name != "pheon.dll"):
raise RuntimeError("Cargo did not produce the expected Pheon shared library")
shutil.copy2(library, Path(self.build_lib) / "pheon" / library.name)
@ -55,7 +64,9 @@ class NativeDistribution(Distribution):
class NativeWheel(bdist_wheel):
def get_tag(self):
# CFFI ABI mode needs the host platform, but no CPython extension ABI.
# CFFI ABI mode needs a platform tag and no CPython extension ABI.
if BUILD_TARGET == WINDOWS_TARGET:
return "py3", "none", "win_amd64"
_, _, platform = super().get_tag()
return "py3", "none", platform
@ -64,4 +75,5 @@ setup(
version=VERSION,
distclass=NativeDistribution,
cmdclass={"build_py": CargoBuild, "bdist_wheel": NativeWheel},
options={"build": {"build_base": f"build/{BUILD_TARGET}"}} if BUILD_TARGET else {},
)