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 10, 2024
1 parent fbaea97 commit aa8db59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
25 changes: 24 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,25 @@ 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)");
```

Note that it is expected to **always** pass the full list of custom configs
used in the package. The recommended way to achieve this is put all the
check-cfg instructions at the top of your `build.rs`.

[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
38 changes: 0 additions & 38 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -1126,44 +1126,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 aa8db59

Please sign in to comment.