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

Add [tool.maturin.include] and [tool.maturin.exclude] #1255

Merged
merged 17 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions guide/src/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ in the `tool.maturin` section of `pyproject.toml`.
```toml
[tool.maturin]
# Include arbitrary files in the sdist
# NOTE: deprecated, please use `include` with `format="sdist"`
sdist-include = []
# Include additional files
include = []
# Exclude files
exclude = []
# Bindings type
bindings = "pyo3"
# Control the platform tag on linux
Expand Down
74 changes: 65 additions & 9 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use crate::project_layout::ProjectLayout;
use crate::python_interpreter::InterpreterKind;
use crate::source_distribution::source_distribution;
use crate::{
compile, BuildArtifact, Metadata21, ModuleWriter, PyProjectToml, PythonInterpreter, Target,
compile, BuildArtifact, Format, Metadata21, ModuleWriter, PyProjectToml, PythonInterpreter,
Target,
};
use anyhow::{anyhow, bail, Context, Result};
use cargo_metadata::Metadata;
use fs_err as fs;
use ignore::overrides::{Override, OverrideBuilder};
use lddtree::Library;
use normpath::PathExt;
use regex::Regex;
use sha2::{Digest, Sha256};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -249,8 +252,9 @@ impl BuildContext {

match self.pyproject_toml.as_ref() {
Some(pyproject) => {
let sdist_path = source_distribution(self, pyproject)
.context("Failed to build source distribution")?;
let sdist_path =
source_distribution(self, pyproject, self.excludes(Format::Sdist)?)
.context("Failed to build source distribution")?;
Ok(Some((sdist_path, "source".to_string())))
}
None => Ok(None),
Expand Down Expand Up @@ -450,6 +454,23 @@ impl BuildContext {
Ok(())
}

fn excludes(&self, format: Format) -> Result<Option<Override>> {
if let Some(pyproject) = self.pyproject_toml.as_ref() {
let pyproject_dir = self.pyproject_toml_path.normalize()?.into_path_buf();
if let Some(glob_patterns) = &pyproject.exclude() {
let mut excludes = OverrideBuilder::new(pyproject_dir.parent().unwrap());
for glob in glob_patterns
.iter()
.filter_map(|glob_pattern| glob_pattern.targets(format))
{
excludes.add(glob)?;
}
return Ok(Some(excludes.build()?));
}
}
Ok(None)
}

fn write_binding_wheel_abi3(
&self,
artifact: BuildArtifact,
Expand All @@ -463,7 +484,13 @@ impl BuildContext {
.get_platform_tag(platform_tags, self.universal2)?;
let tag = format!("cp{}{}-abi3-{}", major, min_minor, platform);

let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &[tag.clone()])?;
let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata21,
&[tag.clone()],
self.excludes(Format::Wheel)?,
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

write_bindings_module(
Expand All @@ -474,6 +501,8 @@ impl BuildContext {
None,
&self.target,
self.editable,
&self.metadata21,
self.pyproject_toml.as_ref(),
)
.context("Failed to add the files to the wheel")?;

Expand Down Expand Up @@ -534,7 +563,13 @@ impl BuildContext {
) -> Result<BuiltWheelMetadata> {
let tag = python_interpreter.get_tag(&self.target, platform_tags, self.universal2)?;

let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &[tag.clone()])?;
let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata21,
&[tag.clone()],
self.excludes(Format::Wheel)?,
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

write_bindings_module(
Expand All @@ -545,6 +580,8 @@ impl BuildContext {
Some(python_interpreter),
&self.target,
self.editable,
&self.metadata21,
self.pyproject_toml.as_ref(),
)
.context("Failed to add the files to the wheel")?;

Expand Down Expand Up @@ -651,7 +688,13 @@ impl BuildContext {
.target
.get_universal_tags(platform_tags, self.universal2)?;

let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &tags)?;
let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata21,
&tags,
self.excludes(Format::Wheel)?,
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

write_cffi_module(
Expand All @@ -663,6 +706,8 @@ impl BuildContext {
&artifact.path,
&self.interpreter[0].executable,
self.editable,
&self.metadata21,
self.pyproject_toml.as_ref(),
)?;

self.add_pth(&mut writer)?;
Expand Down Expand Up @@ -754,7 +799,13 @@ impl BuildContext {
self.metadata21.clone()
};

let mut writer = WheelWriter::new(&tag, &self.out, &metadata21, &tags)?;
let mut writer = WheelWriter::new(
&tag,
&self.out,
&metadata21,
&tags,
self.excludes(Format::Wheel)?,
)?;

if let Some(python_module) = &self.project_layout.python_module {
if self.target.is_wasi() {
Expand All @@ -763,8 +814,13 @@ impl BuildContext {
bail!("Sorry, adding python code to a wasm binary is currently not supported")
}
if !self.editable {
write_python_part(&mut writer, python_module)
.context("Failed to add the python module to the package")?;
write_python_part(
&mut writer,
python_module,
&metadata21,
self.pyproject_toml.as_ref(),
)
.context("Failed to add the python module to the package")?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use crate::module_writer::{
write_dist_info, ModuleWriter, PathWriter, SDistWriter, WheelWriter,
};
pub use crate::new_project::{init_project, new_project, GenerateProjectOptions};
pub use crate::pyproject_toml::PyProjectToml;
pub use crate::pyproject_toml::{Format, Formats, GlobPattern, PyProjectToml};
mbrobbel marked this conversation as resolved.
Show resolved Hide resolved
pub use crate::python_interpreter::PythonInterpreter;
pub use crate::target::Target;
#[cfg(feature = "upload")]
Expand Down
Loading