@@ -20,10 +20,11 @@ use paths::{AbsPath, AbsPathBuf};
2020use rustc_hash:: { FxHashMap , FxHashSet } ;
2121use semver:: Version ;
2222use serde:: Deserialize ;
23+ use toolchain:: Tool ;
2324
2425use crate :: {
2526 cfg_flag:: CfgFlag , utf8_stdout, CargoConfig , CargoFeatures , CargoWorkspace , InvocationLocation ,
26- InvocationStrategy , Package ,
27+ InvocationStrategy , Package , Sysroot ,
2728} ;
2829
2930#[ derive( Debug , Default , Clone , PartialEq , Eq ) ]
@@ -61,6 +62,7 @@ impl WorkspaceBuildScripts {
6162 config : & CargoConfig ,
6263 allowed_features : & FxHashSet < String > ,
6364 workspace_root : & AbsPathBuf ,
65+ sysroot : Option < & Sysroot > ,
6466 ) -> io:: Result < Command > {
6567 let mut cmd = match config. run_build_script_command . as_deref ( ) {
6668 Some ( [ program, args @ ..] ) => {
@@ -69,7 +71,10 @@ impl WorkspaceBuildScripts {
6971 cmd
7072 }
7173 _ => {
72- let mut cmd = Command :: new ( toolchain:: cargo ( ) ) ;
74+ let mut cmd = Command :: new (
75+ Sysroot :: discover_tool ( sysroot, Tool :: Cargo )
76+ . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: NotFound , e) ) ?,
77+ ) ;
7378
7479 cmd. args ( [ "check" , "--quiet" , "--workspace" , "--message-format=json" ] ) ;
7580 cmd. args ( & config. extra_args ) ;
@@ -133,6 +138,7 @@ impl WorkspaceBuildScripts {
133138 workspace : & CargoWorkspace ,
134139 progress : & dyn Fn ( String ) ,
135140 toolchain : & Option < Version > ,
141+ sysroot : Option < & Sysroot > ,
136142 ) -> io:: Result < WorkspaceBuildScripts > {
137143 const RUST_1_62 : Version = Version :: new ( 1 , 62 , 0 ) ;
138144
@@ -151,6 +157,7 @@ impl WorkspaceBuildScripts {
151157 config,
152158 & allowed_features,
153159 & workspace. workspace_root ( ) . to_path_buf ( ) ,
160+ sysroot,
154161 ) ?,
155162 workspace,
156163 current_dir,
@@ -165,6 +172,7 @@ impl WorkspaceBuildScripts {
165172 config,
166173 & allowed_features,
167174 & workspace. workspace_root ( ) . to_path_buf ( ) ,
175+ sysroot,
168176 ) ?;
169177 cmd. args ( [ "-Z" , "unstable-options" , "--keep-going" ] ) . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
170178 let mut res = Self :: run_per_ws ( cmd, workspace, current_dir, progress) ?;
@@ -194,7 +202,7 @@ impl WorkspaceBuildScripts {
194202 ) )
195203 }
196204 } ;
197- let cmd = Self :: build_command ( config, & Default :: default ( ) , workspace_root) ?;
205+ let cmd = Self :: build_command ( config, & Default :: default ( ) , workspace_root, None ) ?;
198206 // NB: Cargo.toml could have been modified between `cargo metadata` and
199207 // `cargo check`. We shouldn't assume that package ids we see here are
200208 // exactly those from `config`.
@@ -415,14 +423,15 @@ impl WorkspaceBuildScripts {
415423 rustc : & CargoWorkspace ,
416424 current_dir : & AbsPath ,
417425 extra_env : & FxHashMap < String , String > ,
426+ sysroot : Option < & Sysroot > ,
418427 ) -> Self {
419428 let mut bs = WorkspaceBuildScripts :: default ( ) ;
420429 for p in rustc. packages ( ) {
421430 bs. outputs . insert ( p, BuildScriptOutput :: default ( ) ) ;
422431 }
423432 let res = ( || {
424433 let target_libdir = ( || {
425- let mut cargo_config = Command :: new ( toolchain :: cargo ( ) ) ;
434+ let mut cargo_config = Command :: new ( Sysroot :: discover_tool ( sysroot , Tool :: Cargo ) ? ) ;
426435 cargo_config. envs ( extra_env) ;
427436 cargo_config
428437 . current_dir ( current_dir)
@@ -431,7 +440,7 @@ impl WorkspaceBuildScripts {
431440 if let Ok ( it) = utf8_stdout ( cargo_config) {
432441 return Ok ( it) ;
433442 }
434- let mut cmd = Command :: new ( toolchain :: rustc ( ) ) ;
443+ let mut cmd = Command :: new ( Sysroot :: discover_tool ( sysroot , Tool :: Rustc ) ? ) ;
435444 cmd. envs ( extra_env) ;
436445 cmd. args ( [ "--print" , "target-libdir" ] ) ;
437446 utf8_stdout ( cmd)
0 commit comments