| 1 |
//! A pyproject.toml as specified in PEP 517 |
| 2 |
|
| 3 |
use crate::auditwheel::AuditWheelMode; |
| 4 |
use crate::PlatformTag; |
| 5 |
use anyhow::{Context, Result}; |
| 6 |
use fs_err as fs; |
| 7 |
use pep440_rs::Version; |
| 8 |
use pep508_rs::VersionOrUrl; |
| 9 |
use pyproject_toml::{BuildSystem, Project}; |
| 10 |
use serde::{Deserialize, Serialize}; |
| 11 |
use std::collections::HashMap; |
| 12 |
use std::path::{Path, PathBuf}; |
| 13 |
use std::str::FromStr; |
| 14 |
|
| 15 |
/// The `[tool]` section of a pyproject.toml |
| 16 |
#[derive(Serialize, Deserialize, Debug, Clone, Default)] |
| 17 |
#[serde(rename_all = "kebab-case")] |
| 18 |
pub struct Tool { |
| 19 |
/// maturin options |
| 20 |
pub maturin: Option<ToolMaturin>, |
| 21 |
} |
| 22 |
|
| 23 |
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] |
| 24 |
#[serde(rename_all = "lowercase")] |
| 25 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 26 |
/// The target format for the include or exclude [GlobPattern]. |
| 27 |
/// |
| 28 |
/// See [Formats]. |
| 29 |
pub enum Format { |
| 30 |
/// Source distribution |
| 31 |
Sdist, |
| 32 |
/// Wheel |
| 33 |
Wheel, |
| 34 |
} |
| 35 |
|
| 36 |
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] |
| 37 |
#[serde(untagged)] |
| 38 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 39 |
/// A single [Format] or multiple [Format] values for a [GlobPattern]. |
| 40 |
pub enum Formats { |
| 41 |
/// A single [Format] value |
| 42 |
Single(Format), |
| 43 |
/// Multiple [Format] values |
| 44 |
Multiple(Vec<Format>), |
| 45 |
} |
| 46 |
|
| 47 |
impl Formats { |
| 48 |
/// Returns `true` if the inner [Format] value(s) target the given [Format] |
| 49 |
pub fn targets(&self, format: Format) -> bool { |
| 50 |
match self { |
| 51 |
Self::Single(val) if val == &format => true, |
| 52 |
Self::Multiple(formats) if formats.contains(&format) => true, |
| 53 |
_ => false, |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] |
| 59 |
#[serde(untagged)] |
| 60 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 61 |
/// A glob pattern for the include and exclude configuration. |
| 62 |
/// |
| 63 |
/// See [PyProjectToml::include] and [PyProject::exclude]. |
| 64 |
/// |
| 65 |
/// Based on <https://python-poetry.org/docs/pyproject/#include-and-exclude>. |
| 66 |
pub enum GlobPattern { |
| 67 |
/// A glob |
| 68 |
Path(String), |
| 69 |
/// A glob `path` with a `format` key to specify one or more [Format] values |
| 70 |
WithFormat { |
| 71 |
/// A glob |
| 72 |
path: String, |
| 73 |
/// One or more [Format] values |
| 74 |
format: Formats, |
| 75 |
}, |
| 76 |
} |
| 77 |
|
| 78 |
impl GlobPattern { |
| 79 |
/// Returns the glob pattern for this pattern if it targets the given [Format], else this returns `None`. |
| 80 |
pub fn targets(&self, format: Format) -> Option<&str> { |
| 81 |
match self { |
| 82 |
// Not specified defaults to both |
| 83 |
Self::Path(ref glob) => Some(glob), |
| 84 |
Self::WithFormat { |
| 85 |
path, |
| 86 |
format: formats, |
| 87 |
} if formats.targets(format) => Some(path), |
| 88 |
_ => None, |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/// Cargo compile target |
| 94 |
#[derive(Serialize, Deserialize, Debug, Clone, Default)] |
| 95 |
#[serde(rename_all = "kebab-case")] |
| 96 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 97 |
pub struct CargoTarget { |
| 98 |
/// Name as given in the `Cargo.toml` or generated from the file name |
| 99 |
pub name: String, |
| 100 |
/// Kind of target ("bin", "cdylib") |
| 101 |
pub kind: Option<CargoCrateType>, |
| 102 |
// TODO: Add bindings option |
| 103 |
// Bridge model, which kind of bindings to use |
| 104 |
// pub bindings: Option<String>, |
| 105 |
} |
| 106 |
|
| 107 |
/// Supported cargo crate types |
| 108 |
#[derive(Serialize, Deserialize, Debug, Clone, Copy)] |
| 109 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 110 |
pub enum CargoCrateType { |
| 111 |
/// Binary executable target |
| 112 |
#[serde(rename = "bin")] |
| 113 |
Bin, |
| 114 |
/// Dynamic system library target |
| 115 |
#[serde(rename = "cdylib")] |
| 116 |
CDyLib, |
| 117 |
/// Dynamic Rust library target |
| 118 |
#[serde(rename = "dylib")] |
| 119 |
DyLib, |
| 120 |
/// Rust library |
| 121 |
#[serde(rename = "lib")] |
| 122 |
Lib, |
| 123 |
/// Rust library for use as an intermediate target |
| 124 |
#[serde(rename = "rlib")] |
| 125 |
RLib, |
| 126 |
/// Static library |
| 127 |
#[serde(rename = "staticlib")] |
| 128 |
StaticLib, |
| 129 |
} |
| 130 |
|
| 131 |
impl From<CargoCrateType> for cargo_metadata::CrateType { |
| 132 |
fn from(value: CargoCrateType) -> Self { |
| 133 |
match value { |
| 134 |
CargoCrateType::Bin => cargo_metadata::CrateType::Bin, |
| 135 |
CargoCrateType::CDyLib => cargo_metadata::CrateType::CDyLib, |
| 136 |
CargoCrateType::DyLib => cargo_metadata::CrateType::DyLib, |
| 137 |
CargoCrateType::Lib => cargo_metadata::CrateType::Lib, |
| 138 |
CargoCrateType::RLib => cargo_metadata::CrateType::RLib, |
| 139 |
CargoCrateType::StaticLib => cargo_metadata::CrateType::StaticLib, |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/// Target configuration |
| 145 |
#[derive(Serialize, Deserialize, Debug, Clone)] |
| 146 |
#[serde(rename_all = "kebab-case")] |
| 147 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 148 |
pub struct TargetConfig { |
| 149 |
/// macOS deployment target version |
| 150 |
#[serde(alias = "macosx-deployment-target")] |
| 151 |
pub macos_deployment_target: Option<String>, |
| 152 |
} |
| 153 |
|
| 154 |
/// Source distribution generator |
| 155 |
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)] |
| 156 |
#[serde(rename_all = "kebab-case")] |
| 157 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 158 |
pub enum SdistGenerator { |
| 159 |
/// Use `cargo package --list` |
| 160 |
#[default] |
| 161 |
Cargo, |
| 162 |
/// Use `git ls-files` |
| 163 |
Git, |
| 164 |
} |
| 165 |
|
| 166 |
/// The `[tool.maturin]` section of a pyproject.toml |
| 167 |
#[derive(Serialize, Deserialize, Debug, Clone, Default)] |
| 168 |
#[serde(rename_all = "kebab-case")] |
| 169 |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] |
| 170 |
pub struct ToolMaturin { |
| 171 |
// maturin specific options |
| 172 |
/// Module name, accepts setuptools style import name like `foo.bar` |
| 173 |
pub module_name: Option<String>, |
| 174 |
/// Include files matching the given glob pattern(s) |
| 175 |
pub include: Option<Vec<GlobPattern>>, |
| 176 |
/// Exclude files matching the given glob pattern(s) |
| 177 |
pub exclude: Option<Vec<GlobPattern>>, |
| 178 |
/// Bindings type |
| 179 |
pub bindings: Option<String>, |
| 180 |
/// Platform compatibility |
| 181 |
#[serde(alias = "manylinux")] |
| 182 |
pub compatibility: Option<PlatformTag>, |
| 183 |
/// Audit wheel mode |
| 184 |
pub auditwheel: Option<AuditWheelMode>, |
| 185 |
/// Skip audit wheel |
| 186 |
#[serde(default)] |
| 187 |
pub skip_auditwheel: bool, |
| 188 |
/// Strip the final binary |
| 189 |
#[serde(default)] |
| 190 |
pub strip: bool, |
| 191 |
/// Source distribution generator |
| 192 |
#[serde(default)] |
| 193 |
pub sdist_generator: SdistGenerator, |
| 194 |
/// The directory with python module, contains `<module_name>/__init__.py` |
| 195 |
pub python_source: Option<PathBuf>, |
| 196 |
/// Python packages to include |
| 197 |
pub python_packages: Option<Vec<String>>, |
| 198 |
/// Path to the wheel directory, defaults to `<module_name>.data` |
| 199 |
pub data: Option<PathBuf>, |
| 200 |
/// Cargo compile targets |
| 201 |
pub targets: Option<Vec<CargoTarget>>, |
| 202 |
/// Target configuration |
| 203 |
#[serde(default, rename = "target")] |
| 204 |
pub target_config: HashMap<String, TargetConfig>, |
| 205 |
// Some customizable cargo options |
| 206 |
/// Build artifacts with the specified Cargo profile |
| 207 |
pub profile: Option<String>, |
| 208 |
/// Space or comma separated list of features to activate |
| 209 |
pub features: Option<Vec<String>>, |
| 210 |
/// Activate all available features |
| 211 |
pub all_features: Option<bool>, |
| 212 |
/// Do not activate the `default` feature |
| 213 |
pub no_default_features: Option<bool>, |
| 214 |
/// Path to Cargo.toml |
| 215 |
pub manifest_path: Option<PathBuf>, |
| 216 |
/// Require Cargo.lock and cache are up to date |
| 217 |
pub frozen: Option<bool>, |
| 218 |
/// Require Cargo.lock is up to date |
| 219 |
pub locked: Option<bool>, |
| 220 |
/// Override a configuration value (unstable) |
| 221 |
pub config: Option<Vec<String>>, |
| 222 |
/// Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details |
| 223 |
pub unstable_flags: Option<Vec<String>>, |
| 224 |
/// Additional rustc arguments |
| 225 |
pub rustc_args: Option<Vec<String>>, |
| 226 |
/// Use base Python executable instead of venv Python executable in PEP 517 build. |
| 227 |
// |
| 228 |
// This can help avoid unnecessary rebuilds, as the Python executable does not change |
| 229 |
// every time. It should not be set when the sdist build requires packages installed |
| 230 |
// in venv. |
| 231 |
#[serde(default)] |
| 232 |
pub use_base_python: bool, |
| 233 |
} |
| 234 |
|
| 235 |
/// A pyproject.toml as specified in PEP 517 |
| 236 |
#[derive(Serialize, Deserialize, Debug, Clone)] |
| 237 |
#[serde(rename_all = "kebab-case")] |
| 238 |
pub struct PyProjectToml { |
| 239 |
/// Build-related data |
| 240 |
pub build_system: BuildSystem, |
| 241 |
/// Project metadata |
| 242 |
pub project: Option<Project>, |
| 243 |
/// PEP 518: The `[tool]` table is where any tool related to your Python project, not just build |
| 244 |
/// tools, can have users specify configuration data as long as they use a sub-table within |
| 245 |
/// `[tool]`, e.g. the flit tool would store its configuration in `[tool.flit]`. |
| 246 |
/// |
| 247 |
/// We use it for `[tool.maturin]` |
| 248 |
pub tool: Option<Tool>, |
| 249 |
} |
| 250 |
|
| 251 |
impl PyProjectToml { |
| 252 |
/// Returns the contents of a pyproject.toml with a `[build-system]` entry or an error |
| 253 |
/// |
| 254 |
/// Does no specific error handling because it's only used to check whether or not to build |
| 255 |
/// source distributions |
| 256 |
pub fn new(pyproject_file: impl AsRef<Path>) -> Result<PyProjectToml> { |
| 257 |
let path = pyproject_file.as_ref(); |
| 258 |
let contents = fs::read_to_string(path)?; |
| 259 |
let pyproject = toml::from_str(&contents).with_context(|| { |
| 260 |
format!( |
| 261 |
"pyproject.toml at {} is invalid", |
| 262 |
pyproject_file.as_ref().display() |
| 263 |
) |
| 264 |
})?; |
| 265 |
Ok(pyproject) |
| 266 |
} |
| 267 |
|
| 268 |
/// Returns the value of `[project.name]` in pyproject.toml |
| 269 |
pub fn project_name(&self) -> Option<&str> { |
| 270 |
self.project.as_ref().map(|project| project.name.as_str()) |
| 271 |
} |
| 272 |
|
| 273 |
/// Returns the values of `[tool.maturin]` in pyproject.toml |
| 274 |
#[inline] |
| 275 |
pub fn maturin(&self) -> Option<&ToolMaturin> { |
| 276 |
self.tool.as_ref()?.maturin.as_ref() |
| 277 |
} |
| 278 |
|
| 279 |
/// Returns the value of `[tool.maturin.module-name]` in pyproject.toml |
| 280 |
pub fn module_name(&self) -> Option<&str> { |
| 281 |
self.maturin()?.module_name.as_deref() |
| 282 |
} |
| 283 |
|
| 284 |
/// Returns the value of `[tool.maturin.include]` in pyproject.toml |
| 285 |
pub fn include(&self) -> Option<&[GlobPattern]> { |
| 286 |
self.maturin()?.include.as_ref().map(AsRef::as_ref) |
| 287 |
} |
| 288 |
|
| 289 |
/// Returns the value of `[tool.maturin.exclude]` in pyproject.toml |
| 290 |
pub fn exclude(&self) -> Option<&[GlobPattern]> { |
| 291 |
self.maturin()?.exclude.as_ref().map(AsRef::as_ref) |
| 292 |
} |
| 293 |
|
| 294 |
/// Returns the value of `[tool.maturin.bindings]` in pyproject.toml |
| 295 |
pub fn bindings(&self) -> Option<&str> { |
| 296 |
self.maturin()?.bindings.as_deref() |
| 297 |
} |
| 298 |
|
| 299 |
/// Returns the value of `[tool.maturin.compatibility]` in pyproject.toml |
| 300 |
pub fn compatibility(&self) -> Option<PlatformTag> { |
| 301 |
self.maturin()?.compatibility |
| 302 |
} |
| 303 |
|
| 304 |
/// Returns the value of `[tool.maturin.auditwheel]` in pyproject.toml |
| 305 |
pub fn auditwheel(&self) -> Option<AuditWheelMode> { |
| 306 |
self.maturin() |
| 307 |
.map(|maturin| maturin.auditwheel) |
| 308 |
.unwrap_or_default() |
| 309 |
} |
| 310 |
|
| 311 |
/// Returns the value of `[tool.maturin.skip-auditwheel]` in pyproject.toml |
| 312 |
pub fn skip_auditwheel(&self) -> bool { |
| 313 |
self.maturin() |
| 314 |
.map(|maturin| maturin.skip_auditwheel) |
| 315 |
.unwrap_or_default() |
| 316 |
} |
| 317 |
|
| 318 |
/// Returns the value of `[tool.maturin.strip]` in pyproject.toml |
| 319 |
pub fn strip(&self) -> bool { |
| 320 |
self.maturin() |
| 321 |
.map(|maturin| maturin.strip) |
| 322 |
.unwrap_or_default() |
| 323 |
} |
| 324 |
|
| 325 |
/// Returns the value of `[tool.maturin.sdist-generator]` in pyproject.toml |
| 326 |
pub fn sdist_generator(&self) -> SdistGenerator { |
| 327 |
self.maturin() |
| 328 |
.map(|maturin| maturin.sdist_generator) |
| 329 |
.unwrap_or_default() |
| 330 |
} |
| 331 |
|
| 332 |
/// Returns the value of `[tool.maturin.python-source]` in pyproject.toml |
| 333 |
pub fn python_source(&self) -> Option<&Path> { |
| 334 |
self.maturin() |
| 335 |
.and_then(|maturin| maturin.python_source.as_deref()) |
| 336 |
} |
| 337 |
|
| 338 |
/// Returns the value of `[tool.maturin.python-packages]` in pyproject.toml |
| 339 |
pub fn python_packages(&self) -> Option<&[String]> { |
| 340 |
self.maturin() |
| 341 |
.and_then(|maturin| maturin.python_packages.as_deref()) |
| 342 |
} |
| 343 |
|
| 344 |
/// Returns the value of `[tool.maturin.data]` in pyproject.toml |
| 345 |
pub fn data(&self) -> Option<&Path> { |
| 346 |
self.maturin().and_then(|maturin| maturin.data.as_deref()) |
| 347 |
} |
| 348 |
|
| 349 |
/// Returns the value of `[tool.maturin.targets]` in pyproject.toml |
| 350 |
pub fn targets(&self) -> Option<Vec<CargoTarget>> { |
| 351 |
self.maturin().and_then(|maturin| maturin.targets.clone()) |
| 352 |
} |
| 353 |
|
| 354 |
/// Returns the value of `[tool.maturin.target.<target>]` in pyproject.toml |
| 355 |
pub fn target_config(&self, target: &str) -> Option<&TargetConfig> { |
| 356 |
self.maturin() |
| 357 |
.and_then(|maturin| maturin.target_config.get(target)) |
| 358 |
} |
| 359 |
|
| 360 |
/// Returns the value of `[tool.maturin.manifest-path]` in pyproject.toml |
| 361 |
pub fn manifest_path(&self) -> Option<&Path> { |
| 362 |
self.maturin()?.manifest_path.as_deref() |
| 363 |
} |
| 364 |
|
| 365 |
/// Warn about `build-system.requires` mismatching expectations. |
| 366 |
/// |
| 367 |
/// Having a pyproject.toml without a version constraint is a bad idea |
| 368 |
/// because at some point we'll have to do breaking changes and then source |
| 369 |
/// distributions would break. |
| 370 |
/// |
| 371 |
/// The second problem we check for is the current maturin version not matching the constraint. |
| 372 |
/// |
| 373 |
/// Returns false if a warning was emitted. |
| 374 |
pub fn warn_bad_maturin_version(&self) -> bool { |
| 375 |
let maturin = env!("CARGO_PKG_NAME"); |
| 376 |
let current_major = env!("CARGO_PKG_VERSION_MAJOR").parse::<usize>().unwrap(); |
| 377 |
let self_version = Version::from_str(env!("CARGO_PKG_VERSION")).unwrap(); |
| 378 |
let requires_maturin = self |
| 379 |
.build_system |
| 380 |
.requires |
| 381 |
.iter() |
| 382 |
.find(|x| x.name.as_ref() == maturin); |
| 383 |
if let Some(requires_maturin) = requires_maturin { |
| 384 |
match requires_maturin.version_or_url.as_ref() { |
| 385 |
Some(VersionOrUrl::VersionSpecifier(version_specifier)) => { |
| 386 |
if !version_specifier.contains(&self_version) { |
| 387 |
eprintln!( |
| 388 |
"⚠️ Warning: You specified {requires_maturin} in pyproject.toml under \ |
| 389 |
`build-system.requires`, but the current {maturin} version is {self_version}", |
| 390 |
); |
| 391 |
return false; |
| 392 |
} |
| 393 |
} |
| 394 |
Some(VersionOrUrl::Url(_)) => { |
| 395 |
// We can't check this |
| 396 |
} |
| 397 |
None => { |
| 398 |
eprintln!( |
| 399 |
"⚠️ Warning: Please use {maturin} in pyproject.toml with a version constraint, \ |
| 400 |
e.g. `requires = [\"{maturin}>={current}.0,<{next}.0\"]`. \ |
| 401 |
This will become an error.", |
| 402 |
maturin = maturin, |
| 403 |
current = current_major, |
| 404 |
next = current_major + 1, |
| 405 |
); |
| 406 |
return false; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
true |
| 411 |
} |
| 412 |
|
| 413 |
/// Having a pyproject.toml without `build-backend` set to `maturin` |
| 414 |
/// may result in build errors when build from source distribution |
| 415 |
/// |
| 416 |
/// Returns true if the pyproject.toml has `build-backend` set to `maturin` |
| 417 |
pub fn warn_missing_build_backend(&self) -> bool { |
| 418 |
let maturin = env!("CARGO_PKG_NAME"); |
| 419 |
if self.build_system.build_backend.as_deref() == Some(maturin) { |
| 420 |
return true; |
| 421 |
} |
| 422 |
|
| 423 |
if std::env::var("MATURIN_NO_MISSING_BUILD_BACKEND_WARNING").is_ok() { |
| 424 |
return false; |
| 425 |
} |
| 426 |
|
| 427 |
eprintln!( |
| 428 |
"⚠️ Warning: `build-backend` in pyproject.toml is not set to `{maturin}`, \ |
| 429 |
packaging tools such as pip will not use maturin to build this project." |
| 430 |
); |
| 431 |
false |
| 432 |
} |
| 433 |
|
| 434 |
/// Having a pyproject.toml project table with neither `version` nor `dynamic = ['version']` |
| 435 |
/// violates https://packaging.python.org/en/latest/specifications/pyproject-toml/#dynamic. |
| 436 |
/// |
| 437 |
/// Returns true if version information is specified correctly or no project table is present. |
| 438 |
pub fn warn_invalid_version_info(&self) -> bool { |
| 439 |
let Some(project) = &self.project else { |
| 440 |
return true; |
| 441 |
}; |
| 442 |
let has_static_version = project.version.is_some(); |
| 443 |
let has_dynamic_version = project |
| 444 |
.dynamic |
| 445 |
.as_ref() |
| 446 |
.is_some_and(|d| d.iter().any(|s| s == "version")); |
| 447 |
if has_static_version && has_dynamic_version { |
| 448 |
eprintln!("⚠️ Warning: `project.dynamic` must not specify `version` when `project.version` is present in pyproject.toml"); |
| 449 |
return false; |
| 450 |
} |
| 451 |
if !has_static_version && !has_dynamic_version { |
| 452 |
eprintln!("⚠️ Warning: `project.version` field is required in pyproject.toml unless it is present in the `project.dynamic` list"); |
| 453 |
return false; |
| 454 |
} |
| 455 |
true |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
#[cfg(test)] |
| 460 |
mod tests { |
| 461 |
use crate::{ |
| 462 |
pyproject_toml::{Format, Formats, GlobPattern, ToolMaturin}, |
| 463 |
PyProjectToml, |
| 464 |
}; |
| 465 |
use expect_test::expect; |
| 466 |
use fs_err as fs; |
| 467 |
use indoc::indoc; |
| 468 |
use pretty_assertions::assert_eq; |
| 469 |
use std::path::Path; |
| 470 |
use tempfile::TempDir; |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn test_parse_tool_maturin() { |
| 474 |
let tmp_dir = TempDir::new().unwrap(); |
| 475 |
let pyproject_file = tmp_dir.path().join("pyproject.toml"); |
| 476 |
|
| 477 |
fs::write( |
| 478 |
&pyproject_file, |
| 479 |
r#"[build-system] |
| 480 |
requires = ["maturin"] |
| 481 |
build-backend = "maturin" |
| 482 |
|
| 483 |
[tool.maturin] |
| 484 |
manylinux = "2010" |
| 485 |
python-packages = ["foo", "bar"] |
| 486 |
manifest-path = "Cargo.toml" |
| 487 |
profile = "dev" |
| 488 |
features = ["foo", "bar"] |
| 489 |
no-default-features = true |
| 490 |
locked = true |
| 491 |
rustc-args = ["-Z", "unstable-options"] |
| 492 |
|
| 493 |
[[tool.maturin.targets]] |
| 494 |
name = "pyo3_pure" |
| 495 |
kind = "lib" |
| 496 |
bindings = "pyo3" |
| 497 |
|
| 498 |
[tool.maturin.target."x86_64-apple-darwin"] |
| 499 |
macos-deployment-target = "10.12" |
| 500 |
"#, |
| 501 |
) |
| 502 |
.unwrap(); |
| 503 |
let pyproject = PyProjectToml::new(pyproject_file).unwrap(); |
| 504 |
assert_eq!(pyproject.manifest_path(), Some(Path::new("Cargo.toml"))); |
| 505 |
|
| 506 |
let maturin = pyproject.maturin().unwrap(); |
| 507 |
assert_eq!(maturin.profile.as_deref(), Some("dev")); |
| 508 |
assert_eq!( |
| 509 |
maturin.features, |
| 510 |
Some(vec!["foo".to_string(), "bar".to_string()]) |
| 511 |
); |
| 512 |
assert!(maturin.all_features.is_none()); |
| 513 |
assert_eq!(maturin.no_default_features, Some(true)); |
| 514 |
assert_eq!(maturin.locked, Some(true)); |
| 515 |
assert!(maturin.frozen.is_none()); |
| 516 |
assert_eq!( |
| 517 |
maturin.rustc_args, |
| 518 |
Some(vec!["-Z".to_string(), "unstable-options".to_string()]) |
| 519 |
); |
| 520 |
assert_eq!( |
| 521 |
maturin.python_packages, |
| 522 |
Some(vec!["foo".to_string(), "bar".to_string()]) |
| 523 |
); |
| 524 |
let targets = maturin.targets.as_ref().unwrap(); |
| 525 |
assert_eq!("pyo3_pure", targets[0].name); |
| 526 |
let target_config = pyproject.target_config("x86_64-apple-darwin").unwrap(); |
| 527 |
assert_eq!( |
| 528 |
target_config.macos_deployment_target.as_deref(), |
| 529 |
Some("10.12") |
| 530 |
); |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn test_warn_missing_maturin_version() { |
| 535 |
let with_constraint = PyProjectToml::new("test-crates/pyo3-pure/pyproject.toml").unwrap(); |
| 536 |
assert!(with_constraint.warn_bad_maturin_version()); |
| 537 |
let without_constraint_dir = TempDir::new().unwrap(); |
| 538 |
let pyproject_file = without_constraint_dir.path().join("pyproject.toml"); |
| 539 |
|
| 540 |
fs::write( |
| 541 |
&pyproject_file, |
| 542 |
r#"[build-system] |
| 543 |
requires = ["maturin"] |
| 544 |
build-backend = "maturin" |
| 545 |
|
| 546 |
[tool.maturin] |
| 547 |
manylinux = "2010" |
| 548 |
"#, |
| 549 |
) |
| 550 |
.unwrap(); |
| 551 |
let without_constraint = PyProjectToml::new(pyproject_file).unwrap(); |
| 552 |
assert!(!without_constraint.warn_bad_maturin_version()); |
| 553 |
} |
| 554 |
|
| 555 |
#[test] |
| 556 |
fn test_warn_incorrect_maturin_version() { |
| 557 |
let without_constraint_dir = TempDir::new().unwrap(); |
| 558 |
let pyproject_file = without_constraint_dir.path().join("pyproject.toml"); |
| 559 |
|
| 560 |
fs::write( |
| 561 |
&pyproject_file, |
| 562 |
r#"[build-system] |
| 563 |
requires = ["maturin==0.0.1"] |
| 564 |
build-backend = "maturin" |
| 565 |
|
| 566 |
[tool.maturin] |
| 567 |
manylinux = "2010" |
| 568 |
"#, |
| 569 |
) |
| 570 |
.unwrap(); |
| 571 |
let without_constraint = PyProjectToml::new(pyproject_file).unwrap(); |
| 572 |
assert!(!without_constraint.warn_bad_maturin_version()); |
| 573 |
} |
| 574 |
|
| 575 |
#[test] |
| 576 |
fn test_warn_invalid_version_info_conflict() { |
| 577 |
let conflict = toml::from_str::<PyProjectToml>( |
| 578 |
r#"[build-system] |
| 579 |
requires = ["maturin==1.0.0"] |
| 580 |
|
| 581 |
[project] |
| 582 |
name = "..." |
| 583 |
version = "1.2.3" |
| 584 |
dynamic = ['version'] |
| 585 |
"#, |
| 586 |
) |
| 587 |
.unwrap(); |
| 588 |
assert!(!conflict.warn_invalid_version_info()); |
| 589 |
} |
| 590 |
|
| 591 |
#[test] |
| 592 |
fn test_warn_invalid_version_info_missing() { |
| 593 |
let missing = toml::from_str::<PyProjectToml>( |
| 594 |
r#"[build-system] |
| 595 |
requires = ["maturin==1.0.0"] |
| 596 |
|
| 597 |
[project] |
| 598 |
name = "..." |
| 599 |
"#, |
| 600 |
) |
| 601 |
.unwrap(); |
| 602 |
assert!(!missing.warn_invalid_version_info()); |
| 603 |
} |
| 604 |
|
| 605 |
#[test] |
| 606 |
fn test_warn_invalid_version_info_ok() { |
| 607 |
let static_ver = toml::from_str::<PyProjectToml>( |
| 608 |
r#"[build-system] |
| 609 |
requires = ["maturin==1.0.0"] |
| 610 |
|
| 611 |
[project] |
| 612 |
name = "..." |
| 613 |
version = "1.2.3" |
| 614 |
"#, |
| 615 |
) |
| 616 |
.unwrap(); |
| 617 |
assert!(static_ver.warn_invalid_version_info()); |
| 618 |
let dynamic_ver = toml::from_str::<PyProjectToml>( |
| 619 |
r#"[build-system] |
| 620 |
requires = ["maturin==1.0.0"] |
| 621 |
|
| 622 |
[project] |
| 623 |
name = "..." |
| 624 |
dynamic = ['version'] |
| 625 |
"#, |
| 626 |
) |
| 627 |
.unwrap(); |
| 628 |
assert!(dynamic_ver.warn_invalid_version_info()); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn deserialize_include_exclude() { |
| 633 |
let single = r#"include = ["single"]"#; |
| 634 |
assert_eq!( |
| 635 |
toml::from_str::<ToolMaturin>(single).unwrap().include, |
| 636 |
Some(vec![GlobPattern::Path("single".to_string())]) |
| 637 |
); |
| 638 |
|
| 639 |
let multiple = r#"include = ["one", "two"]"#; |
| 640 |
assert_eq!( |
| 641 |
toml::from_str::<ToolMaturin>(multiple).unwrap().include, |
| 642 |
Some(vec![ |
| 643 |
GlobPattern::Path("one".to_string()), |
| 644 |
GlobPattern::Path("two".to_string()) |
| 645 |
]) |
| 646 |
); |
| 647 |
|
| 648 |
let single_format = r#"include = [{path = "path", format="sdist"}]"#; |
| 649 |
assert_eq!( |
| 650 |
toml::from_str::<ToolMaturin>(single_format) |
| 651 |
.unwrap() |
| 652 |
.include, |
| 653 |
Some(vec![GlobPattern::WithFormat { |
| 654 |
path: "path".to_string(), |
| 655 |
format: Formats::Single(Format::Sdist) |
| 656 |
},]) |
| 657 |
); |
| 658 |
|
| 659 |
let multiple_formats = r#"include = [{path = "path", format=["sdist", "wheel"]}]"#; |
| 660 |
assert_eq!( |
| 661 |
toml::from_str::<ToolMaturin>(multiple_formats) |
| 662 |
.unwrap() |
| 663 |
.include, |
| 664 |
Some(vec![GlobPattern::WithFormat { |
| 665 |
path: "path".to_string(), |
| 666 |
format: Formats::Multiple(vec![Format::Sdist, Format::Wheel]) |
| 667 |
},]) |
| 668 |
); |
| 669 |
|
| 670 |
let mixed = r#"include = ["one", {path = "two", format="sdist"}, {path = "three", format=["sdist", "wheel"]}]"#; |
| 671 |
assert_eq!( |
| 672 |
toml::from_str::<ToolMaturin>(mixed).unwrap().include, |
| 673 |
Some(vec![ |
| 674 |
GlobPattern::Path("one".to_string()), |
| 675 |
GlobPattern::WithFormat { |
| 676 |
path: "two".to_string(), |
| 677 |
format: Formats::Single(Format::Sdist), |
| 678 |
}, |
| 679 |
GlobPattern::WithFormat { |
| 680 |
path: "three".to_string(), |
| 681 |
format: Formats::Multiple(vec![Format::Sdist, Format::Wheel]) |
| 682 |
} |
| 683 |
]) |
| 684 |
); |
| 685 |
} |
| 686 |
|
| 687 |
#[test] |
| 688 |
fn test_gh_1615() { |
| 689 |
let source = indoc!( |
| 690 |
r#"[build-system] |
| 691 |
requires = [ "maturin>=0.14", "numpy", "wheel", "patchelf",] |
| 692 |
build-backend = "maturin" |
| 693 |
|
| 694 |
[project] |
| 695 |
name = "..." |
| 696 |
license-files = [ "license.txt",] |
| 697 |
requires-python = ">=3.8" |
| 698 |
requires-dist = [ "maturin>=0.14", "...",] |
| 699 |
dependencies = [ "packaging", "...",] |
| 700 |
zip-safe = false |
| 701 |
version = "..." |
| 702 |
readme = "..." |
| 703 |
description = "..." |
| 704 |
classifiers = [ "...",] |
| 705 |
"# |
| 706 |
); |
| 707 |
let temp_dir = TempDir::new().unwrap(); |
| 708 |
let pyproject_toml = temp_dir.path().join("pyproject.toml"); |
| 709 |
fs::write(&pyproject_toml, source).unwrap(); |
| 710 |
let outer_error = PyProjectToml::new(&pyproject_toml).unwrap_err(); |
| 711 |
let inner_error = outer_error.source().unwrap(); |
| 712 |
|
| 713 |
let expected = expect![[r#" |
| 714 |
TOML parse error at line 10, column 16 |
| 715 |
| |
| 716 |
10 | dependencies = [ "packaging", "...",] |
| 717 |
| ^^^^^^^^^^^^^^^^^^^^^^ |
| 718 |
URL requirement must be preceded by a package name. Add the name of the package before the URL (e.g., `package_name @ /path/to/file`). |
| 719 |
... |
| 720 |
^^^ |
| 721 |
"#]]; |
| 722 |
expected.assert_eq(&inner_error.to_string()); |
| 723 |
} |
| 724 |
} |