Skip to content

Commit 08d6962

Browse files
authored
remove setup.py from cffi example (#617)
1 parent 444b5d7 commit 08d6962

6 files changed

Lines changed: 99 additions & 29 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ target
1111
*.egg-info
1212
pyo3
1313
docs/_build
14+
examples/rust_with_cffi/cffi_module.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include Cargo.toml
2+
include build_backend.py
23
include cffi_module.py
34
recursive-include rust *
45
recursive-include python *
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import subprocess
2+
import sys
3+
from pathlib import Path
4+
5+
from setuptools import build_meta as _setuptools_build_meta
6+
7+
_ROOT = Path(__file__).parent
8+
9+
10+
def _generate_cffi_source():
11+
"""Generate the CFFI source file from the Python module.
12+
13+
`cffi` provides a command line tool `gen_src` that can be used to
14+
generate the C source file from a Python module that defines the
15+
CFFI interface.
16+
17+
By implementing an in-tree build backend, we can invoke this before
18+
forwarding to regular setuptools build backend functions.
19+
"""
20+
21+
# cffi documents `gen_src` as a command line tool
22+
subprocess.check_call(
23+
[
24+
sys.executable,
25+
"-m",
26+
"cffi.gen_src",
27+
"exec-python",
28+
"--ffi-var=ffi",
29+
"cffi_module.py",
30+
"cffi_module.c",
31+
],
32+
cwd=_ROOT,
33+
)
34+
35+
36+
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
37+
_generate_cffi_source()
38+
return _setuptools_build_meta.build_wheel(
39+
wheel_directory, config_settings, metadata_directory
40+
)
41+
42+
43+
def build_sdist(sdist_directory, config_settings=None):
44+
_generate_cffi_source()
45+
return _setuptools_build_meta.build_sdist(sdist_directory, config_settings)
46+
47+
48+
def build_editable(wheel_directory, config_settings=None, metadata_directory=None):
49+
_generate_cffi_source()
50+
return _setuptools_build_meta.build_editable(
51+
wheel_directory, config_settings, metadata_directory
52+
)
53+
54+
55+
get_requires_for_build_wheel = _setuptools_build_meta.get_requires_for_build_wheel
56+
get_requires_for_build_sdist = _setuptools_build_meta.get_requires_for_build_sdist
57+
get_requires_for_build_editable = _setuptools_build_meta.get_requires_for_build_editable
58+
prepare_metadata_for_build_wheel = (
59+
_setuptools_build_meta.prepare_metadata_for_build_wheel
60+
)
61+
prepare_metadata_for_build_editable = (
62+
_setuptools_build_meta.prepare_metadata_for_build_editable
63+
)

‎examples/rust_with_cffi/noxfile.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test(session: nox.Session):
1010
session.install(SETUPTOOLS_RUST, "pytest")
1111

1212
try:
13-
session.install("cffi", "--only-binary=cffi")
13+
session.install("cffi>=2.1", "--only-binary=cffi")
1414
except nox.command.CommandFailed:
1515
session.skip("cffi not available on this platform")
1616

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
11
[build-system]
2-
requires = ["setuptools", "setuptools-rust", "cffi"]
3-
build-backend = "setuptools.build_meta"
2+
requires = ["setuptools>=74.1", "setuptools-rust", "cffi>=2.1"]
3+
build-backend = "build_backend"
4+
backend-path = ["."]
5+
6+
[project]
7+
name = "rust-with-cffi"
8+
version = "0.1.0"
9+
classifiers = [
10+
"License :: OSI Approved :: MIT License",
11+
"Development Status :: 3 - Alpha",
12+
"Intended Audience :: Developers",
13+
"Programming Language :: Python",
14+
"Programming Language :: Rust",
15+
"Operating System :: POSIX",
16+
"Operating System :: MacOS :: MacOS X",
17+
]
18+
dependencies = ["cffi"]
19+
20+
[tool.setuptools]
21+
package-dir = { "" = "python" }
22+
include-package-data = true
23+
zip-safe = false
24+
25+
[tool.setuptools.packages.find]
26+
where = ["python"]
27+
28+
[[tool.setuptools.ext-modules]]
29+
name = "rust_with_cffi.cffi"
30+
sources = ["cffi_module.c"]
31+
py-limited-api = true
32+
33+
[[tool.setuptools-rust.ext-modules]]
34+
target = "rust_with_cffi.rust"

‎examples/rust_with_cffi/setup.py‎

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)