2
2
3
3
use std:: iter;
4
4
use std:: path:: PathBuf ;
5
+ use std:: str:: from_utf8;
5
6
use std:: { ops, process:: Command } ;
6
7
7
8
use anyhow:: { Context , Result } ;
@@ -98,6 +99,8 @@ pub struct CargoConfig {
98
99
pub wrap_rustc_in_build_scripts : bool ,
99
100
100
101
pub run_build_script_command : Option < Vec < String > > ,
102
+
103
+ pub extra_env : FxHashMap < String , String > ,
101
104
}
102
105
103
106
impl CargoConfig {
@@ -263,8 +266,8 @@ impl CargoWorkspace {
263
266
let target = config
264
267
. target
265
268
. clone ( )
266
- . or_else ( || cargo_config_build_target ( cargo_toml) )
267
- . or_else ( || rustc_discover_host_triple ( cargo_toml) ) ;
269
+ . or_else ( || cargo_config_build_target ( cargo_toml, config ) )
270
+ . or_else ( || rustc_discover_host_triple ( cargo_toml, config ) ) ;
268
271
269
272
let mut meta = MetadataCommand :: new ( ) ;
270
273
meta. cargo_path ( toolchain:: cargo ( ) ) ;
@@ -292,8 +295,27 @@ impl CargoWorkspace {
292
295
// unclear whether cargo itself supports it.
293
296
progress ( "metadata" . to_string ( ) ) ;
294
297
295
- let meta =
296
- meta. exec ( ) . with_context ( || format ! ( "Failed to run `{:?}`" , meta. cargo_command( ) ) ) ?;
298
+ fn exec_with_env (
299
+ command : & cargo_metadata:: MetadataCommand ,
300
+ extra_env : & FxHashMap < String , String > ,
301
+ ) -> Result < cargo_metadata:: Metadata , cargo_metadata:: Error > {
302
+ let mut command = command. cargo_command ( ) ;
303
+ command. envs ( extra_env) ;
304
+ let output = command. output ( ) ?;
305
+ if !output. status . success ( ) {
306
+ return Err ( cargo_metadata:: Error :: CargoMetadata {
307
+ stderr : String :: from_utf8 ( output. stderr ) ?,
308
+ } ) ;
309
+ }
310
+ let stdout = from_utf8 ( & output. stdout ) ?
311
+ . lines ( )
312
+ . find ( |line| line. starts_with ( '{' ) )
313
+ . ok_or ( cargo_metadata:: Error :: NoJson ) ?;
314
+ cargo_metadata:: MetadataCommand :: parse ( stdout)
315
+ }
316
+
317
+ let meta = exec_with_env ( & meta, & config. extra_env )
318
+ . with_context ( || format ! ( "Failed to run `{:?}`" , meta. cargo_command( ) ) ) ?;
297
319
298
320
Ok ( meta)
299
321
}
@@ -463,8 +485,9 @@ impl CargoWorkspace {
463
485
}
464
486
}
465
487
466
- fn rustc_discover_host_triple ( cargo_toml : & ManifestPath ) -> Option < String > {
488
+ fn rustc_discover_host_triple ( cargo_toml : & ManifestPath , config : & CargoConfig ) -> Option < String > {
467
489
let mut rustc = Command :: new ( toolchain:: rustc ( ) ) ;
490
+ rustc. envs ( & config. extra_env ) ;
468
491
rustc. current_dir ( cargo_toml. parent ( ) ) . arg ( "-vV" ) ;
469
492
tracing:: debug!( "Discovering host platform by {:?}" , rustc) ;
470
493
match utf8_stdout ( rustc) {
@@ -486,8 +509,9 @@ fn rustc_discover_host_triple(cargo_toml: &ManifestPath) -> Option<String> {
486
509
}
487
510
}
488
511
489
- fn cargo_config_build_target ( cargo_toml : & ManifestPath ) -> Option < String > {
512
+ fn cargo_config_build_target ( cargo_toml : & ManifestPath , config : & CargoConfig ) -> Option < String > {
490
513
let mut cargo_config = Command :: new ( toolchain:: cargo ( ) ) ;
514
+ cargo_config. envs ( & config. extra_env ) ;
491
515
cargo_config
492
516
. current_dir ( cargo_toml. parent ( ) )
493
517
. args ( & [ "-Z" , "unstable-options" , "config" , "get" , "build.target" ] )
0 commit comments