Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,15 +870,15 @@ fn rustflags_from_target(
if let Some(target_cfg) = target_cfg {
gctx.target_cfgs()?
.iter()
.filter_map(|(key, cfg)| {
match flag {
Flags::Rust => cfg
.rustflags
.as_ref()
.map(|rustflags| (key, &rustflags.val)),
// `target.cfg(…).rustdocflags` is currently not supported.
Flags::Rustdoc => None,
}
.filter_map(|(key, cfg)| match flag {
Flags::Rust => cfg
.rustflags
.as_ref()
.map(|rustflags| (key, &rustflags.val)),
Flags::Rustdoc => cfg
.rustdocflags
.as_ref()
.map(|rustdocflags| (key, &rustdocflags.val)),
})
.filter(|(key, _rustflags)| CfgExpr::matches_key(key, target_cfg))
.for_each(|(_key, cfg_rustflags)| {
Expand Down
1 change: 1 addition & 0 deletions src/cargo/util/context/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::rc::Rc;
pub struct TargetCfgConfig {
pub runner: OptValue<PathAndArgs>,
pub rustflags: OptValue<StringList>,
pub rustdocflags: OptValue<StringList>,
pub linker: OptValue<ConfigRelativePath>,
// This is here just to ignore fields from normal `TargetConfig` because
// all `[target]` tables are getting deserialized, whether they start with
Expand Down
9 changes: 8 additions & 1 deletion src/doc/src/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ order, with the first one being used:

1. `CARGO_ENCODED_RUSTDOCFLAGS` environment variable.
2. `RUSTDOCFLAGS` environment variable.
3. All matching `target.<triple>.rustdocflags` config entries joined together.
3. All matching `target.<triple>.rustdocflags` and `target.<cfg>.rustdocflags`
config entries joined together.
4. `build.rustdocflags` config value.

Additional flags may also be passed with the [`cargo rustdoc`] command.
Expand Down Expand Up @@ -1394,6 +1395,12 @@ The value may be an array of strings or a space-separated string.
See [`build.rustdocflags`](#buildrustdocflags) for more details on the different
ways to specific extra flags.

#### `target.<cfg>.rustdocflags`

This is similar to the [target rustdocflags](#targettriplerustdocflags), but
using a [`cfg()` expression]. If several `<cfg>` and [`<triple>`] entries
match the current target, the flags are joined together.

#### `target.<triple>.<links>`

The links sub-table provides a way to [override a build script]. When
Expand Down
66 changes: 66 additions & 0 deletions tests/testsuite/rustdocflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,72 @@ fn not_affected_by_target_rustflags() {
.run();
}

#[cargo_test]
fn target_cfg_rustdocflags_works() {
let p = project()
.file("src/lib.rs", "")
.file(
".cargo/config.toml",
r#"
Comment thread
weihanglo marked this conversation as resolved.
[target.'cfg(true)']
rustdocflags = ["--cfg", "from_target_cfg"]

[build]
rustdocflags = ["--cfg", "from_build"]
"#,
)
.build();

p.cargo("doc -v")
.with_stderr_data(str![[r#"
...
[RUNNING] `rustdoc [..] --cfg from_target_cfg[..]`
...
"#]])
.run();
}

#[cargo_test]
fn target_cfg_rustdocflags_works_through_cargo_test() {
let p = project()
.file(
"src/lib.rs",
r#"
//! ```
//! assert!(cfg!(from_target_cfg));
//! ```
"#,
)
.file(
".cargo/config.toml",
r#"
[target.'cfg(true)']
rustdocflags = ["--cfg", "from_target_cfg"]

[build]
rustdocflags = ["--cfg", "from_build"]
"#,
)
.build();

p.cargo("test --doc -v")
.with_stderr_data(str![[r#"
...
[RUNNING] `rustdoc[..]--test[..]--cfg[..]from_target_cfg[..]`

"#]])
.with_stdout_data(str![[r#"

running 1 test
test src/lib.rs - (line 2) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in [ELAPSED]s


"#]])
.run();
}

#[cargo_test]
fn target_triple_rustdocflags_works() {
let host = rustc_host();
Expand Down