Skip to content

Commit d21e8a8

Browse files
committed
refactor: consolidate rustdoc process building in one place
Make it clear by separating static and dynamic rustdocflags.
1 parent b682cbb commit d21e8a8

File tree

1 file changed

+45
-27
lines changed

1 file changed

+45
-27
lines changed

src/cargo/core/compiler/mod.rs

+45-27
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,13 @@ fn prepare_rustc(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilde
697697
Ok(base)
698698
}
699699

700-
/// Creates a unit of work invoking `rustdoc` for documenting the `unit`.
701-
fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
700+
/// Prepares flags and environments we can compute for a `rustdoc` invocation
701+
/// before the job queue starts compiling any unit.
702+
///
703+
/// This builds a static view of the invocation. Flags depending on the
704+
/// completion of other units will be added later in runtime, such as flags
705+
/// from build scripts.
706+
fn prepare_rustdoc(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
702707
let bcx = cx.bcx;
703708
// script_metadata is not needed here, it is only for tests.
704709
let mut rustdoc = cx.compilation.rustdoc_process(unit, None)?;
@@ -712,12 +717,6 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
712717
rustdoc.arg("--target").arg(target.rustc_target());
713718
}
714719
let doc_dir = cx.files().out_dir(unit);
715-
716-
// Create the documentation directory ahead of time as rustdoc currently has
717-
// a bug where concurrent invocations will race to create this directory if
718-
// it doesn't already exist.
719-
paths::create_dir_all(&doc_dir)?;
720-
721720
rustdoc.arg("-o").arg(&doc_dir);
722721
rustdoc.args(&features_args(unit));
723722
rustdoc.args(&check_cfg_args(cx, unit));
@@ -733,10 +732,6 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
733732
let metadata = cx.metadata_for_doc_units[unit];
734733
rustdoc.arg("-C").arg(format!("metadata={}", metadata));
735734

736-
let scrape_output_path = |unit: &Unit| -> CargoResult<PathBuf> {
737-
cx.outputs(unit).map(|outputs| outputs[0].path.clone())
738-
};
739-
740735
if unit.mode.is_doc_scrape() {
741736
debug_assert!(cx.bcx.scrape_units.contains(unit));
742737

@@ -748,7 +743,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
748743

749744
rustdoc
750745
.arg("--scrape-examples-output-path")
751-
.arg(scrape_output_path(unit)?);
746+
.arg(scrape_output_path(cx, unit)?);
752747

753748
// Only scrape example for items from crates in the workspace, to reduce generated file size
754749
for pkg in cx.bcx.ws.members() {
@@ -763,21 +758,9 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
763758
}
764759
}
765760

766-
let should_include_scrape_units = unit.mode.is_doc()
767-
&& cx.bcx.scrape_units.len() > 0
768-
&& cx.bcx.ws.unit_needs_doc_scrape(unit);
769-
let scrape_outputs = if should_include_scrape_units {
761+
if should_include_scrape_units(cx.bcx, unit) {
770762
rustdoc.arg("-Zunstable-options");
771-
Some(
772-
cx.bcx
773-
.scrape_units
774-
.iter()
775-
.map(|unit| Ok((cx.files().metadata(unit), scrape_output_path(unit)?)))
776-
.collect::<CargoResult<HashMap<_, _>>>()?,
777-
)
778-
} else {
779-
None
780-
};
763+
}
781764

782765
build_deps_args(&mut rustdoc, cx, unit)?;
783766
rustdoc::add_root_urls(cx, unit, &mut rustdoc)?;
@@ -788,6 +771,20 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
788771
append_crate_version_flag(unit, &mut rustdoc);
789772
}
790773

774+
Ok(rustdoc)
775+
}
776+
777+
/// Creates a unit of work invoking `rustdoc` for documenting the `unit`.
778+
fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
779+
let mut rustdoc = prepare_rustdoc(cx, unit)?;
780+
781+
let crate_name = unit.target.crate_name();
782+
let doc_dir = cx.files().out_dir(unit);
783+
// Create the documentation directory ahead of time as rustdoc currently has
784+
// a bug where concurrent invocations will race to create this directory if
785+
// it doesn't already exist.
786+
paths::create_dir_all(&doc_dir)?;
787+
791788
let target_desc = unit.target.description_named();
792789
let name = unit.pkg.name().to_string();
793790
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
@@ -796,6 +793,17 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
796793
let target = Target::clone(&unit.target);
797794
let mut output_options = OutputOptions::new(cx, unit);
798795
let script_metadata = cx.find_build_script_metadata(unit);
796+
let scrape_outputs = if should_include_scrape_units(cx.bcx, unit) {
797+
Some(
798+
cx.bcx
799+
.scrape_units
800+
.iter()
801+
.map(|unit| Ok((cx.files().metadata(unit), scrape_output_path(cx, unit)?)))
802+
.collect::<CargoResult<HashMap<_, _>>>()?,
803+
)
804+
} else {
805+
None
806+
};
799807

800808
let failed_scrape_units = Arc::clone(&cx.failed_scrape_units);
801809
let hide_diagnostics_for_scrape_unit = cx.bcx.unit_can_fail_for_docscraping(unit)
@@ -1775,3 +1783,13 @@ fn apply_env_config(config: &crate::Config, cmd: &mut ProcessBuilder) -> CargoRe
17751783
}
17761784
Ok(())
17771785
}
1786+
1787+
/// TODO(doc):
1788+
fn should_include_scrape_units(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
1789+
unit.mode.is_doc() && bcx.scrape_units.len() > 0 && bcx.ws.unit_needs_doc_scrape(unit)
1790+
}
1791+
1792+
/// TODO(doc):
1793+
fn scrape_output_path(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult<PathBuf> {
1794+
cx.outputs(unit).map(|outputs| outputs[0].path.clone())
1795+
}

0 commit comments

Comments
 (0)