Skip to content

Commit fb92ee5

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

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

src/cargo/core/compiler/mod.rs

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

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

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

@@ -747,7 +742,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
747742

748743
rustdoc
749744
.arg("--scrape-examples-output-path")
750-
.arg(scrape_output_path(unit)?);
745+
.arg(scrape_output_path(cx, unit)?);
751746

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

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

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

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

799807
let failed_scrape_units = Arc::clone(&cx.failed_scrape_units);
800808
let hide_diagnostics_for_scrape_unit = cx.bcx.unit_can_fail_for_docscraping(unit)
@@ -1774,3 +1782,14 @@ fn apply_env_config(config: &crate::Config, cmd: &mut ProcessBuilder) -> CargoRe
17741782
}
17751783
Ok(())
17761784
}
1785+
1786+
/// Checks if there are some scrape units waiting to be processed.
1787+
fn should_include_scrape_units(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
1788+
unit.mode.is_doc() && bcx.scrape_units.len() > 0 && bcx.ws.unit_needs_doc_scrape(unit)
1789+
}
1790+
1791+
/// Gets the file path of function call information output from `rustdoc`.
1792+
fn scrape_output_path(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult<PathBuf> {
1793+
assert!(unit.mode.is_doc() || unit.mode.is_doc_scrape());
1794+
cx.outputs(unit).map(|outputs| outputs[0].path.clone())
1795+
}

0 commit comments

Comments
 (0)