Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle dynamic version in pyproject.toml #2391

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::project_layout::ProjectLayout;
use crate::source_distribution::source_distribution;
use crate::target::{Arch, Os};
use crate::{
compile, pyproject_toml::Format, BridgeModel, BuildArtifact, Metadata23, ModuleWriter,
compile, pyproject_toml::Format, BridgeModel, BuildArtifact, Metadata24, ModuleWriter,
PyProjectToml, PythonInterpreter, Target,
};
use anyhow::{anyhow, bail, Context, Result};
Expand All @@ -34,17 +34,17 @@ use tracing::instrument;
/// Insert wasm launcher scripts as entrypoints and the wasmtime dependency
fn bin_wasi_helper(
artifacts_and_files: &[(&BuildArtifact, String)],
mut metadata23: Metadata23,
) -> Result<Metadata23> {
mut metadata24: Metadata24,
) -> Result<Metadata24> {
eprintln!("⚠️ Warning: wasi support is experimental");
// escaped can contain [\w\d.], but i don't know how we'd handle dots correctly here
if metadata23.get_distribution_escaped().contains('.') {
if metadata24.get_distribution_escaped().contains('.') {
bail!(
"Can't build wasm wheel if there is a dot in the name ('{}')",
metadata23.get_distribution_escaped()
metadata24.get_distribution_escaped()
)
}
if !metadata23.entry_points.is_empty() {
if !metadata24.entry_points.is_empty() {
bail!("You can't define entrypoints yourself for a binary project");
}

Expand All @@ -57,29 +57,29 @@ fn bin_wasi_helper(
base_name.to_string(),
format!(
"{}.{}:main",
metadata23.get_distribution_escaped(),
metadata24.get_distribution_escaped(),
base_name.replace('-', "_")
),
);
}

metadata23
metadata24
.entry_points
.insert("console_scripts".to_string(), console_scripts);

// Add our wasmtime default version if the user didn't provide one
if !metadata23
if !metadata24
.requires_dist
.iter()
.any(|requirement| requirement.name.as_ref() == "wasmtime")
{
// Having the wasmtime version hardcoded is not ideal, it's easy enough to overwrite
metadata23
metadata24
.requires_dist
.push(Requirement::from_str("wasmtime>=11.0.0,<12.0.0").unwrap());
}

Ok(metadata23)
Ok(metadata24)
}

/// Contains all the metadata required to build the crate
Expand All @@ -96,12 +96,12 @@ pub struct BuildContext {
/// Parsed pyproject.toml if any
pub pyproject_toml: Option<PyProjectToml>,
/// Python Package Metadata 2.3
pub metadata23: Metadata23,
pub metadata24: Metadata24,
/// The name of the crate
pub crate_name: String,
/// The name of the module can be distinct from the package name, mostly
/// because package names normally contain minuses while module names
/// have underscores. The package name is part of metadata23
/// have underscores. The package name is part of metadata24
pub module_name: String,
/// The path to the Cargo.toml. Required for the cargo invocations
pub manifest_path: PathBuf,
Expand Down Expand Up @@ -436,7 +436,7 @@ impl BuildContext {

fn add_pth(&self, writer: &mut WheelWriter) -> Result<()> {
if self.editable {
writer.add_pth(&self.project_layout, &self.metadata23)?;
writer.add_pth(&self.project_layout, &self.metadata24)?;
}
Ok(())
}
Expand All @@ -463,7 +463,7 @@ impl BuildContext {
"{}{}{}-*.tar.gz",
self.out.display(),
std::path::MAIN_SEPARATOR,
&self.metadata23.get_distribution_escaped(),
&self.metadata24.get_distribution_escaped(),
);
excludes.add(&glob_pattern)?;
}
Expand Down Expand Up @@ -618,7 +618,7 @@ impl BuildContext {
let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata23,
&self.metadata24,
&[tag.clone()],
self.excludes(Format::Wheel)?,
)?;
Expand Down Expand Up @@ -696,7 +696,7 @@ impl BuildContext {
let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata23,
&self.metadata24,
&[tag.clone()],
self.excludes(Format::Wheel)?,
)?;
Expand Down Expand Up @@ -818,7 +818,7 @@ impl BuildContext {
let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata23,
&self.metadata24,
&tags,
self.excludes(Format::Wheel)?,
)?;
Expand Down Expand Up @@ -856,7 +856,7 @@ impl BuildContext {

// Warn if cffi isn't specified in the requirements
if !self
.metadata23
.metadata24
.requires_dist
.iter()
.any(|requirement| requirement.name.as_ref() == "cffi")
Expand Down Expand Up @@ -884,7 +884,7 @@ impl BuildContext {
let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata23,
&self.metadata24,
&tags,
self.excludes(Format::Wheel)?,
)?;
Expand Down Expand Up @@ -942,7 +942,7 @@ impl BuildContext {
_ => unreachable!(),
};

if !self.metadata23.scripts.is_empty() {
if !self.metadata24.scripts.is_empty() {
bail!("Defining scripts and working with a binary doesn't mix well");
}

Expand All @@ -969,16 +969,16 @@ impl BuildContext {
artifacts_and_files.push((artifact, bin_name))
}

let metadata23 = if self.target.is_wasi() {
bin_wasi_helper(&artifacts_and_files, self.metadata23.clone())?
let metadata24 = if self.target.is_wasi() {
bin_wasi_helper(&artifacts_and_files, self.metadata24.clone())?
} else {
self.metadata23.clone()
self.metadata24.clone()
};

let mut writer = WheelWriter::new(
&tag,
&self.out,
&metadata23,
&metadata24,
&tags,
self.excludes(Format::Wheel)?,
)?;
Expand All @@ -1000,9 +1000,9 @@ impl BuildContext {
let mut artifacts_ref = Vec::with_capacity(artifacts.len());
for (artifact, bin_name) in &artifacts_and_files {
artifacts_ref.push(*artifact);
write_bin(&mut writer, &artifact.path, &self.metadata23, bin_name)?;
write_bin(&mut writer, &artifact.path, &self.metadata24, bin_name)?;
if self.target.is_wasi() {
write_wasm_launcher(&mut writer, &self.metadata23, bin_name)?;
write_wasm_launcher(&mut writer, &self.metadata24, bin_name)?;
}
}
self.add_external_libs(&mut writer, &artifacts_ref, ext_libs)?;
Expand Down
6 changes: 3 additions & 3 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ impl BuildContextBuilder {
pyproject_toml_path,
pyproject_toml,
module_name,
metadata23,
metadata24,
mut cargo_options,
cargo_metadata,
mut pyproject_toml_maturin_options,
Expand Down Expand Up @@ -638,7 +638,7 @@ impl BuildContextBuilder {
&build_options,
&bridge,
&target,
metadata23.requires_python.as_ref(),
metadata24.requires_python.as_ref(),
generate_import_lib,
)?
};
Expand Down Expand Up @@ -743,7 +743,7 @@ impl BuildContextBuilder {
project_layout,
pyproject_toml_path,
pyproject_toml,
metadata23,
metadata24,
crate_name,
module_name,
manifest_path: cargo_toml_path,
Expand Down
19 changes: 10 additions & 9 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,16 @@ fn cargo_build_command(
#[cfg(feature = "xwin")]
{
// Don't use xwin if the Windows MSVC compiler can compile to the target
let native_compile = cc::Build::new()
.opt_level(0)
.host(target.host_triple())
.target(target_triple)
.cargo_metadata(false)
.cargo_warnings(false)
.cargo_output(false)
.try_get_compiler()
.is_ok();
let native_compile = target.host_triple().contains("windows-msvc")
&& cc::Build::new()
.opt_level(0)
.host(target.host_triple())
.target(target_triple)
.cargo_metadata(false)
.cargo_warnings(false)
.cargo_output(false)
.try_get_compiler()
.is_ok();
let force_xwin = env::var("MATURIN_USE_XWIN").ok().as_deref() == Some("1");
if !native_compile || force_xwin {
println!("🛠️ Using xwin for cross-compiling to {target_triple}");
Expand Down
8 changes: 4 additions & 4 deletions src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ fn install_dependencies(
interpreter: &PythonInterpreter,
install_backend: &InstallBackend,
) -> Result<()> {
if !build_context.metadata23.requires_dist.is_empty() {
if !build_context.metadata24.requires_dist.is_empty() {
let mut args = vec!["install".to_string()];
args.extend(build_context.metadata23.requires_dist.iter().map(|x| {
args.extend(build_context.metadata24.requires_dist.iter().map(|x| {
let mut pkg = x.clone();
// Remove extra marker to make it installable with pip
// Keep in sync with `Metadata21::merge_pyproject_toml()`!
Expand Down Expand Up @@ -315,7 +315,7 @@ fn configure_as_editable(
println!("✏️ Setting installed package as editable");
install_backend.check_supports_show_files(python)?;
let mut cmd = install_backend.make_command(python);
let cmd = cmd.args(["show", "--files", &build_context.metadata23.name]);
let cmd = cmd.args(["show", "--files", &build_context.metadata24.name]);
debug!("running {:?}", cmd);
let output = cmd.output()?;
ensure!(output.status.success(), "failed to list package files");
Expand Down Expand Up @@ -438,7 +438,7 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
)?;
eprintln!(
"🛠 Installed {}-{}",
build_context.metadata23.name, build_context.metadata23.version
build_context.metadata24.name, build_context.metadata24.version
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use crate::compile::{compile, BuildArtifact};
pub use crate::develop::{develop, DevelopOptions};
#[cfg(feature = "schemars")]
pub use crate::generate_json_schema::{generate_json_schema, GenerateJsonSchemaOptions, Mode};
pub use crate::metadata::{Metadata23, WheelMetadata};
pub use crate::metadata::{Metadata24, WheelMetadata};
pub use crate::module_writer::{
write_dist_info, ModuleWriter, PathWriter, SDistWriter, WheelWriter,
};
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ fn pep517(subcommand: Pep517Command) -> Result<()> {
};

let mut writer = PathWriter::from_path(metadata_directory);
write_dist_info(&mut writer, &context.metadata23, &tags)?;
println!("{}", context.metadata23.get_dist_info_dir().display());
write_dist_info(&mut writer, &context.metadata24, &tags)?;
println!("{}", context.metadata24.get_dist_info_dir().display());
}
Pep517Command::BuildWheel {
build_options,
Expand Down
Loading
Loading