pyodide/pyodide-build/pyodide_build/pypabuild.py

106 lines
3.3 KiB
Python
Raw Normal View History

Use pypa/build (#2272) This resolves #2189. > build isolation would be a bit difficult to use in our case, as for instance > when building scipy we need the patched numpy on the host and not the numpy > version specified in pyproject.toml (which would be unpatched) This is indeed the case, certain packages cannot be isolated. My strategy is to make a list of packages that shouldn't be isolated and add symlinks from the isolated build environment into the `.artifacts` directory to "unisolate" them. Then we remove the unisolated package requirements from the list of packages to install, in case pesky constraints aren't satisfied. In particular, packages that expect to be used with `pypa/build` often feel free to put very specific constraints on their build dependencies (often asking them to be == to a particular version). Specific version constraints is good for build reproducibility and with build isolation doesn't cost anything. So we just ignore the constraints. Hopefully nothing goes wrong. In particular, any package that does stuff both at build time and at runtime and requires synchronization between the build time and run time environments needs the unisolation. This includes cffi with `_cffi_backend.so`, and of course numpy and scipy. pycparser needs to be unisolated because it is a dependency of cffi. Currently I have also unisolated pythran and cython, though these are build time only tools and do not really need to be unisolated. Cython I unisolated specifically because numcodecs needs it but it isn't in the numcodecs build dependencies. Pythran I unisolated because of a problem with the scipy build which I don't fully understand (some problem with long double feature detection).
2022-03-22 05:05:30 +00:00
import contextlib
import os
import sys
import traceback
from itertools import chain
from pathlib import Path
from typing import Mapping
from build import BuildBackendException, ProjectBuilder # type: ignore[import]
from build.__main__ import ( # type: ignore[import]
_STYLES,
_error,
_handle_build_error,
_IsolatedEnvBuilder,
_ProjectBuilder,
)
from build.env import IsolatedEnv # type: ignore[import]
from packaging.requirements import Requirement
from .common import get_hostsitepackages, get_pyversion
UNISOLATED_PACKAGES = ["numpy", "scipy", "cffi", "pycparser", "pythran", "cython"]
def symlink_unisolated_packages(env: IsolatedEnv):
pyversion = get_pyversion()
site_packages_path = f"lib/{pyversion}/site-packages"
env_site_packages = Path(env._path) / site_packages_path
host_site_packages = Path(get_hostsitepackages())
for name in UNISOLATED_PACKAGES:
for path in chain(
host_site_packages.glob(f"{name}*"), host_site_packages.glob(f"_{name}*")
):
(env_site_packages / path.name).unlink(missing_ok=True)
(env_site_packages / path.name).symlink_to(path)
def remove_unisolated_requirements(requires: set[str]) -> set[str]:
for reqstr in list(requires):
req = Requirement(reqstr)
for avoid_name in UNISOLATED_PACKAGES:
if avoid_name in req.name:
requires.remove(reqstr)
return requires
@contextlib.contextmanager
def replace_env(build_env: Mapping[str, str]):
old_environ = dict(os.environ)
os.environ.clear()
os.environ.update(build_env)
try:
yield
finally:
os.environ.clear()
os.environ.update(old_environ)
def install_reqs(env: IsolatedEnv, reqs: set[str]):
env.install(remove_unisolated_requirements(reqs))
def _build_in_isolated_env(
build_env: Mapping[str, str],
builder: ProjectBuilder,
outdir: str,
distribution: str,
) -> str:
with _IsolatedEnvBuilder() as env:
builder.python_executable = env.executable
builder.scripts_dir = env.scripts_dir
# first install the build dependencies
symlink_unisolated_packages(env)
install_reqs(env, builder.build_system_requires)
installed_requires_for_build = False
try:
build_reqs = builder.get_requires_for_build(distribution)
except BuildBackendException:
pass
else:
install_reqs(env, build_reqs)
installed_requires_for_build = True
with replace_env(build_env):
if not installed_requires_for_build:
install_reqs(env, builder.get_requires_for_build(distribution))
return builder.build(distribution, outdir, {})
def build(build_env: Mapping[str, str]):
srcdir = Path.cwd()
outdir = srcdir / "dist"
builder = _ProjectBuilder(srcdir)
distribution = "wheel"
try:
with _handle_build_error():
built = _build_in_isolated_env(
build_env, builder, str(outdir), distribution
)
print("{bold}{green}Successfully built {}{reset}".format(built, **_STYLES))
except Exception as e: # pragma: no cover
tb = traceback.format_exc().strip("\n")
print("\n{dim}{}{reset}\n".format(tb, **_STYLES))
_error(str(e))
sys.exit(1)