Skip to content

Commit

Permalink
Allow extensions to be added via pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri authored and davidhewitt committed Aug 11, 2023
1 parent 646857a commit f2866e4
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## Unreleased
### Packaging
- Include `py.typed` when packaging to denote that setuptools-rust includes type hints. [#338](https://github.com/PyO3/setuptools-rust/pull/338)
- Remove direct imports from `distutils`. [#336](https://github.com/PyO3/setuptools-rust/pull/336)
- Include `py.typed` when packaging to denote that setuptools-rust includes type hints. [#338](https://github.com/PyO3/setuptools-rust/pull/338)

### Added
- Add support for `pyproject.toml` configuration using `[tool.setuptools-rust]` options. [#348](https://github.com/PyO3/setuptools-rust/pull/348)

## 1.6.0 (2023-04-27)
### Changed
Expand Down
273 changes: 273 additions & 0 deletions examples/hello-world-pyprojecttoml/Cargo.lock

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

23 changes: 23 additions & 0 deletions examples/hello-world-pyprojecttoml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "hello-world"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = { version = "0.19.2", features = ["extension-module"] }

[profile.release-lto]
inherits = "release"
lto = true

[lib]
# See https://github.com/PyO3/pyo3 for details
name = "_lib" # private module to be nested into Python package
crate-type = ["cdylib"]
path = "rust/lib.rs"

[[bin]]
name = "print-hello"
path = "rust/print_hello.rs"
6 changes: 6 additions & 0 deletions examples/hello-world-pyprojecttoml/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
graft python
graft rust
graft tests
include Cargo.toml noxfile.py
global-exclude */__pycache__/*
global-exclude *.pyc
20 changes: 20 additions & 0 deletions examples/hello-world-pyprojecttoml/noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from os.path import dirname

import nox

SETUPTOOLS_RUST = dirname(dirname(dirname(__file__)))


@nox.session()
def test(session: nox.Session):
session.install(SETUPTOOLS_RUST, "wheel", "build", "pytest")
# Ensure build works as intended
session.install("--no-build-isolation", ".")
# Test Rust binary
session.run("print-hello")
# Test script wrapper for Python entry-point
session.run("sum-cli", "5", "7")
session.run("rust-demo", "5", "7")
# Test library
session.run("pytest", "tests", *session.posargs)
session.run("python", "-c", "from hello_world import _lib; print(_lib)")
31 changes: 31 additions & 0 deletions examples/hello-world-pyprojecttoml/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[build-system]
requires = ["setuptools", "setuptools-rust"]
build-backend = "setuptools.build_meta"

[project]
name = "hello-world"
version = "1.0"

[project.scripts]
# Python entry-point wrapper to be installed in `$venv/bin`
sum-cli = "hello_world.sum_cli:main" # Python function that uses Rust
rust-demo = "hello_world._lib:demo" # Rust function that uses Python

[tool.setuptools.packages]
# Pure Python packages/modules
find = { where = ["python"] }

[[tool.setuptools-rust.ext-modules]]
# Private Rust extension module to be nested into Python package
target = "hello_world._lib" # The last part of the name (e.g. "_lib") has to match lib.name in Cargo.toml,
# but you can add a prefix to nest it inside of a Python package.
py-limited-api = "auto" # Default value, can be omitted
binding = "PyO3" # Default value, can be omitted
# See reference for RustExtension in https://setuptools-rust.readthedocs.io/en/latest/reference.html

[[tool.setuptools-rust.bins]]
# Rust executable to be installed in `$venv/bin`
target = "print-hello" # Needs to match bin.name in Cargo.toml
args = ["--profile", "release-lto"] # Extra args for Cargo
# See reference for RustBin in https://setuptools-rust.readthedocs.io/en/latest/reference.html
# Note that you can also use Python entry-points as alternative to Rust binaries
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ._lib import sum_as_string # export public parts of the binary extension

__all__ = ["sum_as_string"]
16 changes: 16 additions & 0 deletions examples/hello-world-pyprojecttoml/python/hello_world/sum_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import argparse
import sys

from ._lib import sum_as_string


def main():
parser = argparse.ArgumentParser("sum 2 integers")
parser.add_argument("x", type=int)
parser.add_argument("y", type=int)
args = parser.parse_args()
print(f"{args.x} + {args.y} = {sum_as_string(args.x, args.y)}")


if __name__ == "__main__":
main()

0 comments on commit f2866e4

Please sign in to comment.