Skip to content

Commit

Permalink
Migrate this to use slightly more constrainted monkeypatching
Browse files Browse the repository at this point in the history
The primary benefit of this is that it'll work when you have a single project using both setuptools-rust as well as cffi.
  • Loading branch information
alex committed Jul 27, 2020
1 parent 1143faf commit 9f1ba74
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 81 deletions.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ setup_requires = setuptools>=41; wheel; setuptools_scm[toml]>=3.4.3
distutils.commands =
check_rust=setuptools_rust.check:check_rust
clean_rust=setuptools_rust:clean_rust
build_ext=setuptools_rust:build_ext
build_rust=setuptools_rust:build_rust
test_rust=setuptools_rust:test_rust
tomlgen_rust=setuptools_rust:tomlgen_rust
distutils.setup_keywords =
rust_extensions=setuptools_rust.setuptools_ext:rust_extensions
6 changes: 0 additions & 6 deletions setuptools_rust/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import print_function, absolute_import

from . import patch
from .build import build_rust
from .build_ext import build_ext
from .check import check_rust
from .clean import clean_rust
from .extension import RustExtension
Expand All @@ -16,10 +14,6 @@
"Strip",
"check_rust",
"clean_rust",
"build_ext",
"build_rust",
"test_rust",
)


patch.monkey_patch_dist(build_ext)
28 changes: 0 additions & 28 deletions setuptools_rust/build_ext.py

This file was deleted.

47 changes: 1 addition & 46 deletions setuptools_rust/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,8 @@
wheel = False


# XXX: Need to port the rest of this to setuptools_ext!
def monkey_patch_dist(build_ext):
# allow to use 'rust_extensions' parameter for setup() call
Distribution.rust_extensions = ()

# replace setuptools build_ext
Distribution.orig_get_command_class = Distribution.get_command_class

def get_command_class(self, command):
if command == "build_ext":
if command not in self.cmdclass:
self.cmdclass[command] = build_ext

return self.orig_get_command_class(command)

Distribution.get_command_class = get_command_class

# use custom has_ext_modules
DistDistribution.orig_has_ext_modules = DistDistribution.has_ext_modules

def has_ext_modules(self):
return (
self.ext_modules
and len(self.ext_modules) > 0
or self.rust_extensions
and len(self.rust_extensions) > 0
)

DistDistribution.has_ext_modules = has_ext_modules

# this is required because, install directly access distribution's
# ext_modules attr to check if dist has ext modules
install.orig_finalize_options = install.finalize_options
Expand Down Expand Up @@ -108,21 +81,3 @@ def finalize_options(self):
self.orig_finalize_options()

bdist_wheel.finalize_options = finalize_options

# clean rust project
def run_clean(self):
self.orig_run()

if not self.dry_run:
self.run_command("clean_rust")

clean.orig_run = clean.run
clean.run = run_clean

# check rust project
def run_check(self):
self.orig_run()
self.run_command("check_rust")

check.orig_run = check.run
check.run = run_check
49 changes: 49 additions & 0 deletions setuptools_rust/setuptools_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from distutils import log
from distutils.command.check import check
from distutils.command.clean import clean

from setuptools.command.build_ext import build_ext


def add_rust_extension(dist, rust_extension):
build_ext_base_class = dist.cmdclass.get('build_ext', build_ext)

class build_ext_rust_extension(build_ext_base_class):
def run(self):
if self.distribution.rust_extensions:
log.info("running build_rust")
build_rust = self.get_finalized_command("build_rust")
build_rust.inplace = self.inplace
build_rust.run()

build_ext_base_class.run(self)
dist.cmdclass['build_ext'] = build_ext_rust_extension

clean_base_class = dist.cmdclass.get('clean', clean)

class clean_rust_extension(clean_base_class):
def run(self):
clean_base_class.run(self)
if not self.dry_run:
self.run_command("clean_rust")
dist.cmdclass['clean'] = clean_rust_extension

check_base_class = dist.cmdclass.get('check', check)

class check_rust_extension(check_base_class):
def run(self):
check_base_class.run(self)
self.run_command("check_rust")
dist.cmdclass["check"] = check_rust_extension


def rust_extensions(dist, attr, value):
assert attr == "rust_extensions"

orig_has_ext_modules = dist.has_ext_modules
dist.has_ext_modules = lambda: (
orig_has_ext_modules() or bool(dist.rust_extensions)
)

for rust_extension in value:
add_rust_extension(dist, rust_extension)

0 comments on commit 9f1ba74

Please sign in to comment.