Skip to content

Commit

Permalink
Auto merge of #7159 - Aaron1011:feature/rustdoc-proc-macro-final, r=a…
Browse files Browse the repository at this point in the history
…lexcrichton

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).
  • Loading branch information
bors committed Sep 16, 2019
2 parents 7ac51b7 + b68e854 commit b2d4f20
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
24 changes: 23 additions & 1 deletion src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use semver::Version;

use super::BuildContext;
use crate::core::{Edition, InternedString, Package, PackageId, Target};
use crate::util::{self, join_paths, process, CargoResult, CfgExpr, Config, ProcessBuilder};
use crate::util::{
self, join_paths, process, rustc::Rustc, CargoResult, CfgExpr, Config, ProcessBuilder,
};

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

target_runner: Option<(PathBuf, Vec<String>)>,
supports_rustdoc_crate_type: bool,
}

impl<'cfg> Compilation<'cfg> {
Expand Down Expand Up @@ -109,6 +112,7 @@ impl<'cfg> Compilation<'cfg> {
host: bcx.host_triple().to_string(),
target: bcx.target_triple().to_string(),
target_runner: target_runner(bcx)?,
supports_rustdoc_crate_type: supports_rustdoc_crate_type(bcx.config, &bcx.rustc)?,
})
}

Expand Down Expand Up @@ -140,6 +144,13 @@ impl<'cfg> Compilation<'cfg> {
if target.edition() != Edition::Edition2015 {
p.arg(format!("--edition={}", target.edition()));
}

if self.supports_rustdoc_crate_type {
for crate_type in target.rustc_crate_types() {
p.arg("--crate-type").arg(crate_type);
}
}

Ok(p)
}

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

Ok(None)
}

