Skip to content

Commit

Permalink
Adjust documentation for newly stabilized -Zcheck-cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Mar 11, 2024
1 parent fbaea97 commit fbba427
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
39 changes: 38 additions & 1 deletion src/doc/src/reference/build-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ one detailed below.
compiler.
* [`cargo::rustc-cfg=KEY[="VALUE"]`](#rustc-cfg) --- Enables compile-time `cfg`
settings.
* [`cargo::rustc-check-cfg=CHECK_CFG`](#rustc-check-cfg) -- Enables compile-time
checking of configs.
* [`cargo::rustc-env=VAR=VALUE`](#rustc-env) --- Sets an environment variable.
* [`cargo::rustc-cdylib-link-arg=FLAG`](#rustc-cdylib-link-arg) --- Passes custom
flags to a linker for cdylib crates.
Expand Down Expand Up @@ -233,7 +235,9 @@ equivalent to using [`rustc-link-lib`](#rustc-link-lib) and

The `rustc-cfg` instruction tells Cargo to pass the given value to the
[`--cfg` flag][option-cfg] to the compiler. This may be used for compile-time
detection of features to enable [conditional compilation].
detection of features to enable [conditional compilation]. Each custom cfgs
must **always** be declared using the [`cargo::rustc-check-cfg`](#rustc-check-cfg)
instruction.

Note that this does *not* affect Cargo's dependency resolution. This cannot be
used to enable an optional dependency, or enable other Cargo features.
Expand All @@ -250,6 +254,39 @@ identifier, the value should be a string.
[conditional compilation]: ../../reference/conditional-compilation.md
[option-cfg]: ../../rustc/command-line-arguments.md#option-cfg

### `cargo::rustc-check-cfg=CHECK_CFG` {#rustc-check-cfg}

The `rustc-check-cfg` instruction tells Cargo to pass the given expected
configs to the [`--check-cfg` flag][option-check-cfg] to the compiler. This is
used for compile-time detection of unexpected [conditional compilation] configs.

You can use the instruction like this:

```rust,no_run
// build.rs
println!("cargo::rustc-check-cfg=cfg(foo, values(\"bar\"))");
```

Be aware that the [`--check-cfg` flag][option-check-cfg] expected all possible
configs, regardless of which is currently enabled to **always** be passed.

One way to achieve this is to group the check-cfg and cfg instruction as much as
possible.

<details>

```rust,no_run
// build.rs
println!("cargo::rustc-check-cfg=cfg(foo, values(\"bar\"))");
if foo_bar_condition {
println!("cargo::rustc-cfg=foo=\"bar\"");
}
```

</details>

[option-cfg]: ../../rustc/command-line-arguments.md#option-check-cfg

### `cargo::rustc-env=VAR=VALUE` {#rustc-env}

The `rustc-env` instruction tells Cargo to set the given environment variable
Expand Down
39 changes: 0 additions & 39 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ For the latest nightly, see the [nightly version] of this page.
* [build-std-features](#build-std-features) --- Sets features to use with the standard library.
* [binary-dep-depinfo](#binary-dep-depinfo) --- Causes the dep-info file to track binary dependencies.
* [panic-abort-tests](#panic-abort-tests) --- Allows running tests with the "abort" panic strategy.
* [check-cfg](#check-cfg) --- Compile-time validation of `cfg` expressions.
* [host-config](#host-config) --- Allows setting `[target]`-like configuration settings for host build targets.
* [target-applies-to-host](#target-applies-to-host) --- Alters whether certain flags will be passed to host build targets.
* [gc](#gc) --- Global cache garbage collection.
Expand Down Expand Up @@ -1126,44 +1125,6 @@ You can use the flag like this:
cargo rustdoc -Z unstable-options --output-format json
```

## check-cfg

* RFC: [#3013](https://github.com/rust-lang/rfcs/pull/3013)
* Tracking Issue: [#10554](https://github.com/rust-lang/cargo/issues/10554)

`-Z check-cfg` command line enables compile time checking of Cargo features as well as `rustc`
well known names and values in `#[cfg]`, `cfg!`, `#[link]` and `#[cfg_attr]` with the `rustc`
and `rustdoc` unstable `--check-cfg` command line.

You can use the flag like this:

```
cargo check -Z unstable-options -Z check-cfg
```

### `cargo::rustc-check-cfg=CHECK_CFG`

The `rustc-check-cfg` instruction tells Cargo to pass the given value to the
`--check-cfg` flag to the compiler. This may be used for compile-time
detection of unexpected conditional compilation name and/or values.

This can only be used in combination with `-Zcheck-cfg` otherwise it is ignored
with a warning.

If you want to integrate with Cargo features, only use `-Zcheck-cfg` instead of
trying to do it manually with this option.

You can use the instruction like this:

```rust,no_run
// build.rs
println!("cargo::rustc-check-cfg=cfg(foo, bar)");
```

```
cargo check -Z unstable-options -Z check-cfg
```

## codegen-backend

The `codegen-backend` feature makes it possible to select the codegen backend used by rustc using a profile.
Expand Down

0 comments on commit fbba427

Please sign in to comment.