@@ -14,7 +14,7 @@ use crate::util::context::{GlobalContext, StringList, TargetConfig};
14
14
use crate :: util:: interning:: InternedString ;
15
15
use crate :: util:: { CargoResult , Rustc } ;
16
16
use anyhow:: Context as _;
17
- use cargo_platform:: { Cfg , CfgExpr } ;
17
+ use cargo_platform:: { Cfg , CfgExpr , CheckCfg } ;
18
18
use cargo_util:: { paths, ProcessBuilder } ;
19
19
use serde:: { Deserialize , Serialize } ;
20
20
use std:: cell:: RefCell ;
@@ -43,6 +43,8 @@ pub struct TargetInfo {
43
43
crate_types : RefCell < HashMap < CrateType , Option < ( String , String ) > > > ,
44
44
/// `cfg` information extracted from `rustc --print=cfg`.
45
45
cfg : Vec < Cfg > ,
46
+ /// `CheckCfg` informations extracted from `rustc --print=check-cfg`.
47
+ check_cfg : Option < CheckCfg > ,
46
48
/// Supported values for `-Csplit-debuginfo=` flag, queried from rustc
47
49
support_split_debuginfo : Vec < String > ,
48
50
/// Path to the sysroot.
@@ -204,6 +206,14 @@ impl TargetInfo {
204
206
process. arg ( "--print=crate-name" ) ; // `___` as a delimiter.
205
207
process. arg ( "--print=cfg" ) ;
206
208
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
+
207
217
let ( output, error) = rustc
208
218
. cached_output ( & process, extra_fingerprint)
209
219
. with_context ( || {
@@ -257,6 +267,7 @@ impl TargetInfo {
257
267
let mut res = Vec :: new ( ) ;
258
268
loop {
259
269
match lines. next ( ) {
270
+ Some ( line) if line == "___" => break ,
260
271
Some ( line) => {
261
272
let cfg = Cfg :: from_str ( line) . with_context ( || {
262
273
format ! (
@@ -277,6 +288,18 @@ impl TargetInfo {
277
288
res
278
289
} ;
279
290
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
+
280
303
// recalculate `rustflags` from above now that we have `cfg`
281
304
// information
282
305
let new_flags = extra_args (
@@ -323,6 +346,7 @@ impl TargetInfo {
323
346
) ?
324
347
. into ( ) ,
325
348
cfg,
349
+ check_cfg,
326
350
support_split_debuginfo,
327
351
} ) ;
328
352
}
@@ -345,6 +369,11 @@ impl TargetInfo {
345
369
& self . cfg
346
370
}
347
371
372
+ /// The [`CheckCfg`] settings.
373
+ pub fn check_cfg ( & self ) -> & Option < CheckCfg > {
374
+ & self . check_cfg
375
+ }
376
+
348
377
/// Returns the list of file types generated by the given crate type.
349
378
///
350
379
/// Returns `None` if the target does not support the given crate type.
0 commit comments