Skip to content

Commit

Permalink
Replace package.metadata.maturin.targets with `tool.maturin.targets…
Browse files Browse the repository at this point in the history
…` in `pyproject.toml`
  • Loading branch information
messense committed Mar 25, 2023
1 parent 0b5311e commit c1f3991
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,9 @@ impl BuildOptions {
.clone()
.unwrap_or_else(|| cargo_metadata.target_directory.clone().into_std_path_buf());

let remaining_core_metadata = cargo_toml.remaining_core_metadata();
let config_targets = remaining_core_metadata.targets.as_deref();
let cargo_targets = filter_cargo_targets(&cargo_metadata, bridge, config_targets)?;
let config_targets = pyproject.and_then(|x| x.targets());
let cargo_targets =
filter_cargo_targets(&cargo_metadata, bridge, config_targets.as_deref())?;

let crate_name = cargo_toml.package.name;
Ok(BuildContext {
Expand Down Expand Up @@ -747,7 +747,7 @@ fn validate_bridge_type(
fn filter_cargo_targets(
cargo_metadata: &Metadata,
bridge: BridgeModel,
config_targets: Option<&[crate::cargo_toml::CargoTarget]>,
config_targets: Option<&[crate::pyproject_toml::CargoTarget]>,
) -> Result<Vec<CompileTarget>> {
let root_pkg = cargo_metadata.root_package().unwrap();
let resolved_features = cargo_metadata
Expand Down
19 changes: 0 additions & 19 deletions src/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,10 @@ struct CargoTomlMetadata {
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "kebab-case")]
pub struct RemainingCoreMetadata {
/// Cargo compile targets
pub targets: Option<Vec<CargoTarget>>,
#[serde(flatten)]
pub other: HashMap<String, toml::Value>,
}

/// Cargo compile target
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "kebab-case")]
pub struct CargoTarget {
/// Name as given in the `Cargo.toml` or generated from the file name
pub name: String,
/// Kind of target ("bin", "lib")
pub kind: Option<String>,
// TODO: Add bindings option
// Bridge model, which kind of bindings to use
// pub bindings: Option<String>,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -158,10 +143,6 @@ mod test {

let cargo_toml: Result<CargoToml, _> = toml::from_str(cargo_toml);
assert!(cargo_toml.is_ok());

let maturin = cargo_toml.unwrap().remaining_core_metadata();
let targets = maturin.targets.unwrap();
assert_eq!("pyo3_pure", targets[0].name);
}

#[test]
Expand Down
27 changes: 27 additions & 0 deletions src/pyproject_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ impl GlobPattern {
}
}

/// Cargo compile target
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "kebab-case")]
pub struct CargoTarget {
/// Name as given in the `Cargo.toml` or generated from the file name
pub name: String,
/// Kind of target ("bin", "lib")
pub kind: Option<String>,
// TODO: Add bindings option
// Bridge model, which kind of bindings to use
// pub bindings: Option<String>,
}

/// The `[tool.maturin]` section of a pyproject.toml
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
Expand All @@ -103,6 +116,8 @@ pub struct ToolMaturin {
python_packages: Option<Vec<String>>,
/// Path to the wheel directory, defaults to `<module_name>.data`
data: Option<PathBuf>,
/// Cargo compile targets
pub targets: Option<Vec<CargoTarget>>,
// Some customizable cargo options
/// Build artifacts with the specified Cargo profile
pub profile: Option<String>,
Expand Down Expand Up @@ -228,6 +243,11 @@ impl PyProjectToml {
self.maturin().and_then(|maturin| maturin.data.as_deref())
}

/// Returns the value of `[tool.maturin.targets]` in pyproject.toml
pub fn targets(&self) -> Option<Vec<CargoTarget>> {
self.maturin().and_then(|maturin| maturin.targets.clone())
}

/// Returns the value of `[tool.maturin.manifest-path]` in pyproject.toml
pub fn manifest_path(&self) -> Option<&Path> {
self.maturin()?.manifest_path.as_deref()
Expand Down Expand Up @@ -310,6 +330,11 @@ mod tests {
no-default-features = true
locked = true
rustc-args = ["-Z", "unstable-options"]
[[tool.maturin.targets]]
name = "pyo3_pure"
kind = "lib"
bindings = "pyo3"
"#,
)
.unwrap();
Expand All @@ -334,6 +359,8 @@ mod tests {
maturin.python_packages,
Some(vec!["foo".to_string(), "bar".to_string()])
);
let targets = maturin.targets.as_ref().unwrap();
assert_eq!("pyo3_pure", targets[0].name);
}

#[test]
Expand Down
7 changes: 0 additions & 7 deletions test-crates/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ readme = "../../README.md"
default-run = "hello-world"

[dependencies]

[[package.metadata.maturin.targets]]
name = "hello-world"
bindings = "bin"

[[package.metadata.maturin.targets]]
name = "foo"
7 changes: 7 additions & 0 deletions test-crates/hello-world/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ build-backend = "maturin"

[tool.maturin]
bindings = "bin"

[[tool.maturin.targets]]
name = "hello-world"
bindings = "bin"

[[tool.maturin.targets]]
name = "foo"

0 comments on commit c1f3991

Please sign in to comment.