@@ -14,7 +14,7 @@ use crate::util::context::{GlobalContext, StringList, TargetConfig};
1414use crate :: util:: interning:: InternedString ;
1515use crate :: util:: { CargoResult , Rustc } ;
1616use anyhow:: Context as _;
17- use cargo_platform:: { Cfg , CfgExpr } ;
17+ use cargo_platform:: { Cfg , CfgExpr , CheckCfg } ;
1818use cargo_util:: { paths, ProcessBuilder } ;
1919use serde:: { Deserialize , Serialize } ;
2020use std:: cell:: RefCell ;
@@ -43,6 +43,8 @@ pub struct TargetInfo {
4343 crate_types : RefCell < HashMap < CrateType , Option < ( String , String ) > > > ,
4444 /// `cfg` information extracted from `rustc --print=cfg`.
4545 cfg : Vec < Cfg > ,
46+ /// `CheckCfg` informations extracted from `rustc --print=check-cfg`.
47+ check_cfg : Option < CheckCfg > ,
4648 /// Supported values for `-Csplit-debuginfo=` flag, queried from rustc
4749 support_split_debuginfo : Vec < String > ,
4850 /// Path to the sysroot.
@@ -204,6 +206,14 @@ impl TargetInfo {
204206 process. arg ( "--print=crate-name" ) ; // `___` as a delimiter.
205207 process. arg ( "--print=cfg" ) ;
206208
209+ if gctx. cli_unstable ( ) . check_target_cfgs {
210+ process. arg ( "--print=crate-name" ) ; // `___` as a delimiter.
211+ process. arg ( "--print=check-cfg" ) ;
212+
213+ process. arg ( "--check-cfg=cfg()" ) ; // otherwise `--print=check-cfg` won't output
214+ process. arg ( "-Zunstable-options" ) ; // required by `--print=check-cfg`
215+ }
216+
207217 let ( output, error) = rustc
208218 . cached_output ( & process, extra_fingerprint)
209219 . with_context ( || {
@@ -257,6 +267,7 @@ impl TargetInfo {
257267 let mut res = Vec :: new ( ) ;
258268 loop {
259269 match lines. next ( ) {
270+ Some ( line) if line == "___" => break ,
260271 Some ( line) => {
261272 let cfg = Cfg :: from_str ( line) . with_context ( || {
262273 format ! (
@@ -277,6 +288,18 @@ impl TargetInfo {
277288 res
278289 } ;
279290
291+ let check_cfg = if gctx. cli_unstable ( ) . check_target_cfgs {
292+ let mut check_cfg = CheckCfg :: default ( ) ;
293+ check_cfg. exhaustive = true ;
294+
295+ Some ( lines. fold ( check_cfg, |mut check_cfg, line| {
296+ check_cfg. process_line ( line) ;
297+ check_cfg
298+ } ) )
299+ } else {
300+ None
301+ } ;
302+
280303 // recalculate `rustflags` from above now that we have `cfg`
281304 // information
282305 let new_flags = extra_args (
@@ -323,6 +346,7 @@ impl TargetInfo {
323346 ) ?
324347 . into ( ) ,
325348 cfg,
349+ check_cfg,
326350 support_split_debuginfo,
327351 } ) ;
328352 }
@@ -345,6 +369,11 @@ impl TargetInfo {
345369 & self . cfg
346370 }
347371
372+ /// The [`CheckCfg`] settings.
373+ pub fn check_cfg ( & self ) -> & Option < CheckCfg > {
374+ & self . check_cfg
375+ }
376+
348377 /// Returns the list of file types generated by the given crate type.
349378 ///
350379 /// Returns `None` if the target does not support the given crate type.
0 commit comments