Skip to content

Commit b2d4f20

Browse files
committed
Auto merge of #7159 - Aaron1011:feature/rustdoc-proc-macro-final, r=alexcrichton
Pass --crate-type to rustdoc This supports the [corresponding rustc PR](rust-lang/rust#62855). To enable rustdoc to properly document macros, we pass a new flag '--proc-macro-crate' when documenting a proc-macro crate. This causes rustdoc to enable the proc-macro compiler logic that runs when rustc is building a proc-macro crate. This flag is essentially a more restricted version of '--crate-type=proc-macro'. I didn't think it was necessary to pass the full '--crate-type' flag to rustdoc, when only two options would ever be used (proc-macro vs anything else).
2 parents 7ac51b7 + b68e854 commit b2d4f20

File tree

6 files changed

+75
-20
lines changed

6 files changed

+75
-20
lines changed

src/cargo/core/compiler/compilation.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use semver::Version;
77

88
use super::BuildContext;
99
use crate::core::{Edition, InternedString, Package, PackageId, Target};
10-
use crate::util::{self, join_paths, process, CargoResult, CfgExpr, Config, ProcessBuilder};
10+
use crate::util::{
11+
self, join_paths, process, rustc::Rustc, CargoResult, CfgExpr, Config, ProcessBuilder,
12+
};
1113

1214
pub struct Doctest {
1315
/// The package being doc-tested.
@@ -73,6 +75,7 @@ pub struct Compilation<'cfg> {
7375
primary_unit_rustc_process: Option<ProcessBuilder>,
7476

7577
target_runner: Option<(PathBuf, Vec<String>)>,
78+
supports_rustdoc_crate_type: bool,
7679
}
7780

7881
impl<'cfg> Compilation<'cfg> {
@@ -109,6 +112,7 @@ impl<'cfg> Compilation<'cfg> {
109112
host: bcx.host_triple().to_string(),
110113
target: bcx.target_triple().to_string(),
111114
target_runner: target_runner(bcx)?,
115+
supports_rustdoc_crate_type: supports_rustdoc_crate_type(bcx.config, &bcx.rustc)?,
112116
})
113117
}
114118

@@ -140,6 +144,13 @@ impl<'cfg> Compilation<'cfg> {
140144
if target.edition() != Edition::Edition2015 {
141145
p.arg(format!("--edition={}", target.edition()));
142146
}
147+
148+
if self.supports_rustdoc_crate_type {
149+
for crate_type in target.rustc_crate_types() {
150+
p.arg("--crate-type").arg(crate_type);
151+
}
152+
}
153+
143154
Ok(p)
144155
}
145156

@@ -308,3 +319,14 @@ fn target_runner(bcx: &BuildContext<'_, '_>) -> CargoResult<Option<(PathBuf, Vec
308319

309320
Ok(None)
310321
}
322+
323+
fn supports_rustdoc_crate_type(config: &Config, rustc: &Rustc) -> CargoResult<bool> {
324+
// NOTE: Unconditionally return 'true' once support for
325+
// rustdoc '--crate-type' rides to stable
326+
let mut crate_type_test = process(config.rustdoc()?);
327+
// If '--crate-type' is not supported by rustcoc, this command
328+
// will exit with an error. Otherwise, it will print a help message,
329+
// and exit successfully
330+
crate_type_test.args(&["--crate-type", "proc-macro", "--help"]);
331+
Ok(rustc.cached_output(&crate_type_test).is_ok())
332+
}

tests/testsuite/build_script.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ fn testing_and_such() {
813813
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
814814
[RUNNING] `[..]/foo-[..][EXE]`
815815
[DOCTEST] foo
816-
[RUNNING] `rustdoc --test [..]`",
816+
[RUNNING] `rustdoc [..]--test [..]`",
817817
)
818818
.with_stdout_contains_n("running 0 tests", 2)
819819
.run();
@@ -2747,7 +2747,7 @@ fn doctest_receives_build_link_args() {
27472747

27482748
p.cargo("test -v")
27492749
.with_stderr_contains(
2750-
"[RUNNING] `rustdoc --test [..] --crate-name foo [..]-L native=bar[..]`",
2750+
"[RUNNING] `rustdoc [..]--test [..] --crate-name foo [..]-L native=bar[..]`",
27512751
)
27522752
.run();
27532753
}

