Skip to content

Commit 7125e19

Browse files
committed
Migrate this to use slightly more constrainted monkeypatching
The primary benefit of this is that it'll work when you have a single project using both setuptools-rust as well as cffi.
1 parent c571b34 commit 7125e19

File tree

5 files changed

+126
-163
lines changed

5 files changed

+126
-163
lines changed

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ setup_requires = setuptools>=41; wheel; setuptools_scm[toml]>=3.4.3
3131
distutils.commands =
3232
check_rust=setuptools_rust.check:check_rust
3333
clean_rust=setuptools_rust:clean_rust
34-
build_ext=setuptools_rust:build_ext
3534
build_rust=setuptools_rust:build_rust
3635
test_rust=setuptools_rust:test_rust
3736
tomlgen_rust=setuptools_rust:tomlgen_rust
37+
distutils.setup_keywords =
38+
rust_extensions=setuptools_rust.setuptools_ext:rust_extensions

setuptools_rust/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from __future__ import print_function, absolute_import
22

3-
from . import patch
43
from .build import build_rust
5-
from .build_ext import build_ext
64
from .check import check_rust
75
from .clean import clean_rust
86
from .extension import RustExtension
@@ -16,10 +14,6 @@
1614
"Strip",
1715
"check_rust",
1816
"clean_rust",
19-
"build_ext",
2017
"build_rust",
2118
"test_rust",
2219
)
23-
24-
25-
patch.monkey_patch_dist(build_ext)

setuptools_rust/build_ext.py

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

setuptools_rust/patch.py

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

setuptools_rust/setuptools_ext.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from distutils import log
2+
from distutils.command.check import check
3+
from distutils.command.clean import clean
4+
from distutils.command.install import install
5+
6+
from setuptools.command.build_ext import build_ext
7+
8+
try:
9+
from wheel.bdist_wheel import bdist_wheel
10+
except ImportError:
11+
bdist_wheel = None
12+
13+
14+
def add_rust_extension(dist):
15+
build_ext_base_class = dist.cmdclass.get('build_ext', build_ext)
16+
17+
class build_ext_rust_extension(build_ext_base_class):
18+
def run(self):
19+
if self.distribution.rust_extensions:
20+
log.info("running build_rust")
21+
build_rust = self.get_finalized_command("build_rust")
22+
build_rust.inplace = self.inplace
23+
build_rust.run()
24+
25+
build_ext_base_class.run(self)
26+
dist.cmdclass['build_ext'] = build_ext_rust_extension
27+
28+
clean_base_class = dist.cmdclass.get('clean', clean)
29+
30+
class clean_rust_extension(clean_base_class):
31+
def run(self):
32+
clean_base_class.run(self)
33+
if not self.dry_run:
34+
self.run_command("clean_rust")
35+
dist.cmdclass['clean'] = clean_rust_extension
36+
37+
check_base_class = dist.cmdclass.get('check', check)
38+
39+
class check_rust_extension(check_base_class):
40+
def run(self):
41+
check_base_class.run(self)
42+
self.run_command("check_rust")
43+
dist.cmdclass["check"] = check_rust_extension
44+
45+
install_base_class = dist.cmdclass.get('install', install)
46+
47+
# this is required because, install directly access distribution's
48+
# ext_modules attr to check if dist has ext modules
49+
class install_rust_extension(install_base_class):
50+
def finalize_options(self):
51+
ext_modules = self.distribution.ext_modules
52+
53+
# all ext modules
54+
mods = []
55+
if self.distribution.ext_modules:
56+
mods.extend(self.distribution.ext_modules)
57+
if self.distribution.rust_extensions:
58+
mods.extend(self.distribution.rust_extensions)
59+
60+
scripts = []
61+
for ext in self.distribution.rust_extensions:
62+
scripts.extend(ext.entry_points())
63+
64+
if scripts:
65+
if not self.distribution.entry_points:
66+
self.distribution.entry_points = {"console_scripts": scripts}
67+
else:
68+
ep_scripts = self.distribution.entry_points.get("console_scripts")
69+
if ep_scripts:
70+
for script in scripts:
71+
if script not in ep_scripts:
72+
ep_scripts.append(scripts)
73+
else:
74+
ep_scripts = scripts
75+
76+
self.distribution.entry_points["console_scripts"] = ep_scripts
77+
78+
self.distribution.ext_modules = mods
79+
80+
install_base_class.finalize_options(self)
81+
82+
# restore ext_modules
83+
self.distribution.ext_modules = ext_modules
84+
dist.cmdclass["install"] = install_rust_extension
85+
86+
if bdist_wheel is not None:
87+
bdist_wheel_base_class = dist.cmdclass.get("bdist_wheel", bdist_wheel)
88+
89+
# this is for console entries
90+
class bdist_wheel_rust_extension(bdist_wheel_base_class):
91+
def finalize_options(self):
92+
scripts = []
93+
for ext in self.distribution.rust_extensions:
94+
scripts.extend(ext.entry_points())
95+
96+
if scripts:
97+
if not self.distribution.entry_points:
98+
self.distribution.entry_points = {"console_scripts": scripts}
99+
else:
100+
ep_scripts = self.distribution.entry_points.get("console_scripts")
101+
if ep_scripts:
102+
for script in scripts:
103+
if script not in ep_scripts:
104+
ep_scripts.append(scripts)
105+
else:
106+
ep_scripts = scripts
107+
108+
self.distribution.entry_points["console_scripts"] = ep_scripts
109+
110+
bdist_wheel_base_class.finalize_options(self)
111+
dist.cmdclass["bdist_wheel"] = bdist_wheel_rust_extension
112+
113+
114+
115+
def rust_extensions(dist, attr, value):
116+
assert attr == "rust_extensions"
117+
118+
orig_has_ext_modules = dist.has_ext_modules
119+
dist.has_ext_modules = lambda: (
120+
orig_has_ext_modules() or bool(dist.rust_extensions)
121+
)
122+
123+
if dist.rust_extensions:
124+
add_rust_extension(dist)

0 commit comments

Comments
 (0)