Skip to content

Commit 089d716

Browse files
committed
binding options as an enum
1 parent fa9ef6a commit 089d716

File tree

7 files changed

+47
-23
lines changed

7 files changed

+47
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Cargo.lock
1010
/example/dist/
1111
/example/extensions/target/
1212
/example/*.egg-info
13+
.idea/

setuptools_rust/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .clean import clean_rust
88
from .test import test_rust
99
from .extension import RustExtension
10+
from .utils import Binding
1011

1112
__all__ = ('RustExtension',
1213
'check_rust', 'clean_rust', 'build_ext', 'build_rust', 'test_rust')

setuptools_rust/build.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def build_extension(self, ext):
6060
"Can not file rust extension project file: %s" % ext.path)
6161

6262
features = set(ext.features)
63-
features.update(cpython_feature(pyo3=ext.pyo3))
63+
features.update(cpython_feature(binding=ext.binding))
6464

6565
if ext.debug is None:
6666
debug_build = self.inplace
@@ -74,8 +74,9 @@ def build_extension(self, ext):
7474
quiet = self.qbuild or ext.quiet
7575

7676
# build cargo command
77-
args = (["cargo", "rustc", "--lib", "--manifest-path", ext.path,
78-
"--features", " ".join(features)]
77+
feature_args = ["--features " + " ".join(features)] if features else []
78+
args = (["cargo", "rustc", "--lib", "--manifest-path", ext.path]
79+
+ feature_args
7980
+ list(ext.args or []))
8081
if not debug_build:
8182
args.append("--release")
@@ -158,7 +159,7 @@ def run(self):
158159
rust_version = ext.get_rust_version()
159160
if rust_version is not None and version not in rust_version:
160161
raise DistutilsPlatformError(
161-
"Rust %s does not match extension requirenment %s" % (
162+
"Rust %s does not match extension requirement %s" % (
162163
version, ext.rust_version))
163164

164165
self.build_extension(ext)

setuptools_rust/check.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ def run(self):
5555
"Can not file rust extension project file: %s" % ext.path)
5656

5757
features = set(ext.features)
58-
features.update(cpython_feature(pyo3=ext.pyo3))
58+
features.update(cpython_feature(binding=ext.binding))
5959

6060
# build cargo command
61-
args = (["cargo", "check", "--lib", "--manifest-path", ext.path,
62-
"--features", " ".join(features)]
61+
feature_args = ["--features" + " ".join(features)] if features else []
62+
args = (["cargo", "rustc", "--lib", "--manifest-path", ext.path]
63+
+ feature_args
6364
+ list(ext.args or []))
6465

6566
# Execute cargo command

setuptools_rust/extension.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
import sys
44
from distutils.errors import DistutilsSetupError
5+
from .utils import Binding
6+
57

68
import semantic_version
79

@@ -28,17 +30,19 @@ class RustExtension:
2830
Controls whether --debug or --release is passed to cargo. If set to
2931
None then build type is auto-detect. Inplace build is debug build
3032
otherwise release. Default: None
31-
pyo3 : bool
32-
Controls wich python binding is in use. `pyo3=True` uses PyO3 binding,
33-
`pyo3=False` uses rust-cpython binding,
33+
binding : setuptools_rust.Binding
34+
Controls which python binding is in use.
35+
Binding.PyO3 uses PyO3
36+
Binding.RustCPython uses Rust CPython.
37+
Binding.NoBinding uses no binding.
3438
"""
3539

3640
def __init__(self, name, path,
3741
args=None, features=None, rust_version=None,
38-
quiet=False, debug=None, pyo3=False):
42+
quiet=False, debug=None, binding=Binding.PyO3):
3943
self.name = name
4044
self.args = args
41-
self.pyo3 = pyo3
45+
self.binding = binding
4246
self.rust_version = rust_version
4347
self.quiet = quiet
4448
self.debug = debug

setuptools_rust/test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,18 @@ def run(self):
5757
"Can not file rust extension project file: %s" % ext.path)
5858

5959
features = set(ext.features)
60-
features.update(cpython_feature(ext=False, pyo3=ext.pyo3))
60+
features.update(cpython_feature(ext=False, binding=ext.binding))
6161

6262
# build cargo command
63-
args = (["cargo", "test", '--color', 'always', "--manifest-path",
64-
ext.path, "--features", " ".join(features)]
63+
feature_args = ["--features " + " ".join(features)] if features else []
64+
args = (["cargo", "rustc", "--lib", "--manifest-path", ext.path]
65+
+ feature_args
6566
+ list(ext.args or []))
6667

6768
# Execute cargo command
6869
print(' '.join(args))
6970
try:
70-
subprocess.check_output(args)
71+
subprocess.check_output(args, env=env)
7172
except subprocess.CalledProcessError as e:
7273
raise CompileError(
7374
"cargo failed with code: %d\n%s" % (

setuptools_rust/utils.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,29 @@
66
import semantic_version
77

88

9-
def cpython_feature(ext=True, pyo3=False):
9+
class Binding:
10+
"""
11+
Binding Options
12+
"""
13+
# https://github.com/PyO3/PyO3
14+
PyO3 = 0
15+
# https://github.com/dgrunwald/rust-cpython
16+
RustCPython = 1
17+
# Bring your own binding
18+
NoBinding = 2
19+
20+
21+
def cpython_feature(ext=True, binding=Binding.PyO3):
1022
version = sys.version_info
1123

12-
if pyo3:
24+
if binding is Binding.NoBinding:
25+
return ()
26+
elif binding is Binding.PyO3:
1327
if ext:
1428
return ("pyo3/extension-module",)
1529
else:
1630
return ()
17-
else:
31+
elif binding is Binding.RustCPython:
1832
if (2, 7) < version < (2, 8):
1933
if ext:
2034
return ("cpython/python27-sys", "cpython/extension-module-2-7")
@@ -25,10 +39,11 @@ def cpython_feature(ext=True, pyo3=False):
2539
return ("cpython/python3-sys", "cpython/extension-module")
2640
else:
2741
return ("cpython/python3-sys",)
28-
29-
raise DistutilsPlatformError(
30-
"Unsupported python version: %s" % sys.version)
31-
42+
else:
43+
raise DistutilsPlatformError(
44+
"Unsupported python version: %s" % sys.version)
45+
else:
46+
raise DistutilsPlatformError('Unknown Binding: "{}" '.format(binding))
3247

3348
def get_rust_version():
3449
try:

0 commit comments

Comments
 (0)