1pub mod artifact;
35mod build_config;
36pub(crate) mod build_context;
37mod build_plan;
38pub(crate) mod build_runner;
39mod compilation;
40mod compile_kind;
41mod crate_type;
42mod custom_build;
43pub(crate) mod fingerprint;
44pub mod future_incompat;
45pub(crate) mod job_queue;
46pub(crate) mod layout;
47mod links;
48mod lto;
49mod output_depinfo;
50mod output_sbom;
51pub mod rustdoc;
52pub mod standard_lib;
53mod timings;
54mod unit;
55pub mod unit_dependencies;
56pub mod unit_graph;
57
58use std::borrow::Cow;
59use std::collections::{BTreeMap, HashMap, HashSet};
60use std::env;
61use std::ffi::{OsStr, OsString};
62use std::fmt::Display;
63use std::fs::{self, File};
64use std::io::{BufRead, BufWriter, Write};
65use std::ops::Range;
66use std::path::{Path, PathBuf};
67use std::sync::{Arc, LazyLock};
68
69use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
70use anyhow::{Context as _, Error};
71use cargo_platform::{Cfg, Platform};
72use itertools::Itertools;
73use lazycell::LazyCell;
74use regex::Regex;
75use tracing::{debug, instrument, trace};
76
77pub use self::build_config::UserIntent;
78pub use self::build_config::{BuildConfig, CompileMode, MessageFormat, TimingOutput};
79pub use self::build_context::{
80 BuildContext, FileFlavor, FileType, RustDocFingerprint, RustcTargetData, TargetInfo,
81};
82use self::build_plan::BuildPlan;
83pub use self::build_runner::{BuildRunner, Metadata, UnitHash};
84pub use self::compilation::{Compilation, Doctest, UnitOutput};
85pub use self::compile_kind::{CompileKind, CompileKindFallback, CompileTarget};
86pub use self::crate_type::CrateType;
87pub use self::custom_build::LinkArgTarget;
88pub use self::custom_build::{BuildOutput, BuildScriptOutputs, BuildScripts, LibraryPath};
89pub(crate) use self::fingerprint::DirtyReason;
90pub use self::job_queue::Freshness;
91use self::job_queue::{Job, JobQueue, JobState, Work};
92pub(crate) use self::layout::Layout;
93pub use self::lto::Lto;
94use self::output_depinfo::output_depinfo;
95use self::output_sbom::build_sbom;
96use self::unit_graph::UnitDep;
97use crate::core::compiler::future_incompat::FutureIncompatReport;
98use crate::core::compiler::timings::SectionTiming;
99pub use crate::core::compiler::unit::{Unit, UnitInterner};
100use crate::core::manifest::TargetSourcePath;
101use crate::core::profiles::{PanicStrategy, Profile, StripInner};
102use crate::core::{Feature, PackageId, Target, Verbosity};
103use crate::util::context::WarningHandling;
104use crate::util::errors::{CargoResult, VerboseError};
105use crate::util::interning::InternedString;
106use crate::util::lints::get_key_value;
107use crate::util::machine_message::{self, Message};
108use crate::util::{add_path_args, internal, path_args};
109use cargo_util::{ProcessBuilder, ProcessError, paths};
110use cargo_util_schemas::manifest::TomlDebugInfo;
111use cargo_util_schemas::manifest::TomlTrimPaths;
112use cargo_util_schemas::manifest::TomlTrimPathsValue;
113use rustfix::diagnostics::Applicability;
114pub(crate) use timings::CompilationSection;
115
116const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version";
117
118pub trait Executor: Send + Sync + 'static {
122 fn init(&self, _build_runner: &BuildRunner<'_, '_>, _unit: &Unit) {}
126
127 fn exec(
130 &self,
131 cmd: &ProcessBuilder,
132 id: PackageId,
133 target: &Target,
134 mode: CompileMode,
135 on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
136 on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
137 ) -> CargoResult<()>;
138
139 fn force_rebuild(&self, _unit: &Unit) -> bool {
142 false
143 }
144}
145
146#[derive(Copy, Clone)]
149pub struct DefaultExecutor;
150
151impl Executor for DefaultExecutor {
152 #[instrument(name = "rustc", skip_all, fields(package = id.name().as_str(), process = cmd.to_string()))]
153 fn exec(
154 &self,
155 cmd: &ProcessBuilder,
156 id: PackageId,
157 _target: &Target,
158 _mode: CompileMode,
159 on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
160 on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
161 ) -> CargoResult<()> {
162 cmd.exec_with_streaming(on_stdout_line, on_stderr_line, false)
163 .map(drop)
164 }
165}
166
167#[tracing::instrument(skip(build_runner, jobs, plan, exec))]
177fn compile<'gctx>(
178 build_runner: &mut BuildRunner<'_, 'gctx>,
179 jobs: &mut JobQueue<'gctx>,
180 plan: &mut BuildPlan,
181 unit: &Unit,
182 exec: &Arc<dyn Executor>,
183 force_rebuild: bool,
184) -> CargoResult<()> {
185 let bcx = build_runner.bcx;
186 let build_plan = bcx.build_config.build_plan;
187 if !build_runner.compiled.insert(unit.clone()) {
188 return Ok(());
189 }
190
191 if !unit.skip_non_compile_time_dep {
195 fingerprint::prepare_init(build_runner, unit)?;
198
199 let job = if unit.mode.is_run_custom_build() {
200 custom_build::prepare(build_runner, unit)?
201 } else if unit.mode.is_doc_test() {
202 Job::new_fresh()
204 } else if build_plan {
205 Job::new_dirty(
206 rustc(build_runner, unit, &exec.clone())?,
207 DirtyReason::FreshBuild,
208 )
209 } else {
210 let force = exec.force_rebuild(unit) || force_rebuild;
211 let mut job = fingerprint::prepare_target(build_runner, unit, force)?;
212 job.before(if job.freshness().is_dirty() {
213 let work = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
214 rustdoc(build_runner, unit)?
215 } else {
216 rustc(build_runner, unit, exec)?
217 };
218 work.then(link_targets(build_runner, unit, false)?)
219 } else {
220 let show_diagnostics = unit.show_warnings(bcx.gctx)
223 && build_runner.bcx.gctx.warning_handling()? != WarningHandling::Allow;
224 let manifest = ManifestErrorContext::new(build_runner, unit);
225 let work = replay_output_cache(
226 unit.pkg.package_id(),
227 manifest,
228 &unit.target,
229 build_runner.files().message_cache_path(unit),
230 build_runner.bcx.build_config.message_format,
231 show_diagnostics,
232 );
233 work.then(link_targets(build_runner, unit, true)?)
235 });
236
237 job
238 };
239 jobs.enqueue(build_runner, unit, job)?;
240 }
241
242 let deps = Vec::from(build_runner.unit_deps(unit)); for dep in deps {
245 compile(build_runner, jobs, plan, &dep.unit, exec, false)?;
246 }
247 if build_plan {
248 plan.add(build_runner, unit)?;
249 }
250
251 Ok(())
252}
253
254fn make_failed_scrape_diagnostic(
257 build_runner: &BuildRunner<'_, '_>,
258 unit: &Unit,
259 top_line: impl Display,
260) -> String {
261 let manifest_path = unit.pkg.manifest_path();
262 let relative_manifest_path = manifest_path
263 .strip_prefix(build_runner.bcx.ws.root())
264 .unwrap_or(&manifest_path);
265
266 format!(
267 "\
268{top_line}
269 Try running with `--verbose` to see the error message.
270 If an example should not be scanned, then consider adding `doc-scrape-examples = false` to its `[[example]]` definition in {}",
271 relative_manifest_path.display()
272 )
273}
274
275fn rustc(
277 build_runner: &mut BuildRunner<'_, '_>,
278 unit: &Unit,
279 exec: &Arc<dyn Executor>,
280) -> CargoResult<Work> {
281 let mut rustc = prepare_rustc(build_runner, unit)?;
282 let build_plan = build_runner.bcx.build_config.build_plan;
283
284 let name = unit.pkg.name();
285 let buildkey = unit.buildkey();
286
287 let outputs = build_runner.outputs(unit)?;
288 let root = build_runner.files().out_dir(unit);
289
290 let build_script_outputs = Arc::clone(&build_runner.build_script_outputs);
292 let current_id = unit.pkg.package_id();
293 let manifest = ManifestErrorContext::new(build_runner, unit);
294 let build_scripts = build_runner.build_scripts.get(unit).cloned();
295
296 let pass_l_flag = unit.target.is_lib() || !unit.pkg.targets().iter().any(|t| t.is_lib());
299
300 let dep_info_name =
301 if let Some(c_extra_filename) = build_runner.files().metadata(unit).c_extra_filename() {
302 format!("{}-{}.d", unit.target.crate_name(), c_extra_filename)
303 } else {
304 format!("{}.d", unit.target.crate_name())
305 };
306 let rustc_dep_info_loc = root.join(dep_info_name);
307 let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
308
309 let mut output_options = OutputOptions::new(build_runner, unit);
310 let package_id = unit.pkg.package_id();
311 let target = Target::clone(&unit.target);
312 let mode = unit.mode;
313
314 exec.init(build_runner, unit);
315 let exec = exec.clone();
316
317 let root_output = build_runner.files().host_dest().to_path_buf();
318 let build_dir = build_runner.bcx.ws.build_dir().into_path_unlocked();
319 let pkg_root = unit.pkg.root().to_path_buf();
320 let cwd = rustc
321 .get_cwd()
322 .unwrap_or_else(|| build_runner.bcx.gctx.cwd())
323 .to_path_buf();
324 let fingerprint_dir = build_runner.files().fingerprint_dir(unit);
325 let script_metadatas = build_runner.find_build_script_metadatas(unit);
326 let is_local = unit.is_local();
327 let artifact = unit.artifact;
328 let sbom_files = build_runner.sbom_output_files(unit)?;
329 let sbom = build_sbom(build_runner, unit)?;
330
331 let hide_diagnostics_for_scrape_unit = build_runner.bcx.unit_can_fail_for_docscraping(unit)
332 && !matches!(
333 build_runner.bcx.gctx.shell().verbosity(),
334 Verbosity::Verbose
335 );
336 let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
337 let target_desc = unit.target.description_named();
340 let mut for_scrape_units = build_runner
341 .bcx
342 .scrape_units_have_dep_on(unit)
343 .into_iter()
344 .map(|unit| unit.target.description_named())
345 .collect::<Vec<_>>();
346 for_scrape_units.sort();
347 let for_scrape_units = for_scrape_units.join(", ");
348 make_failed_scrape_diagnostic(build_runner, unit, format_args!("failed to check {target_desc} in package `{name}` as a prerequisite for scraping examples from: {for_scrape_units}"))
349 });
350 if hide_diagnostics_for_scrape_unit {
351 output_options.show_diagnostics = false;
352 }
353 let env_config = Arc::clone(build_runner.bcx.gctx.env_config()?);
354 return Ok(Work::new(move |state| {
355 if artifact.is_true() {
359 paths::create_dir_all(&root)?;
360 }
361
362 if let Some(build_scripts) = build_scripts {
370 let script_outputs = build_script_outputs.lock().unwrap();
371 if !build_plan {
372 add_native_deps(
373 &mut rustc,
374 &script_outputs,
375 &build_scripts,
376 pass_l_flag,
377 &target,
378 current_id,
379 mode,
380 )?;
381 add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
382 }
383 add_custom_flags(&mut rustc, &script_outputs, script_metadatas)?;
384 }
385
386 for output in outputs.iter() {
387 if output.path.extension() == Some(OsStr::new("rmeta")) {
391 let dst = root.join(&output.path).with_extension("rlib");
392 if dst.exists() {
393 paths::remove_file(&dst)?;
394 }
395 }
396
397 if output.hardlink.is_some() && output.path.exists() {
402 _ = paths::remove_file(&output.path).map_err(|e| {
403 tracing::debug!(
404 "failed to delete previous output file `{:?}`: {e:?}",
405 output.path
406 );
407 });
408 }
409 }
410
411 state.running(&rustc);
412 let timestamp = paths::set_invocation_time(&fingerprint_dir)?;
413 if build_plan {
414 state.build_plan(buildkey, rustc.clone(), outputs.clone());
415 } else {
416 for file in sbom_files {
417 tracing::debug!("writing sbom to {}", file.display());
418 let outfile = BufWriter::new(paths::create(&file)?);
419 serde_json::to_writer(outfile, &sbom)?;
420 }
421
422 let result = exec
423 .exec(
424 &rustc,
425 package_id,
426 &target,
427 mode,
428 &mut |line| on_stdout_line(state, line, package_id, &target),
429 &mut |line| {
430 on_stderr_line(
431 state,
432 line,
433 package_id,
434 &manifest,
435 &target,
436 &mut output_options,
437 )
438 },
439 )
440 .map_err(|e| {
441 if output_options.errors_seen == 0 {
442 e
447 } else {
448 verbose_if_simple_exit_code(e)
449 }
450 })
451 .with_context(|| {
452 let warnings = match output_options.warnings_seen {
454 0 => String::new(),
455 1 => "; 1 warning emitted".to_string(),
456 count => format!("; {} warnings emitted", count),
457 };
458 let errors = match output_options.errors_seen {
459 0 => String::new(),
460 1 => " due to 1 previous error".to_string(),
461 count => format!(" due to {} previous errors", count),
462 };
463 let name = descriptive_pkg_name(&name, &target, &mode);
464 format!("could not compile {name}{errors}{warnings}")
465 });
466
467 if let Err(e) = result {
468 if let Some(diagnostic) = failed_scrape_diagnostic {
469 state.warning(diagnostic);
470 }
471
472 return Err(e);
473 }
474
475 debug_assert_eq!(output_options.errors_seen, 0);
477 }
478
479 if rustc_dep_info_loc.exists() {
480 fingerprint::translate_dep_info(
481 &rustc_dep_info_loc,
482 &dep_info_loc,
483 &cwd,
484 &pkg_root,
485 &build_dir,
486 &rustc,
487 is_local,
489 &env_config,
490 )
491 .with_context(|| {
492 internal(format!(
493 "could not parse/generate dep info at: {}",
494 rustc_dep_info_loc.display()
495 ))
496 })?;
497 paths::set_file_time_no_err(dep_info_loc, timestamp);
500 }
501
502 Ok(())
503 }));
504
505 fn add_native_deps(
508 rustc: &mut ProcessBuilder,
509 build_script_outputs: &BuildScriptOutputs,
510 build_scripts: &BuildScripts,
511 pass_l_flag: bool,
512 target: &Target,
513 current_id: PackageId,
514 mode: CompileMode,
515 ) -> CargoResult<()> {
516 let mut library_paths = vec![];
517
518 for key in build_scripts.to_link.iter() {
519 let output = build_script_outputs.get(key.1).ok_or_else(|| {
520 internal(format!(
521 "couldn't find build script output for {}/{}",
522 key.0, key.1
523 ))
524 })?;
525 library_paths.extend(output.library_paths.iter());
526 }
527
528 library_paths.sort_by_key(|p| match p {
534 LibraryPath::CargoArtifact(_) => 0,
535 LibraryPath::External(_) => 1,
536 });
537
538 for path in library_paths.iter() {
539 rustc.arg("-L").arg(path.as_ref());
540 }
541
542 for key in build_scripts.to_link.iter() {
543 let output = build_script_outputs.get(key.1).ok_or_else(|| {
544 internal(format!(
545 "couldn't find build script output for {}/{}",
546 key.0, key.1
547 ))
548 })?;
549
550 if key.0 == current_id {
551 if pass_l_flag {
552 for name in output.library_links.iter() {
553 rustc.arg("-l").arg(name);
554 }
555 }
556 }
557
558 for (lt, arg) in &output.linker_args {
559 if lt.applies_to(target, mode)
565 && (key.0 == current_id || *lt == LinkArgTarget::Cdylib)
566 {
567 rustc.arg("-C").arg(format!("link-arg={}", arg));
568 }
569 }
570 }
571 Ok(())
572 }
573}
574
575fn verbose_if_simple_exit_code(err: Error) -> Error {
576 match err
579 .downcast_ref::<ProcessError>()
580 .as_ref()
581 .and_then(|perr| perr.code)
582 {
583 Some(n) if cargo_util::is_simple_exit_code(n) => VerboseError::new(err).into(),
584 _ => err,
585 }
586}
587
588fn link_targets(
591 build_runner: &mut BuildRunner<'_, '_>,
592 unit: &Unit,
593 fresh: bool,
594) -> CargoResult<Work> {
595 let bcx = build_runner.bcx;
596 let outputs = build_runner.outputs(unit)?;
597 let export_dir = build_runner.files().export_dir();
598 let package_id = unit.pkg.package_id();
599 let manifest_path = PathBuf::from(unit.pkg.manifest_path());
600 let profile = unit.profile.clone();
601 let unit_mode = unit.mode;
602 let features = unit.features.iter().map(|s| s.to_string()).collect();
603 let json_messages = bcx.build_config.emit_json();
604 let executable = build_runner.get_executable(unit)?;
605 let mut target = Target::clone(&unit.target);
606 if let TargetSourcePath::Metabuild = target.src_path() {
607 let path = unit
609 .pkg
610 .manifest()
611 .metabuild_path(build_runner.bcx.ws.build_dir());
612 target.set_src_path(TargetSourcePath::Path(path));
613 }
614
615 Ok(Work::new(move |state| {
616 let mut destinations = vec![];
621 for output in outputs.iter() {
622 let src = &output.path;
623 if !src.exists() {
626 continue;
627 }
628 let Some(dst) = output.hardlink.as_ref() else {
629 destinations.push(src.clone());
630 continue;
631 };
632 destinations.push(dst.clone());
633 paths::link_or_copy(src, dst)?;
634 if let Some(ref path) = output.export_path {
635 let export_dir = export_dir.as_ref().unwrap();
636 paths::create_dir_all(export_dir)?;
637
638 paths::link_or_copy(src, path)?;
639 }
640 }
641
642 if json_messages {
643 let debuginfo = match profile.debuginfo.into_inner() {
644 TomlDebugInfo::None => machine_message::ArtifactDebuginfo::Int(0),
645 TomlDebugInfo::Limited => machine_message::ArtifactDebuginfo::Int(1),
646 TomlDebugInfo::Full => machine_message::ArtifactDebuginfo::Int(2),
647 TomlDebugInfo::LineDirectivesOnly => {
648 machine_message::ArtifactDebuginfo::Named("line-directives-only")
649 }
650 TomlDebugInfo::LineTablesOnly => {
651 machine_message::ArtifactDebuginfo::Named("line-tables-only")
652 }
653 };
654 let art_profile = machine_message::ArtifactProfile {
655 opt_level: profile.opt_level.as_str(),
656 debuginfo: Some(debuginfo),
657 debug_assertions: profile.debug_assertions,
658 overflow_checks: profile.overflow_checks,
659 test: unit_mode.is_any_test(),
660 };
661
662 let msg = machine_message::Artifact {
663 package_id: package_id.to_spec(),
664 manifest_path,
665 target: &target,
666 profile: art_profile,
667 features,
668 filenames: destinations,
669 executable,
670 fresh,
671 }
672 .to_json_string();
673 state.stdout(msg)?;
674 }
675 Ok(())
676 }))
677}
678
679fn add_plugin_deps(
683 rustc: &mut ProcessBuilder,
684 build_script_outputs: &BuildScriptOutputs,
685 build_scripts: &BuildScripts,
686 root_output: &Path,
687) -> CargoResult<()> {
688 let var = paths::dylib_path_envvar();
689 let search_path = rustc.get_env(var).unwrap_or_default();
690 let mut search_path = env::split_paths(&search_path).collect::<Vec<_>>();
691 for (pkg_id, metadata) in &build_scripts.plugins {
692 let output = build_script_outputs
693 .get(*metadata)
694 .ok_or_else(|| internal(format!("couldn't find libs for plugin dep {}", pkg_id)))?;
695 search_path.append(&mut filter_dynamic_search_path(
696 output.library_paths.iter().map(AsRef::as_ref),
697 root_output,
698 ));
699 }
700 let search_path = paths::join_paths(&search_path, var)?;
701 rustc.env(var, &search_path);
702 Ok(())
703}
704
705fn get_dynamic_search_path(path: &Path) -> &Path {
706 match path.to_str().and_then(|s| s.split_once("=")) {
707 Some(("native" | "crate" | "dependency" | "framework" | "all", path)) => Path::new(path),
708 _ => path,
709 }
710}
711
712fn filter_dynamic_search_path<'a, I>(paths: I, root_output: &Path) -> Vec<PathBuf>
718where
719 I: Iterator<Item = &'a PathBuf>,
720{
721 let mut search_path = vec![];
722 for dir in paths {
723 let dir = get_dynamic_search_path(dir);
724 if dir.starts_with(&root_output) {
725 search_path.push(dir.to_path_buf());
726 } else {
727 debug!(
728 "Not including path {} in runtime library search path because it is \
729 outside target root {}",
730 dir.display(),
731 root_output.display()
732 );
733 }
734 }
735 search_path
736}
737
738fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
745 let gctx = build_runner.bcx.gctx;
746 let is_primary = build_runner.is_primary_package(unit);
747 let is_workspace = build_runner.bcx.ws.is_member(&unit.pkg);
748
749 let mut base = build_runner
750 .compilation
751 .rustc_process(unit, is_primary, is_workspace)?;
752 build_base_args(build_runner, &mut base, unit)?;
753 if unit.pkg.manifest().is_embedded() {
754 if !gctx.cli_unstable().script {
755 anyhow::bail!(
756 "parsing `{}` requires `-Zscript`",
757 unit.pkg.manifest_path().display()
758 );
759 }
760 base.arg("-Z").arg("crate-attr=feature(frontmatter)");
761 }
762
763 base.inherit_jobserver(&build_runner.jobserver);
764 build_deps_args(&mut base, build_runner, unit)?;
765 add_cap_lints(build_runner.bcx, unit, &mut base);
766 if let Some(args) = build_runner.bcx.extra_args_for(unit) {
767 base.args(args);
768 }
769 base.args(&unit.rustflags);
770 if gctx.cli_unstable().binary_dep_depinfo {
771 base.arg("-Z").arg("binary-dep-depinfo");
772 }
773 if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
774 base.arg("-Z").arg("checksum-hash-algorithm=blake3");
775 }
776
777 if is_primary {
778 base.env("CARGO_PRIMARY_PACKAGE", "1");
779 let file_list = std::env::join_paths(build_runner.sbom_output_files(unit)?)?;
780 base.env("CARGO_SBOM_PATH", file_list);
781 }
782
783 if unit.target.is_test() || unit.target.is_bench() {
784 let tmp = build_runner
785 .files()
786 .layout(unit.kind)
787 .build_dir()
788 .prepare_tmp()?;
789 base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());
790 }
791
792 Ok(base)
793}
794
795fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
802 let bcx = build_runner.bcx;
803 let mut rustdoc = build_runner.compilation.rustdoc_process(unit, None)?;
805 if unit.pkg.manifest().is_embedded() {
806 if !bcx.gctx.cli_unstable().script {
807 anyhow::bail!(
808 "parsing `{}` requires `-Zscript`",
809 unit.pkg.manifest_path().display()
810 );
811 }
812 rustdoc.arg("-Z").arg("crate-attr=feature(frontmatter)");
813 }
814 rustdoc.inherit_jobserver(&build_runner.jobserver);
815 let crate_name = unit.target.crate_name();
816 rustdoc.arg("--crate-name").arg(&crate_name);
817 add_path_args(bcx.ws, unit, &mut rustdoc);
818 add_cap_lints(bcx, unit, &mut rustdoc);
819
820 if let CompileKind::Target(target) = unit.kind {
821 rustdoc.arg("--target").arg(target.rustc_target());
822 }
823 let doc_dir = build_runner.files().out_dir(unit);
824 rustdoc.arg("-o").arg(&doc_dir);
825 rustdoc.args(&features_args(unit));
826 rustdoc.args(&check_cfg_args(unit));
827
828 add_error_format_and_color(build_runner, &mut rustdoc);
829 add_allow_features(build_runner, &mut rustdoc);
830
831 if build_runner.bcx.gctx.cli_unstable().rustdoc_depinfo {
832 let mut arg =
835 OsString::from("--emit=toolchain-shared-resources,invocation-specific,dep-info=");
836 arg.push(rustdoc_dep_info_loc(build_runner, unit));
837 rustdoc.arg(arg);
838
839 if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
840 rustdoc.arg("-Z").arg("checksum-hash-algorithm=blake3");
841 }
842
843 rustdoc.arg("-Zunstable-options");
844 }
845
846 if let Some(trim_paths) = unit.profile.trim_paths.as_ref() {
847 trim_paths_args_rustdoc(&mut rustdoc, build_runner, unit, trim_paths)?;
848 }
849
850 rustdoc.args(unit.pkg.manifest().lint_rustflags());
851
852 let metadata = build_runner.metadata_for_doc_units[unit];
853 rustdoc
854 .arg("-C")
855 .arg(format!("metadata={}", metadata.c_metadata()));
856
857 if unit.mode.is_doc_scrape() {
858 debug_assert!(build_runner.bcx.scrape_units.contains(unit));
859
860 if unit.target.is_test() {
861 rustdoc.arg("--scrape-tests");
862 }
863
864 rustdoc.arg("-Zunstable-options");
865
866 rustdoc
867 .arg("--scrape-examples-output-path")
868 .arg(scrape_output_path(build_runner, unit)?);
869
870 for pkg in build_runner.bcx.packages.packages() {
872 let names = pkg
873 .targets()
874 .iter()
875 .map(|target| target.crate_name())
876 .collect::<HashSet<_>>();
877 for name in names {
878 rustdoc.arg("--scrape-examples-target-crate").arg(name);
879 }
880 }
881 }
882
883 if should_include_scrape_units(build_runner.bcx, unit) {
884 rustdoc.arg("-Zunstable-options");
885 }
886
887 build_deps_args(&mut rustdoc, build_runner, unit)?;
888 rustdoc::add_root_urls(build_runner, unit, &mut rustdoc)?;
889
890 rustdoc::add_output_format(build_runner, &mut rustdoc)?;
891
892 if let Some(args) = build_runner.bcx.extra_args_for(unit) {
893 rustdoc.args(args);
894 }
895 rustdoc.args(&unit.rustdocflags);
896
897 if !crate_version_flag_already_present(&rustdoc) {
898 append_crate_version_flag(unit, &mut rustdoc);
899 }
900
901 Ok(rustdoc)
902}
903
904fn rustdoc(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<Work> {
906 let mut rustdoc = prepare_rustdoc(build_runner, unit)?;
907
908 let crate_name = unit.target.crate_name();
909 let doc_dir = build_runner.files().out_dir(unit);
910 paths::create_dir_all(&doc_dir)?;
914
915 let target_desc = unit.target.description_named();
916 let name = unit.pkg.name();
917 let build_script_outputs = Arc::clone(&build_runner.build_script_outputs);
918 let package_id = unit.pkg.package_id();
919 let target = Target::clone(&unit.target);
920 let manifest = ManifestErrorContext::new(build_runner, unit);
921
922 let rustdoc_dep_info_loc = rustdoc_dep_info_loc(build_runner, unit);
923 let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
924 let build_dir = build_runner.bcx.ws.build_dir().into_path_unlocked();
925 let pkg_root = unit.pkg.root().to_path_buf();
926 let cwd = rustdoc
927 .get_cwd()
928 .unwrap_or_else(|| build_runner.bcx.gctx.cwd())
929 .to_path_buf();
930 let fingerprint_dir = build_runner.files().fingerprint_dir(unit);
931 let is_local = unit.is_local();
932 let env_config = Arc::clone(build_runner.bcx.gctx.env_config()?);
933 let rustdoc_depinfo_enabled = build_runner.bcx.gctx.cli_unstable().rustdoc_depinfo;
934
935 let mut output_options = OutputOptions::new(build_runner, unit);
936 let script_metadatas = build_runner.find_build_script_metadatas(unit);
937 let scrape_outputs = if should_include_scrape_units(build_runner.bcx, unit) {
938 Some(
939 build_runner
940 .bcx
941 .scrape_units
942 .iter()
943 .map(|unit| {
944 Ok((
945 build_runner.files().metadata(unit).unit_id(),
946 scrape_output_path(build_runner, unit)?,
947 ))
948 })
949 .collect::<CargoResult<HashMap<_, _>>>()?,
950 )
951 } else {
952 None
953 };
954
955 let failed_scrape_units = Arc::clone(&build_runner.failed_scrape_units);
956 let hide_diagnostics_for_scrape_unit = build_runner.bcx.unit_can_fail_for_docscraping(unit)
957 && !matches!(
958 build_runner.bcx.gctx.shell().verbosity(),
959 Verbosity::Verbose
960 );
961 let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
962 make_failed_scrape_diagnostic(
963 build_runner,
964 unit,
965 format_args!("failed to scan {target_desc} in package `{name}` for example code usage"),
966 )
967 });
968 if hide_diagnostics_for_scrape_unit {
969 output_options.show_diagnostics = false;
970 }
971
972 Ok(Work::new(move |state| {
973 add_custom_flags(
974 &mut rustdoc,
975 &build_script_outputs.lock().unwrap(),
976 script_metadatas,
977 )?;
978
979 if let Some(scrape_outputs) = scrape_outputs {
984 let failed_scrape_units = failed_scrape_units.lock().unwrap();
985 for (metadata, output_path) in &scrape_outputs {
986 if !failed_scrape_units.contains(metadata) {
987 rustdoc.arg("--with-examples").arg(output_path);
988 }
989 }
990 }
991
992 let crate_dir = doc_dir.join(&crate_name);
993 if crate_dir.exists() {
994 debug!("removing pre-existing doc directory {:?}", crate_dir);
997 paths::remove_dir_all(crate_dir)?;
998 }
999 state.running(&rustdoc);
1000 let timestamp = paths::set_invocation_time(&fingerprint_dir)?;
1001
1002 let result = rustdoc
1003 .exec_with_streaming(
1004 &mut |line| on_stdout_line(state, line, package_id, &target),
1005 &mut |line| {
1006 on_stderr_line(
1007 state,
1008 line,
1009 package_id,
1010 &manifest,
1011 &target,
1012 &mut output_options,
1013 )
1014 },
1015 false,
1016 )
1017 .map_err(verbose_if_simple_exit_code)
1018 .with_context(|| format!("could not document `{}`", name));
1019
1020 if let Err(e) = result {
1021 if let Some(diagnostic) = failed_scrape_diagnostic {
1022 state.warning(diagnostic);
1023 }
1024
1025 return Err(e);
1026 }
1027
1028 if rustdoc_depinfo_enabled && rustdoc_dep_info_loc.exists() {
1029 fingerprint::translate_dep_info(
1030 &rustdoc_dep_info_loc,
1031 &dep_info_loc,
1032 &cwd,
1033 &pkg_root,
1034 &build_dir,
1035 &rustdoc,
1036 is_local,
1038 &env_config,
1039 )
1040 .with_context(|| {
1041 internal(format_args!(
1042 "could not parse/generate dep info at: {}",
1043 rustdoc_dep_info_loc.display()
1044 ))
1045 })?;
1046 paths::set_file_time_no_err(dep_info_loc, timestamp);
1049 }
1050
1051 Ok(())
1052 }))
1053}
1054
1055fn crate_version_flag_already_present(rustdoc: &ProcessBuilder) -> bool {
1058 rustdoc.get_args().any(|flag| {
1059 flag.to_str()
1060 .map_or(false, |flag| flag.starts_with(RUSTDOC_CRATE_VERSION_FLAG))
1061 })
1062}
1063
1064fn append_crate_version_flag(unit: &Unit, rustdoc: &mut ProcessBuilder) {
1065 rustdoc
1066 .arg(RUSTDOC_CRATE_VERSION_FLAG)
1067 .arg(unit.pkg.version().to_string());
1068}
1069
1070fn add_cap_lints(bcx: &BuildContext<'_, '_>, unit: &Unit, cmd: &mut ProcessBuilder) {
1074 if !unit.show_warnings(bcx.gctx) {
1077 cmd.arg("--cap-lints").arg("allow");
1078
1079 } else if !unit.is_local() {
1082 cmd.arg("--cap-lints").arg("warn");
1083 }
1084}
1085
1086fn add_allow_features(build_runner: &BuildRunner<'_, '_>, cmd: &mut ProcessBuilder) {
1090 if let Some(allow) = &build_runner.bcx.gctx.cli_unstable().allow_features {
1091 use std::fmt::Write;
1092 let mut arg = String::from("-Zallow-features=");
1093 for f in allow {
1094 let _ = write!(&mut arg, "{f},");
1095 }
1096 cmd.arg(arg.trim_end_matches(','));
1097 }
1098}
1099
1100fn add_error_format_and_color(build_runner: &BuildRunner<'_, '_>, cmd: &mut ProcessBuilder) {
1111 let enable_timings = build_runner.bcx.gctx.cli_unstable().section_timings
1112 && !build_runner.bcx.build_config.timing_outputs.is_empty();
1113 if enable_timings {
1114 cmd.arg("-Zunstable-options");
1115 }
1116
1117 cmd.arg("--error-format=json");
1118 let mut json = String::from("--json=diagnostic-rendered-ansi,artifacts,future-incompat");
1119
1120 match build_runner.bcx.build_config.message_format {
1121 MessageFormat::Short | MessageFormat::Json { short: true, .. } => {
1122 json.push_str(",diagnostic-short");
1123 }
1124 _ => {}
1125 }
1126
1127 if enable_timings {
1128 json.push_str(",timings");
1129 }
1130
1131 cmd.arg(json);
1132
1133 let gctx = build_runner.bcx.gctx;
1134 if let Some(width) = gctx.shell().err_width().diagnostic_terminal_width() {
1135 cmd.arg(format!("--diagnostic-width={width}"));
1136 }
1137}
1138
1139fn build_base_args(
1141 build_runner: &BuildRunner<'_, '_>,
1142 cmd: &mut ProcessBuilder,
1143 unit: &Unit,
1144) -> CargoResult<()> {
1145 assert!(!unit.mode.is_run_custom_build());
1146
1147 let bcx = build_runner.bcx;
1148 let Profile {
1149 ref opt_level,
1150 codegen_backend,
1151 codegen_units,
1152 debuginfo,
1153 debug_assertions,
1154 split_debuginfo,
1155 overflow_checks,
1156 rpath,
1157 ref panic,
1158 incremental,
1159 strip,
1160 rustflags: profile_rustflags,
1161 trim_paths,
1162 hint_mostly_unused: profile_hint_mostly_unused,
1163 ..
1164 } = unit.profile.clone();
1165 let hints = unit.pkg.hints().cloned().unwrap_or_default();
1166 let test = unit.mode.is_any_test();
1167
1168 let warn = |msg: &str| {
1169 bcx.gctx.shell().warn(format!(
1170 "{}@{}: {msg}",
1171 unit.pkg.package_id().name(),
1172 unit.pkg.package_id().version()
1173 ))
1174 };
1175 let unit_capped_warn = |msg: &str| {
1176 if unit.show_warnings(bcx.gctx) {
1177 warn(msg)
1178 } else {
1179 Ok(())
1180 }
1181 };
1182
1183 cmd.arg("--crate-name").arg(&unit.target.crate_name());
1184
1185 let edition = unit.target.edition();
1186 edition.cmd_edition_arg(cmd);
1187
1188 add_path_args(bcx.ws, unit, cmd);
1189 add_error_format_and_color(build_runner, cmd);
1190 add_allow_features(build_runner, cmd);
1191
1192 let mut contains_dy_lib = false;
1193 if !test {
1194 for crate_type in &unit.target.rustc_crate_types() {
1195 cmd.arg("--crate-type").arg(crate_type.as_str());
1196 contains_dy_lib |= crate_type == &CrateType::Dylib;
1197 }
1198 }
1199
1200 if unit.mode.is_check() {
1201 cmd.arg("--emit=dep-info,metadata");
1202 } else if build_runner.bcx.gctx.cli_unstable().no_embed_metadata {
1203 if unit.benefits_from_no_embed_metadata() {
1213 cmd.arg("--emit=dep-info,metadata,link");
1214 cmd.args(&["-Z", "embed-metadata=no"]);
1215 } else {
1216 cmd.arg("--emit=dep-info,link");
1217 }
1218 } else {
1219 if !unit.requires_upstream_objects() {
1223 cmd.arg("--emit=dep-info,metadata,link");
1224 } else {
1225 cmd.arg("--emit=dep-info,link");
1226 }
1227 }
1228
1229 let prefer_dynamic = (unit.target.for_host() && !unit.target.is_custom_build())
1230 || (contains_dy_lib && !build_runner.is_primary_package(unit));
1231 if prefer_dynamic {
1232 cmd.arg("-C").arg("prefer-dynamic");
1233 }
1234
1235 if opt_level.as_str() != "0" {
1236 cmd.arg("-C").arg(&format!("opt-level={}", opt_level));
1237 }
1238
1239 if *panic != PanicStrategy::Unwind {
1240 cmd.arg("-C").arg(format!("panic={}", panic));
1241 }
1242 if *panic == PanicStrategy::ImmediateAbort {
1243 cmd.arg("-Z").arg("unstable-options");
1244 }
1245
1246 cmd.args(<o_args(build_runner, unit));
1247
1248 if let Some(backend) = codegen_backend {
1249 cmd.arg("-Z").arg(&format!("codegen-backend={}", backend));
1250 }
1251
1252 if let Some(n) = codegen_units {
1253 cmd.arg("-C").arg(&format!("codegen-units={}", n));
1254 }
1255
1256 let debuginfo = debuginfo.into_inner();
1257 if debuginfo != TomlDebugInfo::None {
1259 cmd.arg("-C").arg(format!("debuginfo={debuginfo}"));
1260 if let Some(split) = split_debuginfo {
1267 if build_runner
1268 .bcx
1269 .target_data
1270 .info(unit.kind)
1271 .supports_debuginfo_split(split)
1272 {
1273 cmd.arg("-C").arg(format!("split-debuginfo={split}"));
1274 }
1275 }
1276 }
1277
1278 if let Some(trim_paths) = trim_paths {
1279 trim_paths_args(cmd, build_runner, unit, &trim_paths)?;
1280 }
1281
1282 cmd.args(unit.pkg.manifest().lint_rustflags());
1283 cmd.args(&profile_rustflags);
1284
1285 if opt_level.as_str() != "0" {
1289 if debug_assertions {
1290 cmd.args(&["-C", "debug-assertions=on"]);
1291 if !overflow_checks {
1292 cmd.args(&["-C", "overflow-checks=off"]);
1293 }
1294 } else if overflow_checks {
1295 cmd.args(&["-C", "overflow-checks=on"]);
1296 }
1297 } else if !debug_assertions {
1298 cmd.args(&["-C", "debug-assertions=off"]);
1299 if overflow_checks {
1300 cmd.args(&["-C", "overflow-checks=on"]);
1301 }
1302 } else if !overflow_checks {
1303 cmd.args(&["-C", "overflow-checks=off"]);
1304 }
1305
1306 if test && unit.target.harness() {
1307 cmd.arg("--test");
1308
1309 if *panic == PanicStrategy::Abort || *panic == PanicStrategy::ImmediateAbort {
1317 cmd.arg("-Z").arg("panic-abort-tests");
1318 }
1319 } else if test {
1320 cmd.arg("--cfg").arg("test");
1321 }
1322
1323 cmd.args(&features_args(unit));
1324 cmd.args(&check_cfg_args(unit));
1325
1326 let meta = build_runner.files().metadata(unit);
1327 cmd.arg("-C")
1328 .arg(&format!("metadata={}", meta.c_metadata()));
1329 if let Some(c_extra_filename) = meta.c_extra_filename() {
1330 cmd.arg("-C")
1331 .arg(&format!("extra-filename=-{c_extra_filename}"));
1332 }
1333
1334 if rpath {
1335 cmd.arg("-C").arg("rpath");
1336 }
1337
1338 cmd.arg("--out-dir")
1339 .arg(&build_runner.files().out_dir(unit));
1340
1341 fn opt(cmd: &mut ProcessBuilder, key: &str, prefix: &str, val: Option<&OsStr>) {
1342 if let Some(val) = val {
1343 let mut joined = OsString::from(prefix);
1344 joined.push(val);
1345 cmd.arg(key).arg(joined);
1346 }
1347 }
1348
1349 if let CompileKind::Target(n) = unit.kind {
1350 cmd.arg("--target").arg(n.rustc_target());
1351 }
1352
1353 opt(
1354 cmd,
1355 "-C",
1356 "linker=",
1357 build_runner
1358 .compilation
1359 .target_linker(unit.kind)
1360 .as_ref()
1361 .map(|s| s.as_ref()),
1362 );
1363 if incremental {
1364 let dir = build_runner.files().incremental_dir(&unit);
1365 opt(cmd, "-C", "incremental=", Some(dir.as_os_str()));
1366 }
1367
1368 let pkg_hint_mostly_unused = match hints.mostly_unused {
1369 None => None,
1370 Some(toml::Value::Boolean(b)) => Some(b),
1371 Some(v) => {
1372 unit_capped_warn(&format!(
1373 "ignoring unsupported value type ({}) for 'hints.mostly-unused', which expects a boolean",
1374 v.type_str()
1375 ))?;
1376 None
1377 }
1378 };
1379 if profile_hint_mostly_unused
1380 .or(pkg_hint_mostly_unused)
1381 .unwrap_or(false)
1382 {
1383 if bcx.gctx.cli_unstable().profile_hint_mostly_unused {
1384 cmd.arg("-Zhint-mostly-unused");
1385 } else {
1386 if profile_hint_mostly_unused.is_some() {
1387 warn(
1389 "ignoring 'hint-mostly-unused' profile option, pass `-Zprofile-hint-mostly-unused` to enable it",
1390 )?;
1391 } else if pkg_hint_mostly_unused.is_some() {
1392 unit_capped_warn(
1393 "ignoring 'hints.mostly-unused', pass `-Zprofile-hint-mostly-unused` to enable it",
1394 )?;
1395 }
1396 }
1397 }
1398
1399 let strip = strip.into_inner();
1400 if strip != StripInner::None {
1401 cmd.arg("-C").arg(format!("strip={}", strip));
1402 }
1403
1404 if unit.is_std {
1405 cmd.arg("-Z")
1411 .arg("force-unstable-if-unmarked")
1412 .env("RUSTC_BOOTSTRAP", "1");
1413 }
1414
1415 if unit.target.is_test() || unit.target.is_bench() {
1417 for bin_target in unit
1418 .pkg
1419 .manifest()
1420 .targets()
1421 .iter()
1422 .filter(|target| target.is_bin())
1423 {
1424 let exe_path = build_runner.files().bin_link_for_target(
1425 bin_target,
1426 unit.kind,
1427 build_runner.bcx,
1428 )?;
1429 let name = bin_target
1430 .binary_filename()
1431 .unwrap_or(bin_target.name().to_string());
1432 let key = format!("CARGO_BIN_EXE_{}", name);
1433 cmd.env(&key, exe_path);
1434 }
1435 }
1436 Ok(())
1437}
1438
1439fn features_args(unit: &Unit) -> Vec<OsString> {
1441 let mut args = Vec::with_capacity(unit.features.len() * 2);
1442
1443 for feat in &unit.features {
1444 args.push(OsString::from("--cfg"));
1445 args.push(OsString::from(format!("feature=\"{}\"", feat)));
1446 }
1447
1448 args
1449}
1450
1451fn trim_paths_args_rustdoc(
1453 cmd: &mut ProcessBuilder,
1454 build_runner: &BuildRunner<'_, '_>,
1455 unit: &Unit,
1456 trim_paths: &TomlTrimPaths,
1457) -> CargoResult<()> {
1458 match trim_paths {
1459 TomlTrimPaths::Values(values) if !values.contains(&TomlTrimPathsValue::Diagnostics) => {
1461 return Ok(());
1462 }
1463 _ => {}
1464 }
1465
1466 cmd.arg("-Zunstable-options");
1468
1469 cmd.arg(package_remap(build_runner, unit));
1472 cmd.arg(build_dir_remap(build_runner));
1473 cmd.arg(sysroot_remap(build_runner, unit));
1474
1475 Ok(())
1476}
1477
1478fn trim_paths_args(
1484 cmd: &mut ProcessBuilder,
1485 build_runner: &BuildRunner<'_, '_>,
1486 unit: &Unit,
1487 trim_paths: &TomlTrimPaths,
1488) -> CargoResult<()> {
1489 if trim_paths.is_none() {
1490 return Ok(());
1491 }
1492
1493 cmd.arg("-Zunstable-options");
1495 cmd.arg(format!("-Zremap-path-scope={trim_paths}"));
1496
1497 cmd.arg(package_remap(build_runner, unit));
1500 cmd.arg(build_dir_remap(build_runner));
1501 cmd.arg(sysroot_remap(build_runner, unit));
1502
1503 Ok(())
1504}
1505
1506fn sysroot_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1511 let mut remap = OsString::from("--remap-path-prefix=");
1512 remap.push({
1513 let mut sysroot = build_runner.bcx.target_data.info(unit.kind).sysroot.clone();
1515 sysroot.push("lib");
1516 sysroot.push("rustlib");
1517 sysroot.push("src");
1518 sysroot.push("rust");
1519 sysroot
1520 });
1521 remap.push("=");
1522 remap.push("/rustc/");
1523 if let Some(commit_hash) = build_runner.bcx.rustc().commit_hash.as_ref() {
1524 remap.push(commit_hash);
1525 } else {
1526 remap.push(build_runner.bcx.rustc().version.to_string());
1527 }
1528 remap
1529}
1530
1531fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1539 let pkg_root = unit.pkg.root();
1540 let ws_root = build_runner.bcx.ws.root();
1541 let mut remap = OsString::from("--remap-path-prefix=");
1542 let source_id = unit.pkg.package_id().source_id();
1543 if source_id.is_git() {
1544 remap.push(
1545 build_runner
1546 .bcx
1547 .gctx
1548 .git_checkouts_path()
1549 .as_path_unlocked(),
1550 );
1551 remap.push("=");
1552 } else if source_id.is_registry() {
1553 remap.push(
1554 build_runner
1555 .bcx
1556 .gctx
1557 .registry_source_path()
1558 .as_path_unlocked(),
1559 );
1560 remap.push("=");
1561 } else if pkg_root.strip_prefix(ws_root).is_ok() {
1562 remap.push(ws_root);
1563 remap.push("=."); } else {
1565 remap.push(pkg_root);
1566 remap.push("=");
1567 remap.push(unit.pkg.name());
1568 remap.push("-");
1569 remap.push(unit.pkg.version().to_string());
1570 }
1571 remap
1572}
1573
1574fn build_dir_remap(build_runner: &BuildRunner<'_, '_>) -> OsString {
1587 let build_dir = build_runner.bcx.ws.build_dir();
1588 let mut remap = OsString::from("--remap-path-prefix=");
1589 remap.push(build_dir.as_path_unlocked());
1590 remap.push("=/cargo/build-dir");
1591 remap
1592}
1593
1594fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
1596 let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1614 let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
1615
1616 arg_feature.push("cfg(feature, values(");
1617 for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1618 if i != 0 {
1619 arg_feature.push(", ");
1620 }
1621 arg_feature.push("\"");
1622 arg_feature.push(feature);
1623 arg_feature.push("\"");
1624 }
1625 arg_feature.push("))");
1626
1627 vec![
1636 OsString::from("--check-cfg"),
1637 OsString::from("cfg(docsrs,test)"),
1638 OsString::from("--check-cfg"),
1639 arg_feature,
1640 ]
1641}
1642
1643fn lto_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsString> {
1645 let mut result = Vec::new();
1646 let mut push = |arg: &str| {
1647 result.push(OsString::from("-C"));
1648 result.push(OsString::from(arg));
1649 };
1650 match build_runner.lto[unit] {
1651 lto::Lto::Run(None) => push("lto"),
1652 lto::Lto::Run(Some(s)) => push(&format!("lto={}", s)),
1653 lto::Lto::Off => {
1654 push("lto=off");
1655 push("embed-bitcode=no");
1656 }
1657 lto::Lto::ObjectAndBitcode => {} lto::Lto::OnlyBitcode => push("linker-plugin-lto"),
1659 lto::Lto::OnlyObject => push("embed-bitcode=no"),
1660 }
1661 result
1662}
1663
1664fn build_deps_args(
1670 cmd: &mut ProcessBuilder,
1671 build_runner: &BuildRunner<'_, '_>,
1672 unit: &Unit,
1673) -> CargoResult<()> {
1674 let bcx = build_runner.bcx;
1675 if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout {
1676 let mut map = BTreeMap::new();
1677
1678 add_dep_arg(&mut map, build_runner, unit);
1680
1681 let paths = map.into_iter().map(|(_, path)| path).sorted_unstable();
1682
1683 for path in paths {
1684 cmd.arg("-L").arg(&{
1685 let mut deps = OsString::from("dependency=");
1686 deps.push(path);
1687 deps
1688 });
1689 }
1690 } else {
1691 cmd.arg("-L").arg(&{
1692 let mut deps = OsString::from("dependency=");
1693 deps.push(build_runner.files().deps_dir(unit));
1694 deps
1695 });
1696 }
1697
1698 if !unit.kind.is_host() {
1701 cmd.arg("-L").arg(&{
1702 let mut deps = OsString::from("dependency=");
1703 deps.push(build_runner.files().host_deps(unit));
1704 deps
1705 });
1706 }
1707
1708 let deps = build_runner.unit_deps(unit);
1709
1710 if !deps
1714 .iter()
1715 .any(|dep| !dep.unit.mode.is_doc() && dep.unit.target.is_linkable())
1716 {
1717 if let Some(dep) = deps.iter().find(|dep| {
1718 !dep.unit.mode.is_doc() && dep.unit.target.is_lib() && !dep.unit.artifact.is_true()
1719 }) {
1720 bcx.gctx.shell().warn(format!(
1721 "The package `{}` \
1722 provides no linkable target. The compiler might raise an error while compiling \
1723 `{}`. Consider adding 'dylib' or 'rlib' to key `crate-type` in `{}`'s \
1724 Cargo.toml. This warning might turn into a hard error in the future.",
1725 dep.unit.target.crate_name(),
1726 unit.target.crate_name(),
1727 dep.unit.target.crate_name()
1728 ))?;
1729 }
1730 }
1731
1732 let mut unstable_opts = false;
1733
1734 let first_custom_build_dep = deps.iter().find(|dep| dep.unit.mode.is_run_custom_build());
1736 if let Some(dep) = first_custom_build_dep {
1737 let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
1738 cmd.env("OUT_DIR", &out_dir);
1739 }
1740
1741 let is_multiple_build_scripts_enabled = unit
1743 .pkg
1744 .manifest()
1745 .unstable_features()
1746 .require(Feature::multiple_build_scripts())
1747 .is_ok();
1748
1749 if is_multiple_build_scripts_enabled {
1750 for dep in deps {
1751 if dep.unit.mode.is_run_custom_build() {
1752 let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
1753 let target_name = dep.unit.target.name();
1754 let out_dir_prefix = target_name
1755 .strip_prefix("build-script-")
1756 .unwrap_or(target_name);
1757 let out_dir_name = format!("{out_dir_prefix}_OUT_DIR");
1758 cmd.env(&out_dir_name, &out_dir);
1759 }
1760 }
1761 }
1762 for arg in extern_args(build_runner, unit, &mut unstable_opts)? {
1763 cmd.arg(arg);
1764 }
1765
1766 for (var, env) in artifact::get_env(build_runner, deps)? {
1767 cmd.env(&var, env);
1768 }
1769
1770 if unstable_opts {
1773 cmd.arg("-Z").arg("unstable-options");
1774 }
1775
1776 Ok(())
1777}
1778
1779fn add_dep_arg<'a, 'b: 'a>(
1780 map: &mut BTreeMap<&'a Unit, PathBuf>,
1781 build_runner: &'b BuildRunner<'b, '_>,
1782 unit: &'a Unit,
1783) {
1784 if map.contains_key(&unit) {
1785 return;
1786 }
1787 map.insert(&unit, build_runner.files().deps_dir(&unit));
1788
1789 for dep in build_runner.unit_deps(unit) {
1790 add_dep_arg(map, build_runner, &dep.unit);
1791 }
1792}
1793
1794fn add_custom_flags(
1798 cmd: &mut ProcessBuilder,
1799 build_script_outputs: &BuildScriptOutputs,
1800 metadata_vec: Option<Vec<UnitHash>>,
1801) -> CargoResult<()> {
1802 if let Some(metadata_vec) = metadata_vec {
1803 for metadata in metadata_vec {
1804 if let Some(output) = build_script_outputs.get(metadata) {
1805 for cfg in output.cfgs.iter() {
1806 cmd.arg("--cfg").arg(cfg);
1807 }
1808 for check_cfg in &output.check_cfgs {
1809 cmd.arg("--check-cfg").arg(check_cfg);
1810 }
1811 for (name, value) in output.env.iter() {
1812 cmd.env(name, value);
1813 }
1814 }
1815 }
1816 }
1817
1818 Ok(())
1819}
1820
1821pub fn extern_args(
1823 build_runner: &BuildRunner<'_, '_>,
1824 unit: &Unit,
1825 unstable_opts: &mut bool,
1826) -> CargoResult<Vec<OsString>> {
1827 let mut result = Vec::new();
1828 let deps = build_runner.unit_deps(unit);
1829
1830 let no_embed_metadata = build_runner.bcx.gctx.cli_unstable().no_embed_metadata;
1831
1832 let mut link_to =
1834 |dep: &UnitDep, extern_crate_name: InternedString, noprelude: bool| -> CargoResult<()> {
1835 let mut value = OsString::new();
1836 let mut opts = Vec::new();
1837 let is_public_dependency_enabled = unit
1838 .pkg
1839 .manifest()
1840 .unstable_features()
1841 .require(Feature::public_dependency())
1842 .is_ok()
1843 || build_runner.bcx.gctx.cli_unstable().public_dependency;
1844 if !dep.public && unit.target.is_lib() && is_public_dependency_enabled {
1845 opts.push("priv");
1846 *unstable_opts = true;
1847 }
1848 if noprelude {
1849 opts.push("noprelude");
1850 *unstable_opts = true;
1851 }
1852 if !opts.is_empty() {
1853 value.push(opts.join(","));
1854 value.push(":");
1855 }
1856 value.push(extern_crate_name.as_str());
1857 value.push("=");
1858
1859 let mut pass = |file| {
1860 let mut value = value.clone();
1861 value.push(file);
1862 result.push(OsString::from("--extern"));
1863 result.push(value);
1864 };
1865
1866 let outputs = build_runner.outputs(&dep.unit)?;
1867
1868 if build_runner.only_requires_rmeta(unit, &dep.unit) || dep.unit.mode.is_check() {
1869 let output = outputs
1871 .iter()
1872 .find(|output| output.flavor == FileFlavor::Rmeta)
1873 .expect("failed to find rmeta dep for pipelined dep");
1874 pass(&output.path);
1875 } else {
1876 for output in outputs.iter() {
1878 if output.flavor == FileFlavor::Linkable {
1879 pass(&output.path);
1880 }
1881 else if no_embed_metadata && output.flavor == FileFlavor::Rmeta {
1885 pass(&output.path);
1886 }
1887 }
1888 }
1889 Ok(())
1890 };
1891
1892 for dep in deps {
1893 if dep.unit.target.is_linkable() && !dep.unit.mode.is_doc() {
1894 link_to(dep, dep.extern_crate_name, dep.noprelude)?;
1895 }
1896 }
1897 if unit.target.proc_macro() {
1898 result.push(OsString::from("--extern"));
1900 result.push(OsString::from("proc_macro"));
1901 }
1902
1903 Ok(result)
1904}
1905
1906fn envify(s: &str) -> String {
1907 s.chars()
1908 .flat_map(|c| c.to_uppercase())
1909 .map(|c| if c == '-' { '_' } else { c })
1910 .collect()
1911}
1912
1913struct OutputOptions {
1916 format: MessageFormat,
1918 cache_cell: Option<(PathBuf, LazyCell<File>)>,
1923 show_diagnostics: bool,
1931 warnings_seen: usize,
1933 errors_seen: usize,
1935}
1936
1937impl OutputOptions {
1938 fn new(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OutputOptions {
1939 let path = build_runner.files().message_cache_path(unit);
1940 drop(fs::remove_file(&path));
1942 let cache_cell = Some((path, LazyCell::new()));
1943 let show_diagnostics =
1944 build_runner.bcx.gctx.warning_handling().unwrap_or_default() != WarningHandling::Allow;
1945 OutputOptions {
1946 format: build_runner.bcx.build_config.message_format,
1947 cache_cell,
1948 show_diagnostics,
1949 warnings_seen: 0,
1950 errors_seen: 0,
1951 }
1952 }
1953}
1954
1955struct ManifestErrorContext {
1961 path: PathBuf,
1963 spans: toml::Spanned<toml::de::DeTable<'static>>,
1965 contents: String,
1967 rename_table: HashMap<InternedString, InternedString>,
1970 requested_kinds: Vec<CompileKind>,
1973 cfgs: Vec<Vec<Cfg>>,
1976 host_name: InternedString,
1977 cwd: PathBuf,
1979 term_width: usize,
1981}
1982
1983fn on_stdout_line(
1984 state: &JobState<'_, '_>,
1985 line: &str,
1986 _package_id: PackageId,
1987 _target: &Target,
1988) -> CargoResult<()> {
1989 state.stdout(line.to_string())?;
1990 Ok(())
1991}
1992
1993fn on_stderr_line(
1994 state: &JobState<'_, '_>,
1995 line: &str,
1996 package_id: PackageId,
1997 manifest: &ManifestErrorContext,
1998 target: &Target,
1999 options: &mut OutputOptions,
2000) -> CargoResult<()> {
2001 if on_stderr_line_inner(state, line, package_id, manifest, target, options)? {
2002 if let Some((path, cell)) = &mut options.cache_cell {
2004 let f = cell.try_borrow_mut_with(|| paths::create(path))?;
2006 debug_assert!(!line.contains('\n'));
2007 f.write_all(line.as_bytes())?;
2008 f.write_all(&[b'\n'])?;
2009 }
2010 }
2011 Ok(())
2012}
2013
2014fn on_stderr_line_inner(
2016 state: &JobState<'_, '_>,
2017 line: &str,
2018 package_id: PackageId,
2019 manifest: &ManifestErrorContext,
2020 target: &Target,
2021 options: &mut OutputOptions,
2022) -> CargoResult<bool> {
2023 if !line.starts_with('{') {
2029 state.stderr(line.to_string())?;
2030 return Ok(true);
2031 }
2032
2033 let mut compiler_message: Box<serde_json::value::RawValue> = match serde_json::from_str(line) {
2034 Ok(msg) => msg,
2035
2036 Err(e) => {
2040 debug!("failed to parse json: {:?}", e);
2041 state.stderr(line.to_string())?;
2042 return Ok(true);
2043 }
2044 };
2045
2046 let count_diagnostic = |level, options: &mut OutputOptions| {
2047 if level == "warning" {
2048 options.warnings_seen += 1;
2049 } else if level == "error" {
2050 options.errors_seen += 1;
2051 }
2052 };
2053
2054 if let Ok(report) = serde_json::from_str::<FutureIncompatReport>(compiler_message.get()) {
2055 for item in &report.future_incompat_report {
2056 count_diagnostic(&*item.diagnostic.level, options);
2057 }
2058 state.future_incompat_report(report.future_incompat_report);
2059 return Ok(true);
2060 }
2061
2062 let res = serde_json::from_str::<SectionTiming>(compiler_message.get());
2063 if let Ok(timing_record) = res {
2064 state.on_section_timing_emitted(timing_record);
2065 return Ok(false);
2066 }
2067
2068 let add_pub_in_priv_diagnostic = |diag: &mut String| -> bool {
2070 static PRIV_DEP_REGEX: LazyLock<Regex> =
2079 LazyLock::new(|| Regex::new("from private dependency '([A-Za-z0-9-_]+)'").unwrap());
2080 if let Some(crate_name) = PRIV_DEP_REGEX.captures(diag).and_then(|m| m.get(1))
2081 && let Some(span) = manifest.find_crate_span(crate_name.as_str())
2082 {
2083 let rel_path = pathdiff::diff_paths(&manifest.path, &manifest.cwd)
2084 .unwrap_or_else(|| manifest.path.clone())
2085 .display()
2086 .to_string();
2087 let report = [Group::with_title(Level::NOTE.secondary_title(format!(
2088 "dependency `{}` declared here",
2089 crate_name.as_str()
2090 )))
2091 .element(
2092 Snippet::source(&manifest.contents)
2093 .path(rel_path)
2094 .annotation(AnnotationKind::Context.span(span)),
2095 )];
2096
2097 let rendered = Renderer::styled()
2098 .term_width(manifest.term_width)
2099 .render(&report);
2100 diag.push_str(&rendered);
2101 diag.push('\n');
2102 return true;
2103 }
2104 false
2105 };
2106
2107 match options.format {
2110 MessageFormat::Human
2115 | MessageFormat::Short
2116 | MessageFormat::Json {
2117 render_diagnostics: true,
2118 ..
2119 } => {
2120 #[derive(serde::Deserialize)]
2121 struct CompilerMessage<'a> {
2122 rendered: String,
2126 #[serde(borrow)]
2127 message: Cow<'a, str>,
2128 #[serde(borrow)]
2129 level: Cow<'a, str>,
2130 children: Vec<PartialDiagnostic>,
2131 code: Option<DiagnosticCode>,
2132 }
2133
2134 #[derive(serde::Deserialize)]
2143 struct PartialDiagnostic {
2144 spans: Vec<PartialDiagnosticSpan>,
2145 }
2146
2147 #[derive(serde::Deserialize)]
2149 struct PartialDiagnosticSpan {
2150 suggestion_applicability: Option<Applicability>,
2151 }
2152
2153 #[derive(serde::Deserialize)]
2154 struct DiagnosticCode {
2155 code: String,
2156 }
2157
2158 if let Ok(mut msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
2159 {
2160 if msg.message.starts_with("aborting due to")
2161 || msg.message.ends_with("warning emitted")
2162 || msg.message.ends_with("warnings emitted")
2163 {
2164 return Ok(true);
2166 }
2167 if msg.rendered.ends_with('\n') {
2169 msg.rendered.pop();
2170 }
2171 let mut rendered = msg.rendered;
2172 if options.show_diagnostics {
2173 let machine_applicable: bool = msg
2174 .children
2175 .iter()
2176 .map(|child| {
2177 child
2178 .spans
2179 .iter()
2180 .filter_map(|span| span.suggestion_applicability)
2181 .any(|app| app == Applicability::MachineApplicable)
2182 })
2183 .any(|b| b);
2184 count_diagnostic(&msg.level, options);
2185 if msg
2186 .code
2187 .is_some_and(|c| c.code == "exported_private_dependencies")
2188 && options.format != MessageFormat::Short
2189 {
2190 add_pub_in_priv_diagnostic(&mut rendered);
2191 }
2192 state.emit_diag(&msg.level, rendered, machine_applicable)?;
2193 }
2194 return Ok(true);
2195 }
2196 }
2197
2198 MessageFormat::Json { ansi, .. } => {
2199 #[derive(serde::Deserialize, serde::Serialize)]
2200 struct CompilerMessage<'a> {
2201 rendered: String,
2202 #[serde(flatten, borrow)]
2203 other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
2204 code: Option<DiagnosticCode<'a>>,
2205 }
2206
2207 #[derive(serde::Deserialize, serde::Serialize)]
2208 struct DiagnosticCode<'a> {
2209 code: String,
2210 #[serde(flatten, borrow)]
2211 other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
2212 }
2213
2214 if let Ok(mut error) =
2215 serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
2216 {
2217 let modified_diag = if error
2218 .code
2219 .as_ref()
2220 .is_some_and(|c| c.code == "exported_private_dependencies")
2221 {
2222 add_pub_in_priv_diagnostic(&mut error.rendered)
2223 } else {
2224 false
2225 };
2226
2227 if !ansi {
2231 error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
2232 }
2233 if !ansi || modified_diag {
2234 let new_line = serde_json::to_string(&error)?;
2235 compiler_message = serde_json::value::RawValue::from_string(new_line)?;
2236 }
2237 }
2238 }
2239 }
2240
2241 #[derive(serde::Deserialize)]
2248 struct ArtifactNotification<'a> {
2249 #[serde(borrow)]
2250 artifact: Cow<'a, str>,
2251 }
2252
2253 if let Ok(artifact) = serde_json::from_str::<ArtifactNotification<'_>>(compiler_message.get()) {
2254 trace!("found directive from rustc: `{}`", artifact.artifact);
2255 if artifact.artifact.ends_with(".rmeta") {
2256 debug!("looks like metadata finished early!");
2257 state.rmeta_produced();
2258 }
2259 return Ok(false);
2260 }
2261
2262 if !options.show_diagnostics {
2267 return Ok(true);
2268 }
2269
2270 #[derive(serde::Deserialize)]
2271 struct CompilerMessage<'a> {
2272 #[serde(borrow)]
2273 message: Cow<'a, str>,
2274 #[serde(borrow)]
2275 level: Cow<'a, str>,
2276 }
2277
2278 if let Ok(msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get()) {
2279 if msg.message.starts_with("aborting due to")
2280 || msg.message.ends_with("warning emitted")
2281 || msg.message.ends_with("warnings emitted")
2282 {
2283 return Ok(true);
2285 }
2286 count_diagnostic(&msg.level, options);
2287 }
2288
2289 let msg = machine_message::FromCompiler {
2290 package_id: package_id.to_spec(),
2291 manifest_path: &manifest.path,
2292 target,
2293 message: compiler_message,
2294 }
2295 .to_json_string();
2296
2297 state.stdout(msg)?;
2301 Ok(true)
2302}
2303
2304impl ManifestErrorContext {
2305 fn new(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> ManifestErrorContext {
2306 let mut duplicates = HashSet::new();
2307 let mut rename_table = HashMap::new();
2308
2309 for dep in build_runner.unit_deps(unit) {
2310 let unrenamed_id = dep.unit.pkg.package_id().name();
2311 if duplicates.contains(&unrenamed_id) {
2312 continue;
2313 }
2314 match rename_table.entry(unrenamed_id) {
2315 std::collections::hash_map::Entry::Occupied(occ) => {
2316 occ.remove_entry();
2317 duplicates.insert(unrenamed_id);
2318 }
2319 std::collections::hash_map::Entry::Vacant(vac) => {
2320 vac.insert(dep.extern_crate_name);
2321 }
2322 }
2323 }
2324
2325 let bcx = build_runner.bcx;
2326 ManifestErrorContext {
2327 path: unit.pkg.manifest_path().to_owned(),
2328 spans: unit.pkg.manifest().document().clone(),
2329 contents: unit.pkg.manifest().contents().to_owned(),
2330 requested_kinds: bcx.target_data.requested_kinds().to_owned(),
2331 host_name: bcx.rustc().host,
2332 rename_table,
2333 cwd: path_args(build_runner.bcx.ws, unit).1,
2334 cfgs: bcx
2335 .target_data
2336 .requested_kinds()
2337 .iter()
2338 .map(|k| bcx.target_data.cfg(*k).to_owned())
2339 .collect(),
2340 term_width: bcx
2341 .gctx
2342 .shell()
2343 .err_width()
2344 .diagnostic_terminal_width()
2345 .unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
2346 }
2347 }
2348
2349 fn requested_target_names(&self) -> impl Iterator<Item = &str> {
2350 self.requested_kinds.iter().map(|kind| match kind {
2351 CompileKind::Host => &self.host_name,
2352 CompileKind::Target(target) => target.short_name(),
2353 })
2354 }
2355
2356 fn find_crate_span(&self, unrenamed: &str) -> Option<Range<usize>> {
2370 let orig_name = self.rename_table.get(unrenamed)?.as_str();
2371
2372 if let Some((k, v)) = get_key_value(&self.spans, &["dependencies", orig_name]) {
2373 if let Some(package) = v.get_ref().as_table().and_then(|t| t.get("package")) {
2382 return Some(package.span());
2383 } else {
2384 return Some(k.span());
2385 }
2386 }
2387
2388 if let Some(target) = self
2393 .spans
2394 .as_ref()
2395 .get("target")
2396 .and_then(|t| t.as_ref().as_table())
2397 {
2398 for (platform, platform_table) in target.iter() {
2399 match platform.as_ref().parse::<Platform>() {
2400 Ok(Platform::Name(name)) => {
2401 if !self.requested_target_names().any(|n| n == name) {
2402 continue;
2403 }
2404 }
2405 Ok(Platform::Cfg(cfg_expr)) => {
2406 if !self.cfgs.iter().any(|cfgs| cfg_expr.matches(cfgs)) {
2407 continue;
2408 }
2409 }
2410 Err(_) => continue,
2411 }
2412
2413 let Some(platform_table) = platform_table.as_ref().as_table() else {
2414 continue;
2415 };
2416
2417 if let Some(deps) = platform_table
2418 .get("dependencies")
2419 .and_then(|d| d.as_ref().as_table())
2420 {
2421 if let Some((k, v)) = deps.get_key_value(orig_name) {
2422 if let Some(package) = v.get_ref().as_table().and_then(|t| t.get("package"))
2423 {
2424 return Some(package.span());
2425 } else {
2426 return Some(k.span());
2427 }
2428 }
2429 }
2430 }
2431 }
2432 None
2433 }
2434}
2435
2436fn replay_output_cache(
2440 package_id: PackageId,
2441 manifest: ManifestErrorContext,
2442 target: &Target,
2443 path: PathBuf,
2444 format: MessageFormat,
2445 show_diagnostics: bool,
2446) -> Work {
2447 let target = target.clone();
2448 let mut options = OutputOptions {
2449 format,
2450 cache_cell: None,
2451 show_diagnostics,
2452 warnings_seen: 0,
2453 errors_seen: 0,
2454 };
2455 Work::new(move |state| {
2456 if !path.exists() {
2457 return Ok(());
2459 }
2460 let file = paths::open(&path)?;
2464 let mut reader = std::io::BufReader::new(file);
2465 let mut line = String::new();
2466 loop {
2467 let length = reader.read_line(&mut line)?;
2468 if length == 0 {
2469 break;
2470 }
2471 let trimmed = line.trim_end_matches(&['\n', '\r'][..]);
2472 on_stderr_line(state, trimmed, package_id, &manifest, &target, &mut options)?;
2473 line.clear();
2474 }
2475 Ok(())
2476 })
2477}
2478
2479fn descriptive_pkg_name(name: &str, target: &Target, mode: &CompileMode) -> String {
2482 let desc_name = target.description_named();
2483 let mode = if mode.is_rustc_test() && !(target.is_test() || target.is_bench()) {
2484 " test"
2485 } else if mode.is_doc_test() {
2486 " doctest"
2487 } else if mode.is_doc() {
2488 " doc"
2489 } else {
2490 ""
2491 };
2492 format!("`{name}` ({desc_name}{mode})")
2493}
2494
2495pub(crate) fn apply_env_config(
2497 gctx: &crate::GlobalContext,
2498 cmd: &mut ProcessBuilder,
2499) -> CargoResult<()> {
2500 for (key, value) in gctx.env_config()?.iter() {
2501 if cmd.get_envs().contains_key(key) {
2503 continue;
2504 }
2505 cmd.env(key, value);
2506 }
2507 Ok(())
2508}
2509
2510fn should_include_scrape_units(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
2512 unit.mode.is_doc() && bcx.scrape_units.len() > 0 && bcx.ws.unit_needs_doc_scrape(unit)
2513}
2514
2515fn scrape_output_path(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<PathBuf> {
2517 assert!(unit.mode.is_doc() || unit.mode.is_doc_scrape());
2518 build_runner
2519 .outputs(unit)
2520 .map(|outputs| outputs[0].path.clone())
2521}
2522
2523fn rustdoc_dep_info_loc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> PathBuf {
2525 let mut loc = build_runner.files().fingerprint_file_path(unit, "");
2526 loc.set_extension("d");
2527 loc
2528}