Skip to content

Commit 7b2af37

Browse files
committed
Add "--cfg docsrs" to all rustdoc invocations
1 parent d5a652f commit 7b2af37

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

crates/metadata/lib.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ impl Metadata {
267267
cargo_args.push("--no-default-features".into());
268268
}
269269

270-
let mut all_rustdoc_args = self.rustdoc_args.clone();
270+
let mut all_rustdoc_args = vec!["--cfg".into(), "docsrs".into()];
271+
all_rustdoc_args.extend_from_slice(&self.rustdoc_args);
271272
all_rustdoc_args.extend_from_slice(rustdoc_args);
272273

273-
if !self.rustc_args.is_empty() || !all_rustdoc_args.is_empty() {
274+
if !self.rustc_args.is_empty() || all_rustdoc_args.len() > 2 {
274275
cargo_args.push("-Z".into());
275276
cargo_args.push("unstable-options".into());
276277
}
@@ -655,14 +656,20 @@ mod test_targets {
655656
mod test_calculations {
656657
use super::*;
657658

658-
fn default_cargo_args() -> Vec<String> {
659-
vec!["rustdoc".into(), "--lib".into(), "-Zrustdoc-map".into()]
659+
fn default_cargo_args(extra_args: &[String]) -> Vec<String> {
660+
let mut args = vec!["rustdoc".into(), "--lib".into(), "-Zrustdoc-map".into()];
661+
args.extend_from_slice(extra_args);
662+
args.extend_from_slice(&[
663+
"--config".into(),
664+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
665+
]);
666+
args
660667
}
661668

662669
#[test]
663670
fn test_defaults() {
664671
let metadata = Metadata::default();
665-
assert_eq!(metadata.cargo_args(&[], &[]), default_cargo_args());
672+
assert_eq!(metadata.cargo_args(&[], &[]), default_cargo_args(&[]));
666673
let env = metadata.environment_variables();
667674
assert_eq!(env.get("DOCS_RS").map(String::as_str), Some("1"));
668675
assert!(env.get("RUSTDOCFLAGS").is_none());
@@ -676,17 +683,15 @@ mod test_calculations {
676683
all_features: true,
677684
..Metadata::default()
678685
};
679-
let mut expected_args = default_cargo_args();
680-
expected_args.push("--all-features".into());
686+
let expected_args = default_cargo_args(&["--all-features".into()]);
681687
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
682688

683689
// no default features
684690
let metadata = Metadata {
685691
no_default_features: true,
686692
..Metadata::default()
687693
};
688-
let mut expected_args = default_cargo_args();
689-
expected_args.push("--no-default-features".into());
694+
let expected_args = default_cargo_args(&["--no-default-features".into()]);
690695
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
691696

692697
// allow passing both even though it's nonsense; cargo will give an error anyway
@@ -695,9 +700,8 @@ mod test_calculations {
695700
no_default_features: true,
696701
..Metadata::default()
697702
};
698-
let mut expected_args = default_cargo_args();
699-
expected_args.push("--all-features".into());
700-
expected_args.push("--no-default-features".into());
703+
let expected_args =
704+
default_cargo_args(&["--all-features".into(), "--no-default-features".into()]);
701705
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
702706

703707
// explicit empty vec
@@ -711,6 +715,8 @@ mod test_calculations {
711715
"-Zrustdoc-map".into(),
712716
"--features".into(),
713717
String::new(),
718+
"--config".into(),
719+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
714720
];
715721
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
716722

@@ -725,6 +731,8 @@ mod test_calculations {
725731
"-Zrustdoc-map".into(),
726732
"--features".into(),
727733
"some_feature".into(),
734+
"--config".into(),
735+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
728736
];
729737
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
730738

@@ -739,6 +747,8 @@ mod test_calculations {
739747
"-Zrustdoc-map".into(),
740748
"--features".into(),
741749
"feature1 feature2".into(),
750+
"--config".into(),
751+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
742752
];
743753
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
744754

@@ -761,7 +771,7 @@ mod test_calculations {
761771
"-Z".into(),
762772
"unstable-options".into(),
763773
"--config".into(),
764-
r#"build.rustdocflags=["-Z", "unstable-options", "--static-root-path", "/", "--cap-lints", "warn"]"#.into(),
774+
r#"build.rustdocflags=["--cfg", "docsrs", "-Z", "unstable-options", "--static-root-path", "/", "--cap-lints", "warn"]"#.into(),
765775
];
766776
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
767777

@@ -782,6 +792,8 @@ mod test_calculations {
782792
"-Ztarget-applies-to-host".into(),
783793
"--config".into(),
784794
"host.rustflags=[\"--cfg\", \"x\"]".into(),
795+
"--config".into(),
796+
"build.rustdocflags=[\"--cfg\", \"docsrs\"]".into(),
785797
];
786798
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
787799

@@ -794,6 +806,8 @@ mod test_calculations {
794806
String::from("rustdoc"),
795807
"--lib".into(),
796808
"-Zrustdoc-map".into(),
809+
"--config".into(),
810+
"build.rustdocflags=[\"--cfg\", \"docsrs\"]".into(),
797811
"-Zbuild-std".into(),
798812
];
799813
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);

templates/core/about/builds.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ <h4 id="setting-a-readme"> <a href="#setting-a-readme">Setting a README</a> </h4
2929
</p>
3030

3131
<h4 id="detecting-docsrs"> <a href="#detecting-docsrs">Detecting Docs.rs</a> </h4>
32+
<p>
33+
To recognize Docs.rs from your Rust code, you can test for the <code>docsrs</code> cfg, e.g.:
34+
{% filter highlight(lang="rust") %}{% filter dedent(levels=3) -%}
35+
#[cfg(docsrs)]
36+
mod documentation;
37+
{%- endfilter %}{% endfilter %}
38+
</p>
3239
<p>
3340
To recognize Docs.rs from <code>build.rs</code> files, you can test for the environment variable <code>DOCS_RS</code>, e.g.:
3441
{% filter highlight(lang="rust") %}{% filter dedent(levels=3) -%}
@@ -48,9 +55,9 @@ <h4 id="cross-compiling"> <a href="#cross-compiling">Cross-compiling</a> </h4>
4855
You can configure how your crate is built by adding <a href="metadata">package metadata</a> to your <code>Cargo.toml</code>, e.g.:
4956
{% filter highlight(lang="toml") %}{% filter dedent -%}
5057
[package.metadata.docs.rs]
51-
rustc-args = ["--cfg", "docsrs"]
58+
rustc-args = ["--cfg", "my_cfg"]
5259
{%- endfilter %}{% endfilter %}
53-
Here, the compiler arguments are set so that <code>#[cfg(docsrs)]</code> (not to be confused with <code>#[cfg(doc)]</code>) can be used for conditional compilation.
60+
Here, the compiler arguments are set so that <code>#[cfg(my_cfg)]</code> (not to be confused with <code>#[cfg(doc)]</code>) can be used for conditional compilation.
5461
This approach is also useful for setting <a href="https://doc.rust-lang.org/cargo/reference/features.html">cargo features</a>.
5562
</p>
5663

0 commit comments

Comments
 (0)