Skip to content

Commit 8e9892f

Browse files
committed
build_rust: remove linker handling that broke cross compilation
1 parent 4c88c6a commit 8e9892f

File tree

1 file changed

+7
-144
lines changed

1 file changed

+7
-144
lines changed

setuptools_rust/build.py

Lines changed: 7 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,10 @@ def build_extension(
113113
self, ext: RustExtension, forced_target_triple: Optional[str] = None
114114
) -> List["_BuiltModule"]:
115115

116-
target_info = self._detect_rust_target(forced_target_triple)
117-
if target_info is not None:
118-
target_triple = target_info.triple
119-
cross_lib = target_info.cross_lib
120-
linker = target_info.linker
121-
# We're ignoring target_info.linker_args for now because we're not
122-
# sure if they will always do the right thing. Might help with some
123-
# of the OS-specific logic if it does.
124-
125-
else:
126-
target_triple = None
127-
cross_lib = None
128-
linker = None
129-
116+
target_triple = self._detect_rust_target(forced_target_triple)
130117
rustc_cfgs = get_rustc_cfgs(target_triple)
131118

132-
env = _prepare_build_environment(cross_lib)
119+
env = _prepare_build_environment()
133120

134121
if not os.path.exists(ext.path):
135122
raise DistutilsFileError(
@@ -150,9 +137,6 @@ def build_extension(
150137

151138
rustflags = []
152139

153-
if linker is not None:
154-
rustflags.extend(["-C", "linker=" + linker])
155-
156140
if ext._uses_exec_binding():
157141
command = [self.cargo, "build", "--manifest-path", ext.path, *cargo_args]
158142

@@ -407,45 +391,12 @@ def _py_limited_api(self) -> _PyLimitedApi:
407391

408392
def _detect_rust_target(
409393
self, forced_target_triple: Optional[str] = None
410-
) -> Optional["_TargetInfo"]:
394+
) -> Optional[str]:
411395
assert self.plat_name is not None
412-
cross_compile_info = _detect_unix_cross_compile_info()
413-
if cross_compile_info is not None:
414-
cross_target_info = cross_compile_info.to_target_info()
415-
if forced_target_triple is not None:
416-
if (
417-
cross_target_info is not None
418-
and not cross_target_info.is_compatible_with(forced_target_triple)
419-
):
420-
self.warn(
421-
f"Forced Rust target `{forced_target_triple}` is not "
422-
f"compatible with deduced Rust target "
423-
f"`{cross_target_info.triple}` - the built package "
424-
f" may not import successfully once installed."
425-
)
426-
427-
# Forcing the target in a cross-compile environment; use
428-
# the cross-compile information in combination with the
429-
# forced target
430-
return _TargetInfo(
431-
forced_target_triple,
432-
cross_compile_info.cross_lib,
433-
cross_compile_info.linker,
434-
cross_compile_info.linker_args,
435-
)
436-
elif cross_target_info is not None:
437-
return cross_target_info
438-
else:
439-
raise DistutilsPlatformError(
440-
"Don't know the correct rust target for system type "
441-
f"{cross_compile_info.host_type}. Please set the "
442-
"CARGO_BUILD_TARGET environment variable."
443-
)
444-
445-
elif forced_target_triple is not None:
396+
if forced_target_triple is not None:
446397
# Automatic target detection can be overridden via the CARGO_BUILD_TARGET
447398
# environment variable or --target command line option
448-
return _TargetInfo.for_triple(forced_target_triple)
399+
return forced_target_triple
449400

450401
# Determine local rust target which needs to be "forced" if necessary
451402
local_rust_target = _adjusted_local_rust_target(self.plat_name)
@@ -457,7 +408,7 @@ def _detect_rust_target(
457408
# check for None first to avoid calling to rustc if not needed
458409
and local_rust_target != get_rust_host()
459410
):
460-
return _TargetInfo.for_triple(local_rust_target)
411+
return local_rust_target
461412

462413
return None
463414

@@ -547,91 +498,6 @@ class _BuiltModule(NamedTuple):
547498
path: str
548499

549500

550-
class _TargetInfo(NamedTuple):
551-
triple: str
552-
cross_lib: Optional[str]
553-
linker: Optional[str]
554-
linker_args: Optional[str]
555-
556-
@staticmethod
557-
def for_triple(triple: str) -> "_TargetInfo":
558-
return _TargetInfo(triple, None, None, None)
559-
560-
def is_compatible_with(self, target: str) -> bool:
561-
if self.triple == target:
562-
return True
563-
564-
# the vendor field can be ignored, so x86_64-pc-linux-gnu is compatible
565-
# with x86_64-unknown-linux-gnu
566-
if _replace_vendor_with_unknown(self.triple) == target:
567-
return True
568-
569-
return False
570-
571-
572-
class _CrossCompileInfo(NamedTuple):
573-
host_type: str
574-
cross_lib: Optional[str]
575-
linker: Optional[str]
576-
linker_args: Optional[str]
577-
578-
def to_target_info(self) -> Optional[_TargetInfo]:
579-
"""Maps this cross compile info to target info.
580-
581-
Returns None if the corresponding target information could not be
582-
deduced.
583-
"""
584-
# hopefully an exact match
585-
targets = get_rust_target_list()
586-
if self.host_type in targets:
587-
return _TargetInfo(
588-
self.host_type, self.cross_lib, self.linker, self.linker_args
589-
)
590-
591-
# the vendor field can be ignored, so x86_64-pc-linux-gnu is compatible
592-
# with x86_64-unknown-linux-gnu
593-
without_vendor = _replace_vendor_with_unknown(self.host_type)
594-
if without_vendor is not None and without_vendor in targets:
595-
return _TargetInfo(
596-
without_vendor, self.cross_lib, self.linker, self.linker_args
597-
)
598-
599-
return None
600-
601-
602-
def _detect_unix_cross_compile_info() -> Optional["_CrossCompileInfo"]:
603-
# See https://github.com/PyO3/setuptools-rust/issues/138
604-
# This is to support cross compiling on *NIX, where plat_name isn't
605-
# necessarily the same as the system we are running on. *NIX systems
606-
# have more detailed information available in sysconfig. We need that
607-
# because plat_name doesn't give us information on e.g., glibc vs musl.
608-
host_type = sysconfig.get_config_var("HOST_GNU_TYPE")
609-
build_type = sysconfig.get_config_var("BUILD_GNU_TYPE")
610-
611-
if not host_type or host_type == build_type:
612-
# not *NIX, or not cross compiling
613-
return None
614-
615-
if "apple-darwin" in host_type and (build_type and "apple-darwin" in build_type):
616-
# On macos and the build and host differ. This is probably an arm
617-
# Python which was built on x86_64. Don't try to handle this for now.
618-
# (See https://github.com/PyO3/setuptools-rust/issues/192)
619-
return None
620-
621-
stdlib = sysconfig.get_path("stdlib")
622-
assert stdlib is not None
623-
cross_lib = os.path.dirname(stdlib)
624-
625-
bldshared = sysconfig.get_config_var("BLDSHARED")
626-
if not bldshared:
627-
linker = None
628-
linker_args = None
629-
else:
630-
[linker, _, linker_args] = bldshared.partition(" ")
631-
632-
return _CrossCompileInfo(host_type, cross_lib, linker, linker_args)
633-
634-
635501
def _replace_vendor_with_unknown(target: str) -> Optional[str]:
636502
"""Replaces vendor in the target triple with unknown.
637503
@@ -644,7 +510,7 @@ def _replace_vendor_with_unknown(target: str) -> Optional[str]:
644510
return "-".join(components)
645511

646512

647-
def _prepare_build_environment(cross_lib: Optional[str]) -> Dict[str, str]:
513+
def _prepare_build_environment() -> Dict[str, str]:
648514
"""Prepares environment variables to use when executing cargo build."""
649515

650516
# Make sure that if pythonXX-sys is used, it builds against the current
@@ -665,9 +531,6 @@ def _prepare_build_environment(cross_lib: Optional[str]) -> Dict[str, str]:
665531
}
666532
)
667533

668-
if cross_lib:
669-
env.setdefault("PYO3_CROSS_LIB_DIR", cross_lib)
670-
671534
env.pop("CARGO", None)
672535
return env
673536

0 commit comments

Comments
 (0)