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 locking;
45mod lto;
46mod output_depinfo;
47mod output_sbom;
48pub mod rustdoc;
49pub mod standard_lib;
50pub mod timings;
51mod unit;
52pub mod unit_dependencies;
53pub mod unit_graph;
54
55use std::borrow::Cow;
56use std::cell::OnceCell;
57use std::collections::{BTreeMap, HashMap, HashSet};
58use std::env;
59use std::ffi::{OsStr, OsString};
60use std::fmt::Display;
61use std::fs::{self, File};
62use std::io::{BufRead, BufWriter, Write};
63use std::ops::Range;
64use std::path::{Path, PathBuf};
65use std::sync::{Arc, LazyLock};
66
67use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
68use anyhow::{Context as _, Error};
69use cargo_platform::{Cfg, Platform};
70use itertools::Itertools;
71use regex::Regex;
72use tracing::{debug, instrument, trace};
73
74pub use self::build_config::UserIntent;
75pub use self::build_config::{BuildConfig, CompileMode, MessageFormat};
76pub use self::build_context::BuildContext;
77pub use self::build_context::FileFlavor;
78pub use self::build_context::FileType;
79pub use self::build_context::RustcTargetData;
80pub use self::build_context::TargetInfo;
81pub use self::build_runner::{BuildRunner, Metadata, UnitHash};
82pub use self::compilation::{Compilation, Doctest, UnitOutput};
83pub use self::compile_kind::{CompileKind, CompileKindFallback, CompileTarget};
84pub use self::crate_type::CrateType;
85pub use self::custom_build::LinkArgTarget;
86pub use self::custom_build::{BuildOutput, BuildScriptOutputs, BuildScripts, LibraryPath};
87pub(crate) use self::fingerprint::DirtyReason;
88pub use self::fingerprint::RustdocFingerprint;
89pub use self::job_queue::Freshness;
90use self::job_queue::{Job, JobQueue, JobState, Work};
91pub(crate) use self::layout::Layout;
92pub use self::lto::Lto;
93use self::output_depinfo::output_depinfo;
94use self::output_sbom::build_sbom;
95use self::unit_graph::UnitDep;
96
97use crate::core::compiler::future_incompat::FutureIncompatReport;
98use crate::core::compiler::locking::LockKey;
99use crate::core::compiler::timings::SectionTiming;
100pub use crate::core::compiler::unit::Unit;
101pub use crate::core::compiler::unit::UnitIndex;
102pub use crate::core::compiler::unit::UnitInterner;
103use crate::core::manifest::TargetSourcePath;
104use crate::core::profiles::{PanicStrategy, Profile, StripInner};
105use crate::core::{Feature, PackageId, Target, Verbosity};
106use crate::lints::get_key_value;
107use crate::util::OnceExt;
108use crate::util::context::WarningHandling;
109use crate::util::errors::{CargoResult, VerboseError};
110use crate::util::interning::InternedString;
111use crate::util::machine_message::{self, Message};
112use crate::util::{add_path_args, internal, path_args};
113
114use cargo_util::{ProcessBuilder, ProcessError, paths};
115use cargo_util_schemas::manifest::TomlDebugInfo;
116use cargo_util_schemas::manifest::TomlTrimPaths;
117use cargo_util_schemas::manifest::TomlTrimPathsValue;
118use rustfix::diagnostics::Applicability;
119
120const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version";
121
122pub trait Executor: Send + Sync + 'static {
126 fn init(&self, _build_runner: &BuildRunner<'_, '_>, _unit: &Unit) {}
130
131 fn exec(
134 &self,
135 cmd: &ProcessBuilder,
136 id: PackageId,
137 target: &Target,
138 mode: CompileMode,
139 on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
140 on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
141 ) -> CargoResult<()>;
142
143 fn force_rebuild(&self, _unit: &Unit) -> bool {
146 false
147 }
148}
149
150#[derive(Copy, Clone)]
153pub struct DefaultExecutor;
154
155impl Executor for DefaultExecutor {
156 #[instrument(name = "rustc", skip_all, fields(package = id.name().as_str(), process = cmd.to_string()))]
157 fn exec(
158 &self,
159 cmd: &ProcessBuilder,
160 id: PackageId,
161 _target: &Target,
162 _mode: CompileMode,
163 on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
164 on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
165 ) -> CargoResult<()> {
166 cmd.exec_with_streaming(on_stdout_line, on_stderr_line, false)
167 .map(drop)
168 }
169}
170
171#[tracing::instrument(skip(build_runner, jobs, exec))]
181fn compile<'gctx>(
182 build_runner: &mut BuildRunner<'_, 'gctx>,
183 jobs: &mut JobQueue<'gctx>,
184 unit: &Unit,
185 exec: &Arc<dyn Executor>,
186 force_rebuild: bool,
187) -> CargoResult<()> {
188 let bcx = build_runner.bcx;
189 if !build_runner.compiled.insert(unit.clone()) {
190 return Ok(());
191 }
192
193 let lock = if build_runner.bcx.gctx.cli_unstable().fine_grain_locking {
194 Some(build_runner.lock_manager.lock_shared(build_runner, unit)?)
195 } else {
196 None
197 };
198
199 if !unit.skip_non_compile_time_dep {
203 fingerprint::prepare_init(build_runner, unit)?;
206
207 let job = if unit.mode.is_run_custom_build() {
208 custom_build::prepare(build_runner, unit)?
209 } else if unit.mode.is_doc_test() {
210 Job::new_fresh()
212 } else {
213 let force = exec.force_rebuild(unit) || force_rebuild;
214 let mut job = fingerprint::prepare_target(build_runner, unit, force)?;
215 job.before(if job.freshness().is_dirty() {
216 let work = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
217 rustdoc(build_runner, unit)?
218 } else {
219 rustc(build_runner, unit, exec)?
220 };
221 work.then(link_targets(build_runner, unit, false)?)
222 } else {
223 let show_diagnostics = unit.show_warnings(bcx.gctx)
226 && build_runner.bcx.gctx.warning_handling()? != WarningHandling::Allow;
227 let manifest = ManifestErrorContext::new(build_runner, unit);
228 let work = replay_output_cache(
229 unit.pkg.package_id(),
230 manifest,
231 &unit.target,
232 build_runner.files().message_cache_path(unit),
233 build_runner.bcx.build_config.message_format,
234 show_diagnostics,
235 );
236 work.then(link_targets(build_runner, unit, true)?)
238 });
239
240 if build_runner.bcx.gctx.cli_unstable().fine_grain_locking && job.freshness().is_dirty()
243 {
244 if let Some(lock) = lock {
245 build_runner.lock_manager.unlock(&lock)?;
252 job.before(prebuild_lock_exclusive(lock.clone()));
253 job.after(downgrade_lock_to_shared(lock));
254 }
255 }
256
257 job
258 };
259 jobs.enqueue(build_runner, unit, job)?;
260 }
261
262 let deps = Vec::from(build_runner.unit_deps(unit)); for dep in deps {
265 compile(build_runner, jobs, &dep.unit, exec, false)?;
266 }
267
268 Ok(())
269}
270
271fn make_failed_scrape_diagnostic(
274 build_runner: &BuildRunner<'_, '_>,
275 unit: &Unit,
276 top_line: impl Display,
277) -> String {
278 let manifest_path = unit.pkg.manifest_path();
279 let relative_manifest_path = manifest_path
280 .strip_prefix(build_runner.bcx.ws.root())
281 .unwrap_or(&manifest_path);
282
283 format!(
284 "\
285{top_line}
286 Try running with `--verbose` to see the error message.
287 If an example should not be scanned, then consider adding `doc-scrape-examples = false` to its `[[example]]` definition in {}",
288 relative_manifest_path.display()
289 )
290}
291
292fn rustc(
294 build_runner: &mut BuildRunner<'_, '_>,
295 unit: &Unit,
296 exec: &Arc<dyn Executor>,
297) -> CargoResult<Work> {
298 let mut rustc = prepare_rustc(build_runner, unit)?;
299
300 let name = unit.pkg.name();
301
302 let outputs = build_runner.outputs(unit)?;
303 let root = build_runner.files().out_dir(unit);
304
305 let build_script_outputs = Arc::clone(&build_runner.build_script_outputs);
307 let current_id = unit.pkg.package_id();
308 let manifest = ManifestErrorContext::new(build_runner, unit);
309 let build_scripts = build_runner.build_scripts.get(unit).cloned();
310
311 let pass_l_flag = unit.target.is_lib() || !unit.pkg.targets().iter().any(|t| t.is_lib());
314
315 let dep_info_name =
316 if let Some(c_extra_filename) = build_runner.files().metadata(unit).c_extra_filename() {
317 format!("{}-{}.d", unit.target.crate_name(), c_extra_filename)
318 } else {
319 format!("{}.d", unit.target.crate_name())
320 };
321 let rustc_dep_info_loc = root.join(dep_info_name);
322 let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
323
324 let mut output_options = OutputOptions::new(build_runner, unit);
325 let package_id = unit.pkg.package_id();
326 let target = Target::clone(&unit.target);
327 let mode = unit.mode;
328
329 exec.init(build_runner, unit);
330 let exec = exec.clone();
331
332 let root_output = build_runner.files().host_dest().map(|v| v.to_path_buf());
333 let build_dir = build_runner.bcx.ws.build_dir().into_path_unlocked();
334 let pkg_root = unit.pkg.root().to_path_buf();
335 let cwd = rustc
336 .get_cwd()
337 .unwrap_or_else(|| build_runner.bcx.gctx.cwd())
338 .to_path_buf();
339 let fingerprint_dir = build_runner.files().fingerprint_dir(unit);
340 let script_metadatas = build_runner.find_build_script_metadatas(unit);
341 let is_local = unit.is_local();
342 let artifact = unit.artifact;
343 let sbom_files = build_runner.sbom_output_files(unit)?;
344 let sbom = build_sbom(build_runner, unit)?;
345
346 let hide_diagnostics_for_scrape_unit = build_runner.bcx.unit_can_fail_for_docscraping(unit)
347 && !matches!(
348 build_runner.bcx.gctx.shell().verbosity(),
349 Verbosity::Verbose
350 );
351 let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
352 let target_desc = unit.target.description_named();
355 let mut for_scrape_units = build_runner
356 .bcx
357 .scrape_units_have_dep_on(unit)
358 .into_iter()
359 .map(|unit| unit.target.description_named())
360 .collect::<Vec<_>>();
361 for_scrape_units.sort();
362 let for_scrape_units = for_scrape_units.join(", ");
363 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}"))
364 });
365 if hide_diagnostics_for_scrape_unit {
366 output_options.show_diagnostics = false;
367 }
368 let env_config = Arc::clone(build_runner.bcx.gctx.env_config()?);
369 return Ok(Work::new(move |state| {
370 if artifact.is_true() {
374 paths::create_dir_all(&root)?;
375 }
376
377 if let Some(build_scripts) = build_scripts {
385 let script_outputs = build_script_outputs.lock().unwrap();
386 add_native_deps(
387 &mut rustc,
388 &script_outputs,
389 &build_scripts,
390 pass_l_flag,
391 &target,
392 current_id,
393 mode,
394 )?;
395 if let Some(ref root_output) = root_output {
396 add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, root_output)?;
397 }
398 add_custom_flags(&mut rustc, &script_outputs, script_metadatas)?;
399 }
400
401 for output in outputs.iter() {
402 if output.path.extension() == Some(OsStr::new("rmeta")) {
406 let dst = root.join(&output.path).with_extension("rlib");
407 if dst.exists() {
408 paths::remove_file(&dst)?;
409 }
410 }
411
412 if output.hardlink.is_some() && output.path.exists() {
417 _ = paths::remove_file(&output.path).map_err(|e| {
418 tracing::debug!(
419 "failed to delete previous output file `{:?}`: {e:?}",
420 output.path
421 );
422 });
423 }
424 }
425
426 state.running(&rustc);
427 let timestamp = paths::set_invocation_time(&fingerprint_dir)?;
428 for file in sbom_files {
429 tracing::debug!("writing sbom to {}", file.display());
430 let outfile = BufWriter::new(paths::create(&file)?);
431 serde_json::to_writer(outfile, &sbom)?;
432 }
433
434 let result = exec
435 .exec(
436 &rustc,
437 package_id,
438 &target,
439 mode,
440 &mut |line| on_stdout_line(state, line, package_id, &target),
441 &mut |line| {
442 on_stderr_line(
443 state,
444 line,
445 package_id,
446 &manifest,
447 &target,
448 &mut output_options,
449 )
450 },
451 )
452 .map_err(|e| {
453 if output_options.errors_seen == 0 {
454 e
459 } else {
460 verbose_if_simple_exit_code(e)
461 }
462 })
463 .with_context(|| {
464 let warnings = match output_options.warnings_seen {
466 0 => String::new(),
467 1 => "; 1 warning emitted".to_string(),
468 count => format!("; {} warnings emitted", count),
469 };
470 let errors = match output_options.errors_seen {
471 0 => String::new(),
472 1 => " due to 1 previous error".to_string(),
473 count => format!(" due to {} previous errors", count),
474 };
475 let name = descriptive_pkg_name(&name, &target, &mode);
476 format!("could not compile {name}{errors}{warnings}")
477 });
478
479 if let Err(e) = result {
480 if let Some(diagnostic) = failed_scrape_diagnostic {
481 state.warning(diagnostic);
482 }
483
484 return Err(e);
485 }
486
487 debug_assert_eq!(output_options.errors_seen, 0);
489
490 if rustc_dep_info_loc.exists() {
491 fingerprint::translate_dep_info(
492 &rustc_dep_info_loc,
493 &dep_info_loc,
494 &cwd,
495 &pkg_root,
496 &build_dir,
497 &rustc,
498 is_local,
500 &env_config,
501 )
502 .with_context(|| {
503 internal(format!(
504 "could not parse/generate dep info at: {}",
505 rustc_dep_info_loc.display()
506 ))
507 })?;
508 paths::set_file_time_no_err(dep_info_loc, timestamp);
511 }
512
513 if mode.is_check() {
527 for output in outputs.iter() {
528 paths::set_file_time_no_err(&output.path, timestamp);
529 }
530 }
531
532 Ok(())
533 }));
534
535 fn add_native_deps(
538 rustc: &mut ProcessBuilder,
539 build_script_outputs: &BuildScriptOutputs,
540 build_scripts: &BuildScripts,
541 pass_l_flag: bool,
542 target: &Target,
543 current_id: PackageId,
544 mode: CompileMode,
545 ) -> CargoResult<()> {
546 let mut library_paths = vec![];
547
548 for key in build_scripts.to_link.iter() {
549 let output = build_script_outputs.get(key.1).ok_or_else(|| {
550 internal(format!(
551 "couldn't find build script output for {}/{}",
552 key.0, key.1
553 ))
554 })?;
555 library_paths.extend(output.library_paths.iter());
556 }
557
558 library_paths.sort_by_key(|p| match p {
564 LibraryPath::CargoArtifact(_) => 0,
565 LibraryPath::External(_) => 1,
566 });
567
568 for path in library_paths.iter() {
569 rustc.arg("-L").arg(path.as_ref());
570 }
571
572 for key in build_scripts.to_link.iter() {
573 let output = build_script_outputs.get(key.1).ok_or_else(|| {
574 internal(format!(
575 "couldn't find build script output for {}/{}",
576 key.0, key.1
577 ))
578 })?;
579
580 if key.0 == current_id {
581 if pass_l_flag {
582 for name in output.library_links.iter() {
583 rustc.arg("-l").arg(name);
584 }
585 }
586 }
587
588 for (lt, arg) in &output.linker_args {
589 if lt.applies_to(target, mode)
595 && (key.0 == current_id || *lt == LinkArgTarget::Cdylib)
596 {
597 rustc.arg("-C").arg(format!("link-arg={}", arg));
598 }
599 }
600 }
601 Ok(())
602 }
603}
604
605fn verbose_if_simple_exit_code(err: Error) -> Error {
606 match err
609 .downcast_ref::<ProcessError>()
610 .as_ref()
611 .and_then(|perr| perr.code)
612 {
613 Some(n) if cargo_util::is_simple_exit_code(n) => VerboseError::new(err).into(),
614 _ => err,
615 }
616}
617
618fn prebuild_lock_exclusive(lock: LockKey) -> Work {
619 Work::new(move |state| {
620 state.lock_exclusive(&lock)?;
621 Ok(())
622 })
623}
624
625fn downgrade_lock_to_shared(lock: LockKey) -> Work {
626 Work::new(move |state| {
627 state.downgrade_to_shared(&lock)?;
628 Ok(())
629 })
630}
631
632fn link_targets(
635 build_runner: &mut BuildRunner<'_, '_>,
636 unit: &Unit,
637 fresh: bool,
638) -> CargoResult<Work> {
639 let bcx = build_runner.bcx;
640 let outputs = build_runner.outputs(unit)?;
641 let export_dir = build_runner.files().export_dir();
642 let package_id = unit.pkg.package_id();
643 let manifest_path = PathBuf::from(unit.pkg.manifest_path());
644 let profile = unit.profile.clone();
645 let unit_mode = unit.mode;
646 let features = unit.features.iter().map(|s| s.to_string()).collect();
647 let json_messages = bcx.build_config.emit_json();
648 let executable = build_runner.get_executable(unit)?;
649 let mut target = Target::clone(&unit.target);
650 if let TargetSourcePath::Metabuild = target.src_path() {
651 let path = unit
653 .pkg
654 .manifest()
655 .metabuild_path(build_runner.bcx.ws.build_dir());
656 target.set_src_path(TargetSourcePath::Path(path));
657 }
658
659 Ok(Work::new(move |state| {
660 let mut destinations = vec![];
665 for output in outputs.iter() {
666 let src = &output.path;
667 if !src.exists() {
670 continue;
671 }
672 let Some(dst) = output.hardlink.as_ref() else {
673 destinations.push(src.clone());
674 continue;
675 };
676 destinations.push(dst.clone());
677 paths::link_or_copy(src, dst)?;
678 if let Some(ref path) = output.export_path {
679 let export_dir = export_dir.as_ref().unwrap();
680 paths::create_dir_all(export_dir)?;
681
682 paths::link_or_copy(src, path)?;
683 }
684 }
685
686 if json_messages {
687 let debuginfo = match profile.debuginfo.into_inner() {
688 TomlDebugInfo::None => machine_message::ArtifactDebuginfo::Int(0),
689 TomlDebugInfo::Limited => machine_message::ArtifactDebuginfo::Int(1),
690 TomlDebugInfo::Full => machine_message::ArtifactDebuginfo::Int(2),
691 TomlDebugInfo::LineDirectivesOnly => {
692 machine_message::ArtifactDebuginfo::Named("line-directives-only")
693 }
694 TomlDebugInfo::LineTablesOnly => {
695 machine_message::ArtifactDebuginfo::Named("line-tables-only")
696 }
697 };
698 let art_profile = machine_message::ArtifactProfile {
699 opt_level: profile.opt_level.as_str(),
700 debuginfo: Some(debuginfo),
701 debug_assertions: profile.debug_assertions,
702 overflow_checks: profile.overflow_checks,
703 test: unit_mode.is_any_test(),
704 };
705
706 let msg = machine_message::Artifact {
707 package_id: package_id.to_spec(),
708 manifest_path,
709 target: &target,
710 profile: art_profile,
711 features,
712 filenames: destinations,
713 executable,
714 fresh,
715 }
716 .to_json_string();
717 state.stdout(msg)?;
718 }
719 Ok(())
720 }))
721}
722
723fn add_plugin_deps(
727 rustc: &mut ProcessBuilder,
728 build_script_outputs: &BuildScriptOutputs,
729 build_scripts: &BuildScripts,
730 root_output: &Path,
731) -> CargoResult<()> {
732 let var = paths::dylib_path_envvar();
733 let search_path = rustc.get_env(var).unwrap_or_default();
734 let mut search_path = env::split_paths(&search_path).collect::<Vec<_>>();
735 for (pkg_id, metadata) in &build_scripts.plugins {
736 let output = build_script_outputs
737 .get(*metadata)
738 .ok_or_else(|| internal(format!("couldn't find libs for plugin dep {}", pkg_id)))?;
739 search_path.append(&mut filter_dynamic_search_path(
740 output.library_paths.iter().map(AsRef::as_ref),
741 root_output,
742 ));
743 }
744 let search_path = paths::join_paths(&search_path, var)?;
745 rustc.env(var, &search_path);
746 Ok(())
747}
748
749fn get_dynamic_search_path(path: &Path) -> &Path {
750 match path.to_str().and_then(|s| s.split_once("=")) {
751 Some(("native" | "crate" | "dependency" | "framework" | "all", path)) => Path::new(path),
752 _ => path,
753 }
754}
755
756fn filter_dynamic_search_path<'a, I>(paths: I, root_output: &Path) -> Vec<PathBuf>
762where
763 I: Iterator<Item = &'a PathBuf>,
764{
765 let mut search_path = vec![];
766 for dir in paths {
767 let dir = get_dynamic_search_path(dir);
768 if dir.starts_with(&root_output) {
769 search_path.push(dir.to_path_buf());
770 } else {
771 debug!(
772 "Not including path {} in runtime library search path because it is \
773 outside target root {}",
774 dir.display(),
775 root_output.display()
776 );
777 }
778 }
779 search_path
780}
781
782fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
789 let gctx = build_runner.bcx.gctx;
790 let is_primary = build_runner.is_primary_package(unit);
791 let is_workspace = build_runner.bcx.ws.is_member(&unit.pkg);
792
793 let mut base = build_runner
794 .compilation
795 .rustc_process(unit, is_primary, is_workspace)?;
796 build_base_args(build_runner, &mut base, unit)?;
797 if unit.pkg.manifest().is_embedded() {
798 if !gctx.cli_unstable().script {
799 anyhow::bail!(
800 "parsing `{}` requires `-Zscript`",
801 unit.pkg.manifest_path().display()
802 );
803 }
804 base.arg("-Z").arg("crate-attr=feature(frontmatter)");
805 }
806
807 base.inherit_jobserver(&build_runner.jobserver);
808 build_deps_args(&mut base, build_runner, unit)?;
809 add_cap_lints(build_runner.bcx, unit, &mut base);
810 if let Some(args) = build_runner.bcx.extra_args_for(unit) {
811 base.args(args);
812 }
813 base.args(&unit.rustflags);
814 if gctx.cli_unstable().binary_dep_depinfo {
815 base.arg("-Z").arg("binary-dep-depinfo");
816 }
817 if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
818 base.arg("-Z").arg("checksum-hash-algorithm=blake3");
819 }
820
821 if is_primary {
822 base.env("CARGO_PRIMARY_PACKAGE", "1");
823 let file_list = build_runner.sbom_output_files(unit)?;
824 if !file_list.is_empty() {
825 let file_list = std::env::join_paths(file_list)?;
826 base.env("CARGO_SBOM_PATH", file_list);
827 }
828 }
829
830 if unit.target.is_test() || unit.target.is_bench() {
831 let tmp = build_runner
832 .files()
833 .layout(unit.kind)
834 .build_dir()
835 .prepare_tmp()?;
836 base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());
837 }
838
839 Ok(base)
840}
841
842fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
849 let bcx = build_runner.bcx;
850 let mut rustdoc = build_runner.compilation.rustdoc_process(unit, None)?;
852 if unit.pkg.manifest().is_embedded() {
853 if !bcx.gctx.cli_unstable().script {
854 anyhow::bail!(
855 "parsing `{}` requires `-Zscript`",
856 unit.pkg.manifest_path().display()
857 );
858 }
859 rustdoc.arg("-Z").arg("crate-attr=feature(frontmatter)");
860 }
861 rustdoc.inherit_jobserver(&build_runner.jobserver);
862 let crate_name = unit.target.crate_name();
863 rustdoc.arg("--crate-name").arg(&crate_name);
864 add_path_args(bcx.ws, unit, &mut rustdoc);
865 add_cap_lints(bcx, unit, &mut rustdoc);
866
867 if let CompileKind::Target(target) = unit.kind {
868 rustdoc.arg("--target").arg(target.rustc_target());
869 }
870 let doc_dir = build_runner.files().out_dir(unit);
871 rustdoc.arg("-o").arg(&doc_dir);
872 rustdoc.args(&features_args(unit));
873 rustdoc.args(&check_cfg_args(unit));
874
875 add_error_format_and_color(build_runner, &mut rustdoc);
876 add_allow_features(build_runner, &mut rustdoc);
877
878 if build_runner.bcx.gctx.cli_unstable().rustdoc_depinfo {
879 let mut arg = if build_runner.bcx.gctx.cli_unstable().rustdoc_mergeable_info {
882 OsString::from("--emit=invocation-specific,dep-info=")
884 } else {
885 OsString::from("--emit=toolchain-shared-resources,invocation-specific,dep-info=")
887 };
888 arg.push(rustdoc_dep_info_loc(build_runner, unit));
889 rustdoc.arg(arg);
890
891 if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
892 rustdoc.arg("-Z").arg("checksum-hash-algorithm=blake3");
893 }
894
895 rustdoc.arg("-Zunstable-options");
896 } else if build_runner.bcx.gctx.cli_unstable().rustdoc_mergeable_info {
897 rustdoc.arg("--emit=invocation-specific");
899 rustdoc.arg("-Zunstable-options");
900 }
901
902 if build_runner.bcx.gctx.cli_unstable().rustdoc_mergeable_info {
903 rustdoc.arg("--merge=none");
905 let mut arg = OsString::from("--parts-out-dir=");
906 arg.push(build_runner.files().deps_dir_new_layout(unit));
908 rustdoc.arg(arg);
909 }
910
911 if let Some(trim_paths) = unit.profile.trim_paths.as_ref() {
912 trim_paths_args_rustdoc(&mut rustdoc, build_runner, unit, trim_paths)?;
913 }
914
915 rustdoc.args(unit.pkg.manifest().lint_rustflags());
916
917 let metadata = build_runner.metadata_for_doc_units[unit];
918 rustdoc
919 .arg("-C")
920 .arg(format!("metadata={}", metadata.c_metadata()));
921
922 if unit.mode.is_doc_scrape() {
923 debug_assert!(build_runner.bcx.scrape_units.contains(unit));
924
925 if unit.target.is_test() {
926 rustdoc.arg("--scrape-tests");
927 }
928
929 rustdoc.arg("-Zunstable-options");
930
931 rustdoc
932 .arg("--scrape-examples-output-path")
933 .arg(scrape_output_path(build_runner, unit)?);
934
935 for pkg in build_runner.bcx.packages.packages() {
937 let names = pkg
938 .targets()
939 .iter()
940 .map(|target| target.crate_name())
941 .collect::<HashSet<_>>();
942 for name in names {
943 rustdoc.arg("--scrape-examples-target-crate").arg(name);
944 }
945 }
946 }
947
948 if should_include_scrape_units(build_runner.bcx, unit) {
949 rustdoc.arg("-Zunstable-options");
950 }
951
952 build_deps_args(&mut rustdoc, build_runner, unit)?;
953 rustdoc::add_root_urls(build_runner, unit, &mut rustdoc)?;
954
955 rustdoc::add_output_format(build_runner, &mut rustdoc)?;
956
957 if let Some(args) = build_runner.bcx.extra_args_for(unit) {
958 rustdoc.args(args);
959 }
960 rustdoc.args(&unit.rustdocflags);
961
962 if !crate_version_flag_already_present(&rustdoc) {
963 append_crate_version_flag(unit, &mut rustdoc);
964 }
965
966 Ok(rustdoc)
967}
968
969fn rustdoc(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<Work> {
971 let mut rustdoc = prepare_rustdoc(build_runner, unit)?;
972
973 let crate_name = unit.target.crate_name();
974 let doc_dir = build_runner.files().out_dir(unit);
975 paths::create_dir_all(&doc_dir)?;
979
980 let target_desc = unit.target.description_named();
981 let name = unit.pkg.name();
982 let build_script_outputs = Arc::clone(&build_runner.build_script_outputs);
983 let package_id = unit.pkg.package_id();
984 let target = Target::clone(&unit.target);
985 let manifest = ManifestErrorContext::new(build_runner, unit);
986
987 let rustdoc_dep_info_loc = rustdoc_dep_info_loc(build_runner, unit);
988 let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
989 let build_dir = build_runner.bcx.ws.build_dir().into_path_unlocked();
990 let pkg_root = unit.pkg.root().to_path_buf();
991 let cwd = rustdoc
992 .get_cwd()
993 .unwrap_or_else(|| build_runner.bcx.gctx.cwd())
994 .to_path_buf();
995 let fingerprint_dir = build_runner.files().fingerprint_dir(unit);
996 let is_local = unit.is_local();
997 let env_config = Arc::clone(build_runner.bcx.gctx.env_config()?);
998 let rustdoc_depinfo_enabled = build_runner.bcx.gctx.cli_unstable().rustdoc_depinfo;
999
1000 let mut output_options = OutputOptions::new(build_runner, unit);
1001 let script_metadatas = build_runner.find_build_script_metadatas(unit);
1002 let scrape_outputs = if should_include_scrape_units(build_runner.bcx, unit) {
1003 Some(
1004 build_runner
1005 .bcx
1006 .scrape_units
1007 .iter()
1008 .map(|unit| {
1009 Ok((
1010 build_runner.files().metadata(unit).unit_id(),
1011 scrape_output_path(build_runner, unit)?,
1012 ))
1013 })
1014 .collect::<CargoResult<HashMap<_, _>>>()?,
1015 )
1016 } else {
1017 None
1018 };
1019
1020 let failed_scrape_units = Arc::clone(&build_runner.failed_scrape_units);
1021 let hide_diagnostics_for_scrape_unit = build_runner.bcx.unit_can_fail_for_docscraping(unit)
1022 && !matches!(
1023 build_runner.bcx.gctx.shell().verbosity(),
1024 Verbosity::Verbose
1025 );
1026 let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
1027 make_failed_scrape_diagnostic(
1028 build_runner,
1029 unit,
1030 format_args!("failed to scan {target_desc} in package `{name}` for example code usage"),
1031 )
1032 });
1033 if hide_diagnostics_for_scrape_unit {
1034 output_options.show_diagnostics = false;
1035 }
1036
1037 Ok(Work::new(move |state| {
1038 add_custom_flags(
1039 &mut rustdoc,
1040 &build_script_outputs.lock().unwrap(),
1041 script_metadatas,
1042 )?;
1043
1044 if let Some(scrape_outputs) = scrape_outputs {
1049 let failed_scrape_units = failed_scrape_units.lock().unwrap();
1050 for (metadata, output_path) in &scrape_outputs {
1051 if !failed_scrape_units.contains(metadata) {
1052 rustdoc.arg("--with-examples").arg(output_path);
1053 }
1054 }
1055 }
1056
1057 let crate_dir = doc_dir.join(&crate_name);
1058 if crate_dir.exists() {
1059 debug!("removing pre-existing doc directory {:?}", crate_dir);
1062 paths::remove_dir_all(crate_dir)?;
1063 }
1064 state.running(&rustdoc);
1065 let timestamp = paths::set_invocation_time(&fingerprint_dir)?;
1066
1067 let result = rustdoc
1068 .exec_with_streaming(
1069 &mut |line| on_stdout_line(state, line, package_id, &target),
1070 &mut |line| {
1071 on_stderr_line(
1072 state,
1073 line,
1074 package_id,
1075 &manifest,
1076 &target,
1077 &mut output_options,
1078 )
1079 },
1080 false,
1081 )
1082 .map_err(verbose_if_simple_exit_code)
1083 .with_context(|| format!("could not document `{}`", name));
1084
1085 if let Err(e) = result {
1086 if let Some(diagnostic) = failed_scrape_diagnostic {
1087 state.warning(diagnostic);
1088 }
1089
1090 return Err(e);
1091 }
1092
1093 if rustdoc_depinfo_enabled && rustdoc_dep_info_loc.exists() {
1094 fingerprint::translate_dep_info(
1095 &rustdoc_dep_info_loc,
1096 &dep_info_loc,
1097 &cwd,
1098 &pkg_root,
1099 &build_dir,
1100 &rustdoc,
1101 is_local,
1103 &env_config,
1104 )
1105 .with_context(|| {
1106 internal(format_args!(
1107 "could not parse/generate dep info at: {}",
1108 rustdoc_dep_info_loc.display()
1109 ))
1110 })?;
1111 paths::set_file_time_no_err(dep_info_loc, timestamp);
1114 }
1115
1116 Ok(())
1117 }))
1118}
1119
1120fn crate_version_flag_already_present(rustdoc: &ProcessBuilder) -> bool {
1123 rustdoc.get_args().any(|flag| {
1124 flag.to_str()
1125 .map_or(false, |flag| flag.starts_with(RUSTDOC_CRATE_VERSION_FLAG))
1126 })
1127}
1128
1129fn append_crate_version_flag(unit: &Unit, rustdoc: &mut ProcessBuilder) {
1130 rustdoc
1131 .arg(RUSTDOC_CRATE_VERSION_FLAG)
1132 .arg(unit.pkg.version().to_string());
1133}
1134
1135fn add_cap_lints(bcx: &BuildContext<'_, '_>, unit: &Unit, cmd: &mut ProcessBuilder) {
1139 if !unit.show_warnings(bcx.gctx) {
1142 cmd.arg("--cap-lints").arg("allow");
1143
1144 } else if !unit.is_local() {
1147 cmd.arg("--cap-lints").arg("warn");
1148 }
1149}
1150
1151fn add_allow_features(build_runner: &BuildRunner<'_, '_>, cmd: &mut ProcessBuilder) {
1155 if let Some(allow) = &build_runner.bcx.gctx.cli_unstable().allow_features {
1156 use std::fmt::Write;
1157 let mut arg = String::from("-Zallow-features=");
1158 for f in allow {
1159 let _ = write!(&mut arg, "{f},");
1160 }
1161 cmd.arg(arg.trim_end_matches(','));
1162 }
1163}
1164
1165fn add_error_format_and_color(build_runner: &BuildRunner<'_, '_>, cmd: &mut ProcessBuilder) {
1176 let enable_timings = build_runner.bcx.gctx.cli_unstable().section_timings
1177 && (build_runner.bcx.build_config.timing_report || build_runner.bcx.logger.is_some());
1178 if enable_timings {
1179 cmd.arg("-Zunstable-options");
1180 }
1181
1182 cmd.arg("--error-format=json");
1183 let mut json = String::from("--json=diagnostic-rendered-ansi,artifacts,future-incompat");
1184
1185 if let MessageFormat::Short | MessageFormat::Json { short: true, .. } =
1186 build_runner.bcx.build_config.message_format
1187 {
1188 json.push_str(",diagnostic-short");
1189 } else if build_runner.bcx.gctx.shell().err_unicode()
1190 && build_runner.bcx.gctx.cli_unstable().rustc_unicode
1191 {
1192 json.push_str(",diagnostic-unicode");
1193 }
1194
1195 if enable_timings {
1196 json.push_str(",timings");
1197 }
1198
1199 cmd.arg(json);
1200
1201 let gctx = build_runner.bcx.gctx;
1202 if let Some(width) = gctx.shell().err_width().diagnostic_terminal_width() {
1203 cmd.arg(format!("--diagnostic-width={width}"));
1204 }
1205}
1206
1207fn build_base_args(
1209 build_runner: &BuildRunner<'_, '_>,
1210 cmd: &mut ProcessBuilder,
1211 unit: &Unit,
1212) -> CargoResult<()> {
1213 assert!(!unit.mode.is_run_custom_build());
1214
1215 let bcx = build_runner.bcx;
1216 let Profile {
1217 ref opt_level,
1218 codegen_backend,
1219 codegen_units,
1220 debuginfo,
1221 debug_assertions,
1222 split_debuginfo,
1223 overflow_checks,
1224 rpath,
1225 ref panic,
1226 incremental,
1227 strip,
1228 rustflags: profile_rustflags,
1229 trim_paths,
1230 hint_mostly_unused: profile_hint_mostly_unused,
1231 ..
1232 } = unit.profile.clone();
1233 let hints = unit.pkg.hints().cloned().unwrap_or_default();
1234 let test = unit.mode.is_any_test();
1235
1236 let warn = |msg: &str| {
1237 bcx.gctx.shell().warn(format!(
1238 "{}@{}: {msg}",
1239 unit.pkg.package_id().name(),
1240 unit.pkg.package_id().version()
1241 ))
1242 };
1243 let unit_capped_warn = |msg: &str| {
1244 if unit.show_warnings(bcx.gctx) {
1245 warn(msg)
1246 } else {
1247 Ok(())
1248 }
1249 };
1250
1251 cmd.arg("--crate-name").arg(&unit.target.crate_name());
1252
1253 let edition = unit.target.edition();
1254 edition.cmd_edition_arg(cmd);
1255
1256 add_path_args(bcx.ws, unit, cmd);
1257 add_error_format_and_color(build_runner, cmd);
1258 add_allow_features(build_runner, cmd);
1259
1260 let mut contains_dy_lib = false;
1261 if !test {
1262 for crate_type in &unit.target.rustc_crate_types() {
1263 cmd.arg("--crate-type").arg(crate_type.as_str());
1264 contains_dy_lib |= crate_type == &CrateType::Dylib;
1265 }
1266 }
1267
1268 if unit.mode.is_check() {
1269 cmd.arg("--emit=dep-info,metadata");
1270 } else if build_runner.bcx.gctx.cli_unstable().no_embed_metadata {
1271 if unit.benefits_from_no_embed_metadata() {
1281 cmd.arg("--emit=dep-info,metadata,link");
1282 cmd.args(&["-Z", "embed-metadata=no"]);
1283 } else {
1284 cmd.arg("--emit=dep-info,link");
1285 }
1286 } else {
1287 if !unit.requires_upstream_objects() {
1291 cmd.arg("--emit=dep-info,metadata,link");
1292 } else {
1293 cmd.arg("--emit=dep-info,link");
1294 }
1295 }
1296
1297 let prefer_dynamic = (unit.target.for_host() && !unit.target.is_custom_build())
1298 || (contains_dy_lib && !build_runner.is_primary_package(unit));
1299 if prefer_dynamic {
1300 cmd.arg("-C").arg("prefer-dynamic");
1301 }
1302
1303 if opt_level.as_str() != "0" {
1304 cmd.arg("-C").arg(&format!("opt-level={}", opt_level));
1305 }
1306
1307 if *panic != PanicStrategy::Unwind {
1308 cmd.arg("-C").arg(format!("panic={}", panic));
1309 }
1310 if *panic == PanicStrategy::ImmediateAbort {
1311 cmd.arg("-Z").arg("unstable-options");
1312 }
1313
1314 cmd.args(<o_args(build_runner, unit));
1315
1316 if let Some(backend) = codegen_backend {
1317 cmd.arg("-Z").arg(&format!("codegen-backend={}", backend));
1318 }
1319
1320 if let Some(n) = codegen_units {
1321 cmd.arg("-C").arg(&format!("codegen-units={}", n));
1322 }
1323
1324 let debuginfo = debuginfo.into_inner();
1325 if debuginfo != TomlDebugInfo::None {
1327 cmd.arg("-C").arg(format!("debuginfo={debuginfo}"));
1328 if let Some(split) = split_debuginfo {
1335 if build_runner
1336 .bcx
1337 .target_data
1338 .info(unit.kind)
1339 .supports_debuginfo_split(split)
1340 {
1341 cmd.arg("-C").arg(format!("split-debuginfo={split}"));
1342 }
1343 }
1344 }
1345
1346 if let Some(trim_paths) = trim_paths {
1347 trim_paths_args(cmd, build_runner, unit, &trim_paths)?;
1348 }
1349
1350 cmd.args(unit.pkg.manifest().lint_rustflags());
1351 cmd.args(&profile_rustflags);
1352
1353 if opt_level.as_str() != "0" {
1357 if debug_assertions {
1358 cmd.args(&["-C", "debug-assertions=on"]);
1359 if !overflow_checks {
1360 cmd.args(&["-C", "overflow-checks=off"]);
1361 }
1362 } else if overflow_checks {
1363 cmd.args(&["-C", "overflow-checks=on"]);
1364 }
1365 } else if !debug_assertions {
1366 cmd.args(&["-C", "debug-assertions=off"]);
1367 if overflow_checks {
1368 cmd.args(&["-C", "overflow-checks=on"]);
1369 }
1370 } else if !overflow_checks {
1371 cmd.args(&["-C", "overflow-checks=off"]);
1372 }
1373
1374 if test && unit.target.harness() {
1375 cmd.arg("--test");
1376
1377 if *panic == PanicStrategy::Abort || *panic == PanicStrategy::ImmediateAbort {
1385 cmd.arg("-Z").arg("panic-abort-tests");
1386 }
1387 } else if test {
1388 cmd.arg("--cfg").arg("test");
1389 }
1390
1391 cmd.args(&features_args(unit));
1392 cmd.args(&check_cfg_args(unit));
1393
1394 let meta = build_runner.files().metadata(unit);
1395 cmd.arg("-C")
1396 .arg(&format!("metadata={}", meta.c_metadata()));
1397 if let Some(c_extra_filename) = meta.c_extra_filename() {
1398 cmd.arg("-C")
1399 .arg(&format!("extra-filename=-{c_extra_filename}"));
1400 }
1401
1402 if rpath {
1403 cmd.arg("-C").arg("rpath");
1404 }
1405
1406 cmd.arg("--out-dir")
1407 .arg(&build_runner.files().out_dir(unit));
1408
1409 fn opt(cmd: &mut ProcessBuilder, key: &str, prefix: &str, val: Option<&OsStr>) {
1410 if let Some(val) = val {
1411 let mut joined = OsString::from(prefix);
1412 joined.push(val);
1413 cmd.arg(key).arg(joined);
1414 }
1415 }
1416
1417 if let CompileKind::Target(n) = unit.kind {
1418 cmd.arg("--target").arg(n.rustc_target());
1419 }
1420
1421 opt(
1422 cmd,
1423 "-C",
1424 "linker=",
1425 build_runner
1426 .compilation
1427 .target_linker(unit.kind)
1428 .as_ref()
1429 .map(|s| s.as_ref()),
1430 );
1431 if incremental {
1432 let dir = build_runner.files().incremental_dir(&unit);
1433 opt(cmd, "-C", "incremental=", Some(dir.as_os_str()));
1434 }
1435
1436 let pkg_hint_mostly_unused = match hints.mostly_unused {
1437 None => None,
1438 Some(toml::Value::Boolean(b)) => Some(b),
1439 Some(v) => {
1440 unit_capped_warn(&format!(
1441 "ignoring unsupported value type ({}) for 'hints.mostly-unused', which expects a boolean",
1442 v.type_str()
1443 ))?;
1444 None
1445 }
1446 };
1447 if profile_hint_mostly_unused
1448 .or(pkg_hint_mostly_unused)
1449 .unwrap_or(false)
1450 {
1451 if bcx.gctx.cli_unstable().profile_hint_mostly_unused {
1452 cmd.arg("-Zhint-mostly-unused");
1453 } else {
1454 if profile_hint_mostly_unused.is_some() {
1455 warn(
1457 "ignoring 'hint-mostly-unused' profile option, pass `-Zprofile-hint-mostly-unused` to enable it",
1458 )?;
1459 } else if pkg_hint_mostly_unused.is_some() {
1460 unit_capped_warn(
1461 "ignoring 'hints.mostly-unused', pass `-Zprofile-hint-mostly-unused` to enable it",
1462 )?;
1463 }
1464 }
1465 }
1466
1467 let strip = strip.into_inner();
1468 if strip != StripInner::None {
1469 cmd.arg("-C").arg(format!("strip={}", strip));
1470 }
1471
1472 if unit.is_std {
1473 cmd.arg("-Z")
1479 .arg("force-unstable-if-unmarked")
1480 .env("RUSTC_BOOTSTRAP", "1");
1481 }
1482
1483 Ok(())
1484}
1485
1486fn features_args(unit: &Unit) -> Vec<OsString> {
1488 let mut args = Vec::with_capacity(unit.features.len() * 2);
1489
1490 for feat in &unit.features {
1491 args.push(OsString::from("--cfg"));
1492 args.push(OsString::from(format!("feature=\"{}\"", feat)));
1493 }
1494
1495 args
1496}
1497
1498fn trim_paths_args_rustdoc(
1500 cmd: &mut ProcessBuilder,
1501 build_runner: &BuildRunner<'_, '_>,
1502 unit: &Unit,
1503 trim_paths: &TomlTrimPaths,
1504) -> CargoResult<()> {
1505 match trim_paths {
1506 TomlTrimPaths::Values(values) if !values.contains(&TomlTrimPathsValue::Diagnostics) => {
1508 return Ok(());
1509 }
1510 _ => {}
1511 }
1512
1513 cmd.arg("-Zunstable-options");
1515
1516 cmd.arg(package_remap(build_runner, unit));
1519 cmd.arg(build_dir_remap(build_runner));
1520 cmd.arg(sysroot_remap(build_runner, unit));
1521
1522 Ok(())
1523}
1524
1525fn trim_paths_args(
1531 cmd: &mut ProcessBuilder,
1532 build_runner: &BuildRunner<'_, '_>,
1533 unit: &Unit,
1534 trim_paths: &TomlTrimPaths,
1535) -> CargoResult<()> {
1536 if trim_paths.is_none() {
1537 return Ok(());
1538 }
1539
1540 cmd.arg("-Zunstable-options");
1542 cmd.arg(format!("-Zremap-path-scope={trim_paths}"));
1543
1544 cmd.arg(package_remap(build_runner, unit));
1547 cmd.arg(build_dir_remap(build_runner));
1548 cmd.arg(sysroot_remap(build_runner, unit));
1549
1550 Ok(())
1551}
1552
1553fn sysroot_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1558 let mut remap = OsString::from("--remap-path-prefix=");
1559 remap.push({
1560 let mut sysroot = build_runner.bcx.target_data.info(unit.kind).sysroot.clone();
1562 sysroot.push("lib");
1563 sysroot.push("rustlib");
1564 sysroot.push("src");
1565 sysroot.push("rust");
1566 sysroot
1567 });
1568 remap.push("=");
1569 remap.push("/rustc/");
1570 if let Some(commit_hash) = build_runner.bcx.rustc().commit_hash.as_ref() {
1571 remap.push(commit_hash);
1572 } else {
1573 remap.push(build_runner.bcx.rustc().version.to_string());
1574 }
1575 remap
1576}
1577
1578fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1586 let pkg_root = unit.pkg.root();
1587 let ws_root = build_runner.bcx.ws.root();
1588 let mut remap = OsString::from("--remap-path-prefix=");
1589 let source_id = unit.pkg.package_id().source_id();
1590 if source_id.is_git() {
1591 remap.push(
1592 build_runner
1593 .bcx
1594 .gctx
1595 .git_checkouts_path()
1596 .as_path_unlocked(),
1597 );
1598 remap.push("=");
1599 } else if source_id.is_registry() {
1600 remap.push(
1601 build_runner
1602 .bcx
1603 .gctx
1604 .registry_source_path()
1605 .as_path_unlocked(),
1606 );
1607 remap.push("=");
1608 } else if pkg_root.strip_prefix(ws_root).is_ok() {
1609 remap.push(ws_root);
1610 remap.push("=."); } else {
1612 remap.push(pkg_root);
1613 remap.push("=");
1614 remap.push(unit.pkg.name());
1615 remap.push("-");
1616 remap.push(unit.pkg.version().to_string());
1617 }
1618 remap
1619}
1620
1621fn build_dir_remap(build_runner: &BuildRunner<'_, '_>) -> OsString {
1634 let build_dir = build_runner.bcx.ws.build_dir();
1635 let mut remap = OsString::from("--remap-path-prefix=");
1636 remap.push(build_dir.as_path_unlocked());
1637 remap.push("=/cargo/build-dir");
1638 remap
1639}
1640
1641fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
1643 let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1661 let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
1662
1663 arg_feature.push("cfg(feature, values(");
1664 for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1665 if i != 0 {
1666 arg_feature.push(", ");
1667 }
1668 arg_feature.push("\"");
1669 arg_feature.push(feature);
1670 arg_feature.push("\"");
1671 }
1672 arg_feature.push("))");
1673
1674 vec![
1683 OsString::from("--check-cfg"),
1684 OsString::from("cfg(docsrs,test)"),
1685 OsString::from("--check-cfg"),
1686 arg_feature,
1687 ]
1688}
1689
1690fn lto_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsString> {
1692 let mut result = Vec::new();
1693 let mut push = |arg: &str| {
1694 result.push(OsString::from("-C"));
1695 result.push(OsString::from(arg));
1696 };
1697 match build_runner.lto[unit] {
1698 lto::Lto::Run(None) => push("lto"),
1699 lto::Lto::Run(Some(s)) => push(&format!("lto={}", s)),
1700 lto::Lto::Off => {
1701 push("lto=off");
1702 push("embed-bitcode=no");
1703 }
1704 lto::Lto::ObjectAndBitcode => {} lto::Lto::OnlyBitcode => push("linker-plugin-lto"),
1706 lto::Lto::OnlyObject => push("embed-bitcode=no"),
1707 }
1708 result
1709}
1710
1711fn build_deps_args(
1717 cmd: &mut ProcessBuilder,
1718 build_runner: &BuildRunner<'_, '_>,
1719 unit: &Unit,
1720) -> CargoResult<()> {
1721 let bcx = build_runner.bcx;
1722
1723 for arg in lib_search_paths(build_runner, unit)? {
1724 cmd.arg(arg);
1725 }
1726
1727 let deps = build_runner.unit_deps(unit);
1728
1729 if !deps
1733 .iter()
1734 .any(|dep| !dep.unit.mode.is_doc() && dep.unit.target.is_linkable())
1735 {
1736 if let Some(dep) = deps.iter().find(|dep| {
1737 !dep.unit.mode.is_doc() && dep.unit.target.is_lib() && !dep.unit.artifact.is_true()
1738 }) {
1739 let dep_name = dep.unit.target.crate_name();
1740 let name = unit.target.crate_name();
1741 bcx.gctx.shell().print_report(&[
1742 Level::WARNING.secondary_title(format!("the package `{dep_name}` provides no linkable target"))
1743 .elements([
1744 Level::NOTE.message(format!("this might cause `{name}` to fail compilation")),
1745 Level::NOTE.message("this warning might turn into a hard error in the future"),
1746 Level::HELP.message(format!("consider adding 'dylib' or 'rlib' to key 'crate-type' in `{dep_name}`'s Cargo.toml"))
1747 ])
1748 ], false)?;
1749 }
1750 }
1751
1752 let mut unstable_opts = false;
1753
1754 let first_custom_build_dep = deps.iter().find(|dep| dep.unit.mode.is_run_custom_build());
1756 if let Some(dep) = first_custom_build_dep {
1757 let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
1758 cmd.env("OUT_DIR", &out_dir);
1759 }
1760
1761 let is_multiple_build_scripts_enabled = unit
1763 .pkg
1764 .manifest()
1765 .unstable_features()
1766 .require(Feature::multiple_build_scripts())
1767 .is_ok();
1768
1769 if is_multiple_build_scripts_enabled {
1770 for dep in deps {
1771 if dep.unit.mode.is_run_custom_build() {
1772 let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
1773 let target_name = dep.unit.target.name();
1774 let out_dir_prefix = target_name
1775 .strip_prefix("build-script-")
1776 .unwrap_or(target_name);
1777 let out_dir_name = format!("{out_dir_prefix}_OUT_DIR");
1778 cmd.env(&out_dir_name, &out_dir);
1779 }
1780 }
1781 }
1782 for arg in extern_args(build_runner, unit, &mut unstable_opts)? {
1783 cmd.arg(arg);
1784 }
1785
1786 for (var, env) in artifact::get_env(build_runner, unit, deps)? {
1787 cmd.env(&var, env);
1788 }
1789
1790 if unstable_opts {
1793 cmd.arg("-Z").arg("unstable-options");
1794 }
1795
1796 Ok(())
1797}
1798
1799fn add_dep_arg<'a, 'b: 'a>(
1800 map: &mut BTreeMap<&'a Unit, PathBuf>,
1801 build_runner: &'b BuildRunner<'b, '_>,
1802 unit: &'a Unit,
1803) {
1804 if map.contains_key(&unit) {
1805 return;
1806 }
1807 map.insert(&unit, build_runner.files().deps_dir(&unit));
1808
1809 for dep in build_runner.unit_deps(unit) {
1810 add_dep_arg(map, build_runner, &dep.unit);
1811 }
1812}
1813
1814fn add_custom_flags(
1818 cmd: &mut ProcessBuilder,
1819 build_script_outputs: &BuildScriptOutputs,
1820 metadata_vec: Option<Vec<UnitHash>>,
1821) -> CargoResult<()> {
1822 if let Some(metadata_vec) = metadata_vec {
1823 for metadata in metadata_vec {
1824 if let Some(output) = build_script_outputs.get(metadata) {
1825 for cfg in output.cfgs.iter() {
1826 cmd.arg("--cfg").arg(cfg);
1827 }
1828 for check_cfg in &output.check_cfgs {
1829 cmd.arg("--check-cfg").arg(check_cfg);
1830 }
1831 for (name, value) in output.env.iter() {
1832 cmd.env(name, value);
1833 }
1834 }
1835 }
1836 }
1837
1838 Ok(())
1839}
1840
1841pub fn lib_search_paths(
1843 build_runner: &BuildRunner<'_, '_>,
1844 unit: &Unit,
1845) -> CargoResult<Vec<OsString>> {
1846 let mut lib_search_paths = Vec::new();
1847 if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout {
1848 let mut map = BTreeMap::new();
1849
1850 add_dep_arg(&mut map, build_runner, unit);
1852
1853 let paths = map.into_iter().map(|(_, path)| path).sorted_unstable();
1854
1855 for path in paths {
1856 let mut deps = OsString::from("dependency=");
1857 deps.push(path);
1858 lib_search_paths.extend(["-L".into(), deps]);
1859 }
1860 } else {
1861 let mut deps = OsString::from("dependency=");
1862 deps.push(build_runner.files().deps_dir(unit));
1863 lib_search_paths.extend(["-L".into(), deps]);
1864 }
1865
1866 if !unit.kind.is_host() {
1869 let mut deps = OsString::from("dependency=");
1870 deps.push(build_runner.files().host_deps(unit));
1871 lib_search_paths.extend(["-L".into(), deps]);
1872 }
1873
1874 Ok(lib_search_paths)
1875}
1876
1877pub fn extern_args(
1879 build_runner: &BuildRunner<'_, '_>,
1880 unit: &Unit,
1881 unstable_opts: &mut bool,
1882) -> CargoResult<Vec<OsString>> {
1883 let mut result = Vec::new();
1884 let deps = build_runner.unit_deps(unit);
1885
1886 let no_embed_metadata = build_runner.bcx.gctx.cli_unstable().no_embed_metadata;
1887
1888 let mut link_to =
1890 |dep: &UnitDep, extern_crate_name: InternedString, noprelude: bool| -> CargoResult<()> {
1891 let mut value = OsString::new();
1892 let mut opts = Vec::new();
1893 let is_public_dependency_enabled = unit
1894 .pkg
1895 .manifest()
1896 .unstable_features()
1897 .require(Feature::public_dependency())
1898 .is_ok()
1899 || build_runner.bcx.gctx.cli_unstable().public_dependency;
1900 if !dep.public && unit.target.is_lib() && is_public_dependency_enabled {
1901 opts.push("priv");
1902 *unstable_opts = true;
1903 }
1904 if noprelude {
1905 opts.push("noprelude");
1906 *unstable_opts = true;
1907 }
1908 if !opts.is_empty() {
1909 value.push(opts.join(","));
1910 value.push(":");
1911 }
1912 value.push(extern_crate_name.as_str());
1913 value.push("=");
1914
1915 let mut pass = |file| {
1916 let mut value = value.clone();
1917 value.push(file);
1918 result.push(OsString::from("--extern"));
1919 result.push(value);
1920 };
1921
1922 let outputs = build_runner.outputs(&dep.unit)?;
1923
1924 if build_runner.only_requires_rmeta(unit, &dep.unit) || dep.unit.mode.is_check() {
1925 let output = outputs
1927 .iter()
1928 .find(|output| output.flavor == FileFlavor::Rmeta)
1929 .expect("failed to find rmeta dep for pipelined dep");
1930 pass(&output.path);
1931 } else {
1932 for output in outputs.iter() {
1934 if output.flavor == FileFlavor::Linkable {
1935 pass(&output.path);
1936 }
1937 else if no_embed_metadata && output.flavor == FileFlavor::Rmeta {
1941 pass(&output.path);
1942 }
1943 }
1944 }
1945 Ok(())
1946 };
1947
1948 for dep in deps {
1949 if dep.unit.target.is_linkable() && !dep.unit.mode.is_doc() {
1950 link_to(dep, dep.extern_crate_name, dep.noprelude)?;
1951 }
1952 }
1953 if unit.target.proc_macro() {
1954 result.push(OsString::from("--extern"));
1956 result.push(OsString::from("proc_macro"));
1957 }
1958
1959 Ok(result)
1960}
1961
1962fn envify(s: &str) -> String {
1963 s.chars()
1964 .flat_map(|c| c.to_uppercase())
1965 .map(|c| if c == '-' { '_' } else { c })
1966 .collect()
1967}
1968
1969struct OutputOptions {
1972 format: MessageFormat,
1974 cache_cell: Option<(PathBuf, OnceCell<File>)>,
1979 show_diagnostics: bool,
1987 warnings_seen: usize,
1989 errors_seen: usize,
1991}
1992
1993impl OutputOptions {
1994 fn new(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OutputOptions {
1995 let path = build_runner.files().message_cache_path(unit);
1996 drop(fs::remove_file(&path));
1998 let cache_cell = Some((path, OnceCell::new()));
1999 let show_diagnostics =
2000 build_runner.bcx.gctx.warning_handling().unwrap_or_default() != WarningHandling::Allow;
2001 OutputOptions {
2002 format: build_runner.bcx.build_config.message_format,
2003 cache_cell,
2004 show_diagnostics,
2005 warnings_seen: 0,
2006 errors_seen: 0,
2007 }
2008 }
2009}
2010
2011struct ManifestErrorContext {
2017 path: PathBuf,
2019 spans: Option<toml::Spanned<toml::de::DeTable<'static>>>,
2021 contents: Option<String>,
2023 rename_table: HashMap<InternedString, InternedString>,
2026 requested_kinds: Vec<CompileKind>,
2029 cfgs: Vec<Vec<Cfg>>,
2032 host_name: InternedString,
2033 cwd: PathBuf,
2035 term_width: usize,
2037}
2038
2039fn on_stdout_line(
2040 state: &JobState<'_, '_>,
2041 line: &str,
2042 _package_id: PackageId,
2043 _target: &Target,
2044) -> CargoResult<()> {
2045 state.stdout(line.to_string())?;
2046 Ok(())
2047}
2048
2049fn on_stderr_line(
2050 state: &JobState<'_, '_>,
2051 line: &str,
2052 package_id: PackageId,
2053 manifest: &ManifestErrorContext,
2054 target: &Target,
2055 options: &mut OutputOptions,
2056) -> CargoResult<()> {
2057 if on_stderr_line_inner(state, line, package_id, manifest, target, options)? {
2058 if let Some((path, cell)) = &mut options.cache_cell {
2060 let f = cell.try_borrow_mut_with(|| paths::create(path))?;
2062 debug_assert!(!line.contains('\n'));
2063 f.write_all(line.as_bytes())?;
2064 f.write_all(&[b'\n'])?;
2065 }
2066 }
2067 Ok(())
2068}
2069
2070fn on_stderr_line_inner(
2072 state: &JobState<'_, '_>,
2073 line: &str,
2074 package_id: PackageId,
2075 manifest: &ManifestErrorContext,
2076 target: &Target,
2077 options: &mut OutputOptions,
2078) -> CargoResult<bool> {
2079 if !line.starts_with('{') {
2085 state.stderr(line.to_string())?;
2086 return Ok(true);
2087 }
2088
2089 let mut compiler_message: Box<serde_json::value::RawValue> = match serde_json::from_str(line) {
2090 Ok(msg) => msg,
2091
2092 Err(e) => {
2096 debug!("failed to parse json: {:?}", e);
2097 state.stderr(line.to_string())?;
2098 return Ok(true);
2099 }
2100 };
2101
2102 let count_diagnostic = |level, options: &mut OutputOptions| {
2103 if level == "warning" {
2104 options.warnings_seen += 1;
2105 } else if level == "error" {
2106 options.errors_seen += 1;
2107 }
2108 };
2109
2110 if let Ok(report) = serde_json::from_str::<FutureIncompatReport>(compiler_message.get()) {
2111 for item in &report.future_incompat_report {
2112 count_diagnostic(&*item.diagnostic.level, options);
2113 }
2114 state.future_incompat_report(report.future_incompat_report);
2115 return Ok(true);
2116 }
2117
2118 let res = serde_json::from_str::<SectionTiming>(compiler_message.get());
2119 if let Ok(timing_record) = res {
2120 state.on_section_timing_emitted(timing_record);
2121 return Ok(false);
2122 }
2123
2124 let add_pub_in_priv_diagnostic = |diag: &mut String| -> bool {
2126 static PRIV_DEP_REGEX: LazyLock<Regex> =
2135 LazyLock::new(|| Regex::new("from private dependency '([A-Za-z0-9-_]+)'").unwrap());
2136 if let Some(crate_name) = PRIV_DEP_REGEX.captures(diag).and_then(|m| m.get(1))
2137 && let Some(ref contents) = manifest.contents
2138 && let Some(span) = manifest.find_crate_span(crate_name.as_str())
2139 {
2140 let rel_path = pathdiff::diff_paths(&manifest.path, &manifest.cwd)
2141 .unwrap_or_else(|| manifest.path.clone())
2142 .display()
2143 .to_string();
2144 let report = [Group::with_title(Level::NOTE.secondary_title(format!(
2145 "dependency `{}` declared here",
2146 crate_name.as_str()
2147 )))
2148 .element(
2149 Snippet::source(contents)
2150 .path(rel_path)
2151 .annotation(AnnotationKind::Context.span(span)),
2152 )];
2153
2154 let rendered = Renderer::styled()
2155 .term_width(manifest.term_width)
2156 .render(&report);
2157 diag.push_str(&rendered);
2158 diag.push('\n');
2159 return true;
2160 }
2161 false
2162 };
2163
2164 match options.format {
2167 MessageFormat::Human
2172 | MessageFormat::Short
2173 | MessageFormat::Json {
2174 render_diagnostics: true,
2175 ..
2176 } => {
2177 #[derive(serde::Deserialize)]
2178 struct CompilerMessage<'a> {
2179 rendered: String,
2183 #[serde(borrow)]
2184 message: Cow<'a, str>,
2185 #[serde(borrow)]
2186 level: Cow<'a, str>,
2187 children: Vec<PartialDiagnostic>,
2188 code: Option<DiagnosticCode>,
2189 }
2190
2191 #[derive(serde::Deserialize)]
2200 struct PartialDiagnostic {
2201 spans: Vec<PartialDiagnosticSpan>,
2202 }
2203
2204 #[derive(serde::Deserialize)]
2206 struct PartialDiagnosticSpan {
2207 suggestion_applicability: Option<Applicability>,
2208 }
2209
2210 #[derive(serde::Deserialize)]
2211 struct DiagnosticCode {
2212 code: String,
2213 }
2214
2215 if let Ok(mut msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
2216 {
2217 if msg.message.starts_with("aborting due to")
2218 || msg.message.ends_with("warning emitted")
2219 || msg.message.ends_with("warnings emitted")
2220 {
2221 return Ok(true);
2223 }
2224 if msg.rendered.ends_with('\n') {
2226 msg.rendered.pop();
2227 }
2228 let mut rendered = msg.rendered;
2229 if options.show_diagnostics {
2230 let machine_applicable: bool = msg
2231 .children
2232 .iter()
2233 .map(|child| {
2234 child
2235 .spans
2236 .iter()
2237 .filter_map(|span| span.suggestion_applicability)
2238 .any(|app| app == Applicability::MachineApplicable)
2239 })
2240 .any(|b| b);
2241 count_diagnostic(&msg.level, options);
2242 if msg
2243 .code
2244 .as_ref()
2245 .is_some_and(|c| c.code == "exported_private_dependencies")
2246 && options.format != MessageFormat::Short
2247 {
2248 add_pub_in_priv_diagnostic(&mut rendered);
2249 }
2250 let lint = msg.code.is_some();
2251 state.emit_diag(&msg.level, rendered, lint, machine_applicable)?;
2252 }
2253 return Ok(true);
2254 }
2255 }
2256
2257 MessageFormat::Json { ansi, .. } => {
2258 #[derive(serde::Deserialize, serde::Serialize)]
2259 struct CompilerMessage<'a> {
2260 rendered: String,
2261 #[serde(flatten, borrow)]
2262 other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
2263 code: Option<DiagnosticCode<'a>>,
2264 }
2265
2266 #[derive(serde::Deserialize, serde::Serialize)]
2267 struct DiagnosticCode<'a> {
2268 code: String,
2269 #[serde(flatten, borrow)]
2270 other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
2271 }
2272
2273 if let Ok(mut error) =
2274 serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
2275 {
2276 let modified_diag = if error
2277 .code
2278 .as_ref()
2279 .is_some_and(|c| c.code == "exported_private_dependencies")
2280 {
2281 add_pub_in_priv_diagnostic(&mut error.rendered)
2282 } else {
2283 false
2284 };
2285
2286 if !ansi {
2290 error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
2291 }
2292 if !ansi || modified_diag {
2293 let new_line = serde_json::to_string(&error)?;
2294 compiler_message = serde_json::value::RawValue::from_string(new_line)?;
2295 }
2296 }
2297 }
2298 }
2299
2300 #[derive(serde::Deserialize)]
2307 struct ArtifactNotification<'a> {
2308 #[serde(borrow)]
2309 artifact: Cow<'a, str>,
2310 }
2311
2312 if let Ok(artifact) = serde_json::from_str::<ArtifactNotification<'_>>(compiler_message.get()) {
2313 trace!("found directive from rustc: `{}`", artifact.artifact);
2314 if artifact.artifact.ends_with(".rmeta") {
2315 debug!("looks like metadata finished early!");
2316 state.rmeta_produced();
2317 }
2318 return Ok(false);
2319 }
2320
2321 if !options.show_diagnostics {
2326 return Ok(true);
2327 }
2328
2329 #[derive(serde::Deserialize)]
2330 struct CompilerMessage<'a> {
2331 #[serde(borrow)]
2332 message: Cow<'a, str>,
2333 #[serde(borrow)]
2334 level: Cow<'a, str>,
2335 }
2336
2337 if let Ok(msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get()) {
2338 if msg.message.starts_with("aborting due to")
2339 || msg.message.ends_with("warning emitted")
2340 || msg.message.ends_with("warnings emitted")
2341 {
2342 return Ok(true);
2344 }
2345 count_diagnostic(&msg.level, options);
2346 }
2347
2348 let msg = machine_message::FromCompiler {
2349 package_id: package_id.to_spec(),
2350 manifest_path: &manifest.path,
2351 target,
2352 message: compiler_message,
2353 }
2354 .to_json_string();
2355
2356 state.stdout(msg)?;
2360 Ok(true)
2361}
2362
2363impl ManifestErrorContext {
2364 fn new(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> ManifestErrorContext {
2365 let mut duplicates = HashSet::new();
2366 let mut rename_table = HashMap::new();
2367
2368 for dep in build_runner.unit_deps(unit) {
2369 let unrenamed_id = dep.unit.pkg.package_id().name();
2370 if duplicates.contains(&unrenamed_id) {
2371 continue;
2372 }
2373 match rename_table.entry(unrenamed_id) {
2374 std::collections::hash_map::Entry::Occupied(occ) => {
2375 occ.remove_entry();
2376 duplicates.insert(unrenamed_id);
2377 }
2378 std::collections::hash_map::Entry::Vacant(vac) => {
2379 vac.insert(dep.extern_crate_name);
2380 }
2381 }
2382 }
2383
2384 let bcx = build_runner.bcx;
2385 ManifestErrorContext {
2386 path: unit.pkg.manifest_path().to_owned(),
2387 spans: unit.pkg.manifest().document().cloned(),
2388 contents: unit.pkg.manifest().contents().map(String::from),
2389 requested_kinds: bcx.target_data.requested_kinds().to_owned(),
2390 host_name: bcx.rustc().host,
2391 rename_table,
2392 cwd: path_args(build_runner.bcx.ws, unit).1,
2393 cfgs: bcx
2394 .target_data
2395 .requested_kinds()
2396 .iter()
2397 .map(|k| bcx.target_data.cfg(*k).to_owned())
2398 .collect(),
2399 term_width: bcx
2400 .gctx
2401 .shell()
2402 .err_width()
2403 .diagnostic_terminal_width()
2404 .unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
2405 }
2406 }
2407
2408 fn requested_target_names(&self) -> impl Iterator<Item = &str> {
2409 self.requested_kinds.iter().map(|kind| match kind {
2410 CompileKind::Host => &self.host_name,
2411 CompileKind::Target(target) => target.short_name(),
2412 })
2413 }
2414
2415 fn find_crate_span(&self, unrenamed: &str) -> Option<Range<usize>> {
2429 let Some(ref spans) = self.spans else {
2430 return None;
2431 };
2432
2433 let orig_name = self.rename_table.get(unrenamed)?.as_str();
2434
2435 if let Some((k, v)) = get_key_value(&spans, &["dependencies", orig_name]) {
2436 if let Some(package) = v.get_ref().as_table().and_then(|t| t.get("package")) {
2445 return Some(package.span());
2446 } else {
2447 return Some(k.span());
2448 }
2449 }
2450
2451 if let Some(target) = spans
2456 .as_ref()
2457 .get("target")
2458 .and_then(|t| t.as_ref().as_table())
2459 {
2460 for (platform, platform_table) in target.iter() {
2461 match platform.as_ref().parse::<Platform>() {
2462 Ok(Platform::Name(name)) => {
2463 if !self.requested_target_names().any(|n| n == name) {
2464 continue;
2465 }
2466 }
2467 Ok(Platform::Cfg(cfg_expr)) => {
2468 if !self.cfgs.iter().any(|cfgs| cfg_expr.matches(cfgs)) {
2469 continue;
2470 }
2471 }
2472 Err(_) => continue,
2473 }
2474
2475 let Some(platform_table) = platform_table.as_ref().as_table() else {
2476 continue;
2477 };
2478
2479 if let Some(deps) = platform_table
2480 .get("dependencies")
2481 .and_then(|d| d.as_ref().as_table())
2482 {
2483 if let Some((k, v)) = deps.get_key_value(orig_name) {
2484 if let Some(package) = v.get_ref().as_table().and_then(|t| t.get("package"))
2485 {
2486 return Some(package.span());
2487 } else {
2488 return Some(k.span());
2489 }
2490 }
2491 }
2492 }
2493 }
2494 None
2495 }
2496}
2497
2498fn replay_output_cache(
2502 package_id: PackageId,
2503 manifest: ManifestErrorContext,
2504 target: &Target,
2505 path: PathBuf,
2506 format: MessageFormat,
2507 show_diagnostics: bool,
2508) -> Work {
2509 let target = target.clone();
2510 let mut options = OutputOptions {
2511 format,
2512 cache_cell: None,
2513 show_diagnostics,
2514 warnings_seen: 0,
2515 errors_seen: 0,
2516 };
2517 Work::new(move |state| {
2518 if !path.exists() {
2519 return Ok(());
2521 }
2522 let file = paths::open(&path)?;
2526 let mut reader = std::io::BufReader::new(file);
2527 let mut line = String::new();
2528 loop {
2529 let length = reader.read_line(&mut line)?;
2530 if length == 0 {
2531 break;
2532 }
2533 let trimmed = line.trim_end_matches(&['\n', '\r'][..]);
2534 on_stderr_line(state, trimmed, package_id, &manifest, &target, &mut options)?;
2535 line.clear();
2536 }
2537 Ok(())
2538 })
2539}
2540
2541fn descriptive_pkg_name(name: &str, target: &Target, mode: &CompileMode) -> String {
2544 let desc_name = target.description_named();
2545 let mode = if mode.is_rustc_test() && !(target.is_test() || target.is_bench()) {
2546 " test"
2547 } else if mode.is_doc_test() {
2548 " doctest"
2549 } else if mode.is_doc() {
2550 " doc"
2551 } else {
2552 ""
2553 };
2554 format!("`{name}` ({desc_name}{mode})")
2555}
2556
2557pub(crate) fn apply_env_config(
2559 gctx: &crate::GlobalContext,
2560 cmd: &mut ProcessBuilder,
2561) -> CargoResult<()> {
2562 for (key, value) in gctx.env_config()?.iter() {
2563 if cmd.get_envs().contains_key(key) {
2565 continue;
2566 }
2567 cmd.env(key, value);
2568 }
2569 Ok(())
2570}
2571
2572fn should_include_scrape_units(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
2574 unit.mode.is_doc() && bcx.scrape_units.len() > 0 && bcx.ws.unit_needs_doc_scrape(unit)
2575}
2576
2577fn scrape_output_path(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<PathBuf> {
2579 assert!(unit.mode.is_doc() || unit.mode.is_doc_scrape());
2580 build_runner
2581 .outputs(unit)
2582 .map(|outputs| outputs[0].path.clone())
2583}
2584
2585fn rustdoc_dep_info_loc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> PathBuf {
2587 let mut loc = build_runner.files().fingerprint_file_path(unit, "");
2588 loc.set_extension("d");
2589 loc
2590}