Skip to content

Commit 60a5026

Browse files
committed
Adjust documentation for newly stabilized -Zcheck-cfg
1 parent a5669e8 commit 60a5026

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed

src/doc/src/reference/build-script-examples.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ values](https://github.com/sfackler/rust-openssl/blob/dc72a8e2c429e46c275e528b61
456456
```rust,ignore
457457
// (portion of build.rs)
458458
459+
println!("cargo::rustc-check-cfg=cfg(ossl101,ossl102)");
460+
println!("cargo::rustc-check-cfg=cfg(ossl110,ossl110g,ossl111)");
461+
459462
if let Ok(version) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
460463
let version = u64::from_str_radix(&version, 16).unwrap();
461464

src/doc/src/reference/build-scripts.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

234236
The `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

238243
Note that this does *not* affect Cargo's dependency resolution. This cannot be
239244
used 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

src/doc/src/reference/unstable.md

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ For the latest nightly, see the [nightly version] of this page.
8383
* [build-std-features](#build-std-features) --- Sets features to use with the standard library.
8484
* [binary-dep-depinfo](#binary-dep-depinfo) --- Causes the dep-info file to track binary dependencies.
8585
* [panic-abort-tests](#panic-abort-tests) --- Allows running tests with the "abort" panic strategy.
86-
* [check-cfg](#check-cfg) --- Compile-time validation of `cfg` expressions.
8786
* [host-config](#host-config) --- Allows setting `[target]`-like configuration settings for host build targets.
8887
* [target-applies-to-host](#target-applies-to-host) --- Alters whether certain flags will be passed to host build targets.
8988
* [gc](#gc) --- Global cache garbage collection.
@@ -1154,44 +1153,6 @@ You can use the flag like this:
11541153
cargo rustdoc -Z unstable-options --output-format json
11551154
```
11561155

1157-
## check-cfg
1158-
1159-
* RFC: [#3013](https://github.com/rust-lang/rfcs/pull/3013)
1160-
* Tracking Issue: [#10554](https://github.com/rust-lang/cargo/issues/10554)
1161-
1162-
`-Z check-cfg` command line enables compile time checking of Cargo features as well as `rustc`
1163-
well known names and values in `#[cfg]`, `cfg!`, `#[link]` and `#[cfg_attr]` with the `rustc`
1164-
and `rustdoc` unstable `--check-cfg` command line.
1165-
1166-
You can use the flag like this:
1167-
1168-
```
1169-
cargo check -Z unstable-options -Z check-cfg
1170-
```
1171-
1172-
### `cargo::rustc-check-cfg=CHECK_CFG`
1173-
1174-
The `rustc-check-cfg` instruction tells Cargo to pass the given value to the
1175-
`--check-cfg` flag to the compiler. This may be used for compile-time
1176-
detection of unexpected conditional compilation name and/or values.
1177-
1178-
This can only be used in combination with `-Zcheck-cfg` otherwise it is ignored
1179-
with a warning.
1180-
1181-
If you want to integrate with Cargo features, only use `-Zcheck-cfg` instead of
1182-
trying to do it manually with this option.
1183-
1184-
You can use the instruction like this:
1185-
1186-
```rust,no_run
1187-
// build.rs
1188-
println!("cargo::rustc-check-cfg=cfg(foo, bar)");
1189-
```
1190-
1191-
```
1192-
cargo check -Z unstable-options -Z check-cfg
1193-
```
1194-
11951156
## codegen-backend
11961157

11971158
The `codegen-backend` feature makes it possible to select the codegen backend used by rustc using a profile.
@@ -1798,3 +1759,11 @@ The `-Z registry-auth` feature has been stabilized in the 1.74 release with the
17981759
requirement that a credential-provider is configured.
17991760

18001761
See [Registry Authentication](registry-authentication.md) documentation for details.
1762+
1763+
## check-cfg
1764+
1765+
The `-Z check-cfg` feature has been stabilized in the 1.80 release by making it the
1766+
default behavior.
1767+
1768+
See the [build script documentation](build-scripts.md#rustc-check-cfg) for informations
1769+
about specifying custom cfgs.

0 commit comments

Comments
 (0)