Skip to content

Commit

Permalink
Merge #1479
Browse files Browse the repository at this point in the history
1479: Enable `extension-module` feature in `pyproject.toml` in project templates r=konstin a=messense

Implements #325 (comment).

Thanks for the idea `@jmhodges!`

Co-authored-by: messense <messense@icloud.com>
  • Loading branch information
bors[bot] and messense authored Feb 13, 2023
2 parents 7a503f9 + 739430b commit f69fdcc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 25 additions & 13 deletions guide/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,42 @@ rand = "0.8.4"

[dependencies.pyo3]
version = "0.18.0"
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
# "abi3-py37" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.7
features = ["extension-module", "abi3-py37"]
features = ["abi3-py37"]
```

Add a `pyproject.toml` to configure [PEP 518](https://peps.python.org/pep-0518/) build system requirements
and enable the `extension-module` feature of pyo3.

```toml
[build-system]
requires = ["maturin>=0.14,<0.15"]
build-backend = "maturin"

[tool.maturin]
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
features = ["pyo3/extension-module"]
```

### Use `maturin new`

New projects can also be quickly created using the `maturin new` command:

```bash
USAGE:
maturin new [FLAGS] [OPTIONS] <path>
maturin new --help
Create a new cargo project

FLAGS:
-h, --help Prints help information
--mixed Use mixed Rust/Python project layout
-V, --version Prints version information
Usage: maturin new [OPTIONS] <PATH>

OPTIONS:
-b, --bindings <bindings> Which kind of bindings to use [possible values: pyo3, rust-cpython, cffi, bin]
--name <name> Set the resulting package name, defaults to the directory name
Arguments:
<PATH> Project path

ARGS:
<path> Project path
Options:
--name <NAME> Set the resulting package name, defaults to the directory name
--mixed Use mixed Rust/Python project layout
--src Use Python first src layout for mixed Rust/Python project
-b, --bindings <BINDINGS> Which kind of bindings to use [possible values: pyo3, rust-cpython, cffi, uniffi, bin]
-h, --help Print help information
```

The above process can be achieved by running `maturin new -b pyo3 guessing_game`
Expand Down
4 changes: 2 additions & 2 deletions src/templates/Cargo.toml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ crate-type = ["cdylib"]

[dependencies]
{% if bindings == "pyo3" -%}
pyo3 = { version = "0.18.0", features = ["extension-module"] }
pyo3 = "0.18.1"
{% elif bindings == "rust-cpython" -%}
cpython = { version = "0.7.1", features = ["extension-module"] }
cpython = "0.7.1"
{% elif bindings == "uniffi" -%}
uniffi = "0.21.0"
uniffi_macros = "0.21.0"
Expand Down
7 changes: 6 additions & 1 deletion src/templates/pyproject.toml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ classifiers = [
dependencies = ["cffi"]
{%- endif %}

{% if bindings == "cffi" or bindings == "bin" or mixed_non_src -%}
{% if bindings in ["bin", "cffi", "pyo3", "rust-cpython"] or mixed_non_src -%}
[tool.maturin]
{% if bindings == "cffi" or bindings == "bin" -%}
bindings = "{{ bindings }}"
{% endif -%}
{% if mixed_non_src -%}
python-source = "python"
{% endif -%}
{% if bindings == "pyo3" -%}
features = ["pyo3/extension-module"]
{% elif bindings == "rust-cpython" -%}
features = ["cpython/extension-module"]
{% endif -%}
{% endif -%}

0 comments on commit f69fdcc

Please sign in to comment.