@@ -20,31 +20,41 @@ pub fn get(
2020 target : Option < & str > ,
2121 extra_env : & FxHashMap < String , String > ,
2222) -> 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 {
2430 RustcDataLayoutConfig :: Cargo ( sysroot, cargo_toml) => {
2531 let cargo = Sysroot :: discover_tool ( sysroot, toolchain:: Tool :: Cargo ) ?;
2632 let mut cmd = Command :: new ( cargo) ;
2733 cmd. envs ( extra_env) ;
2834 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" ] )
3036 . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
3137 if let Some ( target) = target {
3238 cmd. args ( [ "--target" , target] ) ;
3339 }
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+ }
4446 }
45- utf8_stdout ( cmd)
4647 }
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) ?)
5060}
0 commit comments