22
33use std:: process:: Command ;
44
5+ use anyhow:: Result ;
56use paths:: AbsPath ;
67
78use crate :: { cfg_flag:: CfgFlag , utf8_stdout} ;
@@ -18,41 +19,39 @@ pub(crate) fn get(cargo_toml: Option<&AbsPath>, target: Option<&str>) -> Vec<Cfg
1819 }
1920 }
2021
21- let rustc_cfgs = {
22- cargo_toml
23- . and_then ( |cargo_toml| {
24- let mut cargo_config = Command :: new ( toolchain:: cargo ( ) ) ;
25- cargo_config. current_dir ( cargo_toml. parent ( ) . unwrap ( ) ) . args ( & [
26- "+nightly" ,
27- "-Z" ,
28- "unstable-options" ,
29- "rustc" ,
30- "--print" ,
31- "cfg" ,
32- ] ) ;
33- if let Some ( target) = target {
34- cargo_config. args ( & [ "--target" , target] ) ;
35- }
36- utf8_stdout ( cargo_config) . ok ( )
37- } )
38- . map_or_else (
39- || {
40- // using unstable cargo features failed, fall back to using plain rustc
41- let mut cmd = Command :: new ( toolchain:: rustc ( ) ) ;
42- cmd. args ( & [ "--print" , "cfg" , "-O" ] ) ;
43- if let Some ( target) = target {
44- cmd. args ( & [ "--target" , target] ) ;
45- }
46- utf8_stdout ( cmd)
47- } ,
48- Ok ,
49- )
50- } ;
51-
52- match rustc_cfgs {
22+ match get_rust_cfgs ( cargo_toml, target) {
5323 Ok ( rustc_cfgs) => res. extend ( rustc_cfgs. lines ( ) . map ( |it| it. parse ( ) . unwrap ( ) ) ) ,
5424 Err ( e) => log:: error!( "failed to get rustc cfgs: {:#}" , e) ,
5525 }
5626
5727 res
5828}
29+
30+ fn get_rust_cfgs ( cargo_toml : Option < & AbsPath > , target : Option < & str > ) -> Result < String > {
31+ let cargo_rust_cfgs = match cargo_toml {
32+ Some ( cargo_toml) => {
33+ let mut cargo_config = Command :: new ( toolchain:: cargo ( ) ) ;
34+ cargo_config
35+ . current_dir ( cargo_toml. parent ( ) . unwrap ( ) )
36+ . args ( & [ "-Z" , "unstable-options" , "rustc" , "--print" , "cfg" ] )
37+ . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
38+ if let Some ( target) = target {
39+ cargo_config. args ( & [ "--target" , target] ) ;
40+ }
41+ utf8_stdout ( cargo_config) . ok ( )
42+ }
43+ None => None ,
44+ } ;
45+ match cargo_rust_cfgs {
46+ Some ( stdout) => Ok ( stdout) ,
47+ None => {
48+ // using unstable cargo features failed, fall back to using plain rustc
49+ let mut cmd = Command :: new ( toolchain:: rustc ( ) ) ;
50+ cmd. args ( & [ "--print" , "cfg" , "-O" ] ) ;
51+ if let Some ( target) = target {
52+ cmd. args ( & [ "--target" , target] ) ;
53+ }
54+ utf8_stdout ( cmd)
55+ }
56+ }
57+ }
0 commit comments