maturin 1.12.0

Build and publish crates with pyo3, cffi and uniffi bindings as well as rust binaries as python packages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use crate::build_options::{CargoOptions, extract_cargo_metadata_args};
use crate::{CargoToml, Metadata24, PyProjectToml};
use anyhow::{Context, Result, bail, format_err};
use cargo_metadata::{Metadata, MetadataCommand};
use normpath::PathExt as _;
use std::collections::HashSet;
use std::env;
use std::io;
use std::path::{Path, PathBuf};
use tracing::{debug, instrument, warn};

const PYPROJECT_TOML: &str = "pyproject.toml";

/// Whether this project is pure rust or rust mixed with python and whether it has wheel data
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectLayout {
    /// Contains the absolute path to the project root directory
    pub project_root: PathBuf,
    /// Contains the absolute path to the python source directory
    pub python_dir: PathBuf,
    /// Contains the canonicalized (i.e. absolute) path to the python part of the project
    /// If none, we have a rust crate compiled into a shared library with only some glue python for cffi
    /// If some, we have a python package that is extended by a native rust module.
    pub python_module: Option<PathBuf>,
    /// Python packages to include
    pub python_packages: Vec<String>,
    /// Contains the canonicalized (i.e. absolute) path to the rust part of the project
    pub rust_module: PathBuf,
    /// Rust extension name
    pub extension_name: String,
    /// The location of the wheel data, if any
    pub data: Option<PathBuf>,
}

/// Project resolver
#[derive(Clone, Debug)]
pub struct ProjectResolver {
    /// Project layout
    pub project_layout: ProjectLayout,
    /// Cargo.toml path
    pub cargo_toml_path: PathBuf,
    /// Parsed Cargo.toml
    pub cargo_toml: CargoToml,
    /// pyproject.toml path
    pub pyproject_toml_path: PathBuf,
    /// Parsed pyproject.toml
    pub pyproject_toml: Option<PyProjectToml>,
    /// Rust module name
    pub module_name: String,
    /// Python Package Metadata 2.3
    pub metadata24: Metadata24,
    /// Cargo options
    pub cargo_options: CargoOptions,
    /// Cargo.toml as resolved by [cargo_metadata]
    pub cargo_metadata: Metadata,
    /// maturin options specified in pyproject.toml
    pub pyproject_toml_maturin_options: Vec<&'static str>,
}

