pheon/setup.py
Jan Petykiewicz a723ba2265 Simplify Python bindings and package builds
Read each input path through one opened stream, including gzip detection, and
use direct numeric converter lookup while preserving byte-order handling and
caller data. Read the package version from installed metadata and report range
errors consistently as ValueError.

Require the supplied Cargo.lock instead of generating it during builds, use
the standard sdist command, and update package checks and development guides.

Validation: 15 Python tests pass against source and the rebuilt installed
wheel; the source archive and platform wheel build successfully.
2026-11-16 20:44:56 -08:00

67 lines
2.2 KiB
Python

"""Bundle Cargo's C ABI library in a platform wheel, without a Python ABI tie."""
import json
from pathlib import Path
import shutil
import subprocess
import sys
import tomllib
from setuptools import Distribution, setup
from setuptools.command.build_py import build_py
from wheel.bdist_wheel import bdist_wheel
ROOT = Path(__file__).resolve().parent
VERSION = tomllib.loads((ROOT / "Cargo.toml").read_text())["package"]["version"]
if not (ROOT / "Cargo.lock").is_file():
raise FileNotFoundError("Source builds require the supplied Cargo.lock")
class CargoBuild(build_py):
def run(self):
super().run()
result = subprocess.run(
["cargo", "build", "--release", "--lib", "--locked", "--message-format=json"],
cwd=ROOT, capture_output=True, text=True,
)
sys.stderr.write(result.stderr)
library = None
for line in result.stdout.splitlines():
try:
message = json.loads(line)
except json.JSONDecodeError:
print(line, file=sys.stderr)
continue
if message.get("reason") == "compiler-message":
sys.stderr.write(message["message"].get("rendered") or "")
if (message.get("reason") == "compiler-artifact"
and message["target"]["name"] == "pheon"
and "cdylib" in message["target"]["crate_types"]):
for filename in message["filenames"]:
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")
shutil.copy2(library, Path(self.build_lib) / "pheon" / library.name)
class NativeDistribution(Distribution):
def has_ext_modules(self):
return True
class NativeWheel(bdist_wheel):
def get_tag(self):
# CFFI ABI mode needs the host platform, but no CPython extension ABI.
_, _, platform = super().get_tag()
return "py3", "none", platform
setup(
version=VERSION,
distclass=NativeDistribution,
cmdclass={"build_py": CargoBuild, "bdist_wheel": NativeWheel},
)