@@ -20,31 +20,41 @@ pub fn get(
20
20
target : Option < & str > ,
21
21
extra_env : & FxHashMap < String , String > ,
22
22
) -> anyhow:: Result < String > {
23
- let output = match config {
23
+ let process = |output : String | {
24
+ ( || Some ( output. split_once ( r#""data-layout": ""# ) ?. 1 . split_once ( '"' ) ?. 0 . to_owned ( ) ) ) ( )
25
+ . ok_or_else ( || {
26
+ anyhow:: format_err!( "could not fetch target-spec-json from command output" )
27
+ } )
28
+ } ;
29
+ let sysroot = match config {
24
30
RustcDataLayoutConfig :: Cargo ( sysroot, cargo_toml) => {
25
31
let cargo = Sysroot :: discover_tool ( sysroot, toolchain:: Tool :: Cargo ) ?;
26
32
let mut cmd = Command :: new ( cargo) ;
27
33
cmd. envs ( extra_env) ;
28
34
cmd. current_dir ( cargo_toml. parent ( ) )
29
- . args ( [ "-Z" , "unstable-options" , "--print" , "target-spec-json" ] )
35
+ . args ( [ "rustc" , "--" , " -Z", "unstable-options" , "--print" , "target-spec-json" ] )
30
36
. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
31
37
if let Some ( target) = target {
32
38
cmd. args ( [ "--target" , target] ) ;
33
39
}
34
- utf8_stdout ( cmd)
35
- }
36
- RustcDataLayoutConfig :: Rustc ( sysroot) => {
37
- let rustc = Sysroot :: discover_tool ( sysroot, toolchain:: Tool :: Rustc ) ?;
38
- let mut cmd = Command :: new ( rustc) ;
39
- cmd. envs ( extra_env)
40
- . args ( [ "-Z" , "unstable-options" , "--print" , "target-spec-json" ] )
41
- . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
42
- if let Some ( target) = target {
43
- cmd. args ( [ "--target" , target] ) ;
40
+ match utf8_stdout ( cmd) {
41
+ Ok ( output) => return process ( output) ,
42
+ Err ( e) => {
43
+ tracing:: warn!( "failed to run `cargo rustc --print target-spec-json`, falling back to invoking rustc directly: {e}" ) ;
44
+ sysroot
45
+ }
44
46
}
45
- utf8_stdout ( cmd)
46
47
}
47
- } ?;
48
- ( || Some ( output. split_once ( r#""data-layout": ""# ) ?. 1 . split_once ( '"' ) ?. 0 . to_owned ( ) ) ) ( )
49
- . ok_or_else ( || anyhow:: format_err!( "could not fetch target-spec-json from command output" ) )
48
+ RustcDataLayoutConfig :: Rustc ( sysroot) => sysroot,
49
+ } ;
50
+
51
+ let rustc = Sysroot :: discover_tool ( sysroot, toolchain:: Tool :: Rustc ) ?;
52
+ let mut cmd = Command :: new ( rustc) ;
53
+ cmd. envs ( extra_env)
54
+ . args ( [ "-Z" , "unstable-options" , "--print" , "target-spec-json" ] )
55
+ . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
56
+ if let Some ( target) = target {
57
+ cmd. args ( [ "--target" , target] ) ;
58
+ }
59
+ process ( utf8_stdout ( cmd) ?)
50
60
}
0 commit comments