Skip to content

Commit 11e4cb7

Browse files
committed
Reuse logic to get bdist_wheel command object
1 parent 48e146e commit 11e4cb7

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

setuptools_rust/build.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import pkg_resources
2323
from semantic_version import Version
24+
from setuptools import Distribution
2425
from setuptools.command.build import build as CommandBuild
2526
from setuptools.command.build_ext import build_ext as CommandBuildExt
2627
from setuptools.command.build_ext import get_abi3_suffix
@@ -39,6 +40,12 @@
3940
logger = logging.getLogger(__name__)
4041

4142

43+
try:
44+
from wheel.bdist_wheel import bdist_wheel as CommandBdistWheel
45+
except ImportError: # wheel installation might be deferred in PEP 517
46+
from setuptools import Command as CommandBdistWheel
47+
48+
4249
def _check_cargo_supports_crate_type_option() -> bool:
4350
version = get_rust_version()
4451

@@ -447,17 +454,13 @@ def get_dylib_ext_path(self, ext: RustExtension, target_fname: str) -> str:
447454
return ext_path
448455

449456
def _py_limited_api(self) -> _PyLimitedApi:
450-
bdist_wheel = self.distribution.get_command_obj("bdist_wheel", create=False)
457+
bdist_wheel = _get_bdist_wheel_cmd(self.distribution, create=False)
451458

452459
if bdist_wheel is None:
453460
# wheel package is not installed, not building a limited-api wheel
454461
return False
455462
else:
456-
from wheel.bdist_wheel import bdist_wheel as CommandBdistWheel
457-
458-
bdist_wheel_command = cast(CommandBdistWheel, bdist_wheel) # type: ignore[no-any-unimported]
459-
bdist_wheel_command.ensure_finalized()
460-
return cast(_PyLimitedApi, bdist_wheel_command.py_limited_api)
463+
return cast(_PyLimitedApi, bdist_wheel.py_limited_api)
461464

462465
def _detect_rust_target(
463466
self, forced_target_triple: Optional[str] = None
@@ -752,3 +755,15 @@ def _replace_cross_target_dir(path: str, ext: RustExtension, *, quiet: bool) ->
752755
cross_target_dir = ext._metadata(cargo="cross", quiet=quiet)["target_directory"]
753756
local_target_dir = ext._metadata(cargo="cargo", quiet=quiet)["target_directory"]
754757
return path.replace(cross_target_dir, local_target_dir)
758+
759+
760+
def _get_bdist_wheel_cmd( # type: ignore[no-any-unimported]
761+
dist: Distribution,
762+
create: bool = True
763+
) -> Optional[CommandBdistWheel]:
764+
try:
765+
cmd_obj = dist.get_command_obj("bdist_wheel", create=create)
766+
cmd_obj.ensure_finalized()
767+
return cast(CommandBdistWheel, cmd_obj) # type: ignore [no-any-unimported]
768+
except Exception:
769+
return None

setuptools_rust/setuptools_ext.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from setuptools.dist import Distribution
1717
from typing_extensions import Literal
1818

19+
from .build import _get_bdist_wheel_cmd
1920
from .extension import Binding, RustBin, RustExtension, Strip
2021

2122
try:
@@ -169,11 +170,8 @@ def run(self) -> None:
169170
build_ext_base_class.run(self)
170171

171172
def _get_wheel_plat_name(self) -> Optional[str]:
172-
try:
173-
cmd = self.distribution.get_command_obj("bdist_wheel")
174-
return cast(Optional[str], cmd.plat_name)
175-
except Exception: # unlikely scenario: `wheel` not installed
176-
return None
173+
cmd = _get_bdist_wheel_cmd(self.distribution)
174+
return cast(Optional[str], getattr(cmd, "plat_name", None))
177175

178176
dist.cmdclass["build_ext"] = build_ext_rust_extension
179177

0 commit comments

Comments
 (0)