22
33use std:: process:: Command ;
44
5+ use anyhow:: Context ;
56use rustc_hash:: FxHashMap ;
67
78use crate :: { cfg_flag:: CfgFlag , utf8_stdout, ManifestPath , Sysroot } ;
89
10+ pub ( crate ) enum Config < ' a > {
11+ Cargo ( & ' a ManifestPath ) ,
12+ Explicit ( & ' a Sysroot ) ,
13+ Discover ,
14+ }
15+
916pub ( crate ) fn get (
10- cargo_toml : Option < & ManifestPath > ,
11- sysroot : Option < & Sysroot > ,
1217 target : Option < & str > ,
1318 extra_env : & FxHashMap < String , String > ,
19+ config : Config < ' _ > ,
1420) -> Vec < CfgFlag > {
1521 let _p = profile:: span ( "rustc_cfg::get" ) ;
1622 let mut res = Vec :: with_capacity ( 6 * 2 + 1 ) ;
@@ -26,64 +32,68 @@ pub(crate) fn get(
2632 // Add miri cfg, which is useful for mir eval in stdlib
2733 res. push ( CfgFlag :: Atom ( "miri" . into ( ) ) ) ;
2834
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 {
3049 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" )
3955 }
40- Err ( e) => tracing:: error!( "failed to get rustc cfgs: {e:?}" ) ,
4156 }
4257
4358 res
4459}
4560
4661fn get_rust_cfgs (
47- cargo_toml : Option < & ManifestPath > ,
48- sysroot : Option < & Sysroot > ,
4962 target : Option < & str > ,
5063 extra_env : & FxHashMap < String , String > ,
64+ config : Config < ' _ > ,
5165) -> 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+ }
6776
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)
7383 }
74- None => {
84+ Config :: Discover => {
7585 let rustc = toolchain:: rustc ( ) ;
7686 tracing:: debug!( ?rustc, "using rustc from env" ) ;
77- rustc
87+ Command :: new ( rustc)
7888 }
7989 } ;
8090
81- // using unstable cargo features failed, fall back to using plain rustc
82- let mut cmd = Command :: new ( rustc) ;
8391 cmd. envs ( extra_env) ;
8492 cmd. args ( [ "--print" , "cfg" , "-O" ] ) ;
8593 if let Some ( target) = target {
8694 cmd. args ( [ "--target" , target] ) ;
8795 }
88- utf8_stdout ( cmd)
96+
97+ let out = utf8_stdout ( cmd) . context ( "Unable to run `rustc`" ) ?;
98+ Ok ( out)
8999}
0 commit comments