Skip to content

Commit 1b8a85a

Browse files
authored
Merge pull request #156 from davidhewitt/fix-optional-wheel
build: ensure wheel can be an optional dependency
2 parents b56d126 + 4580f98 commit 1b8a85a

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

setuptools_rust/build.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import os
44
import platform
55
import shutil
6-
import sys
76
import subprocess
7+
import sys
88
import sysconfig
99
from distutils.errors import (
1010
CompileError,
@@ -13,12 +13,19 @@
1313
DistutilsPlatformError,
1414
)
1515
from distutils.sysconfig import get_config_var
16-
from setuptools.command.build_ext import get_abi3_suffix
1716
from subprocess import check_output
1817

18+
from setuptools.command.build_ext import get_abi3_suffix
19+
1920
from .command import RustCommand
2021
from .extension import Binding, RustExtension, Strip
21-
from .utils import binding_features, get_rust_target_info, get_rust_target_list
22+
from .utils import (
23+
PyLimitedApi,
24+
binding_features,
25+
get_rust_target_info,
26+
get_rust_target_list,
27+
)
28+
2229

2330
class _TargetInfo:
2431
def __init__(self, triple=None, cross_lib=None, linker=None, link_args=None):
@@ -203,10 +210,9 @@ def build_extension(self, ext: RustExtension, target_triple=None):
203210
f"can't find Rust extension project file: {ext.path}"
204211
)
205212

206-
bdist_wheel = self.get_finalized_command('bdist_wheel')
207213
features = {
208214
*ext.features,
209-
*binding_features(ext, py_limited_api=bdist_wheel.py_limited_api)
215+
*binding_features(ext, py_limited_api=self._py_limited_api())
210216
}
211217

212218
debug_build = ext.debug if ext.debug is not None else self.inplace
@@ -421,12 +427,11 @@ def get_dylib_ext_path(
421427
target_fname: str
422428
) -> str:
423429
build_ext = self.get_finalized_command("build_ext")
424-
bdist_wheel = self.get_finalized_command("bdist_wheel")
425430

426431
filename = build_ext.get_ext_fullpath(target_fname)
427432

428433
if (
429-
(ext.py_limited_api == "auto" and bdist_wheel.py_limited_api)
434+
(ext.py_limited_api == "auto" and self._py_limited_api())
430435
or (ext.py_limited_api)
431436
):
432437
abi3_suffix = get_abi3_suffix()
@@ -463,3 +468,13 @@ def create_universal2_binary(output_path, input_paths):
463468
with open(input_path, "rb") as f:
464469
fat.add(f.read())
465470
fat.write_to(output_path)
471+
472+
def _py_limited_api(self) -> PyLimitedApi:
473+
bdist_wheel = self.distribution.get_command_obj("bdist_wheel", create=0)
474+
475+
if bdist_wheel is None:
476+
# wheel package is not installed, not building a limited-api wheel
477+
return False
478+
else:
479+
bdist_wheel.ensure_finalized()
480+
return bdist_wheel.py_limited_api

setuptools_rust/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import subprocess
22
from distutils.errors import DistutilsPlatformError
33
from typing import Set, Union
4-
from typing_extensions import Literal
54

65
import semantic_version
6+
from typing_extensions import Literal
77

88
from .extension import Binding, RustExtension
99

10+
PyLimitedApi = Union[Literal["cp36", "cp37", "cp38", "cp39"], bool]
1011

1112
def binding_features(
1213
ext: RustExtension,
13-
py_limited_api: Union[Literal["cp36", "cp37", "cp38", "cp39"], bool],
14+
py_limited_api: PyLimitedApi,
1415
) -> Set[str]:
1516
if ext.binding in (Binding.NoBinding, Binding.Exec):
1617
return set()

0 commit comments

Comments
 (0)