impl ProjectResolver {
    /// Resolve project layout
    pub fn resolve(
        cargo_manifest_path: Option<PathBuf>,
        mut cargo_options: CargoOptions,
        editable_install: bool,
    ) -> Result<Self> {
        let (manifest_file, pyproject_file) =
            Self::resolve_manifest_paths(cargo_manifest_path, &cargo_options)?;
        if !manifest_file.is_file() {
            bail!(
                "{} is not the path to a Cargo.toml",
                manifest_file.display()
            );
        }
        // Sanity checks in debug build
        debug_assert!(
            manifest_file.is_absolute(),
            "manifest_file {} is not absolute",
            manifest_file.display()
        );
        debug_assert!(
            pyproject_file.is_absolute(),
            "pyproject_file {} is not absolute",
            pyproject_file.display()
        );

        // Set Cargo manifest path
        cargo_options.manifest_path = Some(manifest_file.clone());

        let cargo_toml = CargoToml::from_path(&manifest_file)?;
        cargo_toml.check_removed_python_metadata()?;

        let manifest_dir = manifest_file.parent().unwrap();
        let pyproject_toml: Option<PyProjectToml> = if pyproject_file.is_file() {
            let pyproject = PyProjectToml::new(&pyproject_file)?;
            pyproject.warn_bad_maturin_version();
            pyproject.warn_missing_build_backend();
            Some(pyproject)
        } else {
            None
        };
        let pyproject = pyproject_toml.as_ref();
        let tool_maturin = pyproject.and_then(|p| p.maturin());

        let pyproject_toml_maturin_options = if let Some(tool_maturin) = tool_maturin {
            cargo_options.merge_with_pyproject_toml(tool_maturin.clone(), editable_install)
        } else {
            Vec::new()
        };

        let cargo_metadata = Self::resolve_cargo_metadata(&manifest_file, &cargo_options)?;

        let mut metadata24 = Metadata24::from_cargo_toml(manifest_dir, &cargo_metadata)
            .context("Failed to parse Cargo.toml into python metadata")?;
        if let Some(pyproject) = pyproject {
            let pyproject_dir = pyproject_file.parent().unwrap();
            metadata24.merge_pyproject_toml(pyproject_dir, pyproject)?;
        }

        let crate_name = &cargo_toml.package.name;

        // If the package name contains minuses, you must declare a module with
        // underscores as lib name
        // Precedence:
        //  * Explicitly declared pyproject.toml `tool.maturin.module-name`
        //  * Cargo.toml `lib.name`
        //  * pyproject.toml `project.name`
        //  * Cargo.toml `package.name`
        let module_name = pyproject
            .and_then(|x| x.module_name())
            .or(cargo_toml.lib.as_ref().and_then(|lib| lib.name.as_deref()))
            .or(pyproject
                .and_then(|pyproject| pyproject.project.as_ref())
                .map(|project| project.name.as_str()))
            .unwrap_or(crate_name)
            .to_owned();

        let project_root = if pyproject_file.is_file() {
            pyproject_file.parent().unwrap_or(manifest_dir)
        } else {
            manifest_dir
        };
        let python_packages = pyproject
            .and_then(|x| x.python_packages())
            .unwrap_or_default()
            .to_vec();
        let py_root = match pyproject.and_then(|x| x.python_source()) {
            Some(py_src) => project_root
                .join(py_src)
                .normalize()
                .with_context(|| {
                    format!(
                        "python-source is set to `{}` but the directory does not exist. \
                        Either create the directory or remove the `python-source` setting \
                        from pyproject.toml.",
                        py_src.display()
                    )
                })?
                .into_path_buf(),
            None => match pyproject.and_then(|x| x.project_name()) {
                Some(project_name) => {
                    // Detect src layout
                    let rust_cargo_toml_found =
                        project_root.join("rust").join("Cargo.toml").is_file();
                    let import_name = project_name.replace('-', "_");
                    let mut package_init = HashSet::new();
                    package_init.insert(
                        project_root
                            .join("src")
                            .join(import_name)
                            .join("__init__.py"),
                    );
                    for package in &python_packages {
                        package_init
                            .insert(project_root.join("src").join(package).join("__init__.py"));
                    }
                    let python_src_found = package_init.iter().any(|x| x.is_file());
                    if rust_cargo_toml_found && python_src_found {
                        project_root.join("src")
                    } else {
                        project_root.to_path_buf()
                    }
                }
                None => project_root.to_path_buf(),
            },
        };
        let data = pyproject.and_then(|x| x.data()).map(|data| {
            if data.is_absolute() {
                data.to_path_buf()
            } else {
                project_root.join(data)
            }
        });
        let custom_python_source = pyproject.and_then(|x| x.python_source()).is_some();
        let project_layout = ProjectLayout::determine(
            project_root,
            &module_name,
            py_root,
            python_packages,
            data,
            custom_python_source,
        )?;
        Ok(Self {
            project_layout,
            cargo_toml_path: manifest_file,
            cargo_toml,
            pyproject_toml_path: pyproject_file,
            pyproject_toml,
            module_name,
            metadata24,
            cargo_options,
            cargo_metadata,
            pyproject_toml_maturin_options,
        })
    }