tests/testsuite/profile_targets.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ fn profile_selection_test() {
306306
[RUNNING] `[..]/deps/foo-[..]`
307307
[RUNNING] `[..]/deps/test1-[..]`
308308
[DOCTEST] foo
309-
[RUNNING] `rustdoc --test [..]
309+
[RUNNING] `rustdoc [..]--test [..]
310310
").run();
311311
p.cargo("test -vv")
312312
.with_stderr_unordered(
@@ -319,7 +319,7 @@ fn profile_selection_test() {
319319
[RUNNING] `[..]/deps/foo-[..]`
320320
[RUNNING] `[..]/deps/test1-[..]`
321321
[DOCTEST] foo
322-
[RUNNING] `rustdoc --test [..]
322+
[RUNNING] `rustdoc [..]--test [..]
323323
",
324324
)
325325
.run();
@@ -371,7 +371,7 @@ fn profile_selection_test_release() {
371371
[RUNNING] `[..]/deps/foo-[..]`
372372
[RUNNING] `[..]/deps/test1-[..]`
373373
[DOCTEST] foo
374-
[RUNNING] `rustdoc --test [..]`
374+
[RUNNING] `rustdoc [..]--test [..]`
375375
").run();
376376
p.cargo("test --release -vv")
377377
.with_stderr_unordered(
@@ -384,7 +384,7 @@ fn profile_selection_test_release() {
384384
[RUNNING] `[..]/deps/foo-[..]`
385385
[RUNNING] `[..]/deps/test1-[..]`
386386
[DOCTEST] foo
387-
[RUNNING] `rustdoc --test [..]
387+
[RUNNING] `rustdoc [..]--test [..]
388388
",
389389
)
390390
.run();
@@ -633,7 +633,7 @@ fn profile_selection_doc() {
633633
[COMPILING] bar [..]
634634
[DOCUMENTING] bar [..]
635635
[RUNNING] `[..] rustc --crate-name bar bar/src/lib.rs [..]--crate-type lib --emit=[..]link -C codegen-units=1 -C debuginfo=2 [..]
636-
[RUNNING] `rustdoc --crate-name bar bar/src/lib.rs [..]
636+
[RUNNING] `rustdoc [..]--crate-name bar bar/src/lib.rs [..]
637637
[RUNNING] `[..] rustc --crate-name bar bar/src/lib.rs [..]--crate-type lib --emit=[..]metadata -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]
638638
[COMPILING] bdep [..]
639639
[RUNNING] `[..] rustc --crate-name bdep bdep/src/lib.rs [..]--crate-type lib --emit=[..]link -C codegen-units=1 -C debuginfo=2 [..]
@@ -642,7 +642,7 @@ fn profile_selection_doc() {
642642
[RUNNING] `[..]target/debug/build/foo-[..]/build-script-build`
643643
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
644644
[DOCUMENTING] foo [..]
645-
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]
645+
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]
646646
[FINISHED] dev [unoptimized + debuginfo] [..]
647647
").run();
648648
}

tests/testsuite/rename_deps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn can_run_doc_tests() {
263263
.with_stderr_contains(
264264
"\
265265
[DOCTEST] foo
266-
[RUNNING] `rustdoc --test [CWD]/src/lib.rs \
266+
[RUNNING] `rustdoc [..]--test [CWD]/src/lib.rs \
267267
[..] \
268268
--extern bar=[CWD]/target/debug/deps/libbar-[..].rlib \
269269
--extern baz=[CWD]/target/debug/deps/libbar-[..].rlib \

tests/testsuite/rustdoc.rs

+40-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::support::{basic_manifest, project};
1+
use crate::support::{basic_manifest, is_nightly, project};
22

33
#[cargo_test]
44
fn rustdoc_simple() {
@@ -8,7 +8,7 @@ fn rustdoc_simple() {
88
.with_stderr(
99
"\
1010
[DOCUMENTING] foo v0.0.1 ([CWD])
11-
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
11+
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
1212
-o [CWD]/target/doc \
1313
[..] \
1414
-L dependency=[CWD]/target/debug/deps`
@@ -26,7 +26,7 @@ fn rustdoc_args() {
2626
.with_stderr(
2727
"\
2828
[DOCUMENTING] foo v0.0.1 ([CWD])
29-
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
29+
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
3030
-o [CWD]/target/doc \
3131
[..] \
3232
--cfg=foo \
@@ -66,7 +66,7 @@ fn rustdoc_foo_with_bar_dependency() {
6666
[CHECKING] bar v0.0.1 ([..])
6767
[RUNNING] `rustc [..]bar/src/lib.rs [..]`
6868
[DOCUMENTING] foo v0.0.1 ([CWD])
69-
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
69+
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
7070
-o [CWD]/target/doc \
7171
[..] \
7272
--cfg=foo \
@@ -105,7 +105,7 @@ fn rustdoc_only_bar_dependency() {
105105
.with_stderr(
106106
"\
107107
[DOCUMENTING] bar v0.0.1 ([..])
108-
[RUNNING] `rustdoc --crate-name bar [..]bar/src/lib.rs [..]\
108+
[RUNNING] `rustdoc [..]--crate-name bar [..]bar/src/lib.rs [..]\
109109
-o [CWD]/target/doc \
110110
[..] \
111111
--cfg=foo \
@@ -127,7 +127,7 @@ fn rustdoc_same_name_documents_lib() {
127127
.with_stderr(
128128
"\
129129
[DOCUMENTING] foo v0.0.1 ([..])
130-
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
130+
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
131131
-o [CWD]/target/doc \
132132
[..] \
133133
--cfg=foo \
@@ -161,6 +161,39 @@ fn features() {
161161
.run();
162162
}
163163

164+
#[cargo_test]
165+
fn proc_macro_crate_type() {
166+
// NOTE - Remove this once 'rustdoc --crate-type'
167+
// rides to stable
168+
if !is_nightly() {
169+
return;
170+
}
171+
let p = project()
172+
.file(
173+
"Cargo.toml",
174+
r#"
175+
[package]
176+
name = "foo"
177+
version = "0.0.1"
178+
authors = []
179+
180+
[lib]
181+
proc-macro = true
182+
183+
"#,
184+
)
185+
.file("src/lib.rs", "")
186+
.build();
187+
188+
p.cargo("rustdoc --verbose")
189+
.with_stderr_contains(
190+
"\
191+
[RUNNING] `rustdoc --crate-type proc-macro [..]`
192+
",
193+
)
194+
.run();
195+
}
196+
164197
#[cargo_test]
165198
#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))]
166199
fn rustdoc_target() {
@@ -170,7 +203,7 @@ fn rustdoc_target() {
170203
.with_stderr(
171204
"\
172205
[DOCUMENTING] foo v0.0.1 ([..])
173-
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
206+
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
174207
--target x86_64-unknown-linux-gnu \
175208
-o [CWD]/target/x86_64-unknown-linux-gnu/doc \
176209
[..] \

tests/testsuite/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn cargo_test_release() {
9797
[RUNNING] `[..]target/release/deps/foo-[..][EXE]`
9898
[RUNNING] `[..]target/release/deps/test-[..][EXE]`
9999
[DOCTEST] foo
100-
[RUNNING] `rustdoc --test [..]lib.rs[..]`",
100+
[RUNNING] `rustdoc [..]--test [..]lib.rs[..]`",
101101
)
102102
.with_stdout_contains_n("test test ... ok", 2)
103103
.with_stdout_contains("running 0 tests")
@@ -2702,15 +2702,15 @@ fn pass_correct_cfgs_flags_to_rustdoc() {
27022702
.with_stderr_contains(
27032703
"\
27042704
[DOCTEST] feature_a
2705-
[RUNNING] `rustdoc --test [..]mock_serde_codegen[..]`",
2705+
[RUNNING] `rustdoc [..]--test [..]mock_serde_codegen[..]`",
27062706
)
27072707
.run();
27082708

27092709
p.cargo("test --verbose")
27102710
.with_stderr_contains(
27112711
"\
27122712
[DOCTEST] foo
2713-
[RUNNING] `rustdoc --test [..]feature_a[..]`",
2713+
[RUNNING] `rustdoc [..]--test [..]feature_a[..]`",
27142714
)
27152715
.run();
27162716
}

0 commit comments

Comments
 (0)