Skip to content

Commit 06e69c1

Browse files
abravalheridavidhewitt
authored andcommitted
Allow extensions to be added via pyproject.toml
1 parent 646857a commit 06e69c1

File tree

13 files changed

+459
-3
lines changed

13 files changed

+459
-3
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
## Unreleased
44
### Packaging
5-
- Include `py.typed` when packaging to denote that setuptools-rust includes type hints. [#338](https://github.com/PyO3/setuptools-rust/pull/338)
65
- Remove direct imports from `distutils`. [#336](https://github.com/PyO3/setuptools-rust/pull/336)
6+
- Include `py.typed` when packaging to denote that setuptools-rust includes type hints. [#338](https://github.com/PyO3/setuptools-rust/pull/338)
7+
8+
### Added
9+
- Add support for `pyproject.toml` configuration using `[tool.setuptools-rust]` options. [#348](https://github.com/PyO3/setuptools-rust/pull/348)
710

811
## 1.6.0 (2023-04-27)
912
### Changed

examples/hello-world-pyprojecttoml/Cargo.lock

+273
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "hello-world"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
pyo3 = { version = "0.19.2", features = ["extension-module"] }
10+
11+
[profile.release-lto]
12+
inherits = "release"
13+
lto = true
14+
15+
[lib]
16+
# See https://github.com/PyO3/pyo3 for details
17+
name = "_lib" # private module to be nested into Python package
18+
crate-type = ["cdylib"]
19+
path = "rust/lib.rs"
20+
21+
[[bin]]
22+
name = "print-hello"
23+
path = "rust/print_hello.rs"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
graft python
2+
graft rust
3+
graft tests
4+
include Cargo.toml noxfile.py
5+
global-exclude */__pycache__/*
6+
global-exclude *.pyc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from os.path import dirname
2+
3+
import nox
4+
5+
SETUPTOOLS_RUST = dirname(dirname(dirname(__file__)))
6+
7+
8+
@nox.session()
9+
def test(session: nox.Session):
10+
session.install(SETUPTOOLS_RUST, "wheel", "build", "pytest")
11+
# Ensure build works as intended
12+
session.install("--no-build-isolation", ".")
13+
# Test Rust binary
14+
session.run("print-hello")
15+
# Test script wrapper for Python entry-point
16+
session.run("sum-cli", "5", "7")
17+
session.run("rust-demo", "5", "7")
18+
# Test library
19+
session.run("pytest", "tests", *session.posargs)
20+
session.run("python", "-c", "from hello_world import _lib; print(_lib)")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[build-system]
2+
requires = ["setuptools", "setuptools-rust"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "hello-world"
7+
version = "1.0"
8+
9+
[project.scripts]
10+
# Python entry-point wrapper to be installed in `$venv/bin`
11+
sum-cli = "hello_world.sum_cli:main" # Python function that uses Rust
12+
rust-demo = "hello_world._lib:demo" # Rust function that uses Python
13+
14+
[tool.setuptools.packages]
15+
# Pure Python packages/modules
16+
find = { where = ["python"] }
17+
18+
[[tool.setuptools-rust.ext-modules]]
19+
# Private Rust extension module to be nested into Python package
20+
target = "hello_world._lib" # The last part of the name (e.g. "_lib") has to match lib.name in Cargo.toml,
21+
# but you can add a prefix to nest it inside of a Python package.
22+
py-limited-api = "auto" # Default value, can be omitted
23+
binding = "PyO3" # Default value, can be omitted
24+
# See reference for RustExtension in https://setuptools-rust.readthedocs.io/en/latest/reference.html
25+
26+
[[tool.setuptools-rust.bins]]
27+
# Rust executable to be installed in `$venv/bin`
28+
target = "print-hello" # Needs to match bin.name in Cargo.toml
29+
args = ["--profile", "release-lto"] # Extra args for Cargo
30+
# See reference for RustBin in https://setuptools-rust.readthedocs.io/en/latest/reference.html
31+
# Note that you can also use Python entry-points as alternative to Rust binaries
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from ._lib import sum_as_string # export public parts of the binary extension
2+
3+
__all__ = ["sum_as_string"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import argparse
2+
import sys
3+
4+
from ._lib import sum_as_string
5+
6+
7+
def main():
8+
parser = argparse.ArgumentParser("sum 2 integers")
9+
parser.add_argument("x", type=int)
10+
parser.add_argument("y", type=int)
11+
args = parser.parse_args()
12+
print(f"{args.x} + {args.y} = {sum_as_string(args.x, args.y)}")
13+
14+
15+
if __name__ == "__main__":
16+
main()

0 commit comments

Comments
 (0)