Skip to content

Commit e9b85eb

Browse files
committed
Reuse logic to get bdist_wheel command object
1 parent 81ec0b0 commit e9b85eb

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

setuptools_rust/build.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from typing import Dict, List, NamedTuple, Optional, Set, Tuple, cast
2020

2121
import pkg_resources
22+
from semantic_version import Version
23+
from setuptools import Distribution
2224
from setuptools.command.build import build as CommandBuild
2325
from setuptools.command.build_ext import build_ext as CommandBuildExt
2426
from setuptools.command.build_ext import get_abi3_suffix
@@ -36,6 +38,12 @@
3638
logger = logging.getLogger(__name__)
3739

3840

41+
try:
42+
from wheel.bdist_wheel import bdist_wheel as CommandBdistWheel
43+
except ImportError: # wheel installation might be deferred in PEP 517
44+
from setuptools import Command as CommandBdistWheel
45+
46+
3947
def _check_cargo_supports_crate_type_option() -> bool:
4048
version = get_rust_version()
4149

@@ -468,17 +476,13 @@ def get_dylib_ext_path(self, ext: RustExtension, target_fname: str) -> str:
468476
return ext_path
469477

470478
def _py_limited_api(self) -> _PyLimitedApi:
471-
bdist_wheel = self.distribution.get_command_obj("bdist_wheel", create=False)
479+
bdist_wheel = _get_bdist_wheel_cmd(self.distribution, create=False)
472480

473481
if bdist_wheel is None:
474482
# wheel package is not installed, not building a limited-api wheel
475483
return False
476484
else:
477-
from wheel.bdist_wheel import bdist_wheel as CommandBdistWheel
478-
479-
bdist_wheel_command = cast(CommandBdistWheel, bdist_wheel) # type: ignore[no-any-unimported]
480-
bdist_wheel_command.ensure_finalized()
481-
return cast(_PyLimitedApi, bdist_wheel_command.py_limited_api)
485+
return cast(_PyLimitedApi, bdist_wheel.py_limited_api)
482486

483487
def _detect_rust_target(
484488
self, forced_target_triple: Optional[str] = None
@@ -773,3 +777,15 @@ def _replace_cross_target_dir(path: str, ext: RustExtension, *, quiet: bool) ->
773777
cross_target_dir = ext._metadata(cargo="cross", quiet=quiet)["target_directory"]
774778
local_target_dir = ext._metadata(cargo="cargo", quiet=quiet)["target_directory"]
775779
return path.replace(cross_target_dir, local_target_dir)
780+
781+
782+
def _get_bdist_wheel_cmd( # type: ignore[no-any-unimported]
783+
dist: Distribution,
784+
create: bool = True
785+
) -> Optional[CommandBdistWheel]:
786+
try:
787+
cmd_obj = dist.get_command_obj("bdist_wheel", create=create)
788+
cmd_obj.ensure_finalized()
789+
return cast(CommandBdistWheel, cmd_obj) # type: ignore [no-any-unimported]
790+
except Exception:
791+
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)