Skip to content

Commit 3d47dd4

Browse files
committed
Auto merge of #13197 - weihanglo:target-triple-rustdocflags, r=epage
feat: support `target.<triple>.rustdocflags` officially
2 parents be41d1e + 6131e99 commit 3d47dd4

File tree

4 files changed

+107
-17
lines changed

4 files changed

+107
-17
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ impl Flags {
698698
/// For those artifacts, _only_ `host.*.rustflags` is respected, and no other configuration
699699
/// sources, _regardless of the value of `target-applies-to-host`_. This is counterintuitive, but
700700
/// necessary to retain backwards compatibility with older versions of Cargo.
701+
///
702+
/// Rules above also applies to rustdoc. Just the key would be `rustdocflags`/`RUSTDOCFLAGS`.
701703
fn extra_args(
702704
gctx: &GlobalContext,
703705
requested_kinds: &[CompileKind],
@@ -800,7 +802,6 @@ fn rustflags_from_target(
800802
.as_ref()
801803
.map(|rustflags| (key, &rustflags.val)),
802804
// `target.cfg(…).rustdocflags` is currently not supported.
803-
// In fact, neither is `target.<triple>.rustdocflags`.
804805
Flags::Rustdoc => None,
805806
}
806807
})

src/cargo/util/config/target.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ pub struct TargetCfgConfig {
2121
}
2222

2323
/// Config definition of a `[target]` table or `[host]`.
24-
#[derive(Debug, Clone)]
24+
#[derive(Debug, Clone, Default)]
2525
pub struct TargetConfig {
2626
/// Process to run as a wrapper for `cargo run`, `test`, and `bench` commands.
2727
pub runner: OptValue<PathAndArgs>,
2828
/// Additional rustc flags to pass.
2929
pub rustflags: OptValue<StringList>,
30+
/// Additional rustdoc flags to pass.
31+
pub rustdocflags: OptValue<StringList>,
3032
/// The path of the linker for this target.
3133
pub linker: OptValue<ConfigRelativePath>,
3234
/// Build script override for the given library name.
@@ -95,12 +97,7 @@ pub(super) fn load_host_triple(gctx: &GlobalContext, triple: &str) -> CargoResul
9597
};
9698
load_config_table(gctx, &host_prefix)
9799
} else {
98-
Ok(TargetConfig {
99-
runner: None,
100-
rustflags: None,
101-
linker: None,
102-
links_overrides: BTreeMap::new(),
103-
})
100+
Ok(TargetConfig::default())
104101
}
105102
}
106103

