|
| 1 | +# Cargo specifics - Checking conditional configurations |
| 2 | + |
| 3 | +<!-- |
| 4 | +This page is the canonical place for describing the interaction between Cargo |
| 5 | +and --check-cfg. It is placed in the rustc book rather than the Cargo book since |
| 6 | +check-cfg is primarely a Rust/rustc feature, at least --check-cfg and the |
| 7 | +unexpected_cfgs are owned by rustc, not Cargo. |
| 8 | +--> |
| 9 | + |
| 10 | +> This document is intented to summarize the principal ways Cargo interacts with |
| 11 | +> the `unexpected_cfgs` lint and `--check-cfg` flag. It is not intended to provide |
| 12 | +> individual details, for that refer to the [`--check-cfg` documentation](../check-cfg.md) and |
| 13 | +> to the [Cargo book](../../cargo/index.html). |
| 14 | +
|
| 15 | +Cargo always enables [checking conditional configurations](../check-cfg.md) with Rust 1.80 and upper. |
| 16 | + |
| 17 | +This is to help with verifying that the crate is correctly handling conditional compilation for |
| 18 | +different target platforms or features. It ensures that the cfg settings are consistent between |
| 19 | +what is intended and what is used, helping to catch potential bugs or errors early in the |
| 20 | +development process. |
| 21 | + |
| 22 | +This document express different ways users can declare expected cfgs, and in particular for |
| 23 | +custom configs[^1] with Cargo as the build system. |
| 24 | + |
| 25 | +[^1]: In Cargo point-of-view: a custom cfg is one that is neither defined by `rustc` nor by a Cargo |
| 26 | +feature. Think of `tokio_unstable`, `has_foo`, ... but not `feature = "lasers"`, `unix` or |
| 27 | +`debug_assertions` |
| 28 | + |
| 29 | +## Cargo feature |
| 30 | + |
| 31 | +*See the [`[features]` section in the Cargo book][cargo-features] for more details.* |
| 32 | + |
| 33 | +With the `[features]` table Cargo provide a mechanism to express conditional compilation and |
| 34 | +optional dependencies. Cargo *automatically* declares corresponding cfgs for every feature as |
| 35 | +expected. |
| 36 | + |
| 37 | +`Cargo.toml`: |
| 38 | +```toml |
| 39 | +[features] |
| 40 | +serde = ["dep:serde"] |
| 41 | +my_feature = [] |
| 42 | +``` |
| 43 | + |
| 44 | +[cargo-features]: ../../cargo/reference/features.html |
| 45 | + |
| 46 | +## `check-cfg` in `[lints.rust]` table |
| 47 | + |
| 48 | +<!-- Note that T-Cargo considers `[lints.rust.unexpected_cfgs.check-cfg]` to be an |
| 49 | +implementation detail and is therefor not documented in Cargo, we therefor do that ourself --> |
| 50 | + |
| 51 | +*See the [`[lints]` section in the Cargo book][cargo-lints-table] for more details.* |
| 52 | + |
| 53 | +When using a custom config not set via a build-script, Cargo provides the custom lint config |
| 54 | +`check-cfg` under `[lints.rust.unexpected_cfgs]`. |
| 55 | + |
| 56 | +It can be used to set custom static [`--check-cfg`](../check-cfg.md) args, it is mainly useful when |
| 57 | +the list of expected cfgs is known is advance. |
| 58 | + |
| 59 | +`Cargo.toml`: |
| 60 | +```toml |
| 61 | +[lints.rust] |
| 62 | +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] } |
| 63 | + |
| 64 | +# or |
| 65 | + |
| 66 | +[lints.rust.unexpected_cfgs] |
| 67 | +level = "warn" |
| 68 | +check-cfg = ['cfg(has_foo)'] |
| 69 | +``` |
| 70 | + |
| 71 | +[cargo-lints-table]: ../../cargo/reference/manifest.html#the-lints-section |
| 72 | + |
| 73 | +## `cargo::rustc-check-cfg` for `build.rs`/build-script |
| 74 | + |
| 75 | +*See the [`cargo::rustc-check-cfg` section in the Cargo book][cargo-rustc-check-cfg] for more details.* |
| 76 | + |
| 77 | +When setting a custom config with [`cargo::rustc-cfg`][cargo-rustc-cfg], Cargo provides the |
| 78 | +corrolary instruction: [`cargo::rustc-check-cfg`][cargo-rustc-check-cfg] to expect custom configs. |
| 79 | + |
| 80 | +`build.rs`: |
| 81 | +```rust,ignore (cannot-test-this-because-has_foo-isnt-declared) |
| 82 | +fn main() { |
| 83 | + println!("cargo::rustc-check-cfg=cfg(has_foo)"); |
| 84 | + // ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80 |
| 85 | + if has_foo() { |
| 86 | + println!("cargo::rustc-cfg=has_foo"); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +> Each `cargo::rustc-cfg` should have an accompanying **unconditional** `cargo::rustc-check-cfg` |
| 92 | +> directive to avoid warnings like this: `unexpected cfg condition name: has_foo`. |
| 93 | +
|
| 94 | +[cargo-rustc-cfg]: ../../cargo/reference/build-scripts.html#rustc-cfg |
| 95 | +[cargo-rustc-check-cfg]: ../../cargo/reference/build-scripts.html#rustc-check-cfg |
0 commit comments