Skip to content

Commit b5ec9bb

Browse files
committed
Add Cargo specific doc regarding his interaction with --check-cfg
1 parent 7cb84fb commit b5ec9bb

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/doc/rustc/src/SUMMARY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
- [Profile-guided Optimization](profile-guided-optimization.md)
8585
- [Instrumentation-based Code Coverage](instrument-coverage.md)
8686
- [Linker-plugin-based LTO](linker-plugin-lto.md)
87-
- [Checking conditional configurations](check-cfg.md)
87+
- [Checking Conditional Configurations](check-cfg.md)
88+
- [Cargo Specifics](check-cfg/cargo-specifics.md)
8889
- [Exploit Mitigations](exploit-mitigations.md)
8990
- [Symbol Mangling](symbol-mangling/index.md)
9091
- [v0 Symbol Format](symbol-mangling/v0.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Cargo Specifics - Checking Conditional Configurations
2+
3+
<!--
4+
This page is currently (as of May 2024) the canonical place for describing the interaction
5+
between Cargo and --check-cfg. It is placed in the rustc book rather than the Cargo book
6+
since check-cfg is primarely a Rust/rustc feature and is therefor consider by T-cargo to
7+
be an implementation detail, at least --check-cfg and the unexpected_cfgs are owned by
8+
rustc, not Cargo.
9+
-->
10+
11+
This document is intented to summarize the principal ways Cargo interacts with
12+
the `unexpected_cfgs` lint and `--check-cfg` flag. It is not intended to provide
13+
individual details, for that refer to the [`--check-cfg` documentation](../check-cfg.md) and
14+
to the [Cargo book](../../cargo/index.html).
15+
16+
## Cargo feature
17+
18+
*See the [`[features]` section in the Cargo book][cargo-features] for more details.*
19+
20+
With the `[features]` table Cargo provides a mechanism to express conditional compilation and
21+
optional dependencies. Cargo *automatically* declares corresponding cfgs for every feature as
22+
expected.
23+
24+
`Cargo.toml`:
25+
```toml
26+
[features]
27+
serde = ["dep:serde"]
28+
my_feature = []
29+
```
30+
31+
[cargo-features]: ../../cargo/reference/features.html
32+
33+
## `check-cfg` in `[lints.rust]` table
34+
35+
<!-- Note that T-Cargo considers `[lints.rust.unexpected_cfgs.check-cfg]` to be an
36+
implementation detail and is therefor not documented in Cargo, we therefor do that ourself -->
37+
38+
*See the [`[lints]` section in the Cargo book][cargo-lints-table] for more details.*
39+
40+
When using a staticlly known custom config (ie. not dependant on a build-script), Cargo provides
41+
the custom lint config `check-cfg` under `[lints.rust.unexpected_cfgs]`.
42+
43+
It can be used to set custom static [`--check-cfg`](../check-cfg.md) args, it is mainly useful when
44+
the list of expected cfgs is known is advance.
45+
46+
`Cargo.toml`:
47+
```toml
48+
[lints.rust]
49+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] }
50+
```
51+
52+
[cargo-lints-table]: ../../cargo/reference/manifest.html#the-lints-section
53+
54+
## `cargo::rustc-check-cfg` for `build.rs`/build-script
55+
56+
*See the [`cargo::rustc-check-cfg` section in the Cargo book][cargo-rustc-check-cfg] for more details.*
57+
58+
When setting a custom config with [`cargo::rustc-cfg`][cargo-rustc-cfg], Cargo provides the
59+
corollary instruction: [`cargo::rustc-check-cfg`][cargo-rustc-check-cfg] to expect custom configs.
60+
61+
`build.rs`:
62+
```rust,ignore (cannot-test-this-because-has_foo-isnt-declared)
63+
fn main() {
64+
println!("cargo::rustc-check-cfg=cfg(has_foo)");
65+
// ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80
66+
if has_foo() {
67+
println!("cargo::rustc-cfg=has_foo");
68+
}
69+
}
70+
```
71+
72+
[cargo-rustc-cfg]: ../../cargo/reference/build-scripts.html#rustc-cfg
73+
[cargo-rustc-check-cfg]: ../../cargo/reference/build-scripts.html#rustc-check-cfg

0 commit comments

Comments
 (0)