    /// Get cargo manifest file path and pyproject.toml path
    fn resolve_manifest_paths(
        cargo_manifest_path: Option<PathBuf>,
        cargo_options: &CargoOptions,
    ) -> Result<(PathBuf, PathBuf)> {
        // use command line argument if specified
        if let Some(path) = cargo_manifest_path {
            let path = path
                .normalize()
                .with_context(|| {
                    format!(
                        "manifest path `{}` does not exist or is invalid",
                        path.display()
                    )
                })?
                .into_path_buf();
            debug!(
                "Using cargo manifest path from command line argument: {:?}",
                path
            );
            let workspace_root = Self::resolve_cargo_metadata(&path, cargo_options)?.workspace_root;
            let workspace_parent = workspace_root.parent().unwrap_or(&workspace_root);
            for parent in path.ancestors().skip(1) {
                // Allow looking outside to the parent directory of Cargo workspace root
                if !dunce::simplified(parent).starts_with(workspace_parent) {
                    break;
                }
                let pyproject_file = parent.join(PYPROJECT_TOML);
                if pyproject_file.is_file() {
                    debug!("Found pyproject.toml at {:?}", pyproject_file);
                    return Ok((path, pyproject_file));
                }
            }
            let pyproject_file = path.parent().unwrap().join(PYPROJECT_TOML);
            debug!("Trying pyproject.toml at {:?}", pyproject_file);
            return Ok((path, pyproject_file));
        }
        // check `manifest-path` option in pyproject.toml
        let current_dir = env::current_dir()
            .context("Failed to detect current directory ಠ_ಠ")?
            .normalize()?
            .into_path_buf();
        let pyproject_file = current_dir.join(PYPROJECT_TOML);
        if pyproject_file.is_file() {
            debug!(
                "Found pyproject.toml in working directory at {:?}",
                pyproject_file
            );
            let pyproject = PyProjectToml::new(&pyproject_file)?;
            if let Some(path) = pyproject.manifest_path() {
                debug!("Using cargo manifest path from pyproject.toml {:?}", path);
                return Ok((
                    path.normalize()
                        .with_context(|| {
                            format!(
                                "manifest path `{}` does not exist or is invalid",
                                path.display()
                            )
                        })?
                        .into_path_buf(),
                    pyproject_file,
                ));
            } else {
                // Detect src layout:
                //
                // my-project
                // ├── README.md
                // ├── pyproject.toml
                // ├── src
                // │   └── my_project
                // │       ├── __init__.py
                // │       └── bar.py
                // └── rust
                //     ├── Cargo.toml
                //     └── src
                //         └── lib.rs
                let path = current_dir.join("rust").join("Cargo.toml");
                if path.is_file() {
                    debug!("Python first src-layout detected");
                    if pyproject.python_source().is_some() {
                        // python source directory is specified in pyproject.toml
                        return Ok((path, pyproject_file));
                    } else if let Some(project_name) = pyproject.project_name() {
                        // Check if python source directory in `src/<project_name>`
                        let import_name = project_name.replace('-', "_");
                        let mut package_init = HashSet::new();
                        package_init.insert(
                            current_dir
                                .join("src")
                                .join(import_name)
                                .join("__init__.py"),
                        );
                        for package in pyproject.python_packages().unwrap_or_default() {
                            package_init
                                .insert(current_dir.join("src").join(package).join("__init__.py"));
                        }
                        if package_init.iter().any(|x| x.is_file()) {
                            return Ok((path, pyproject_file));
                        }
                    }
                }
            }
        }
        // check Cargo.toml in current directory
        let path = current_dir.join("Cargo.toml");
        if path.exists() {
            debug!(
                "Using cargo manifest path from working directory: {:?}",
                path
            );
            Ok((path, current_dir.join(PYPROJECT_TOML)))
        } else {
            Err(format_err!(
                "Can't find {} (in {})",
                path.display(),
                current_dir.display()
            ))
        }
    }

