diff --git a/src/bin/cargo/command_prelude.rs b/src/bin/cargo/command_prelude.rs index 813c5c5d4e3..5785c7baf81 100644 --- a/src/bin/cargo/command_prelude.rs +++ b/src/bin/cargo/command_prelude.rs @@ -315,6 +315,7 @@ pub trait ArgMatchesExt { ), target_rustdoc_args: None, target_rustc_args: None, + local_rustdoc_args: None, export_dir: None, }; Ok(opts) diff --git a/src/bin/cargo/commands/doc.rs b/src/bin/cargo/commands/doc.rs index 7a43e464a29..3bbcae52b64 100644 --- a/src/bin/cargo/commands/doc.rs +++ b/src/bin/cargo/commands/doc.rs @@ -51,7 +51,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { deps: !args.is_present("no-deps"), }; let mut compile_opts = args.compile_options(config, mode)?; - compile_opts.target_rustdoc_args = if args.is_present("document-private-items") { + compile_opts.local_rustdoc_args = if args.is_present("document-private-items") { Some(vec!["--document-private-items".to_string()]) } else { None diff --git a/src/cargo/core/compiler/build_context/mod.rs b/src/cargo/core/compiler/build_context/mod.rs index 094b61bb28a..465dd1095b5 100644 --- a/src/cargo/core/compiler/build_context/mod.rs +++ b/src/cargo/core/compiler/build_context/mod.rs @@ -24,12 +24,8 @@ pub struct BuildContext<'a, 'cfg: 'a> { pub resolve: &'a Resolve, pub profiles: &'a Profiles, pub build_config: &'a BuildConfig, - /// This is a workaround to carry the extra compiler args for either - /// `rustc` or `rustdoc` given on the command-line for the commands `cargo - /// rustc` and `cargo rustdoc`. These commands only support one target, - /// but we don't want the args passed to any dependencies, so we include - /// the `Unit` corresponding to the top-level target. - pub extra_compiler_args: Option<(Unit<'a>, Vec)>, + /// Extra compiler args for either `rustc` or `rustdoc`. + pub extra_compiler_args: HashMap, Vec>, pub packages: &'a PackageSet<'cfg>, /// Information about the compiler @@ -51,7 +47,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> { config: &'cfg Config, build_config: &'a BuildConfig, profiles: &'a Profiles, - extra_compiler_args: Option<(Unit<'a>, Vec)>, + extra_compiler_args: HashMap, Vec>, ) -> CargoResult> { let incremental_env = match env::var("CARGO_INCREMENTAL") { Ok(v) => Some(v == "1"), @@ -200,12 +196,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> { } pub fn extra_args_for(&self, unit: &Unit<'a>) -> Option<&Vec> { - if let Some((ref args_unit, ref args)) = self.extra_compiler_args { - if args_unit == unit { - return Some(args); - } - } - None + self.extra_compiler_args.get(unit) } /// Return the list of filenames read by cargo to generate the BuildContext diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 6e830970c15..abb3be686a3 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::fs; use std::path::Path; @@ -97,7 +98,7 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> { opts.config, &build_config, profiles, - None, + HashMap::new(), )?; let mut cx = Context::new(config, &bcx)?; cx.prepare_units(None, &units)?; diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index fe5623ce62a..58d1771042f 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -53,11 +53,13 @@ pub struct CompileOptions<'a> { /// Filter to apply to the root package to select which targets will be /// built. pub filter: CompileFilter, - /// Extra arguments to be passed to rustdoc (for main crate and dependencies) + /// Extra arguments to be passed to rustdoc (single target only) pub target_rustdoc_args: Option>, /// The specified target will be compiled with all the available arguments, /// note that this only accounts for the *final* invocation of rustc pub target_rustc_args: Option>, + /// Extra arguments passed to all selected targets for rustdoc. + pub local_rustdoc_args: Option>, /// The directory to copy final artifacts to. Note that even if `out_dir` is /// set, a copy of artifacts still could be found a `target/(debug\release)` /// as usual. @@ -80,6 +82,7 @@ impl<'a> CompileOptions<'a> { }, target_rustdoc_args: None, target_rustc_args: None, + local_rustdoc_args: None, export_dir: None, }) } @@ -219,6 +222,7 @@ pub fn compile_ws<'a>( ref filter, ref target_rustdoc_args, ref target_rustc_args, + ref local_rustdoc_args, ref export_dir, } = *options; @@ -265,8 +269,6 @@ pub fn compile_ws<'a>( let profiles = ws.profiles(); profiles.validate_packages(&mut config.shell(), &packages)?; - let mut extra_compiler_args = None; - let units = generate_targets( ws, profiles, @@ -277,6 +279,7 @@ pub fn compile_ws<'a>( build_config, )?; + let mut extra_compiler_args = HashMap::new(); if let Some(args) = extra_args { if units.len() != 1 { bail!( @@ -286,7 +289,14 @@ pub fn compile_ws<'a>( extra_args_name ); } - extra_compiler_args = Some((units[0], args)); + extra_compiler_args.insert(units[0], args); + } + if let Some(args) = local_rustdoc_args { + for unit in &units { + if unit.mode.is_doc() { + extra_compiler_args.insert(*unit, args.clone()); + } + } } let ret = { diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 5967b6be978..2b062840d6e 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -444,6 +444,7 @@ fn run_verify(ws: &Workspace, tar: &FileLock, opts: &PackageOpts) -> CargoResult }, target_rustdoc_args: None, target_rustc_args: None, + local_rustdoc_args: None, export_dir: None, }, &exec, diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 895c05422be..a5b52b2bb42 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -1206,6 +1206,31 @@ fn doc_private_items() { ); } +#[test] +fn doc_private_ws() { + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["a", "b"] + "#, + ).file("a/Cargo.toml", &basic_manifest("a", "0.0.1")) + .file("a/src/lib.rs", "fn p() {}") + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "fn p2() {}") + .file("b/src/main.rs", "fn main() {}") + .build(); + p.cargo("doc --all --bins --lib --document-private-items -v") + .with_stderr_contains( + "[RUNNING] `rustdoc [..] a/src/lib.rs [..]--document-private-items[..]", + ).with_stderr_contains( + "[RUNNING] `rustdoc [..] b/src/lib.rs [..]--document-private-items[..]", + ).with_stderr_contains( + "[RUNNING] `rustdoc [..] b/src/main.rs [..]--document-private-items[..]", + ).run(); +} + const BAD_INTRA_LINK_LIB: &str = r#" #![deny(intra_doc_link_resolution_failure)]