| 1 |
"""Install PEP 517 build requirements into the active Python environment.""" |
| 2 |
|
| 3 |
import importlib |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
if sys.version_info < (3, 11): |
| 11 |
subprocess.run( |
| 12 |
[sys.executable, "-m", "pip", "install", "tomli"], check=True |
| 13 |
) |
| 14 |
|
| 15 |
tomllib = importlib.import_module( |
| 16 |
"tomllib" if sys.version_info >= (3, 11) else "tomli" |
| 17 |
) |
| 18 |
project_dir = Path(__file__).resolve().parents[2] |
| 19 |
with (project_dir / "pyproject.toml").open("rb") as fid: |
| 20 |
requirements = tomllib.load(fid)["build-system"]["requires"] |
| 21 |
|
| 22 |
subprocess.run( |
| 23 |
[sys.executable, "-m", "pip", "install", *requirements], check=True |
| 24 |
) |
| 25 |
|
| 26 |
# cibuildwheel creates its virtual environment below a randomized temporary |
| 27 |
# directory on macOS and Windows. Keep pybind11's headers at a stable path so |
| 28 |
# that the absolute include directory does not invalidate compiler cache keys. |
| 29 |
if "CCACHE_DIR" in os.environ: |
| 30 |
pybind11 = importlib.import_module("pybind11") |
| 31 |
if pybind11.__file__ is None: |
| 32 |
raise RuntimeError("Cannot locate the installed pybind11 package") |
| 33 |
pybind11_source = Path(pybind11.__file__).parent |
| 34 |
pybind11_destination = ( |
| 35 |
Path(os.environ["CCACHE_DIR"]).parent |
| 36 |
/ "build-requirements" |
| 37 |
/ "pybind11" |
| 38 |
) |
| 39 |
shutil.rmtree(pybind11_destination, ignore_errors=True) |
| 40 |
shutil.copytree(pybind11_source, pybind11_destination) |