Skip to content

Commit 63e1ed6

Browse files
authored
Merge pull request #187 from davidhewitt/windows-env-detection
windows: don't force msvc toolchain if using gnu
2 parents 357557f + 5f901a3 commit 63e1ed6

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
### Fixed
99
- Fix incompatibility with Python 3.6.0 using default values for NamedTuple classes. [#184](https://github.com/PyO3/setuptools-rust/pull/184)
10+
- Stop forcing the `msvc` Rust toolchain for Windows environments using the `gnu` toolchain. [#187](https://github.com/PyO3/setuptools-rust/pull/187)
1011

1112
## 1.0.0 (2021-11-21)
1213
### Added

setuptools_rust/build.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def build_extension(
124124
cross_lib = None
125125
linker = None
126126

127-
rust_target_info = get_rust_target_info(target_triple)
127+
rustc_cfgs = _get_rustc_cfgs(target_triple)
128128

129129
env = _prepare_build_environment(cross_lib)
130130

@@ -174,7 +174,7 @@ def build_extension(
174174

175175
# Tell musl targets not to statically link libc. See
176176
# https://github.com/rust-lang/rust/issues/59302 for details.
177-
if b'target_env="musl"' in rust_target_info:
177+
if rustc_cfgs.get("target_env") == "musl":
178178
rustc_args.extend(["-C", "target-feature=-crt-static"])
179179

180180
command = [
@@ -368,7 +368,7 @@ def _py_limited_api(self) -> PyLimitedApi:
368368
def _detect_rust_target(
369369
self, forced_target_triple: Optional[str]
370370
) -> Optional["_TargetInfo"]:
371-
cross_compile_info = detect_unix_cross_compile_info()
371+
cross_compile_info = _detect_unix_cross_compile_info()
372372
if cross_compile_info is not None:
373373
cross_target_info = cross_compile_info.to_target_info()
374374
if forced_target_triple is not None:
@@ -417,8 +417,12 @@ def _detect_local_rust_target(self) -> Optional["_TargetInfo"]:
417417
# If we are on a 64-bit machine, but running a 32-bit Python, then
418418
# we'll target a 32-bit Rust build.
419419
if self.plat_name == "win32":
420+
if _get_rustc_cfgs(None).get("target_env") == "gnu":
421+
return _TargetInfo.for_triple("i686-pc-windows-gnu")
420422
return _TargetInfo.for_triple("i686-pc-windows-msvc")
421423
elif self.plat_name == "win-amd64":
424+
if _get_rustc_cfgs(None).get("target_env") == "gnu":
425+
return _TargetInfo.for_triple("x86_64-pc-windows-gnu")
422426
return _TargetInfo.for_triple("x86_64-pc-windows-msvc")
423427
elif self.plat_name.startswith("macosx-") and platform.machine() == "x86_64":
424428
# x86_64 or arm64 macOS targeting x86_64
@@ -561,7 +565,7 @@ def to_target_info(self) -> Optional[_TargetInfo]:
561565
return None
562566

563567

564-
def detect_unix_cross_compile_info() -> Optional["_CrossCompileInfo"]:
568+
def _detect_unix_cross_compile_info() -> Optional["_CrossCompileInfo"]:
565569
# See https://github.com/PyO3/setuptools-rust/issues/138
566570
# This is to support cross compiling on *NIX, where plat_name isn't
567571
# necessarily the same as the system we are running on. *NIX systems
@@ -588,6 +592,21 @@ def detect_unix_cross_compile_info() -> Optional["_CrossCompileInfo"]:
588592
return _CrossCompileInfo(host_type, cross_lib, linker, linker_args)
589593

590594

595+
_RustcCfgs = Dict[str, Optional[str]]
596+
597+
598+
def _get_rustc_cfgs(target_triple: Optional[str]) -> _RustcCfgs:
599+
cfgs: _RustcCfgs = {}
600+
for entry in get_rust_target_info(target_triple):
601+
maybe_split = entry.split("=", maxsplit=1)
602+
if len(maybe_split) == 2:
603+
cfgs[maybe_split[0]] = maybe_split[1].strip('"')
604+
else:
605+
assert len(maybe_split) == 1
606+
cfgs[maybe_split[0]] = None
607+
return cfgs
608+
609+
591610
def _replace_vendor_with_unknown(target: str) -> Optional[str]:
592611
"""Replaces vendor in the target triple with unknown.
593612

0 commit comments

Comments
 (0)