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 9 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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Fixes the compatibility tags for Pyston.
* Set default macOS deployment target version if `MACOSX_DEPLOYMENT_TARGET` isn't specified in [#1251](https://github.com/PyO3/maturin/pull/1251)
* Add support for 32-bit x86 FreeBSD target in [#1254](https://github.com/PyO3/maturin/pull/1254)
* Add `[tool.maturin.include]` and `[tool.maturin.exclude]` and deprecate `[tool.maturin.sdist-include]` [#1255](https://github.com/PyO3/maturin/pull/1255)

## [0.13.7] - 2022-10-29

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ compatibility = "linux"

`manylinux` option is also accepted as an alias of `compatibility` for backward compatibility with old version of maturin.

To include arbitrary files in the sdist for use during compilation specify `sdist-include` as an array of globs:
To include arbitrary files in the sdist for use during compilation specify `include` as an array of `path` globs with `format` set to `sdist`:

```toml
[tool.maturin]
sdist-include = ["path/**/*"]
include = [{ path = "path/**/*", format = "sdist" }]
```

There's a `maturin sdist` command for only building a source distribution as workaround for [pypa/pip#6041](https://github.com/pypa/pip/issues/6041).
Expand Down
4 changes: 2 additions & 2 deletions guide/src/distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ compatibility = "linux"

`manylinux` option is also accepted as an alias of `compatibility` for backward compatibility with old version of maturin.

To include arbitrary files in the sdist for use during compilation specify `sdist-include` as an array of globs:
To include arbitrary files in the sdist for use during compilation specify `include` as an array of `path` globs with `format` set to `sdist`:

```toml
[tool.maturin]
sdist-include = ["path/**/*"]
include = [{ path = "path/**/*", format = "sdist" }]
```

## Build Wheels
Expand Down
28 changes: 28 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 Expand Up @@ -138,3 +143,26 @@ unstable-flags = []
# Extra arguments that will be passed to rustc as `cargo rustc [...] -- [...] [arg1] [arg2]`
rustc-args = []
```

The `[tool.maturin.include]` and `[tool.maturin.exclude]` configuration are
inspired by
[Poetry](https://python-poetry.org/docs/pyproject/#include-and-exclude).

To specify files or globs directly:

```toml
include = ["path/**/*", "some/other/file"]
```

To specify a specific target format (`sdist` or `wheel`):

```toml
include = [
{ path = "path/**/*", format = "sdist" },
{ path = "all", format = ["sdist", "wheel"] },
{ path = "for/wheel/**/*", format = "wheel" }
]
```

The default behavior is apply these configurations to both `sdist` and `wheel`
targets.
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