2424from setuptools .command .build_ext import get_abi3_suffix
2525from setuptools .command .install_scripts import install_scripts as CommandInstallScripts
2626
27- from ._utils import format_called_process_error
27+ from ._utils import check_subprocess_output , format_called_process_error , Env
2828from .command import RustCommand
2929from .extension import Binding , RustBin , RustExtension , Strip
3030from .rustc_info import (
4545 from setuptools import Command as CommandBdistWheel # type: ignore[assignment]
4646
4747
48- def _check_cargo_supports_crate_type_option () -> bool :
49- version = get_rust_version ()
48+ def _check_cargo_supports_crate_type_option (env : Optional [ Env ] ) -> bool :
49+ version = get_rust_version (env )
5050
5151 if version is None :
5252 return False
@@ -144,10 +144,10 @@ def run_for_extension(self, ext: RustExtension) -> None:
144144 def build_extension (
145145 self , ext : RustExtension , forced_target_triple : Optional [str ] = None
146146 ) -> List ["_BuiltModule" ]:
147- target_triple = self ._detect_rust_target (forced_target_triple )
148- rustc_cfgs = get_rustc_cfgs (target_triple )
147+ target_triple = self ._detect_rust_target (forced_target_triple , ext . env )
148+ rustc_cfgs = get_rustc_cfgs (target_triple , ext . env )
149149
150- env = _prepare_build_environment ()
150+ env = _prepare_build_environment (ext . env )
151151
152152 if not os .path .exists (ext .path ):
153153 raise FileError (
@@ -156,7 +156,7 @@ def build_extension(
156156
157157 quiet = self .qbuild or ext .quiet
158158 debug = self ._is_debug_build (ext )
159- use_cargo_crate_type = _check_cargo_supports_crate_type_option ()
159+ use_cargo_crate_type = _check_cargo_supports_crate_type_option (ext . env )
160160
161161 package_id = ext .metadata (quiet = quiet )["resolve" ]["root" ]
162162 if package_id is None :
@@ -252,7 +252,7 @@ def build_extension(
252252 # If quiet, capture all output and only show it in the exception
253253 # If not quiet, forward all cargo output to stderr
254254 stderr = subprocess .PIPE if quiet else None
255- cargo_messages = subprocess . check_output (
255+ cargo_messages = check_subprocess_output (
256256 command ,
257257 env = env ,
258258 stderr = stderr ,
@@ -417,7 +417,7 @@ def install_extension(
417417 args .insert (0 , "strip" )
418418 args .append (ext_path )
419419 try :
420- subprocess . check_output (args )
420+ check_subprocess_output (args , env = None )
421421 except subprocess .CalledProcessError :
422422 pass
423423
@@ -477,7 +477,7 @@ def _py_limited_api(self) -> _PyLimitedApi:
477477 return cast (_PyLimitedApi , bdist_wheel .py_limited_api )
478478
479479 def _detect_rust_target (
480- self , forced_target_triple : Optional [str ] = None
480+ self , forced_target_triple : Optional [str ], env : Env
481481 ) -> Optional [str ]:
482482 assert self .plat_name is not None
483483 if forced_target_triple is not None :
@@ -486,14 +486,14 @@ def _detect_rust_target(
486486 return forced_target_triple
487487
488488 # Determine local rust target which needs to be "forced" if necessary
489- local_rust_target = _adjusted_local_rust_target (self .plat_name )
489+ local_rust_target = _adjusted_local_rust_target (self .plat_name , env )
490490
491491 # Match cargo's behaviour of not using an explicit target if the
492492 # target we're compiling for is the host
493493 if (
494494 local_rust_target is not None
495495 # check for None first to avoid calling to rustc if not needed
496- and local_rust_target != get_rust_host ()
496+ and local_rust_target != get_rust_host (env )
497497 ):
498498 return local_rust_target
499499
@@ -566,7 +566,7 @@ def create_universal2_binary(output_path: str, input_paths: List[str]) -> None:
566566 # Try lipo first
567567 command = ["lipo" , "-create" , "-output" , output_path , * input_paths ]
568568 try :
569- subprocess . check_output (command , text = True )
569+ check_subprocess_output (command , env = None , text = True )
570570 except subprocess .CalledProcessError as e :
571571 output = e .output
572572 raise CompileError ("lipo failed with code: %d\n %s" % (e .returncode , output ))
@@ -609,7 +609,7 @@ def _replace_vendor_with_unknown(target: str) -> Optional[str]:
609609 return "-" .join (components )
610610
611611
612- def _prepare_build_environment () -> Dict [str , str ]:
612+ def _prepare_build_environment (env : Env ) -> Dict [str , str ]:
613613 """Prepares environment variables to use when executing cargo build."""
614614
615615 base_executable = None
@@ -625,20 +625,18 @@ def _prepare_build_environment() -> Dict[str, str]:
625625 # executing python interpreter.
626626 bindir = os .path .dirname (executable )
627627
628- env = os .environ .copy ()
629- env .update (
628+ env_vars = ( env . env or os .environ ) .copy ()
629+ env_vars .update (
630630 {
631631 # disables rust's pkg-config seeking for specified packages,
632632 # which causes pythonXX-sys to fall back to detecting the
633633 # interpreter from the path.
634- "PATH" : os .path .join (bindir , os .environ .get ("PATH" , "" )),
635- "PYTHON_SYS_EXECUTABLE" : os .environ .get (
636- "PYTHON_SYS_EXECUTABLE" , executable
637- ),
638- "PYO3_PYTHON" : os .environ .get ("PYO3_PYTHON" , executable ),
634+ "PATH" : os .path .join (bindir , env_vars .get ("PATH" , "" )),
635+ "PYTHON_SYS_EXECUTABLE" : env_vars .get ("PYTHON_SYS_EXECUTABLE" , executable ),
636+ "PYO3_PYTHON" : env_vars .get ("PYO3_PYTHON" , executable ),
639637 }
640638 )
641- return env
639+ return env_vars
642640
643641
644642def _is_py_limited_api (
@@ -692,19 +690,19 @@ def _binding_features(
692690_PyLimitedApi = Literal ["cp37" , "cp38" , "cp39" , "cp310" , "cp311" , "cp312" , True , False ]
693691
694692
695- def _adjusted_local_rust_target (plat_name : str ) -> Optional [str ]:
693+ def _adjusted_local_rust_target (plat_name : str , env : Env ) -> Optional [str ]:
696694 """Returns the local rust target for the given `plat_name`, if it is
697695 necessary to 'force' a specific target for correctness."""
698696
699697 # If we are on a 64-bit machine, but running a 32-bit Python, then
700698 # we'll target a 32-bit Rust build.
701699 if plat_name == "win32" :
702- if get_rustc_cfgs (None ).get ("target_env" ) == "gnu" :
700+ if get_rustc_cfgs (None , env ).get ("target_env" ) == "gnu" :
703701 return "i686-pc-windows-gnu"
704702 else :
705703 return "i686-pc-windows-msvc"
706704 elif plat_name == "win-amd64" :
707- if get_rustc_cfgs (None ).get ("target_env" ) == "gnu" :
705+ if get_rustc_cfgs (None , env ).get ("target_env" ) == "gnu" :
708706 return "x86_64-pc-windows-gnu"
709707 else :
710708 return "x86_64-pc-windows-msvc"
0 commit comments