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