    #[instrument(skip_all)]
    fn resolve_cargo_metadata(
        manifest_path: &Path,
        cargo_options: &CargoOptions,
    ) -> Result<Metadata> {
        debug!("Resolving cargo metadata from {:?}", manifest_path);
        let cargo_metadata_extra_args = extract_cargo_metadata_args(cargo_options)?;
        let result = MetadataCommand::new()
            // Force resolving metadata using cargo instead of instead of $CARGO env var
            // to avoid getting wrong file path like target directory, for example `cross` would
            // output paths in docker while we want paths on host.
            .cargo_path("cargo")
            .manifest_path(manifest_path)
            .verbose(true)
            .other_options(cargo_metadata_extra_args)
            .exec();

        let cargo_metadata = match result {
            Ok(cargo_metadata) => cargo_metadata,
            Err(cargo_metadata::Error::Io(inner)) if inner.kind() == io::ErrorKind::NotFound => {
                // NotFound is the specific error when cargo is not in PATH
                return Err(inner)
                    .context("Cargo metadata failed. Do you have cargo in your PATH?");
            }
            Err(err) => {
                return Err(err)
                    .context("Cargo metadata failed. Does your crate compile with `cargo build`?");
            }
        };
        Ok(cargo_metadata)
    }
}

impl ProjectLayout {
    /// Checks whether a python module exists besides Cargo.toml with the right name
    fn determine(
        project_root: &Path,
        module_name: &str,
        python_root: PathBuf,
        python_packages: Vec<String>,
        data: Option<PathBuf>,
        custom_python_source: bool,
    ) -> Result<ProjectLayout> {
        // A dot in the module name means the extension module goes into the module folder specified by the path
        let parts: Vec<&str> = module_name.split('.').collect();
        let (python_module, rust_module, extension_name) = if parts.len() > 1 {
            let mut rust_module = python_root.clone();
            rust_module.extend(&parts[0..parts.len() - 1]);
            (
                python_root.join(parts[0]),
                rust_module,
                parts[parts.len() - 1].to_string(),
            )
        } else {
            (
                python_root.join(module_name),
                python_root.join(module_name),
                module_name.to_string(),
            )
        };
        // If no __init__.py exists, we can't treat it as python source module,
        // it could be a Rust crate with the same name as the module.
        let python_module = if !python_module.join("__init__.py").is_file()
            && python_module.join("Cargo.toml").is_file()
        {
            debug!("No __init__.py file found in {}", python_module.display());
            None
        } else {
            Some(python_module)
        };
        debug!(
            project_root = %project_root.display(),
            python_dir = %python_root.display(),
            rust_module = %rust_module.display(),
            python_module = ?python_module,
            extension_name = %extension_name,
            module_name = %module_name,
            "Project layout resolved"
        );

        let data = if let Some(data) = data {
            if !data.is_dir() {
                bail!("No such data directory {}", data.display());
            }
            Some(data)
        } else if project_root.join(format!("{module_name}.data")).is_dir() {
            Some(project_root.join(format!("{module_name}.data")))
        } else {
            None
        };

        if let Some(python_module) = python_module {
            if python_module.is_dir() {
                eprintln!("🍹 Building a mixed python/rust project");

                Ok(ProjectLayout {
                    project_root: project_root.to_path_buf(),
                    python_dir: python_root,
                    python_packages,
                    python_module: Some(python_module),
                    rust_module,
                    extension_name,
                    data,
                })
            } else {
                if custom_python_source {
                    bail!(
                        "python-source is set to `{}`, but the python module at `{}` \
                        does not exist. Either create the Python module or remove the \
                        `python-source` setting from pyproject.toml.",
                        python_root.display(),
                        python_module.display()
                    );
                }

                Ok(ProjectLayout {
                    project_root: project_root.to_path_buf(),
                    python_dir: python_root,
                    python_packages,
                    python_module: None,
                    rust_module: project_root.to_path_buf(),
                    extension_name,
                    data,
                })
            }
        } else {
            Ok(ProjectLayout {
                project_root: project_root.to_path_buf(),
                python_dir: python_root,
                python_packages,
                python_module: None,
                rust_module: project_root.to_path_buf(),
                extension_name,
                data,
            })
        }
    }
}