@@ -130,6 +130,8 @@ one detailed below.
130130 compiler.
131131* [ ` cargo::rustc-cfg=KEY[="VALUE"] ` ] ( #rustc-cfg ) --- Enables compile-time ` cfg `
132132 settings.
133+ * [ ` cargo::rustc-check-cfg=CHECK_CFG ` ] ( #rustc-check-cfg ) -- Register custom ` cfg ` s as
134+ expected for compile-time checking of configs.
133135* [ ` cargo::rustc-env=VAR=VALUE ` ] ( #rustc-env ) --- Sets an environment variable.
134136* [ ` cargo::rustc-cdylib-link-arg=FLAG ` ] ( #rustc-cdylib-link-arg ) --- Passes custom
135137 flags to a linker for cdylib crates.
@@ -233,7 +235,10 @@ equivalent to using [`rustc-link-lib`](#rustc-link-lib) and
233235
234236The ` rustc-cfg ` instruction tells Cargo to pass the given value to the
235237[ ` --cfg ` flag] [ option-cfg ] to the compiler. This may be used for compile-time
236- detection of features to enable [ conditional compilation] .
238+ detection of features to enable [ conditional compilation] . Custom cfgs
239+ must either be expected using the [ ` cargo::rustc-check-cfg ` ] ( #rustc-check-cfg )
240+ instruction or usage will need to allow the [ ` unexpected_cfgs ` ] [ unexpected-cfgs ]
241+ lint to avoid unexpected cfgs warnings.
237242
238243Note that this does * not* affect Cargo's dependency resolution. This cannot be
239244used to enable an optional dependency, or enable other Cargo features.
@@ -249,6 +254,41 @@ identifier, the value should be a string.
249254[ cargo features ] : features.md
250255[ conditional compilation ] : ../../reference/conditional-compilation.md
251256[ option-cfg ] : ../../rustc/command-line-arguments.md#option-cfg
257+ [ unexpected-cfgs ] : ../../rustc/lints/listing/warn-by-default.md#unexpected-cfgs
258+
259+ ### ` cargo::rustc-check-cfg=CHECK_CFG ` {#rustc-check-cfg}
260+
261+ Add to the list of expected config names and values that is used when checking
262+ the _ reachable_ cfg expressions.
263+
264+ For details on the syntax of ` CHECK_CFG ` , see ` rustc ` [ ` --check-cfg ` flag] [ option-check-cfg ] .
265+ See also the [ ` unexpected_cfgs ` ] [ unexpected-cfgs ] lint.
266+
267+ The instruction can be used like this:
268+
269+ ``` rust,no_run
270+ // build.rs
271+ println!("cargo::rustc-check-cfg=cfg(foo, values(\"bar\"))");
272+ ```
273+
274+ Note that all possible cfgs should be defined, regardless of which cfgs are
275+ currently enabled. This includes all possible values of a given cfg name.
276+
277+ It is recommended to group the ` cargo::rustc-check-cfg ` and
278+ [ ` cargo::rustc-cfg ` ] [ option-cfg ] instructions as closely as possible in order to
279+ avoid typos, missing check-cfg, stalled cfgs...
280+
281+ #### Example of using ` cargo::rustc-check-cfg ` and ` cargo::rustc-cfg ` together
282+
283+ ``` rust,no_run
284+ // build.rs
285+ println!("cargo::rustc-check-cfg=cfg(foo, values(\"bar\"))");
286+ if foo_bar_condition {
287+ println!("cargo::rustc-cfg=foo=\"bar\"");
288+ }
289+ ```
290+
291+ [ option-check-cfg ] : ../../rustc/command-line-arguments.md#option-check-cfg
252292
253293### ` cargo::rustc-env=VAR=VALUE ` {#rustc-env}
254294
0 commit comments