fn supports_rustdoc_crate_type(config: &Config, rustc: &Rustc) -> CargoResult<bool> {
// NOTE: Unconditionally return 'true' once support for
// rustdoc '--crate-type' rides to stable
let mut crate_type_test = process(config.rustdoc()?);
// If '--crate-type' is not supported by rustcoc, this command
// will exit with an error. Otherwise, it will print a help message,
// and exit successfully
crate_type_test.args(&["--crate-type", "proc-macro", "--help"]);
Ok(rustc.cached_output(&crate_type_test).is_ok())
}
4 changes: 2 additions & 2 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ fn testing_and_such() {
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `[..]/foo-[..][EXE]`
[DOCTEST] foo
[RUNNING] `rustdoc --test [..]`",
[RUNNING] `rustdoc [..]--test [..]`",
)
.with_stdout_contains_n("running 0 tests", 2)
.run();
Expand Down Expand Up @@ -2747,7 +2747,7 @@ fn doctest_receives_build_link_args() {

p.cargo("test -v")
.with_stderr_contains(
"[RUNNING] `rustdoc --test [..] --crate-name foo [..]-L native=bar[..]`",
"[RUNNING] `rustdoc [..]--test [..] --crate-name foo [..]-L native=bar[..]`",
)
.run();
}
Expand Down
12 changes: 6 additions & 6 deletions tests/testsuite/profile_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ fn profile_selection_test() {
[RUNNING] `[..]/deps/foo-[..]`
[RUNNING] `[..]/deps/test1-[..]`
[DOCTEST] foo
[RUNNING] `rustdoc --test [..]
[RUNNING] `rustdoc [..]--test [..]
").run();
p.cargo("test -vv")
.with_stderr_unordered(
Expand All @@ -319,7 +319,7 @@ fn profile_selection_test() {
[RUNNING] `[..]/deps/foo-[..]`
[RUNNING] `[..]/deps/test1-[..]`
[DOCTEST] foo
[RUNNING] `rustdoc --test [..]
[RUNNING] `rustdoc [..]--test [..]
",
)
.run();
Expand Down Expand Up @@ -371,7 +371,7 @@ fn profile_selection_test_release() {
[RUNNING] `[..]/deps/foo-[..]`
[RUNNING] `[..]/deps/test1-[..]`
[DOCTEST] foo
[RUNNING] `rustdoc --test [..]`
[RUNNING] `rustdoc [..]--test [..]`
").run();
p.cargo("test --release -vv")
.with_stderr_unordered(
Expand All @@ -384,7 +384,7 @@ fn profile_selection_test_release() {
[RUNNING] `[..]/deps/foo-[..]`
[RUNNING] `[..]/deps/test1-[..]`
[DOCTEST] foo
[RUNNING] `rustdoc --test [..]
[RUNNING] `rustdoc [..]--test [..]
",
)
.run();
Expand Down Expand Up @@ -633,7 +633,7 @@ fn profile_selection_doc() {
[COMPILING] bar [..]
[DOCUMENTING] bar [..]
[RUNNING] `[..] rustc --crate-name bar bar/src/lib.rs [..]--crate-type lib --emit=[..]link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `rustdoc --crate-name bar bar/src/lib.rs [..]
[RUNNING] `rustdoc [..]--crate-name bar bar/src/lib.rs [..]
[RUNNING] `[..] rustc --crate-name bar bar/src/lib.rs [..]--crate-type lib --emit=[..]metadata -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]
[COMPILING] bdep [..]
[RUNNING] `[..] rustc --crate-name bdep bdep/src/lib.rs [..]--crate-type lib --emit=[..]link -C codegen-units=1 -C debuginfo=2 [..]
Expand All @@ -642,7 +642,7 @@ fn profile_selection_doc() {
[RUNNING] `[..]target/debug/build/foo-[..]/build-script-build`
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[DOCUMENTING] foo [..]
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]
[FINISHED] dev [unoptimized + debuginfo] [..]
").run();
}
2 changes: 1 addition & 1 deletion tests/testsuite/rename_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn can_run_doc_tests() {
.with_stderr_contains(
"\
[DOCTEST] foo
[RUNNING] `rustdoc --test [CWD]/src/lib.rs \
[RUNNING] `rustdoc [..]--test [CWD]/src/lib.rs \
[..] \
--extern bar=[CWD]/target/debug/deps/libbar-[..].rlib \
--extern baz=[CWD]/target/debug/deps/libbar-[..].rlib \
Expand Down
47 changes: 40 additions & 7 deletions tests/testsuite/rustdoc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::support::{basic_manifest, project};
use crate::support::{basic_manifest, is_nightly, project};

#[cargo_test]
fn rustdoc_simple() {
Expand All @@ -8,7 +8,7 @@ fn rustdoc_simple() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([CWD])
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
[..] \
-L dependency=[CWD]/target/debug/deps`
Expand All @@ -26,7 +26,7 @@ fn rustdoc_args() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([CWD])
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
[..] \
--cfg=foo \
Expand Down Expand Up @@ -66,7 +66,7 @@ fn rustdoc_foo_with_bar_dependency() {
[CHECKING] bar v0.0.1 ([..])
[RUNNING] `rustc [..]bar/src/lib.rs [..]`
[DOCUMENTING] foo v0.0.1 ([CWD])
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
[..] \
--cfg=foo \
Expand Down Expand Up @@ -105,7 +105,7 @@ fn rustdoc_only_bar_dependency() {
.with_stderr(
"\
[DOCUMENTING] bar v0.0.1 ([..])
[RUNNING] `rustdoc --crate-name bar [..]bar/src/lib.rs [..]\
[RUNNING] `rustdoc [..]--crate-name bar [..]bar/src/lib.rs [..]\
-o [CWD]/target/doc \
[..] \
--cfg=foo \
Expand All @@ -127,7 +127,7 @@ fn rustdoc_same_name_documents_lib() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([..])
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
[..] \
--cfg=foo \
Expand Down Expand Up @@ -161,6 +161,39 @@ fn features() {
.run();
}

#[cargo_test]
fn proc_macro_crate_type() {
// NOTE - Remove this once 'rustdoc --crate-type'
// rides to stable
if !is_nightly() {
return;
}
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[lib]
proc-macro = true
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("rustdoc --verbose")
.with_stderr_contains(
"\
[RUNNING] `rustdoc --crate-type proc-macro [..]`
",
)
.run();
}

#[cargo_test]
#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))]
fn rustdoc_target() {
Expand All @@ -170,7 +203,7 @@ fn rustdoc_target() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([..])
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\
--target x86_64-unknown-linux-gnu \
-o [CWD]/target/x86_64-unknown-linux-gnu/doc \
[..] \
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn cargo_test_release() {
[RUNNING] `[..]target/release/deps/foo-[..][EXE]`
[RUNNING] `[..]target/release/deps/test-[..][EXE]`
[DOCTEST] foo
[RUNNING] `rustdoc --test [..]lib.rs[..]`",
[RUNNING] `rustdoc [..]--test [..]lib.rs[..]`",
)
.with_stdout_contains_n("test test ... ok", 2)
.with_stdout_contains("running 0 tests")
Expand Down Expand Up @@ -2702,15 +2702,15 @@ fn pass_correct_cfgs_flags_to_rustdoc() {
.with_stderr_contains(
"\
[DOCTEST] feature_a
[RUNNING] `rustdoc --test [..]mock_serde_codegen[..]`",
[RUNNING] `rustdoc [..]--test [..]mock_serde_codegen[..]`",
)
.run();

p.cargo("test --verbose")
.with_stderr_contains(
"\
[DOCTEST] foo
[RUNNING] `rustdoc --test [..]feature_a[..]`",
[RUNNING] `rustdoc [..]--test [..]feature_a[..]`",
)
.run();
}
Expand Down

0 comments on commit b2d4f20

Please sign in to comment.