@@ -116,9 +113,10 @@ fn load_config_table(gctx: &GlobalContext, prefix: &str) -> CargoResult<TargetCo
116113
// because it causes serde to use `deserialize_map` which means the config
117114
// deserializer does not know which keys to deserialize, which means
118115
// environment variables would not work.
119-
let runner: OptValue<PathAndArgs> = gctx.get(&format!("{}.runner", prefix))?;
120-
let rustflags: OptValue<StringList> = gctx.get(&format!("{}.rustflags", prefix))?;
121-
let linker: OptValue<ConfigRelativePath> = gctx.get(&format!("{}.linker", prefix))?;
116+
let runner: OptValue<PathAndArgs> = gctx.get(&format!("{prefix}.runner"))?;
117+
let rustflags: OptValue<StringList> = gctx.get(&format!("{prefix}.rustflags"))?;
118+
let rustdocflags: OptValue<StringList> = gctx.get(&format!("{prefix}.rustdocflags"))?;
119+
let linker: OptValue<ConfigRelativePath> = gctx.get(&format!("{prefix}.linker"))?;
122120
// Links do not support environment variables.
123121
let target_key = ConfigKey::from_str(prefix);
124122
let links_overrides = match gctx.get_table(&target_key)? {
@@ -128,6 +126,7 @@ fn load_config_table(gctx: &GlobalContext, prefix: &str) -> CargoResult<TargetCo
128126
Ok(TargetConfig {
129127
runner,
130128
rustflags,
129+
rustdocflags,
131130
linker,
132131
links_overrides,
133132
})
@@ -144,7 +143,7 @@ fn parse_links_overrides(
144143
// Skip these keys, it shares the namespace with `TargetConfig`.
145144
match lib_name.as_str() {
146145
// `ar` is a historical thing.
147-
"ar" | "linker" | "runner" | "rustflags" => continue,
146+
"ar" | "linker" | "runner" | "rustflags" | "rustdocflags" => continue,
148147
_ => {}
149148
}
150149
let mut output = BuildOutput::default();

src/doc/src/reference/config.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ tag = "…" # tag name for the git repository
158158
rev = "" # revision for the git repository
159159

160160
[target.<triple>]
161-
linker = "" # linker to use
162-
runner = "" # wrapper to run executables
163-
rustflags = ["", ""] # custom flags for `rustc`
161+
linker = "" # linker to use
162+
runner = "" # wrapper to run executables
163+
rustflags = ["", ""] # custom flags for `rustc`
164+
rustdocflags = ["", ""] # custom flags for `rustdoc`
164165

165166
[target.<cfg>]
166167
runner = "" # wrapper to run executables
@@ -500,15 +501,22 @@ appropriate profile setting.
500501
Extra command-line flags to pass to `rustdoc`. The value may be an array of
501502
strings or a space-separated string.
502503

503-
There are three mutually exclusive sources of extra flags. They are checked in
504+
There are four mutually exclusive sources of extra flags. They are checked in
504505
order, with the first one being used:
505506

506507
1. `CARGO_ENCODED_RUSTDOCFLAGS` environment variable.
507508
2. `RUSTDOCFLAGS` environment variable.
508-
3. `build.rustdocflags` config value.
509+
3. All matching `target.<triple>.rustdocflags` config entries joined together.
510+
4. `build.rustdocflags` config value.
509511

510512
Additional flags may also be passed with the [`cargo rustdoc`] command.
511513

514+
> **Caution**: Due to the low-level nature of passing flags directly to the
515+
> compiler, this may cause a conflict with future versions of Cargo which may
516+
> issue the same or similar flags on its own which may interfere with the
517+
> flags you specify. This is an area where Cargo may not always be backwards
518+
> compatible.
519+
512520
#### `build.incremental`
513521
* Type: bool
514522
* Default: from profile
@@ -1216,6 +1224,17 @@ This is similar to the [target rustflags](#targettriplerustflags), but
12161224
using a [`cfg()` expression]. If several `<cfg>` and [`<triple>`] entries
12171225
match the current target, the flags are joined together.
12181226

1227+
#### `target.<triple>.rustdocflags`
1228+
* Type: string or array of strings
1229+
* Default: none
1230+
* Environment: `CARGO_TARGET_<triple>_RUSTDOCFLAGS`
1231+
1232+
Passes a set of custom flags to the compiler for this [`<triple>`].
1233+
The value may be an array of strings or a space-separated string.
1234+
1235+
See [`build.rustdocflags`](#buildrustdocflags) for more details on the different
1236+
ways to specific extra flags.
1237+
12191238
#### `target.<triple>.<links>`
12201239

12211240
The links sub-table provides a way to [override a build script]. When

tests/testsuite/rustdocflags.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Tests for setting custom rustdoc flags.
22
33
use cargo_test_support::project;
4+
use cargo_test_support::rustc_host;
5+
use cargo_test_support::rustc_host_env;
46

57
#[cargo_test]
68
fn parses_env() {
@@ -159,3 +161,72 @@ fn not_affected_by_target_rustflags() {
159161
.with_stderr_contains("[RUNNING] `rustdoc [..] --cfg foo[..]`")
160162
.run();
161163
}
164+
165+
#[cargo_test]
166+
fn target_triple_rustdocflags_works() {
167+
let host = rustc_host();
168+
let host_env = rustc_host_env();
169+
let p = project().file("src/lib.rs", "").build();
170+
171+
// target.triple.rustdocflags in env works
172+
p.cargo("doc -v")
173+
.env(
174+
&format!("CARGO_TARGET_{host_env}_RUSTDOCFLAGS"),
175+
"--cfg=foo",
176+
)
177+
.with_stderr_contains("[RUNNING] `rustdoc[..]--cfg[..]foo[..]`")
178+
.run();
179+
180+
// target.triple.rustdocflags in config works
181+
p.cargo("doc -v")
182+
.arg("--config")
183+
.arg(format!("target.{host}.rustdocflags=['--cfg', 'foo']"))
184+
.with_stderr_contains("[RUNNING] `rustdoc[..]--cfg[..]foo[..]`")
185+
.run();
186+
}
187+
188+
#[cargo_test]
189+
fn target_triple_rustdocflags_works_through_cargo_test() {
190+
let host = rustc_host();
191+
let host_env = rustc_host_env();
192+
let p = project()
193+
.file(
194+
"src/lib.rs",
195+
r#"
196+
//! ```
197+
//! assert!(cfg!(foo));
198+
//! ```
199+
"#,
200+
)
201+
.build();
202+
203+
// target.triple.rustdocflags in env works
204+
p.cargo("test --doc -v")
205+
.env(
206+
&format!("CARGO_TARGET_{host_env}_RUSTDOCFLAGS"),
207+
"--cfg=foo",
208+
)
209+
.with_stderr_contains("[RUNNING] `rustdoc[..]--test[..]--cfg[..]foo[..]`")
210+
.with_stdout_contains(
211+
"\
212+
running 1 test
213+
test src/lib.rs - (line 2) ... ok
214+
215+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]",
216+
)
217+
.run();
218+
219+
// target.triple.rustdocflags in config works
220+
p.cargo("test --doc -v")
221+
.arg("--config")
222+
.arg(format!("target.{host}.rustdocflags=['--cfg', 'foo']"))
223+
.with_stderr_contains("[RUNNING] `rustdoc[..]--test[..]--cfg[..]foo[..]`")
224+
.with_stdout_contains(
225+
"\
226+
running 1 test
227+
test src/lib.rs - (line 2) ... ok
228+
229+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]",
230+
)
231+
.run();
232+
}

0 commit comments

Comments
 (0)