2
2
3
3
use std:: process:: Command ;
4
4
5
+ use anyhow:: Context ;
5
6
use rustc_hash:: FxHashMap ;
6
7
7
8
use crate :: { cfg_flag:: CfgFlag , utf8_stdout, ManifestPath , Sysroot } ;
8
9
10
+ pub ( crate ) enum Config < ' a > {
11
+ Cargo ( & ' a ManifestPath ) ,
12
+ Explicit ( & ' a Sysroot ) ,
13
+ Discover ,
14
+ }
15
+
9
16
pub ( crate ) fn get (
10
- cargo_toml : Option < & ManifestPath > ,
11
- sysroot : Option < & Sysroot > ,
12
17
target : Option < & str > ,
13
18
extra_env : & FxHashMap < String , String > ,
19
+ config : Config < ' _ > ,
14
20
) -> Vec < CfgFlag > {
15
21
let _p = profile:: span ( "rustc_cfg::get" ) ;
16
22
let mut res = Vec :: with_capacity ( 6 * 2 + 1 ) ;
@@ -26,64 +32,68 @@ pub(crate) fn get(
26
32
// Add miri cfg, which is useful for mir eval in stdlib
27
33
res. push ( CfgFlag :: Atom ( "miri" . into ( ) ) ) ;
28
34
29
- match get_rust_cfgs ( cargo_toml, sysroot, target, extra_env) {
35
+ let rustc_cfgs = get_rust_cfgs ( target, extra_env, config) ;
36
+
37
+ let rustc_cfgs = match rustc_cfgs {
38
+ Ok ( cfgs) => cfgs,
39
+ Err ( e) => {
40
+ tracing:: error!( ?e, "failed to get rustc cfgs" ) ;
41
+ return res;
42
+ }
43
+ } ;
44
+
45
+ let rustc_cfgs =
46
+ rustc_cfgs. lines ( ) . map ( |it| it. parse :: < CfgFlag > ( ) ) . collect :: < Result < Vec < _ > , _ > > ( ) ;
47
+
48
+ match rustc_cfgs {
30
49
Ok ( rustc_cfgs) => {
31
- tracing:: debug!(
32
- "rustc cfgs found: {:?}" ,
33
- rustc_cfgs
34
- . lines( )
35
- . map( |it| it. parse:: <CfgFlag >( ) . map( |it| it. to_string( ) ) )
36
- . collect:: <Vec <_>>( )
37
- ) ;
38
- res. extend ( rustc_cfgs. lines ( ) . filter_map ( |it| it. parse ( ) . ok ( ) ) ) ;
50
+ tracing:: debug!( ?rustc_cfgs, "rustc cfgs found" ) ;
51
+ res. extend ( rustc_cfgs) ;
52
+ }
53
+ Err ( e) => {
54
+ tracing:: error!( ?e, "failed to get rustc cfgs" )
39
55
}
40
- Err ( e) => tracing:: error!( "failed to get rustc cfgs: {e:?}" ) ,
41
56
}
42
57
43
58
res
44
59
}
45
60
46
61
fn get_rust_cfgs (
47
- cargo_toml : Option < & ManifestPath > ,
48
- sysroot : Option < & Sysroot > ,
49
62
target : Option < & str > ,
50
63
extra_env : & FxHashMap < String , String > ,
64
+ config : Config < ' _ > ,
51
65
) -> anyhow:: Result < String > {
52
- if let Some ( cargo_toml) = cargo_toml {
53
- let mut cargo_config = Command :: new ( toolchain:: cargo ( ) ) ;
54
- cargo_config. envs ( extra_env) ;
55
- cargo_config
56
- . current_dir ( cargo_toml. parent ( ) )
57
- . args ( [ "rustc" , "-Z" , "unstable-options" , "--print" , "cfg" ] )
58
- . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
59
- if let Some ( target) = target {
60
- cargo_config. args ( [ "--target" , target] ) ;
61
- }
62
- match utf8_stdout ( cargo_config) {
63
- Ok ( it) => return Ok ( it) ,
64
- Err ( e) => tracing:: debug!( "{e:?}: falling back to querying rustc for cfgs" ) ,
65
- }
66
- }
66
+ let mut cmd = match config {
67
+ Config :: Cargo ( cargo_toml) => {
68
+ let mut cmd = Command :: new ( toolchain:: cargo ( ) ) ;
69
+ cmd. envs ( extra_env) ;
70
+ cmd. current_dir ( cargo_toml. parent ( ) )
71
+ . args ( [ "rustc" , "-Z" , "unstable-options" , "--print" , "cfg" ] )
72
+ . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
73
+ if let Some ( target) = target {
74
+ cmd. args ( [ "--target" , target] ) ;
75
+ }
67
76
68
- let rustc = match sysroot {
69
- Some ( sysroot) => {
70
- let rustc = sysroot. discover_rustc ( ) ?. into ( ) ;
71
- tracing:: debug!( ?rustc, "using rustc from sysroot" ) ;
72
- rustc
77
+ return utf8_stdout ( cmd) . context ( "Unable to run `cargo rustc`" ) ;
78
+ }
79
+ Config :: Explicit ( sysroot) => {
80
+ let rustc: std:: path:: PathBuf = sysroot. discover_rustc ( ) ?. into ( ) ;
81
+ tracing:: debug!( ?rustc, "using explicit rustc from sysroot" ) ;
82
+ Command :: new ( rustc)
73
83
}
74
- None => {
84
+ Config :: Discover => {
75
85
let rustc = toolchain:: rustc ( ) ;
76
86
tracing:: debug!( ?rustc, "using rustc from env" ) ;
77
- rustc
87
+ Command :: new ( rustc)
78
88
}
79
89
} ;
80
90
81
- // using unstable cargo features failed, fall back to using plain rustc
82
- let mut cmd = Command :: new ( rustc) ;
83
91
cmd. envs ( extra_env) ;
84
92
cmd. args ( [ "--print" , "cfg" , "-O" ] ) ;
85
93
if let Some ( target) = target {
86
94
cmd. args ( [ "--target" , target] ) ;
87
95
}
88
- utf8_stdout ( cmd)
96
+
97
+ let out = utf8_stdout ( cmd) . context ( "Unable to run `rustc`" ) ?;
98
+ Ok ( out)
89
99
}
0 commit comments