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:
konsti 2025-03-06 20:27:20 +01:00 committed by GitHub
parent d4a805544f
commit bf4c7afe8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 842 additions and 96 deletions

View file

@ -1,4 +1,5 @@
mod metadata;
mod serde_verbatim;
mod source_dist;
mod wheel;
@ -394,8 +395,8 @@ mod tests {
license = { file = "license.txt" }
[build-system]
requires = ["uv>=0.5.15,<0.6"]
build-backend = "uv"
requires = ["uv_build>=0.5.15,<0.6"]
build-backend = "uv_build"
"#
},
)
@ -462,8 +463,8 @@ mod tests {
version = "1.0.0"
[build-system]
requires = ["uv>=0.5.15,<0.6"]
build-backend = "uv"
requires = ["uv_build>=0.5.15,<0.6"]
build-backend = "uv_build"
"#
},
)

View file

@ -19,6 +19,7 @@ use uv_pep508::{
};
use uv_pypi_types::{Metadata23, VerbatimParsedUrl};
use crate::serde_verbatim::SerdeVerbatim;
use crate::Error;
/// By default, we ignore generated python files.
@ -161,14 +162,14 @@ impl PyProjectToml {
///
/// ```toml
/// [build-system]
/// requires = ["uv>=0.4.15,<5"]
/// build-backend = "uv"
/// requires = ["uv_build>=0.4.15,<5"]
/// build-backend = "uv_build"
/// ```
pub fn check_build_system(&self, uv_version: &str) -> Vec<String> {
let mut warnings = Vec::new();
if self.build_system.build_backend.as_deref() != Some("uv") {
if self.build_system.build_backend.as_deref() != Some("uv_build") {
warnings.push(format!(
r#"The value for `build_system.build-backend` should be `"uv"`, not `"{}"`"#,
r#"The value for `build_system.build-backend` should be `"uv_build"`, not `"{}"`"#,
self.build_system.build_backend.clone().unwrap_or_default()
));
}
@ -189,7 +190,7 @@ impl PyProjectToml {
warnings.push(expected());
return warnings;
};
if uv_requirement.name.as_str() != "uv" {
if uv_requirement.name.as_str() != "uv-build" {
warnings.push(expected());
return warnings;
}
@ -221,10 +222,13 @@ impl PyProjectToml {
if !bounded {
warnings.push(format!(
"`build_system.requires = [\"{uv_requirement}\"]` is missing an \
upper bound on the uv version such as `<{next_breaking}`. \
Without bounding the uv version, the source distribution will break \
when a future, breaking version of uv is released.",
"`build_system.requires = [\"{}\"]` is missing an \
upper bound on the `uv_build` version such as `<{next_breaking}`. \
Without bounding the `uv_build` version, the source distribution will break \
when a future, breaking version of `uv_build` is released.",
// Use an underscore consistently, to avoid confusing users between a package name with dash and a
// module name with underscore
uv_requirement.verbatim()
));
}
@ -768,7 +772,7 @@ pub(crate) enum Contact {
#[serde(rename_all = "kebab-case")]
struct BuildSystem {
/// PEP 508 dependencies required to execute the build system.
requires: Vec<Requirement<VerbatimParsedUrl>>,
requires: Vec<SerdeVerbatim<Requirement<VerbatimParsedUrl>>>,
/// A string naming a Python object that will be used to perform the build.
build_backend: Option<String>,
/// <https://peps.python.org/pep-0517/#in-tree-build-backends>
@ -940,8 +944,8 @@ mod tests {
{payload}
[build-system]
requires = ["uv>=0.4.15,<5"]
build-backend = "uv"
requires = ["uv_build>=0.4.15,<5"]
build-backend = "uv_build"
"#
}
}
@ -1023,8 +1027,8 @@ mod tests {
foo-bar = "foo:bar"
[build-system]
requires = ["uv>=0.4.15,<5"]
build-backend = "uv"
requires = ["uv_build>=0.4.15,<5"]
build-backend = "uv_build"
"#
};
@ -1150,8 +1154,8 @@ mod tests {
foo-bar = "foo:bar"
[build-system]
requires = ["uv>=0.4.15,<5"]
build-backend = "uv"
requires = ["uv_build>=0.4.15,<5"]
build-backend = "uv_build"
"#
};
@ -1231,13 +1235,13 @@ mod tests {
version = "0.1.0"
[build-system]
requires = ["uv"]
build-backend = "uv"
requires = ["uv_build"]
build-backend = "uv_build"
"#};
let pyproject_toml = PyProjectToml::parse(contents).unwrap();
assert_snapshot!(
pyproject_toml.check_build_system("0.4.15+test").join("\n"),
@r###"`build_system.requires = ["uv"]` is missing an upper bound on the uv version such as `<0.5`. Without bounding the uv version, the source distribution will break when a future, breaking version of uv is released."###
@r###"`build_system.requires = ["uv_build"]` is missing an upper bound on the `uv_build` version such as `<0.5`. Without bounding the `uv_build` version, the source distribution will break when a future, breaking version of `uv_build` is released."###
);
}
@ -1249,8 +1253,8 @@ mod tests {
version = "0.1.0"
[build-system]
requires = ["uv>=0.4.15,<5", "wheel"]
build-backend = "uv"
requires = ["uv_build>=0.4.15,<5", "wheel"]
build-backend = "uv_build"
"#};
let pyproject_toml = PyProjectToml::parse(contents).unwrap();
assert_snapshot!(
@ -1268,7 +1272,7 @@ mod tests {
[build-system]
requires = ["setuptools"]
build-backend = "uv"
build-backend = "uv_build"
"#};
let pyproject_toml = PyProjectToml::parse(contents).unwrap();
assert_snapshot!(
@ -1285,13 +1289,13 @@ mod tests {
version = "0.1.0"
[build-system]
requires = ["uv>=0.4.15,<5"]
requires = ["uv_build>=0.4.15,<5"]
build-backend = "setuptools"
"#};
let pyproject_toml = PyProjectToml::parse(contents).unwrap();
assert_snapshot!(
pyproject_toml.check_build_system("0.4.15+test").join("\n"),
@r###"The value for `build_system.build-backend` should be `"uv"`, not `"setuptools"`"###
@r###"The value for `build_system.build-backend` should be `"uv_build"`, not `"setuptools"`"###
);
}

View file

@ -0,0 +1,54 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::{Display, Formatter};
use std::ops::Deref;
use std::str::FromStr;
/// Preserves the verbatim string representation when deserializing `T`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct SerdeVerbatim<T> {
verbatim: String,
inner: T,
}
impl<T> SerdeVerbatim<T> {
pub(crate) fn verbatim(&self) -> &str {
&self.verbatim
}
}
impl<T> Deref for SerdeVerbatim<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T: Display> Display for SerdeVerbatim<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl<'de, T: FromStr> Deserialize<'de> for SerdeVerbatim<T>
where
<T as FromStr>::Err: Display,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let verbatim = String::deserialize(deserializer)?;
let inner = T::from_str(&verbatim).map_err(serde::de::Error::custom)?;
Ok(Self { verbatim, inner })
}
}
impl<T: Serialize> Serialize for SerdeVerbatim<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.inner.serialize(serializer)
}
}