3
3
import glob
4
4
import json
5
5
import os
6
- import pkg_resources
7
6
import platform
8
7
import shutil
9
8
import subprocess
20
19
from pathlib import Path
21
20
from typing import Dict , Iterable , List , NamedTuple , Optional , Set , Tuple , cast
22
21
22
+ import pkg_resources
23
23
from setuptools .command .build import build as CommandBuild # type: ignore[import]
24
24
from setuptools .command .build_ext import build_ext as CommandBuildExt
25
25
from setuptools .command .build_ext import get_abi3_suffix
@@ -131,23 +131,10 @@ def build_extension(
131
131
self , ext : RustExtension , forced_target_triple : Optional [str ] = None
132
132
) -> List ["_BuiltModule" ]:
133
133
134
- target_info = self ._detect_rust_target (forced_target_triple )
135
- if target_info is not None :
136
- target_triple = target_info .triple
137
- cross_lib = target_info .cross_lib
138
- linker = target_info .linker
139
- # We're ignoring target_info.linker_args for now because we're not
140
- # sure if they will always do the right thing. Might help with some
141
- # of the OS-specific logic if it does.
142
-
143
- else :
144
- target_triple = None
145
- cross_lib = None
146
- linker = None
147
-
134
+ target_triple = self ._detect_rust_target (forced_target_triple )
148
135
rustc_cfgs = get_rustc_cfgs (target_triple )
149
136
150
- env = _prepare_build_environment (cross_lib )
137
+ env = _prepare_build_environment ()
151
138
152
139
if not os .path .exists (ext .path ):
153
140
raise DistutilsFileError (
@@ -163,9 +150,6 @@ def build_extension(
163
150
164
151
rustflags = []
165
152
166
- if linker is not None :
167
- rustflags .extend (["-C" , "linker=" + linker ])
168
-
169
153
if ext ._uses_exec_binding ():
170
154
command = [
171
155
self .cargo ,
@@ -435,45 +419,12 @@ def _py_limited_api(self) -> _PyLimitedApi:
435
419
436
420
def _detect_rust_target (
437
421
self , forced_target_triple : Optional [str ] = None
438
- ) -> Optional ["_TargetInfo" ]:
422
+ ) -> Optional [str ]:
439
423
assert self .plat_name is not None
440
- cross_compile_info = _detect_unix_cross_compile_info ()
441
- if cross_compile_info is not None :
442
- cross_target_info = cross_compile_info .to_target_info ()
443
- if forced_target_triple is not None :
444
- if (
445
- cross_target_info is not None
446
- and not cross_target_info .is_compatible_with (forced_target_triple )
447
- ):
448
- self .warn (
449
- f"Forced Rust target `{ forced_target_triple } ` is not "
450
- f"compatible with deduced Rust target "
451
- f"`{ cross_target_info .triple } ` - the built package "
452
- f" may not import successfully once installed."
453
- )
454
-
455
- # Forcing the target in a cross-compile environment; use
456
- # the cross-compile information in combination with the
457
- # forced target
458
- return _TargetInfo (
459
- forced_target_triple ,
460
- cross_compile_info .cross_lib ,
461
- cross_compile_info .linker ,
462
- cross_compile_info .linker_args ,
463
- )
464
- elif cross_target_info is not None :
465
- return cross_target_info
466
- else :
467
- raise DistutilsPlatformError (
468
- "Don't know the correct rust target for system type "
469
- f"{ cross_compile_info .host_type } . Please set the "
470
- "CARGO_BUILD_TARGET environment variable."
471
- )
472
-
473
- elif forced_target_triple is not None :
424
+ if forced_target_triple is not None :
474
425
# Automatic target detection can be overridden via the CARGO_BUILD_TARGET
475
426
# environment variable or --target command line option
476
- return _TargetInfo . for_triple ( forced_target_triple )
427
+ return forced_target_triple
477
428
478
429
# Determine local rust target which needs to be "forced" if necessary
479
430
local_rust_target = _adjusted_local_rust_target (self .plat_name )
@@ -485,7 +436,7 @@ def _detect_rust_target(
485
436
# check for None first to avoid calling to rustc if not needed
486
437
and local_rust_target != get_rust_host ()
487
438
):
488
- return _TargetInfo . for_triple ( local_rust_target )
439
+ return local_rust_target
489
440
490
441
return None
491
442
@@ -575,91 +526,6 @@ class _BuiltModule(NamedTuple):
575
526
path : str
576
527
577
528
578
- class _TargetInfo (NamedTuple ):
579
- triple : str
580
- cross_lib : Optional [str ]
581
- linker : Optional [str ]
582
- linker_args : Optional [str ]
583
-
584
- @staticmethod
585
- def for_triple (triple : str ) -> "_TargetInfo" :
586
- return _TargetInfo (triple , None , None , None )
587
-
588
- def is_compatible_with (self , target : str ) -> bool :
589
- if self .triple == target :
590
- return True
591
-
592
- # the vendor field can be ignored, so x86_64-pc-linux-gnu is compatible
593
- # with x86_64-unknown-linux-gnu
594
- if _replace_vendor_with_unknown (self .triple ) == target :
595
- return True
596
-
597
- return False
598
-
599
-
600
- class _CrossCompileInfo (NamedTuple ):
601
- host_type : str
602
- cross_lib : Optional [str ]
603
- linker : Optional [str ]
604
- linker_args : Optional [str ]
605
-
606
- def to_target_info (self ) -> Optional [_TargetInfo ]:
607
- """Maps this cross compile info to target info.
608
-
609
- Returns None if the corresponding target information could not be
610
- deduced.
611
- """
612
- # hopefully an exact match
613
- targets = get_rust_target_list ()
614
- if self .host_type in targets :
615
- return _TargetInfo (
616
- self .host_type , self .cross_lib , self .linker , self .linker_args
617
- )
618
-
619
- # the vendor field can be ignored, so x86_64-pc-linux-gnu is compatible
620
- # with x86_64-unknown-linux-gnu
621
- without_vendor = _replace_vendor_with_unknown (self .host_type )
622
- if without_vendor is not None and without_vendor in targets :
623
- return _TargetInfo (
624
- without_vendor , self .cross_lib , self .linker , self .linker_args
625
- )
626
-
627
- return None
628
-
629
-
630
- def _detect_unix_cross_compile_info () -> Optional ["_CrossCompileInfo" ]:
631
- # See https://github.com/PyO3/setuptools-rust/issues/138
632
- # This is to support cross compiling on *NIX, where plat_name isn't
633
- # necessarily the same as the system we are running on. *NIX systems
634
- # have more detailed information available in sysconfig. We need that
635
- # because plat_name doesn't give us information on e.g., glibc vs musl.
636
- host_type = sysconfig .get_config_var ("HOST_GNU_TYPE" )
637
- build_type = sysconfig .get_config_var ("BUILD_GNU_TYPE" )
638
-
639
- if not host_type or host_type == build_type :
640
- # not *NIX, or not cross compiling
641
- return None
642
-
643
- if "apple-darwin" in host_type and (build_type and "apple-darwin" in build_type ):
644
- # On macos and the build and host differ. This is probably an arm
645
- # Python which was built on x86_64. Don't try to handle this for now.
646
- # (See https://github.com/PyO3/setuptools-rust/issues/192)
647
- return None
648
-
649
- stdlib = sysconfig .get_path ("stdlib" )
650
- assert stdlib is not None
651
- cross_lib = os .path .dirname (stdlib )
652
-
653
- bldshared = sysconfig .get_config_var ("BLDSHARED" )
654
- if not bldshared :
655
- linker = None
656
- linker_args = None
657
- else :
658
- [linker , _ , linker_args ] = bldshared .partition (" " )
659
-
660
- return _CrossCompileInfo (host_type , cross_lib , linker , linker_args )
661
-
662
-
663
529
def _replace_vendor_with_unknown (target : str ) -> Optional [str ]:
664
530
"""Replaces vendor in the target triple with unknown.
665
531
@@ -672,7 +538,7 @@ def _replace_vendor_with_unknown(target: str) -> Optional[str]:
672
538
return "-" .join (components )
673
539
674
540
675
- def _prepare_build_environment (cross_lib : Optional [ str ] ) -> Dict [str , str ]:
541
+ def _prepare_build_environment () -> Dict [str , str ]:
676
542
"""Prepares environment variables to use when executing cargo build."""
677
543
678
544
# Make sure that if pythonXX-sys is used, it builds against the current
@@ -692,10 +558,6 @@ def _prepare_build_environment(cross_lib: Optional[str]) -> Dict[str, str]:
692
558
"PYO3_PYTHON" : os .environ .get ("PYO3_PYTHON" , sys .executable ),
693
559
}
694
560
)
695
-
696
- if cross_lib :
697
- env .setdefault ("PYO3_CROSS_LIB_DIR" , cross_lib )
698
-
699
561
return env
700
562
701
563
0 commit comments