mirror of
https://github.com/astral-sh/uv.git
synced 2025-10-09 18:12:07 +00:00
A minimal build backend for uv: uv_build (#11446)
uv itself is a large package with many dependencies and lots of features. To build a package using the uv build backend, you shouldn't have to download and install the entirety of uv. For platform where we don't provide wheels, it should be possible and fast to compile the uv build backend. To that end, we're introducing a python package that contains a trimmed down version of uv that only contains the build backend, with a minimal dependency tree in rust. The `uv_build` package is publish from CI just like uv itself. It is part of the workspace, but has much less dependencies for its own binary. We're using cargo deny to enforce that the network stack is not part of the dependencies. A new build profile ensure we're getting the minimum possible binary size for a rust binary. --------- Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
parent
d4a805544f
commit
bf4c7afe8b
26 changed files with 842 additions and 96 deletions
|
@ -1,33 +1,8 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
if os.environ.get("UV_PREVIEW"):
|
||||
from ._build_backend import *
|
||||
from ._find_uv import find_uv_bin
|
||||
|
||||
if os.environ.get("UV_PREVIEW"):
|
||||
__all__ = [
|
||||
"find_uv_bin",
|
||||
# PEP 517 hook `build_sdist`.
|
||||
"build_sdist",
|
||||
# PEP 517 hook `build_wheel`.
|
||||
"build_wheel",
|
||||
# PEP 660 hook `build_editable`.
|
||||
"build_editable",
|
||||
# PEP 517 hook `get_requires_for_build_sdist`.
|
||||
"get_requires_for_build_sdist",
|
||||
# PEP 517 hook `get_requires_for_build_wheel`.
|
||||
"get_requires_for_build_wheel",
|
||||
# PEP 517 hook `prepare_metadata_for_build_wheel`.
|
||||
"prepare_metadata_for_build_wheel",
|
||||
# PEP 660 hook `get_requires_for_build_editable`.
|
||||
"get_requires_for_build_editable",
|
||||
# PEP 660 hook `prepare_metadata_for_build_editable`.
|
||||
"prepare_metadata_for_build_editable",
|
||||
]
|
||||
else:
|
||||
__all__ = ["find_uv_bin"]
|
||||
__all__ = ["find_uv_bin"]
|
||||
|
||||
|
||||
def __getattr__(attr_name: str) -> object:
|
||||
|
@ -41,8 +16,9 @@ def __getattr__(attr_name: str) -> object:
|
|||
"get_requires_for_build_editable",
|
||||
"prepare_metadata_for_build_editable",
|
||||
}:
|
||||
err = f"Using `uv.{attr_name}` is not allowed. The uv build backend requires preview mode to be enabled, e.g., via the `UV_PREVIEW=1` environment variable."
|
||||
err = (
|
||||
f"Using `uv.{attr_name}` is not allowed; build backend functionality is in the `uv_build` package. "
|
||||
f"Did you mean to use `uv_build` as your build system?"
|
||||
)
|
||||
raise AttributeError(err)
|
||||
|
||||
err = f"module 'uv' has no attribute '{attr_name}'"
|
||||
raise AttributeError(err)
|
||||
raise AttributeError(f"module `{__name__}` has no attribute `{attr_name}`")
|
||||
|
|
|
@ -1,128 +0,0 @@
|
|||
"""
|
||||
Python shims for the PEP 517 and PEP 660 build backend.
|
||||
|
||||
Major imports in this module are required to be lazy:
|
||||
```
|
||||
$ hyperfine \
|
||||
"/usr/bin/python3 -c \"print('hi')\"" \
|
||||
"/usr/bin/python3 -c \"from subprocess import check_call; print('hi')\""
|
||||
Base: Time (mean ± σ): 11.0 ms ± 1.7 ms [User: 8.5 ms, System: 2.5 ms]
|
||||
With import: Time (mean ± σ): 15.2 ms ± 2.0 ms [User: 12.3 ms, System: 2.9 ms]
|
||||
Base 1.38 ± 0.28 times faster than with import
|
||||
```
|
||||
|
||||
The same thing goes for the typing module, so we use Python 3.10 type annotations that
|
||||
don't require importing typing but then quote them so earlier Python version ignore
|
||||
them while IDEs and type checker can see through the quotes.
|
||||
"""
|
||||
|
||||
TYPE_CHECKING = False
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Mapping, Sequence # noqa:I001
|
||||
from typing import Any # noqa:I001
|
||||
|
||||
|
||||
def warn_config_settings(config_settings: "Mapping[Any, Any] | None" = None) -> None:
|
||||
import sys
|
||||
|
||||
if config_settings:
|
||||
print("Warning: Config settings are not supported", file=sys.stderr)
|
||||
|
||||
|
||||
def call(
|
||||
args: "Sequence[str]", config_settings: "Mapping[Any, Any] | None" = None
|
||||
) -> str:
|
||||
"""Invoke a uv subprocess and return the filename from stdout."""
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
warn_config_settings(config_settings)
|
||||
# Unlike `find_uv_bin`, this mechanism must work according to PEP 517
|
||||
uv_bin = shutil.which("uv")
|
||||
if uv_bin is None:
|
||||
raise RuntimeError("uv was not properly installed")
|
||||
# Forward stderr, capture stdout for the filename
|
||||
result = subprocess.run([uv_bin, *args], stdout=subprocess.PIPE)
|
||||
if result.returncode != 0:
|
||||
sys.exit(result.returncode)
|
||||
# If there was extra stdout, forward it (there should not be extra stdout)
|
||||
stdout = result.stdout.decode("utf-8").strip().splitlines(keepends=True)
|
||||
sys.stdout.writelines(stdout[:-1])
|
||||
# Fail explicitly instead of an irrelevant stacktrace
|
||||
if not stdout:
|
||||
print("uv subprocess did not return a filename on stdout", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
return stdout[-1].strip()
|
||||
|
||||
|
||||
def build_sdist(
|
||||
sdist_directory: str, config_settings: "Mapping[Any, Any] | None" = None
|
||||
) -> str:
|
||||
"""PEP 517 hook `build_sdist`."""
|
||||
args = ["build-backend", "build-sdist", sdist_directory]
|
||||
return call(args, config_settings)
|
||||
|
||||
|
||||
def build_wheel(
|
||||
wheel_directory: str,
|
||||
config_settings: "Mapping[Any, Any] | None" = None,
|
||||
metadata_directory: "str | None" = None,
|
||||
) -> str:
|
||||
"""PEP 517 hook `build_wheel`."""
|
||||
args = ["build-backend", "build-wheel", wheel_directory]
|
||||
if metadata_directory:
|
||||
args.extend(["--metadata-directory", metadata_directory])
|
||||
return call(args, config_settings)
|
||||
|
||||
|
||||
def get_requires_for_build_sdist(
|
||||
config_settings: "Mapping[Any, Any] | None" = None,
|
||||
) -> "Sequence[str]":
|
||||
"""PEP 517 hook `get_requires_for_build_sdist`."""
|
||||
warn_config_settings(config_settings)
|
||||
return []
|
||||
|
||||
|
||||
def get_requires_for_build_wheel(
|
||||
config_settings: "Mapping[Any, Any] | None" = None,
|
||||
) -> "Sequence[str]":
|
||||
"""PEP 517 hook `get_requires_for_build_wheel`."""
|
||||
warn_config_settings(config_settings)
|
||||
return []
|
||||
|
||||
|
||||
def prepare_metadata_for_build_wheel(
|
||||
metadata_directory: str, config_settings: "Mapping[Any, Any] | None" = None
|
||||
) -> str:
|
||||
"""PEP 517 hook `prepare_metadata_for_build_wheel`."""
|
||||
args = ["build-backend", "prepare-metadata-for-build-wheel", metadata_directory]
|
||||
return call(args, config_settings)
|
||||
|
||||
|
||||
def build_editable(
|
||||
wheel_directory: str,
|
||||
config_settings: "Mapping[Any, Any] | None" = None,
|
||||
metadata_directory: "str | None" = None,
|
||||
) -> str:
|
||||
"""PEP 660 hook `build_editable`."""
|
||||
args = ["build-backend", "build-editable", wheel_directory]
|
||||
if metadata_directory:
|
||||
args.extend(["--metadata-directory", metadata_directory])
|
||||
return call(args, config_settings)
|
||||
|
||||
|
||||
def get_requires_for_build_editable(
|
||||
config_settings: "Mapping[Any, Any] | None" = None,
|
||||
) -> "Sequence[str]":
|
||||
"""PEP 660 hook `get_requires_for_build_editable`."""
|
||||
warn_config_settings(config_settings)
|
||||
return []
|
||||
|
||||
|
||||
def prepare_metadata_for_build_editable(
|
||||
metadata_directory: str, config_settings: "Mapping[Any, Any] | None" = None
|
||||
) -> str:
|
||||
"""PEP 660 hook `prepare_metadata_for_build_editable`."""
|
||||
args = ["build-backend", "prepare-metadata-for-build-editable", metadata_directory]
|
||||
return call(args, config_settings)
|
Loading…
Add table
Add a link
Reference in a new issue