Skip to content

Commit

Permalink
Use [tool.maturin] options from pyproject.toml in build command
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Aug 8, 2021
1 parent a37c450 commit c85277c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::python_interpreter::InterpreterKind;
use crate::BuildContext;
use crate::CargoToml;
use crate::Metadata21;
use crate::PyProjectToml;
use crate::PythonInterpreter;
use crate::Target;
use anyhow::{bail, format_err, Context, Result};
Expand Down Expand Up @@ -130,6 +131,12 @@ impl BuildOptions {

let cargo_toml = CargoToml::from_path(&manifest_file)?;
let manifest_dir = manifest_file.parent().unwrap();
let pyproject: Option<PyProjectToml> = if manifest_dir.join("pyproject.toml").is_file() {
Some(PyProjectToml::new(manifest_dir).context("pyproject.toml is invalid")?)
} else {
None
};
let pyproject = pyproject.as_ref();
let metadata21 = Metadata21::from_cargo_toml(&cargo_toml, &manifest_dir)
.context("Failed to parse Cargo.toml into python metadata")?;
let extra_metadata = cargo_toml.remaining_core_metadata();
Expand Down Expand Up @@ -183,7 +190,12 @@ impl BuildOptions {
}
};

let bridge = find_bridge(&cargo_metadata, self.bindings.as_deref())?;
let bridge = find_bridge(
&cargo_metadata,
self.bindings
.as_deref()
.or_else(|| pyproject.as_ref().and_then(|x| x.bindings())),
)?;

if bridge != BridgeModel::Bin && module_name.contains('-') {
bail!(
Expand Down Expand Up @@ -228,6 +240,12 @@ impl BuildOptions {
universal2 = true;
}
};
let strip = pyproject.map(|x| x.strip()).unwrap_or_default() || strip;
let skip_auditwheel =
pyproject.map(|x| x.skip_auditwheel()).unwrap_or_default() || self.skip_auditwheel;
let platform_tag = self
.platform_tag
.or_else(|| pyproject.and_then(|x| x.compatibility()));

Ok(BuildContext {
target,
Expand All @@ -240,8 +258,8 @@ impl BuildOptions {
out: wheel_dir,
release,
strip,
skip_auditwheel: self.skip_auditwheel,
platform_tag: self.platform_tag,
skip_auditwheel,
platform_tag,
cargo_extra_args,
rustc_extra_args,
interpreter,
Expand Down
60 changes: 59 additions & 1 deletion src/pyproject_toml.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::PlatformTag;
use anyhow::{format_err, Context, Result};
use pyproject_toml::PyProjectToml as ProjectToml;
use serde::{Deserialize, Serialize};
Expand All @@ -16,6 +17,15 @@ pub struct Tool {
#[serde(rename_all = "kebab-case")]
pub struct ToolMaturin {
sdist_include: Option<Vec<String>>,
bindings: Option<String>,
cargo_extra_args: Option<String>,
#[serde(alias = "manylinux")]
compatibility: Option<PlatformTag>,
rustc_extra_args: Option<String>,
#[serde(default)]
skip_auditwheel: bool,
#[serde(default)]
strip: bool,
}

/// A pyproject.toml as specified in PEP 517
Expand Down Expand Up @@ -57,11 +67,59 @@ impl PyProjectToml {
Ok(pyproject)
}

/// Returns the value of `[maturin.sdist-include]` in pyproject.toml
/// Returns the value of `[tool.maturin.sdist-include]` in pyproject.toml
pub fn sdist_include(&self) -> Option<&Vec<String>> {
self.tool.as_ref()?.maturin.as_ref()?.sdist_include.as_ref()
}

/// Returns the value of `[tool.maturin.bindings]` in pyproject.toml
pub fn bindings(&self) -> Option<&str> {
self.tool.as_ref()?.maturin.as_ref()?.bindings.as_deref()
}

/// Returns the value of `[tool.maturin.cargo-extra-args]` in pyproject.toml
pub fn cargo_extra_args(&self) -> Option<&str> {
self.tool
.as_ref()?
.maturin
.as_ref()?
.cargo_extra_args
.as_deref()
}

/// Returns the value of `[tool.maturin.compatibility]` in pyproject.toml
pub fn compatibility(&self) -> Option<PlatformTag> {
self.tool.as_ref()?.maturin.as_ref()?.compatibility
}

/// Returns the value of `[tool.maturin.rustc-extra-args]` in pyproject.toml
pub fn rustc_extra_args(&self) -> Option<&str> {
self.tool
.as_ref()?
.maturin
.as_ref()?
.rustc_extra_args
.as_deref()
}

/// Returns the value of `[tool.maturin.skip-auditwheel]` in pyproject.toml
pub fn skip_auditwheel(&self) -> bool {
self.tool
.as_ref()
.and_then(|tool| tool.maturin.as_ref())
.map(|maturin| maturin.skip_auditwheel)
.unwrap_or_default()
}

/// Returns the value of `[tool.maturin.strip]` in pyproject.toml
pub fn strip(&self) -> bool {
self.tool
.as_ref()
.and_then(|tool| tool.maturin.as_ref())
.map(|maturin| maturin.strip)
.unwrap_or_default()
}

/// Having a pyproject.toml without a version constraint is a bad idea
/// because at some point we'll have to do breaking changes and then source
/// distributions would break
Expand Down

0 comments on commit c85277c

Please sign in to comment.