cargo/core/compiler/
mod.rs

1//! # Interact with the compiler
2//!
3//! If you consider [`ops::cargo_compile::compile`] as a `rustc` driver but on
4//! Cargo side, this module is kinda the `rustc_interface` for that merits.
5//! It contains all the interaction between Cargo and the rustc compiler,
6//! from preparing the context for the entire build process, to scheduling
7//! and executing each unit of work (e.g. running `rustc`), to managing and
8//! caching the output artifact of a build.
9//!
10//! However, it hasn't yet exposed a clear definition of each phase or session,
11//! like what rustc has done. Also, no one knows if Cargo really needs that.
12//! To be pragmatic, here we list a handful of items you may want to learn:
13//!
14//! * [`BuildContext`] is a static context containing all information you need
15//!   before a build gets started.
16//! * [`BuildRunner`] is the center of the world, coordinating a running build and
17//!   collecting information from it.
18//! * [`custom_build`] is the home of build script executions and output parsing.
19//! * [`fingerprint`] not only defines but also executes a set of rules to
20//!   determine if a re-compile is needed.
21//! * [`job_queue`] is where the parallelism, job scheduling, and communication
22//!   machinery happen between Cargo and the compiler.
23//! * [`layout`] defines and manages output artifacts of a build in the filesystem.
24//! * [`unit_dependencies`] is for building a dependency graph for compilation
25//!   from a result of dependency resolution.
26//! * [`Unit`] contains sufficient information to build something, usually
27//!   turning into a compiler invocation in a later phase.
28//!
29//! [`ops::cargo_compile::compile`]: crate::ops::compile
30
31pub 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;
49pub mod 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::BuildContext;
76pub use self::build_context::FileFlavor;
77pub use self::build_context::FileType;
78pub use self::build_context::RustcTargetData;
79pub use self::build_context::TargetInfo;
80pub use self::build_runner::{BuildRunner, Metadata, UnitHash};
81pub use self::compilation::{Compilation, Doctest, UnitOutput};
82pub use self::compile_kind::{CompileKind, CompileKindFallback, CompileTarget};
83pub use self::crate_type::CrateType;
84pub use self::custom_build::LinkArgTarget;
85pub use self::custom_build::{BuildOutput, BuildScriptOutputs, BuildScripts, LibraryPath};
86pub(crate) use self::fingerprint::DirtyReason;
87pub use self::fingerprint::RustdocFingerprint;
88pub use self::job_queue::Freshness;
89use self::job_queue::{Job, JobQueue, JobState, Work};
90pub(crate) use self::layout::Layout;
91pub use self::lto::Lto;
92use self::output_depinfo::output_depinfo;
93use self::output_sbom::build_sbom;
94use self::unit_graph::UnitDep;
95
96use crate::core::compiler::future_incompat::FutureIncompatReport;
97use crate::core::compiler::timings::SectionTiming;
98pub use crate::core::compiler::unit::{Unit, UnitInterner};
99use crate::core::manifest::TargetSourcePath;
100use crate::core::profiles::{PanicStrategy, Profile, StripInner};
101use crate::core::{Feature, PackageId, Target, Verbosity};
102use crate::lints::get_key_value;
103use crate::util::OnceExt;
104use crate::util::context::WarningHandling;
105use crate::util::errors::{CargoResult, VerboseError};
106use crate::util::interning::InternedString;
107use crate::util::machine_message::{self, Message};
108use crate::util::{add_path_args, internal, path_args};
109
110use cargo_util::{ProcessBuilder, ProcessError, paths};
111use cargo_util_schemas::manifest::TomlDebugInfo;
112use cargo_util_schemas::manifest::TomlTrimPaths;
113use cargo_util_schemas::manifest::TomlTrimPathsValue;
114use rustfix::diagnostics::Applicability;
115pub(crate) use timings::CompilationSection;
116
117const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version";
118
119/// A glorified callback for executing calls to rustc. Rather than calling rustc
120/// directly, we'll use an `Executor`, giving clients an opportunity to intercept
121/// the build calls.
122pub trait Executor: Send + Sync + 'static {
123    /// Called after a rustc process invocation is prepared up-front for a given
124    /// unit of work (may still be modified for runtime-known dependencies, when
125    /// the work is actually executed).
126    fn init(&self, _build_runner: &BuildRunner<'_, '_>, _unit: &Unit) {}
127
128    /// In case of an `Err`, Cargo will not continue with the build process for
129    /// this package.
130    fn exec(
131        &self,
132        cmd: &ProcessBuilder,
133        id: PackageId,
134        target: &Target,
135        mode: CompileMode,
136        on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
137        on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
138    ) -> CargoResult<()>;
139
140    /// Queried when queuing each unit of work. If it returns true, then the
141    /// unit will always be rebuilt, independent of whether it needs to be.
142    fn force_rebuild(&self, _unit: &Unit) -> bool {
143        false
144    }
145}
146
147/// A `DefaultExecutor` calls rustc without doing anything else. It is Cargo's
148/// default behaviour.
149#[derive(Copy, Clone)]
150pub struct DefaultExecutor;
151
152impl Executor for DefaultExecutor {
153    #[instrument(name = "rustc", skip_all, fields(package = id.name().as_str(), process = cmd.to_string()))]
154    fn exec(
155        &self,
156        cmd: &ProcessBuilder,
157        id: PackageId,
158        _target: &Target,
159        _mode: CompileMode,
160        on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
161        on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
162    ) -> CargoResult<()> {
163        cmd.exec_with_streaming(on_stdout_line, on_stderr_line, false)
164            .map(drop)
165    }
166}
167
168/// Builds up and enqueue a list of pending jobs onto the `job` queue.
169///
170/// Starting from the `unit`, this function recursively calls itself to build
171/// all jobs for dependencies of the `unit`. Each of these jobs represents
172/// compiling a particular package.
173///
174/// Note that **no actual work is executed as part of this**, that's all done
175/// next as part of [`JobQueue::execute`] function which will run everything
176/// in order with proper parallelism.
177#[tracing::instrument(skip(build_runner, jobs, exec))]
178fn compile<'gctx>(
179    build_runner: &mut BuildRunner<'_, 'gctx>,
180    jobs: &mut JobQueue<'gctx>,
181    unit: &Unit,
182    exec: &Arc<dyn Executor>,
183    force_rebuild: bool,
184) -> CargoResult<()> {
185    let bcx = build_runner.bcx;
186    if !build_runner.compiled.insert(unit.clone()) {
187        return Ok(());
188    }
189
190    // If we are in `--compile-time-deps` and the given unit is not a compile time
191    // dependency, skip compiling the unit and jumps to dependencies, which still
192    // have chances to be compile time dependencies
193    if !unit.skip_non_compile_time_dep {
194        // Build up the work to be done to compile this unit, enqueuing it once
195        // we've got everything constructed.
196        fingerprint::prepare_init(build_runner, unit)?;
197
198        let job = if unit.mode.is_run_custom_build() {
199            custom_build::prepare(build_runner, unit)?
200        } else if unit.mode.is_doc_test() {
201            // We run these targets later, so this is just a no-op for now.
202            Job::new_fresh()
203        } else {
204            let force = exec.force_rebuild(unit) || force_rebuild;
205            let mut job = fingerprint::prepare_target(build_runner, unit, force)?;
206            job.before(if job.freshness().is_dirty() {
207                let work = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
208                    rustdoc(build_runner, unit)?
209                } else {
210                    rustc(build_runner, unit, exec)?
211                };
212                work.then(link_targets(build_runner, unit, false)?)
213            } else {
214                // We always replay the output cache,
215                // since it might contain future-incompat-report messages
216                let show_diagnostics = unit.show_warnings(bcx.gctx)
217                    && build_runner.bcx.gctx.warning_handling()? != WarningHandling::Allow;
218                let manifest = ManifestErrorContext::new(build_runner, unit);
219                let work = replay_output_cache(
220                    unit.pkg.package_id(),
221                    manifest,
222                    &unit.target,
223                    build_runner.files().message_cache_path(unit),
224                    build_runner.bcx.build_config.message_format,
225                    show_diagnostics,
226                );
227                // Need to link targets on both the dirty and fresh.
228                work.then(link_targets(build_runner, unit, true)?)
229            });
230
231            job
232        };
233        jobs.enqueue(build_runner, unit, job)?;
234    }
235
236    // Be sure to compile all dependencies of this target as well.
237    let deps = Vec::from(build_runner.unit_deps(unit)); // Create vec due to mutable borrow.
238    for dep in deps {
239        compile(build_runner, jobs, &dep.unit, exec, false)?;
240    }
241
242    Ok(())
243}
244
245/// Generates the warning message used when fallible doc-scrape units fail,
246/// either for rustdoc or rustc.
247fn make_failed_scrape_diagnostic(
248    build_runner: &BuildRunner<'_, '_>,
249    unit: &Unit,
250    top_line: impl Display,
251) -> String {
252    let manifest_path = unit.pkg.manifest_path();
253    let relative_manifest_path = manifest_path
254        .strip_prefix(build_runner.bcx.ws.root())
255        .unwrap_or(&manifest_path);
256
257    format!(
258        "\
259{top_line}
260    Try running with `--verbose` to see the error message.
261    If an example should not be scanned, then consider adding `doc-scrape-examples = false` to its `[[example]]` definition in {}",
262        relative_manifest_path.display()
263    )
264}
265
266/// Creates a unit of work invoking `rustc` for building the `unit`.
267fn rustc(
268    build_runner: &mut BuildRunner<'_, '_>,
269    unit: &Unit,
270    exec: &Arc<dyn Executor>,
271) -> CargoResult<Work> {
272    let mut rustc = prepare_rustc(build_runner, unit)?;
273
274    let name = unit.pkg.name();
275
276    let outputs = build_runner.outputs(unit)?;
277    let root = build_runner.files().out_dir(unit);
278
279    // Prepare the native lib state (extra `-L` and `-l` flags).
280    let build_script_outputs = Arc::clone(&build_runner.build_script_outputs);
281    let current_id = unit.pkg.package_id();
282    let manifest = ManifestErrorContext::new(build_runner, unit);
283    let build_scripts = build_runner.build_scripts.get(unit).cloned();
284
285    // If we are a binary and the package also contains a library, then we
286    // don't pass the `-l` flags.
287    let pass_l_flag = unit.target.is_lib() || !unit.pkg.targets().iter().any(|t| t.is_lib());
288
289    let dep_info_name =
290        if let Some(c_extra_filename) = build_runner.files().metadata(unit).c_extra_filename() {
291            format!("{}-{}.d", unit.target.crate_name(), c_extra_filename)
292        } else {
293            format!("{}.d", unit.target.crate_name())
294        };
295    let rustc_dep_info_loc = root.join(dep_info_name);
296    let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
297
298    let mut output_options = OutputOptions::new(build_runner, unit);
299    let package_id = unit.pkg.package_id();
300    let target = Target::clone(&unit.target);
301    let mode = unit.mode;
302
303    exec.init(build_runner, unit);
304    let exec = exec.clone();
305
306    let root_output = build_runner.files().host_dest().map(|v| v.to_path_buf());
307    let build_dir = build_runner.bcx.ws.build_dir().into_path_unlocked();
308    let pkg_root = unit.pkg.root().to_path_buf();
309    let cwd = rustc
310        .get_cwd()
311        .unwrap_or_else(|| build_runner.bcx.gctx.cwd())
312        .to_path_buf();
313    let fingerprint_dir = build_runner.files().fingerprint_dir(unit);
314    let script_metadatas = build_runner.find_build_script_metadatas(unit);
315    let is_local = unit.is_local();
316    let artifact = unit.artifact;
317    let sbom_files = build_runner.sbom_output_files(unit)?;
318    let sbom = build_sbom(build_runner, unit)?;
319
320    let hide_diagnostics_for_scrape_unit = build_runner.bcx.unit_can_fail_for_docscraping(unit)
321        && !matches!(
322            build_runner.bcx.gctx.shell().verbosity(),
323            Verbosity::Verbose
324        );
325    let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
326        // If this unit is needed for doc-scraping, then we generate a diagnostic that
327        // describes the set of reverse-dependencies that cause the unit to be needed.
328        let target_desc = unit.target.description_named();
329        let mut for_scrape_units = build_runner
330            .bcx
331            .scrape_units_have_dep_on(unit)
332            .into_iter()
333            .map(|unit| unit.target.description_named())
334            .collect::<Vec<_>>();
335        for_scrape_units.sort();
336        let for_scrape_units = for_scrape_units.join(", ");
337        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}"))
338    });
339    if hide_diagnostics_for_scrape_unit {
340        output_options.show_diagnostics = false;
341    }
342    let env_config = Arc::clone(build_runner.bcx.gctx.env_config()?);
343    return Ok(Work::new(move |state| {
344        // Artifacts are in a different location than typical units,
345        // hence we must assure the crate- and target-dependent
346        // directory is present.
347        if artifact.is_true() {
348            paths::create_dir_all(&root)?;
349        }
350
351        // Only at runtime have we discovered what the extra -L and -l
352        // arguments are for native libraries, so we process those here. We
353        // also need to be sure to add any -L paths for our plugins to the
354        // dynamic library load path as a plugin's dynamic library may be
355        // located somewhere in there.
356        // Finally, if custom environment variables have been produced by
357        // previous build scripts, we include them in the rustc invocation.
358        if let Some(build_scripts) = build_scripts {
359            let script_outputs = build_script_outputs.lock().unwrap();
360            add_native_deps(
361                &mut rustc,
362                &script_outputs,
363                &build_scripts,
364                pass_l_flag,
365                &target,
366                current_id,
367                mode,
368            )?;
369            if let Some(ref root_output) = root_output {
370                add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, root_output)?;
371            }
372            add_custom_flags(&mut rustc, &script_outputs, script_metadatas)?;
373        }
374
375        for output in outputs.iter() {
376            // If there is both an rmeta and rlib, rustc will prefer to use the
377            // rlib, even if it is older. Therefore, we must delete the rlib to
378            // force using the new rmeta.
379            if output.path.extension() == Some(OsStr::new("rmeta")) {
380                let dst = root.join(&output.path).with_extension("rlib");
381                if dst.exists() {
382                    paths::remove_file(&dst)?;
383                }
384            }
385
386            // Some linkers do not remove the executable, but truncate and modify it.
387            // That results in the old hard-link being modified even after renamed.
388            // We delete the old artifact here to prevent this behavior from confusing users.
389            // See rust-lang/cargo#8348.
390            if output.hardlink.is_some() && output.path.exists() {
391                _ = paths::remove_file(&output.path).map_err(|e| {
392                    tracing::debug!(
393                        "failed to delete previous output file `{:?}`: {e:?}",
394                        output.path
395                    );
396                });
397            }
398        }
399
400        state.running(&rustc);
401        let timestamp = paths::set_invocation_time(&fingerprint_dir)?;
402        for file in sbom_files {
403            tracing::debug!("writing sbom to {}", file.display());
404            let outfile = BufWriter::new(paths::create(&file)?);
405            serde_json::to_writer(outfile, &sbom)?;
406        }
407
408        let result = exec
409            .exec(
410                &rustc,
411                package_id,
412                &target,
413                mode,
414                &mut |line| on_stdout_line(state, line, package_id, &target),
415                &mut |line| {
416                    on_stderr_line(
417                        state,
418                        line,
419                        package_id,
420                        &manifest,
421                        &target,
422                        &mut output_options,
423                    )
424                },
425            )
426            .map_err(|e| {
427                if output_options.errors_seen == 0 {
428                    // If we didn't expect an error, do not require --verbose to fail.
429                    // This is intended to debug
430                    // https://github.com/rust-lang/crater/issues/733, where we are seeing
431                    // Cargo exit unsuccessfully while seeming to not show any errors.
432                    e
433                } else {
434                    verbose_if_simple_exit_code(e)
435                }
436            })
437            .with_context(|| {
438                // adapted from rustc_errors/src/lib.rs
439                let warnings = match output_options.warnings_seen {
440                    0 => String::new(),
441                    1 => "; 1 warning emitted".to_string(),
442                    count => format!("; {} warnings emitted", count),
443                };
444                let errors = match output_options.errors_seen {
445                    0 => String::new(),
446                    1 => " due to 1 previous error".to_string(),
447                    count => format!(" due to {} previous errors", count),
448                };
449                let name = descriptive_pkg_name(&name, &target, &mode);
450                format!("could not compile {name}{errors}{warnings}")
451            });
452
453        if let Err(e) = result {
454            if let Some(diagnostic) = failed_scrape_diagnostic {
455                state.warning(diagnostic);
456            }
457
458            return Err(e);
459        }
460
461        // Exec should never return with success *and* generate an error.
462        debug_assert_eq!(output_options.errors_seen, 0);
463
464        if rustc_dep_info_loc.exists() {
465            fingerprint::translate_dep_info(
466                &rustc_dep_info_loc,
467                &dep_info_loc,
468                &cwd,
469                &pkg_root,
470                &build_dir,
471                &rustc,
472                // Do not track source files in the fingerprint for registry dependencies.
473                is_local,
474                &env_config,
475            )
476            .with_context(|| {
477                internal(format!(
478                    "could not parse/generate dep info at: {}",
479                    rustc_dep_info_loc.display()
480                ))
481            })?;
482            // This mtime shift allows Cargo to detect if a source file was
483            // modified in the middle of the build.
484            paths::set_file_time_no_err(dep_info_loc, timestamp);
485        }
486
487        // This mtime shift for .rmeta is a workaround as rustc incremental build
488        // since rust-lang/rust#114669 (1.90.0) skips unnecessary rmeta generation.
489        //
490        // The situation is like this:
491        //
492        // 1. When build script execution's external dependendies
493        //    (rerun-if-changed, rerun-if-env-changed) got updated,
494        //    the execution unit reran and got a newer mtime.
495        // 2. rustc type-checked the associated crate, though with incremental
496        //    compilation, no rmeta regeneration. Its `.rmeta` stays old.
497        // 3. Run `cargo check` again. Cargo found build script execution had
498        //    a new mtime than existing crate rmeta, so re-checking the crate.
499        //    However the check is a no-op (input has no change), so stuck.
500        if mode.is_check() {
501            for output in outputs.iter() {
502                paths::set_file_time_no_err(&output.path, timestamp);
503            }
504        }
505
506        Ok(())
507    }));
508
509    // Add all relevant `-L` and `-l` flags from dependencies (now calculated and
510    // present in `state`) to the command provided.
511    fn add_native_deps(
512        rustc: &mut ProcessBuilder,
513        build_script_outputs: &BuildScriptOutputs,
514        build_scripts: &BuildScripts,
515        pass_l_flag: bool,
516        target: &Target,
517        current_id: PackageId,
518        mode: CompileMode,
519    ) -> CargoResult<()> {
520        let mut library_paths = vec![];
521
522        for key in build_scripts.to_link.iter() {
523            let output = build_script_outputs.get(key.1).ok_or_else(|| {
524                internal(format!(
525                    "couldn't find build script output for {}/{}",
526                    key.0, key.1
527                ))
528            })?;
529            library_paths.extend(output.library_paths.iter());
530        }
531
532        // NOTE: This very intentionally does not use the derived ord from LibraryPath because we need to
533        // retain relative ordering within the same type (i.e. not lexicographic). The use of a stable sort
534        // is also important here because it ensures that paths of the same type retain the same relative
535        // ordering (for an unstable sort to work here, the list would need to retain the idx of each element
536        // and then sort by that idx when the type is equivalent.
537        library_paths.sort_by_key(|p| match p {
538            LibraryPath::CargoArtifact(_) => 0,
539            LibraryPath::External(_) => 1,
540        });
541
542        for path in library_paths.iter() {
543            rustc.arg("-L").arg(path.as_ref());
544        }
545
546        for key in build_scripts.to_link.iter() {
547            let output = build_script_outputs.get(key.1).ok_or_else(|| {
548                internal(format!(
549                    "couldn't find build script output for {}/{}",
550                    key.0, key.1
551                ))
552            })?;
553
554            if key.0 == current_id {
555                if pass_l_flag {
556                    for name in output.library_links.iter() {
557                        rustc.arg("-l").arg(name);
558                    }
559                }
560            }
561
562            for (lt, arg) in &output.linker_args {
563                // There was an unintentional change where cdylibs were
564                // allowed to be passed via transitive dependencies. This
565                // clause should have been kept in the `if` block above. For
566                // now, continue allowing it for cdylib only.
567                // See https://github.com/rust-lang/cargo/issues/9562
568                if lt.applies_to(target, mode)
569                    && (key.0 == current_id || *lt == LinkArgTarget::Cdylib)
570                {
571                    rustc.arg("-C").arg(format!("link-arg={}", arg));
572                }
573            }
574        }
575        Ok(())
576    }
577}
578
579fn verbose_if_simple_exit_code(err: Error) -> Error {
580    // If a signal on unix (`code == None`) or an abnormal termination
581    // on Windows (codes like `0xC0000409`), don't hide the error details.
582    match err
583        .downcast_ref::<ProcessError>()
584        .as_ref()
585        .and_then(|perr| perr.code)
586    {
587        Some(n) if cargo_util::is_simple_exit_code(n) => VerboseError::new(err).into(),
588        _ => err,
589    }
590}
591
592/// Link the compiled target (often of form `foo-{metadata_hash}`) to the
593/// final target. This must happen during both "Fresh" and "Compile".
594fn link_targets(
595    build_runner: &mut BuildRunner<'_, '_>,
596    unit: &Unit,
597    fresh: bool,
598) -> CargoResult<Work> {
599    let bcx = build_runner.bcx;
600    let outputs = build_runner.outputs(unit)?;
601    let export_dir = build_runner.files().export_dir();
602    let package_id = unit.pkg.package_id();
603    let manifest_path = PathBuf::from(unit.pkg.manifest_path());
604    let profile = unit.profile.clone();
605    let unit_mode = unit.mode;
606    let features = unit.features.iter().map(|s| s.to_string()).collect();
607    let json_messages = bcx.build_config.emit_json();
608    let executable = build_runner.get_executable(unit)?;
609    let mut target = Target::clone(&unit.target);
610    if let TargetSourcePath::Metabuild = target.src_path() {
611        // Give it something to serialize.
612        let path = unit
613            .pkg
614            .manifest()
615            .metabuild_path(build_runner.bcx.ws.build_dir());
616        target.set_src_path(TargetSourcePath::Path(path));
617    }
618
619    Ok(Work::new(move |state| {
620        // If we're a "root crate", e.g., the target of this compilation, then we
621        // hard link our outputs out of the `deps` directory into the directory
622        // above. This means that `cargo build` will produce binaries in
623        // `target/debug` which one probably expects.
624        let mut destinations = vec![];
625        for output in outputs.iter() {
626            let src = &output.path;
627            // This may have been a `cargo rustc` command which changes the
628            // output, so the source may not actually exist.
629            if !src.exists() {
630                continue;
631            }
632            let Some(dst) = output.hardlink.as_ref() else {
633                destinations.push(src.clone());
634                continue;
635            };
636            destinations.push(dst.clone());
637            paths::link_or_copy(src, dst)?;
638            if let Some(ref path) = output.export_path {
639                let export_dir = export_dir.as_ref().unwrap();
640                paths::create_dir_all(export_dir)?;
641
642                paths::link_or_copy(src, path)?;
643            }
644        }
645
646        if json_messages {
647            let debuginfo = match profile.debuginfo.into_inner() {
648                TomlDebugInfo::None => machine_message::ArtifactDebuginfo::Int(0),
649                TomlDebugInfo::Limited => machine_message::ArtifactDebuginfo::Int(1),
650                TomlDebugInfo::Full => machine_message::ArtifactDebuginfo::Int(2),
651                TomlDebugInfo::LineDirectivesOnly => {
652                    machine_message::ArtifactDebuginfo::Named("line-directives-only")
653                }
654                TomlDebugInfo::LineTablesOnly => {
655                    machine_message::ArtifactDebuginfo::Named("line-tables-only")
656                }
657            };
658            let art_profile = machine_message::ArtifactProfile {
659                opt_level: profile.opt_level.as_str(),
660                debuginfo: Some(debuginfo),
661                debug_assertions: profile.debug_assertions,
662                overflow_checks: profile.overflow_checks,
663                test: unit_mode.is_any_test(),
664            };
665
666            let msg = machine_message::Artifact {
667                package_id: package_id.to_spec(),
668                manifest_path,
669                target: &target,
670                profile: art_profile,
671                features,
672                filenames: destinations,
673                executable,
674                fresh,
675            }
676            .to_json_string();
677            state.stdout(msg)?;
678        }
679        Ok(())
680    }))
681}
682
683// For all plugin dependencies, add their -L paths (now calculated and present
684// in `build_script_outputs`) to the dynamic library load path for the command
685// to execute.
686fn add_plugin_deps(
687    rustc: &mut ProcessBuilder,
688    build_script_outputs: &BuildScriptOutputs,
689    build_scripts: &BuildScripts,
690    root_output: &Path,
691) -> CargoResult<()> {
692    let var = paths::dylib_path_envvar();
693    let search_path = rustc.get_env(var).unwrap_or_default();
694    let mut search_path = env::split_paths(&search_path).collect::<Vec<_>>();
695    for (pkg_id, metadata) in &build_scripts.plugins {
696        let output = build_script_outputs
697            .get(*metadata)
698            .ok_or_else(|| internal(format!("couldn't find libs for plugin dep {}", pkg_id)))?;
699        search_path.append(&mut filter_dynamic_search_path(
700            output.library_paths.iter().map(AsRef::as_ref),
701            root_output,
702        ));
703    }
704    let search_path = paths::join_paths(&search_path, var)?;
705    rustc.env(var, &search_path);
706    Ok(())
707}
708
709fn get_dynamic_search_path(path: &Path) -> &Path {
710    match path.to_str().and_then(|s| s.split_once("=")) {
711        Some(("native" | "crate" | "dependency" | "framework" | "all", path)) => Path::new(path),
712        _ => path,
713    }
714}
715
716// Determine paths to add to the dynamic search path from -L entries
717//
718// Strip off prefixes like "native=" or "framework=" and filter out directories
719// **not** inside our output directory since they are likely spurious and can cause
720// clashes with system shared libraries (issue #3366).
721fn filter_dynamic_search_path<'a, I>(paths: I, root_output: &Path) -> Vec<PathBuf>
722where
723    I: Iterator<Item = &'a PathBuf>,
724{
725    let mut search_path = vec![];
726    for dir in paths {
727        let dir = get_dynamic_search_path(dir);
728        if dir.starts_with(&root_output) {
729            search_path.push(dir.to_path_buf());
730        } else {
731            debug!(
732                "Not including path {} in runtime library search path because it is \
733                 outside target root {}",
734                dir.display(),
735                root_output.display()
736            );
737        }
738    }
739    search_path
740}
741
742/// Prepares flags and environments we can compute for a `rustc` invocation
743/// before the job queue starts compiling any unit.
744///
745/// This builds a static view of the invocation. Flags depending on the
746/// completion of other units will be added later in runtime, such as flags
747/// from build scripts.
748fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
749    let gctx = build_runner.bcx.gctx;
750    let is_primary = build_runner.is_primary_package(unit);
751    let is_workspace = build_runner.bcx.ws.is_member(&unit.pkg);
752
753    let mut base = build_runner
754        .compilation
755        .rustc_process(unit, is_primary, is_workspace)?;
756    build_base_args(build_runner, &mut base, unit)?;
757    if unit.pkg.manifest().is_embedded() {
758        if !gctx.cli_unstable().script {
759            anyhow::bail!(
760                "parsing `{}` requires `-Zscript`",
761                unit.pkg.manifest_path().display()
762            );
763        }
764        base.arg("-Z").arg("crate-attr=feature(frontmatter)");
765    }
766
767    base.inherit_jobserver(&build_runner.jobserver);
768    build_deps_args(&mut base, build_runner, unit)?;
769    add_cap_lints(build_runner.bcx, unit, &mut base);
770    if let Some(args) = build_runner.bcx.extra_args_for(unit) {
771        base.args(args);
772    }
773    base.args(&unit.rustflags);
774    if gctx.cli_unstable().binary_dep_depinfo {
775        base.arg("-Z").arg("binary-dep-depinfo");
776    }
777    if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
778        base.arg("-Z").arg("checksum-hash-algorithm=blake3");
779    }
780
781    if is_primary {
782        base.env("CARGO_PRIMARY_PACKAGE", "1");
783        let file_list = build_runner.sbom_output_files(unit)?;
784        if !file_list.is_empty() {
785            let file_list = std::env::join_paths(file_list)?;
786            base.env("CARGO_SBOM_PATH", file_list);
787        }
788    }
789
790    if unit.target.is_test() || unit.target.is_bench() {
791        let tmp = build_runner
792            .files()
793            .layout(unit.kind)
794            .build_dir()
795            .prepare_tmp()?;
796        base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());
797    }
798
799    Ok(base)
800}
801
802/// Prepares flags and environments we can compute for a `rustdoc` invocation
803/// before the job queue starts compiling any unit.
804///
805/// This builds a static view of the invocation. Flags depending on the
806/// completion of other units will be added later in runtime, such as flags
807/// from build scripts.
808fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
809    let bcx = build_runner.bcx;
810    // script_metadata is not needed here, it is only for tests.
811    let mut rustdoc = build_runner.compilation.rustdoc_process(unit, None)?;
812    if unit.pkg.manifest().is_embedded() {
813        if !bcx.gctx.cli_unstable().script {
814            anyhow::bail!(
815                "parsing `{}` requires `-Zscript`",
816                unit.pkg.manifest_path().display()
817            );
818        }
819        rustdoc.arg("-Z").arg("crate-attr=feature(frontmatter)");
820    }
821    rustdoc.inherit_jobserver(&build_runner.jobserver);
822    let crate_name = unit.target.crate_name();
823    rustdoc.arg("--crate-name").arg(&crate_name);
824    add_path_args(bcx.ws, unit, &mut rustdoc);
825    add_cap_lints(bcx, unit, &mut rustdoc);
826
827    if let CompileKind::Target(target) = unit.kind {
828        rustdoc.arg("--target").arg(target.rustc_target());
829    }
830    let doc_dir = build_runner.files().out_dir(unit);
831    rustdoc.arg("-o").arg(&doc_dir);
832    rustdoc.args(&features_args(unit));
833    rustdoc.args(&check_cfg_args(unit));
834
835    add_error_format_and_color(build_runner, &mut rustdoc);
836    add_allow_features(build_runner, &mut rustdoc);
837
838    if build_runner.bcx.gctx.cli_unstable().rustdoc_depinfo {
839        // toolchain-shared-resources is required for keeping the shared styling resources
840        // invocation-specific is required for keeping the original rustdoc emission
841        let mut arg = if build_runner.bcx.gctx.cli_unstable().rustdoc_mergeable_info {
842            // toolchain resources are written at the end, at the same time as merging
843            OsString::from("--emit=invocation-specific,dep-info=")
844        } else {
845            // if not using mergeable CCI, everything is written every time
846            OsString::from("--emit=toolchain-shared-resources,invocation-specific,dep-info=")
847        };
848        arg.push(rustdoc_dep_info_loc(build_runner, unit));
849        rustdoc.arg(arg);
850
851        if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
852            rustdoc.arg("-Z").arg("checksum-hash-algorithm=blake3");
853        }
854
855        rustdoc.arg("-Zunstable-options");
856    } else if build_runner.bcx.gctx.cli_unstable().rustdoc_mergeable_info {
857        // toolchain resources are written at the end, at the same time as merging
858        rustdoc.arg("--emit=invocation-specific");
859        rustdoc.arg("-Zunstable-options");
860    }
861
862    if build_runner.bcx.gctx.cli_unstable().rustdoc_mergeable_info {
863        // write out mergeable data to be imported
864        rustdoc.arg("--merge=none");
865        let mut arg = OsString::from("--parts-out-dir=");
866        // `-Zrustdoc-mergeable-info` always uses the new layout.
867        arg.push(build_runner.files().deps_dir_new_layout(unit));
868        rustdoc.arg(arg);
869    }
870
871    if let Some(trim_paths) = unit.profile.trim_paths.as_ref() {
872        trim_paths_args_rustdoc(&mut rustdoc, build_runner, unit, trim_paths)?;
873    }
874
875    rustdoc.args(unit.pkg.manifest().lint_rustflags());
876
877    let metadata = build_runner.metadata_for_doc_units[unit];
878    rustdoc
879        .arg("-C")
880        .arg(format!("metadata={}", metadata.c_metadata()));
881
882    if unit.mode.is_doc_scrape() {
883        debug_assert!(build_runner.bcx.scrape_units.contains(unit));
884
885        if unit.target.is_test() {
886            rustdoc.arg("--scrape-tests");
887        }
888
889        rustdoc.arg("-Zunstable-options");
890
891        rustdoc
892            .arg("--scrape-examples-output-path")
893            .arg(scrape_output_path(build_runner, unit)?);
894
895        // Only scrape example for items from crates in the workspace, to reduce generated file size
896        for pkg in build_runner.bcx.packages.packages() {
897            let names = pkg
898                .targets()
899                .iter()
900                .map(|target| target.crate_name())
901                .collect::<HashSet<_>>();
902            for name in names {
903                rustdoc.arg("--scrape-examples-target-crate").arg(name);
904            }
905        }
906    }
907
908    if should_include_scrape_units(build_runner.bcx, unit) {
909        rustdoc.arg("-Zunstable-options");
910    }
911
912    build_deps_args(&mut rustdoc, build_runner, unit)?;
913    rustdoc::add_root_urls(build_runner, unit, &mut rustdoc)?;
914
915    rustdoc::add_output_format(build_runner, &mut rustdoc)?;
916
917    if let Some(args) = build_runner.bcx.extra_args_for(unit) {
918        rustdoc.args(args);
919    }
920    rustdoc.args(&unit.rustdocflags);
921
922    if !crate_version_flag_already_present(&rustdoc) {
923        append_crate_version_flag(unit, &mut rustdoc);
924    }
925
926    Ok(rustdoc)
927}
928
929/// Creates a unit of work invoking `rustdoc` for documenting the `unit`.
930fn rustdoc(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<Work> {
931    let mut rustdoc = prepare_rustdoc(build_runner, unit)?;
932
933    let crate_name = unit.target.crate_name();
934    let doc_dir = build_runner.files().out_dir(unit);
935    // Create the documentation directory ahead of time as rustdoc currently has
936    // a bug where concurrent invocations will race to create this directory if
937    // it doesn't already exist.
938    paths::create_dir_all(&doc_dir)?;
939
940    let target_desc = unit.target.description_named();
941    let name = unit.pkg.name();
942    let build_script_outputs = Arc::clone(&build_runner.build_script_outputs);
943    let package_id = unit.pkg.package_id();
944    let target = Target::clone(&unit.target);
945    let manifest = ManifestErrorContext::new(build_runner, unit);
946
947    let rustdoc_dep_info_loc = rustdoc_dep_info_loc(build_runner, unit);
948    let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
949    let build_dir = build_runner.bcx.ws.build_dir().into_path_unlocked();
950    let pkg_root = unit.pkg.root().to_path_buf();
951    let cwd = rustdoc
952        .get_cwd()
953        .unwrap_or_else(|| build_runner.bcx.gctx.cwd())
954        .to_path_buf();
955    let fingerprint_dir = build_runner.files().fingerprint_dir(unit);
956    let is_local = unit.is_local();
957    let env_config = Arc::clone(build_runner.bcx.gctx.env_config()?);
958    let rustdoc_depinfo_enabled = build_runner.bcx.gctx.cli_unstable().rustdoc_depinfo;
959
960    let mut output_options = OutputOptions::new(build_runner, unit);
961    let script_metadatas = build_runner.find_build_script_metadatas(unit);
962    let scrape_outputs = if should_include_scrape_units(build_runner.bcx, unit) {
963        Some(
964            build_runner
965                .bcx
966                .scrape_units
967                .iter()
968                .map(|unit| {
969                    Ok((
970                        build_runner.files().metadata(unit).unit_id(),
971                        scrape_output_path(build_runner, unit)?,
972                    ))
973                })
974                .collect::<CargoResult<HashMap<_, _>>>()?,
975        )
976    } else {
977        None
978    };
979
980    let failed_scrape_units = Arc::clone(&build_runner.failed_scrape_units);
981    let hide_diagnostics_for_scrape_unit = build_runner.bcx.unit_can_fail_for_docscraping(unit)
982        && !matches!(
983            build_runner.bcx.gctx.shell().verbosity(),
984            Verbosity::Verbose
985        );
986    let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
987        make_failed_scrape_diagnostic(
988            build_runner,
989            unit,
990            format_args!("failed to scan {target_desc} in package `{name}` for example code usage"),
991        )
992    });
993    if hide_diagnostics_for_scrape_unit {
994        output_options.show_diagnostics = false;
995    }
996
997    Ok(Work::new(move |state| {
998        add_custom_flags(
999            &mut rustdoc,
1000            &build_script_outputs.lock().unwrap(),
1001            script_metadatas,
1002        )?;
1003
1004        // Add the output of scraped examples to the rustdoc command.
1005        // This action must happen after the unit's dependencies have finished,
1006        // because some of those deps may be Docscrape units which have failed.
1007        // So we dynamically determine which `--with-examples` flags to pass here.
1008        if let Some(scrape_outputs) = scrape_outputs {
1009            let failed_scrape_units = failed_scrape_units.lock().unwrap();
1010            for (metadata, output_path) in &scrape_outputs {
1011                if !failed_scrape_units.contains(metadata) {
1012                    rustdoc.arg("--with-examples").arg(output_path);
1013                }
1014            }
1015        }
1016
1017        let crate_dir = doc_dir.join(&crate_name);
1018        if crate_dir.exists() {
1019            // Remove output from a previous build. This ensures that stale
1020            // files for removed items are removed.
1021            debug!("removing pre-existing doc directory {:?}", crate_dir);
1022            paths::remove_dir_all(crate_dir)?;
1023        }
1024        state.running(&rustdoc);
1025        let timestamp = paths::set_invocation_time(&fingerprint_dir)?;
1026
1027        let result = rustdoc
1028            .exec_with_streaming(
1029                &mut |line| on_stdout_line(state, line, package_id, &target),
1030                &mut |line| {
1031                    on_stderr_line(
1032                        state,
1033                        line,
1034                        package_id,
1035                        &manifest,
1036                        &target,
1037                        &mut output_options,
1038                    )
1039                },
1040                false,
1041            )
1042            .map_err(verbose_if_simple_exit_code)
1043            .with_context(|| format!("could not document `{}`", name));
1044
1045        if let Err(e) = result {
1046            if let Some(diagnostic) = failed_scrape_diagnostic {
1047                state.warning(diagnostic);
1048            }
1049
1050            return Err(e);
1051        }
1052
1053        if rustdoc_depinfo_enabled && rustdoc_dep_info_loc.exists() {
1054            fingerprint::translate_dep_info(
1055                &rustdoc_dep_info_loc,
1056                &dep_info_loc,
1057                &cwd,
1058                &pkg_root,
1059                &build_dir,
1060                &rustdoc,
1061                // Should we track source file for doc gen?
1062                is_local,
1063                &env_config,
1064            )
1065            .with_context(|| {
1066                internal(format_args!(
1067                    "could not parse/generate dep info at: {}",
1068                    rustdoc_dep_info_loc.display()
1069                ))
1070            })?;
1071            // This mtime shift allows Cargo to detect if a source file was
1072            // modified in the middle of the build.
1073            paths::set_file_time_no_err(dep_info_loc, timestamp);
1074        }
1075
1076        Ok(())
1077    }))
1078}
1079
1080// The --crate-version flag could have already been passed in RUSTDOCFLAGS
1081// or as an extra compiler argument for rustdoc
1082fn crate_version_flag_already_present(rustdoc: &ProcessBuilder) -> bool {
1083    rustdoc.get_args().any(|flag| {
1084        flag.to_str()
1085            .map_or(false, |flag| flag.starts_with(RUSTDOC_CRATE_VERSION_FLAG))
1086    })
1087}
1088
1089fn append_crate_version_flag(unit: &Unit, rustdoc: &mut ProcessBuilder) {
1090    rustdoc
1091        .arg(RUSTDOC_CRATE_VERSION_FLAG)
1092        .arg(unit.pkg.version().to_string());
1093}
1094
1095/// Adds [`--cap-lints`] to the command to execute.
1096///
1097/// [`--cap-lints`]: https://doc.rust-lang.org/nightly/rustc/lints/levels.html#capping-lints
1098fn add_cap_lints(bcx: &BuildContext<'_, '_>, unit: &Unit, cmd: &mut ProcessBuilder) {
1099    // If this is an upstream dep we don't want warnings from, turn off all
1100    // lints.
1101    if !unit.show_warnings(bcx.gctx) {
1102        cmd.arg("--cap-lints").arg("allow");
1103
1104    // If this is an upstream dep but we *do* want warnings, make sure that they
1105    // don't fail compilation.
1106    } else if !unit.is_local() {
1107        cmd.arg("--cap-lints").arg("warn");
1108    }
1109}
1110
1111/// Forwards [`-Zallow-features`] if it is set for cargo.
1112///
1113/// [`-Zallow-features`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#allow-features
1114fn add_allow_features(build_runner: &BuildRunner<'_, '_>, cmd: &mut ProcessBuilder) {
1115    if let Some(allow) = &build_runner.bcx.gctx.cli_unstable().allow_features {
1116        use std::fmt::Write;
1117        let mut arg = String::from("-Zallow-features=");
1118        for f in allow {
1119            let _ = write!(&mut arg, "{f},");
1120        }
1121        cmd.arg(arg.trim_end_matches(','));
1122    }
1123}
1124
1125/// Adds [`--error-format`] to the command to execute.
1126///
1127/// Cargo always uses JSON output. This has several benefits, such as being
1128/// easier to parse, handles changing formats (for replaying cached messages),
1129/// ensures atomic output (so messages aren't interleaved), allows for
1130/// intercepting messages like rmeta artifacts, etc. rustc includes a
1131/// "rendered" field in the JSON message with the message properly formatted,
1132/// which Cargo will extract and display to the user.
1133///
1134/// [`--error-format`]: https://doc.rust-lang.org/nightly/rustc/command-line-arguments.html#--error-format-control-how-errors-are-produced
1135fn add_error_format_and_color(build_runner: &BuildRunner<'_, '_>, cmd: &mut ProcessBuilder) {
1136    let enable_timings = build_runner.bcx.gctx.cli_unstable().section_timings
1137        && (!build_runner.bcx.build_config.timing_outputs.is_empty()
1138            || build_runner.bcx.logger.is_some());
1139    if enable_timings {
1140        cmd.arg("-Zunstable-options");
1141    }
1142
1143    cmd.arg("--error-format=json");
1144    let mut json = String::from("--json=diagnostic-rendered-ansi,artifacts,future-incompat");
1145
1146    if let MessageFormat::Short | MessageFormat::Json { short: true, .. } =
1147        build_runner.bcx.build_config.message_format
1148    {
1149        json.push_str(",diagnostic-short");
1150    } else if build_runner.bcx.gctx.shell().err_unicode()
1151        && build_runner.bcx.gctx.cli_unstable().rustc_unicode
1152    {
1153        json.push_str(",diagnostic-unicode");
1154    }
1155
1156    if enable_timings {
1157        json.push_str(",timings");
1158    }
1159
1160    cmd.arg(json);
1161
1162    let gctx = build_runner.bcx.gctx;
1163    if let Some(width) = gctx.shell().err_width().diagnostic_terminal_width() {
1164        cmd.arg(format!("--diagnostic-width={width}"));
1165    }
1166}
1167
1168/// Adds essential rustc flags and environment variables to the command to execute.
1169fn build_base_args(
1170    build_runner: &BuildRunner<'_, '_>,
1171    cmd: &mut ProcessBuilder,
1172    unit: &Unit,
1173) -> CargoResult<()> {
1174    assert!(!unit.mode.is_run_custom_build());
1175
1176    let bcx = build_runner.bcx;
1177    let Profile {
1178        ref opt_level,
1179        codegen_backend,
1180        codegen_units,
1181        debuginfo,
1182        debug_assertions,
1183        split_debuginfo,
1184        overflow_checks,
1185        rpath,
1186        ref panic,
1187        incremental,
1188        strip,
1189        rustflags: profile_rustflags,
1190        trim_paths,
1191        hint_mostly_unused: profile_hint_mostly_unused,
1192        ..
1193    } = unit.profile.clone();
1194    let hints = unit.pkg.hints().cloned().unwrap_or_default();
1195    let test = unit.mode.is_any_test();
1196
1197    let warn = |msg: &str| {
1198        bcx.gctx.shell().warn(format!(
1199            "{}@{}: {msg}",
1200            unit.pkg.package_id().name(),
1201            unit.pkg.package_id().version()
1202        ))
1203    };
1204    let unit_capped_warn = |msg: &str| {
1205        if unit.show_warnings(bcx.gctx) {
1206            warn(msg)
1207        } else {
1208            Ok(())
1209        }
1210    };
1211
1212    cmd.arg("--crate-name").arg(&unit.target.crate_name());
1213
1214    let edition = unit.target.edition();
1215    edition.cmd_edition_arg(cmd);
1216
1217    add_path_args(bcx.ws, unit, cmd);
1218    add_error_format_and_color(build_runner, cmd);
1219    add_allow_features(build_runner, cmd);
1220
1221    let mut contains_dy_lib = false;
1222    if !test {
1223        for crate_type in &unit.target.rustc_crate_types() {
1224            cmd.arg("--crate-type").arg(crate_type.as_str());
1225            contains_dy_lib |= crate_type == &CrateType::Dylib;
1226        }
1227    }
1228
1229    if unit.mode.is_check() {
1230        cmd.arg("--emit=dep-info,metadata");
1231    } else if build_runner.bcx.gctx.cli_unstable().no_embed_metadata {
1232        // Nightly rustc supports the -Zembed-metadata=no flag, which tells it to avoid including
1233        // full metadata in rlib/dylib artifacts, to save space on disk. In this case, metadata
1234        // will only be stored in .rmeta files.
1235        // When we use this flag, we should also pass --emit=metadata to all artifacts that
1236        // contain useful metadata (rlib/dylib/proc macros), so that a .rmeta file is actually
1237        // generated. If we didn't do this, the full metadata would not get written anywhere.
1238        // However, we do not want to pass --emit=metadata to artifacts that never produce useful
1239        // metadata, such as binaries, because that would just unnecessarily create empty .rmeta
1240        // files on disk.
1241        if unit.benefits_from_no_embed_metadata() {
1242            cmd.arg("--emit=dep-info,metadata,link");
1243            cmd.args(&["-Z", "embed-metadata=no"]);
1244        } else {
1245            cmd.arg("--emit=dep-info,link");
1246        }
1247    } else {
1248        // If we don't use -Zembed-metadata=no, we emit .rmeta files only for rlib outputs.
1249        // This metadata may be used in this session for a pipelined compilation, or it may
1250        // be used in a future Cargo session as part of a pipelined compile.
1251        if !unit.requires_upstream_objects() {
1252            cmd.arg("--emit=dep-info,metadata,link");
1253        } else {
1254            cmd.arg("--emit=dep-info,link");
1255        }
1256    }
1257
1258    let prefer_dynamic = (unit.target.for_host() && !unit.target.is_custom_build())
1259        || (contains_dy_lib && !build_runner.is_primary_package(unit));
1260    if prefer_dynamic {
1261        cmd.arg("-C").arg("prefer-dynamic");
1262    }
1263
1264    if opt_level.as_str() != "0" {
1265        cmd.arg("-C").arg(&format!("opt-level={}", opt_level));
1266    }
1267
1268    if *panic != PanicStrategy::Unwind {
1269        cmd.arg("-C").arg(format!("panic={}", panic));
1270    }
1271    if *panic == PanicStrategy::ImmediateAbort {
1272        cmd.arg("-Z").arg("unstable-options");
1273    }
1274
1275    cmd.args(&lto_args(build_runner, unit));
1276
1277    if let Some(backend) = codegen_backend {
1278        cmd.arg("-Z").arg(&format!("codegen-backend={}", backend));
1279    }
1280
1281    if let Some(n) = codegen_units {
1282        cmd.arg("-C").arg(&format!("codegen-units={}", n));
1283    }
1284
1285    let debuginfo = debuginfo.into_inner();
1286    // Shorten the number of arguments if possible.
1287    if debuginfo != TomlDebugInfo::None {
1288        cmd.arg("-C").arg(format!("debuginfo={debuginfo}"));
1289        // This is generally just an optimization on build time so if we don't
1290        // pass it then it's ok. The values for the flag (off, packed, unpacked)
1291        // may be supported or not depending on the platform, so availability is
1292        // checked per-value. For example, at the time of writing this code, on
1293        // Windows the only stable valid value for split-debuginfo is "packed",
1294        // while on Linux "unpacked" is also stable.
1295        if let Some(split) = split_debuginfo {
1296            if build_runner
1297                .bcx
1298                .target_data
1299                .info(unit.kind)
1300                .supports_debuginfo_split(split)
1301            {
1302                cmd.arg("-C").arg(format!("split-debuginfo={split}"));
1303            }
1304        }
1305    }
1306
1307    if let Some(trim_paths) = trim_paths {
1308        trim_paths_args(cmd, build_runner, unit, &trim_paths)?;
1309    }
1310
1311    cmd.args(unit.pkg.manifest().lint_rustflags());
1312    cmd.args(&profile_rustflags);
1313
1314    // `-C overflow-checks` is implied by the setting of `-C debug-assertions`,
1315    // so we only need to provide `-C overflow-checks` if it differs from
1316    // the value of `-C debug-assertions` we would provide.
1317    if opt_level.as_str() != "0" {
1318        if debug_assertions {
1319            cmd.args(&["-C", "debug-assertions=on"]);
1320            if !overflow_checks {
1321                cmd.args(&["-C", "overflow-checks=off"]);
1322            }
1323        } else if overflow_checks {
1324            cmd.args(&["-C", "overflow-checks=on"]);
1325        }
1326    } else if !debug_assertions {
1327        cmd.args(&["-C", "debug-assertions=off"]);
1328        if overflow_checks {
1329            cmd.args(&["-C", "overflow-checks=on"]);
1330        }
1331    } else if !overflow_checks {
1332        cmd.args(&["-C", "overflow-checks=off"]);
1333    }
1334
1335    if test && unit.target.harness() {
1336        cmd.arg("--test");
1337
1338        // Cargo has historically never compiled `--test` binaries with
1339        // `panic=abort` because the `test` crate itself didn't support it.
1340        // Support is now upstream, however, but requires an unstable flag to be
1341        // passed when compiling the test. We require, in Cargo, an unstable
1342        // flag to pass to rustc, so register that here. Eventually this flag
1343        // will simply not be needed when the behavior is stabilized in the Rust
1344        // compiler itself.
1345        if *panic == PanicStrategy::Abort || *panic == PanicStrategy::ImmediateAbort {
1346            cmd.arg("-Z").arg("panic-abort-tests");
1347        }
1348    } else if test {
1349        cmd.arg("--cfg").arg("test");
1350    }
1351
1352    cmd.args(&features_args(unit));
1353    cmd.args(&check_cfg_args(unit));
1354
1355    let meta = build_runner.files().metadata(unit);
1356    cmd.arg("-C")
1357        .arg(&format!("metadata={}", meta.c_metadata()));
1358    if let Some(c_extra_filename) = meta.c_extra_filename() {
1359        cmd.arg("-C")
1360            .arg(&format!("extra-filename=-{c_extra_filename}"));
1361    }
1362
1363    if rpath {
1364        cmd.arg("-C").arg("rpath");
1365    }
1366
1367    cmd.arg("--out-dir")
1368        .arg(&build_runner.files().out_dir(unit));
1369
1370    fn opt(cmd: &mut ProcessBuilder, key: &str, prefix: &str, val: Option<&OsStr>) {
1371        if let Some(val) = val {
1372            let mut joined = OsString::from(prefix);
1373            joined.push(val);
1374            cmd.arg(key).arg(joined);
1375        }
1376    }
1377
1378    if let CompileKind::Target(n) = unit.kind {
1379        cmd.arg("--target").arg(n.rustc_target());
1380    }
1381
1382    opt(
1383        cmd,
1384        "-C",
1385        "linker=",
1386        build_runner
1387            .compilation
1388            .target_linker(unit.kind)
1389            .as_ref()
1390            .map(|s| s.as_ref()),
1391    );
1392    if incremental {
1393        let dir = build_runner.files().incremental_dir(&unit);
1394        opt(cmd, "-C", "incremental=", Some(dir.as_os_str()));
1395    }
1396
1397    let pkg_hint_mostly_unused = match hints.mostly_unused {
1398        None => None,
1399        Some(toml::Value::Boolean(b)) => Some(b),
1400        Some(v) => {
1401            unit_capped_warn(&format!(
1402                "ignoring unsupported value type ({}) for 'hints.mostly-unused', which expects a boolean",
1403                v.type_str()
1404            ))?;
1405            None
1406        }
1407    };
1408    if profile_hint_mostly_unused
1409        .or(pkg_hint_mostly_unused)
1410        .unwrap_or(false)
1411    {
1412        if bcx.gctx.cli_unstable().profile_hint_mostly_unused {
1413            cmd.arg("-Zhint-mostly-unused");
1414        } else {
1415            if profile_hint_mostly_unused.is_some() {
1416                // Profiles come from the top-level unit, so we don't use `unit_capped_warn` here.
1417                warn(
1418                    "ignoring 'hint-mostly-unused' profile option, pass `-Zprofile-hint-mostly-unused` to enable it",
1419                )?;
1420            } else if pkg_hint_mostly_unused.is_some() {
1421                unit_capped_warn(
1422                    "ignoring 'hints.mostly-unused', pass `-Zprofile-hint-mostly-unused` to enable it",
1423                )?;
1424            }
1425        }
1426    }
1427
1428    let strip = strip.into_inner();
1429    if strip != StripInner::None {
1430        cmd.arg("-C").arg(format!("strip={}", strip));
1431    }
1432
1433    if unit.is_std {
1434        // -Zforce-unstable-if-unmarked prevents the accidental use of
1435        // unstable crates within the sysroot (such as "extern crate libc" or
1436        // any non-public crate in the sysroot).
1437        //
1438        // RUSTC_BOOTSTRAP allows unstable features on stable.
1439        cmd.arg("-Z")
1440            .arg("force-unstable-if-unmarked")
1441            .env("RUSTC_BOOTSTRAP", "1");
1442    }
1443
1444    // Add `CARGO_BIN_EXE_` environment variables for building tests.
1445    if unit.target.is_test() || unit.target.is_bench() {
1446        for bin_target in unit
1447            .pkg
1448            .manifest()
1449            .targets()
1450            .iter()
1451            .filter(|target| target.is_bin())
1452        {
1453            // For `cargo check` builds we do not uplift the CARGO_BIN_EXE_ artifacts to the
1454            // artifact-dir. We do not want to provide a path to a non-existent binary but we still
1455            // need to provide *something* so `env!("CARGO_BIN_EXE_...")` macros will compile.
1456            let exe_path = build_runner
1457                .files()
1458                .bin_link_for_target(bin_target, unit.kind, build_runner.bcx)?
1459                .map(|path| path.as_os_str().to_os_string())
1460                .unwrap_or_else(|| OsString::from(format!("placeholder:{}", bin_target.name())));
1461
1462            let name = bin_target
1463                .binary_filename()
1464                .unwrap_or(bin_target.name().to_string());
1465            let key = format!("CARGO_BIN_EXE_{}", name);
1466            cmd.env(&key, exe_path);
1467        }
1468    }
1469    Ok(())
1470}
1471
1472/// All active features for the unit passed as `--cfg features=<feature-name>`.
1473fn features_args(unit: &Unit) -> Vec<OsString> {
1474    let mut args = Vec::with_capacity(unit.features.len() * 2);
1475
1476    for feat in &unit.features {
1477        args.push(OsString::from("--cfg"));
1478        args.push(OsString::from(format!("feature=\"{}\"", feat)));
1479    }
1480
1481    args
1482}
1483
1484/// Like [`trim_paths_args`] but for rustdoc invocations.
1485fn trim_paths_args_rustdoc(
1486    cmd: &mut ProcessBuilder,
1487    build_runner: &BuildRunner<'_, '_>,
1488    unit: &Unit,
1489    trim_paths: &TomlTrimPaths,
1490) -> CargoResult<()> {
1491    match trim_paths {
1492        // rustdoc supports diagnostics trimming only.
1493        TomlTrimPaths::Values(values) if !values.contains(&TomlTrimPathsValue::Diagnostics) => {
1494            return Ok(());
1495        }
1496        _ => {}
1497    }
1498
1499    // feature gate was checked during manifest/config parsing.
1500    cmd.arg("-Zunstable-options");
1501
1502    // Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
1503    // We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
1504    cmd.arg(package_remap(build_runner, unit));
1505    cmd.arg(build_dir_remap(build_runner));
1506    cmd.arg(sysroot_remap(build_runner, unit));
1507
1508    Ok(())
1509}
1510
1511/// Generates the `--remap-path-scope` and `--remap-path-prefix` for [RFC 3127].
1512/// See also unstable feature [`-Ztrim-paths`].
1513///
1514/// [RFC 3127]: https://rust-lang.github.io/rfcs/3127-trim-paths.html
1515/// [`-Ztrim-paths`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-trim-paths-option
1516fn trim_paths_args(
1517    cmd: &mut ProcessBuilder,
1518    build_runner: &BuildRunner<'_, '_>,
1519    unit: &Unit,
1520    trim_paths: &TomlTrimPaths,
1521) -> CargoResult<()> {
1522    if trim_paths.is_none() {
1523        return Ok(());
1524    }
1525
1526    // feature gate was checked during manifest/config parsing.
1527    cmd.arg("-Zunstable-options");
1528    cmd.arg(format!("-Zremap-path-scope={trim_paths}"));
1529
1530    // Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
1531    // We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
1532    cmd.arg(package_remap(build_runner, unit));
1533    cmd.arg(build_dir_remap(build_runner));
1534    cmd.arg(sysroot_remap(build_runner, unit));
1535
1536    Ok(())
1537}
1538
1539/// Path prefix remap rules for sysroot.
1540///
1541/// This remap logic aligns with rustc:
1542/// <https://github.com/rust-lang/rust/blob/c2ef3516/src/bootstrap/src/lib.rs#L1113-L1116>
1543fn sysroot_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1544    let mut remap = OsString::from("--remap-path-prefix=");
1545    remap.push({
1546        // See also `detect_sysroot_src_path()`.
1547        let mut sysroot = build_runner.bcx.target_data.info(unit.kind).sysroot.clone();
1548        sysroot.push("lib");
1549        sysroot.push("rustlib");
1550        sysroot.push("src");
1551        sysroot.push("rust");
1552        sysroot
1553    });
1554    remap.push("=");
1555    remap.push("/rustc/");
1556    if let Some(commit_hash) = build_runner.bcx.rustc().commit_hash.as_ref() {
1557        remap.push(commit_hash);
1558    } else {
1559        remap.push(build_runner.bcx.rustc().version.to_string());
1560    }
1561    remap
1562}
1563
1564/// Path prefix remap rules for dependencies.
1565///
1566/// * Git dependencies: remove `~/.cargo/git/checkouts` prefix.
1567/// * Registry dependencies: remove `~/.cargo/registry/src` prefix.
1568/// * Others (e.g. path dependencies):
1569///     * relative paths to workspace root if inside the workspace directory.
1570///     * otherwise remapped to `<pkg>-<version>`.
1571fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1572    let pkg_root = unit.pkg.root();
1573    let ws_root = build_runner.bcx.ws.root();
1574    let mut remap = OsString::from("--remap-path-prefix=");
1575    let source_id = unit.pkg.package_id().source_id();
1576    if source_id.is_git() {
1577        remap.push(
1578            build_runner
1579                .bcx
1580                .gctx
1581                .git_checkouts_path()
1582                .as_path_unlocked(),
1583        );
1584        remap.push("=");
1585    } else if source_id.is_registry() {
1586        remap.push(
1587            build_runner
1588                .bcx
1589                .gctx
1590                .registry_source_path()
1591                .as_path_unlocked(),
1592        );
1593        remap.push("=");
1594    } else if pkg_root.strip_prefix(ws_root).is_ok() {
1595        remap.push(ws_root);
1596        remap.push("=."); // remap to relative rustc work dir explicitly
1597    } else {
1598        remap.push(pkg_root);
1599        remap.push("=");
1600        remap.push(unit.pkg.name());
1601        remap.push("-");
1602        remap.push(unit.pkg.version().to_string());
1603    }
1604    remap
1605}
1606
1607/// Remap all paths pointing to `build.build-dir`,
1608/// i.e., `[BUILD_DIR]/debug/deps/foo-[HASH].dwo` would be remapped to
1609/// `/cargo/build-dir/debug/deps/foo-[HASH].dwo`
1610/// (note the `/cargo/build-dir` prefix).
1611///
1612/// This covers scenarios like:
1613///
1614/// * Build script generated code. For example, a build script may call `file!`
1615///   macros, and the associated crate uses [`include!`] to include the expanded
1616///   [`file!`] macro in-place via the `OUT_DIR` environment.
1617/// * On Linux, `DW_AT_GNU_dwo_name` that contains paths to split debuginfo
1618///   files (dwp and dwo).
1619fn build_dir_remap(build_runner: &BuildRunner<'_, '_>) -> OsString {
1620    let build_dir = build_runner.bcx.ws.build_dir();
1621    let mut remap = OsString::from("--remap-path-prefix=");
1622    remap.push(build_dir.as_path_unlocked());
1623    remap.push("=/cargo/build-dir");
1624    remap
1625}
1626
1627/// Generates the `--check-cfg` arguments for the `unit`.
1628fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
1629    // The routine below generates the --check-cfg arguments. Our goals here are to
1630    // enable the checking of conditionals and pass the list of declared features.
1631    //
1632    // In the simplified case, it would resemble something like this:
1633    //
1634    //   --check-cfg=cfg() --check-cfg=cfg(feature, values(...))
1635    //
1636    // but having `cfg()` is redundant with the second argument (as well-known names
1637    // and values are implicitly enabled when one or more `--check-cfg` argument is
1638    // passed) so we don't emit it and just pass:
1639    //
1640    //   --check-cfg=cfg(feature, values(...))
1641    //
1642    // This way, even if there are no declared features, the config `feature` will
1643    // still be expected, meaning users would get "unexpected value" instead of name.
1644    // This wasn't always the case, see rust-lang#119930 for some details.
1645
1646    let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1647    let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
1648
1649    arg_feature.push("cfg(feature, values(");
1650    for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1651        if i != 0 {
1652            arg_feature.push(", ");
1653        }
1654        arg_feature.push("\"");
1655        arg_feature.push(feature);
1656        arg_feature.push("\"");
1657    }
1658    arg_feature.push("))");
1659
1660    // In addition to the package features, we also include the `test` cfg (since
1661    // compiler-team#785, as to be able to someday apply it conditionally), as well
1662    // the `docsrs` cfg from the docs.rs service.
1663    //
1664    // We include `docsrs` here (in Cargo) instead of rustc, since there is a much closer
1665    // relationship between Cargo and docs.rs than rustc and docs.rs. In particular, all
1666    // users of docs.rs use Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
1667
1668    vec![
1669        OsString::from("--check-cfg"),
1670        OsString::from("cfg(docsrs,test)"),
1671        OsString::from("--check-cfg"),
1672        arg_feature,
1673    ]
1674}
1675
1676/// Adds LTO related codegen flags.
1677fn lto_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsString> {
1678    let mut result = Vec::new();
1679    let mut push = |arg: &str| {
1680        result.push(OsString::from("-C"));
1681        result.push(OsString::from(arg));
1682    };
1683    match build_runner.lto[unit] {
1684        lto::Lto::Run(None) => push("lto"),
1685        lto::Lto::Run(Some(s)) => push(&format!("lto={}", s)),
1686        lto::Lto::Off => {
1687            push("lto=off");
1688            push("embed-bitcode=no");
1689        }
1690        lto::Lto::ObjectAndBitcode => {} // this is rustc's default
1691        lto::Lto::OnlyBitcode => push("linker-plugin-lto"),
1692        lto::Lto::OnlyObject => push("embed-bitcode=no"),
1693    }
1694    result
1695}
1696
1697/// Adds dependency-relevant rustc flags and environment variables
1698/// to the command to execute, such as [`-L`] and [`--extern`].
1699///
1700/// [`-L`]: https://doc.rust-lang.org/nightly/rustc/command-line-arguments.html#-l-add-a-directory-to-the-library-search-path
1701/// [`--extern`]: https://doc.rust-lang.org/nightly/rustc/command-line-arguments.html#--extern-specify-where-an-external-library-is-located
1702fn build_deps_args(
1703    cmd: &mut ProcessBuilder,
1704    build_runner: &BuildRunner<'_, '_>,
1705    unit: &Unit,
1706) -> CargoResult<()> {
1707    let bcx = build_runner.bcx;
1708
1709    for arg in lib_search_paths(build_runner, unit)? {
1710        cmd.arg(arg);
1711    }
1712
1713    let deps = build_runner.unit_deps(unit);
1714
1715    // If there is not one linkable target but should, rustc fails later
1716    // on if there is an `extern crate` for it. This may turn into a hard
1717    // error in the future (see PR #4797).
1718    if !deps
1719        .iter()
1720        .any(|dep| !dep.unit.mode.is_doc() && dep.unit.target.is_linkable())
1721    {
1722        if let Some(dep) = deps.iter().find(|dep| {
1723            !dep.unit.mode.is_doc() && dep.unit.target.is_lib() && !dep.unit.artifact.is_true()
1724        }) {
1725            let dep_name = dep.unit.target.crate_name();
1726            let name = unit.target.crate_name();
1727            bcx.gctx.shell().print_report(&[
1728                Level::WARNING.secondary_title(format!("the package `{dep_name}` provides no linkable target"))
1729                    .elements([
1730                        Level::NOTE.message(format!("this might cause `{name}` to fail compilation")),
1731                        Level::NOTE.message("this warning might turn into a hard error in the future"),
1732                        Level::HELP.message(format!("consider adding 'dylib' or 'rlib' to key 'crate-type' in `{dep_name}`'s Cargo.toml"))
1733                    ])
1734            ], false)?;
1735        }
1736    }
1737
1738    let mut unstable_opts = false;
1739
1740    // Add `OUT_DIR` environment variables for build scripts
1741    let first_custom_build_dep = deps.iter().find(|dep| dep.unit.mode.is_run_custom_build());
1742    if let Some(dep) = first_custom_build_dep {
1743        let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
1744        cmd.env("OUT_DIR", &out_dir);
1745    }
1746
1747    // Adding output directory for each build script
1748    let is_multiple_build_scripts_enabled = unit
1749        .pkg
1750        .manifest()
1751        .unstable_features()
1752        .require(Feature::multiple_build_scripts())
1753        .is_ok();
1754
1755    if is_multiple_build_scripts_enabled {
1756        for dep in deps {
1757            if dep.unit.mode.is_run_custom_build() {
1758                let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
1759                let target_name = dep.unit.target.name();
1760                let out_dir_prefix = target_name
1761                    .strip_prefix("build-script-")
1762                    .unwrap_or(target_name);
1763                let out_dir_name = format!("{out_dir_prefix}_OUT_DIR");
1764                cmd.env(&out_dir_name, &out_dir);
1765            }
1766        }
1767    }
1768    for arg in extern_args(build_runner, unit, &mut unstable_opts)? {
1769        cmd.arg(arg);
1770    }
1771
1772    for (var, env) in artifact::get_env(build_runner, deps)? {
1773        cmd.env(&var, env);
1774    }
1775
1776    // This will only be set if we're already using a feature
1777    // requiring nightly rust
1778    if unstable_opts {
1779        cmd.arg("-Z").arg("unstable-options");
1780    }
1781
1782    Ok(())
1783}
1784
1785fn add_dep_arg<'a, 'b: 'a>(
1786    map: &mut BTreeMap<&'a Unit, PathBuf>,
1787    build_runner: &'b BuildRunner<'b, '_>,
1788    unit: &'a Unit,
1789) {
1790    if map.contains_key(&unit) {
1791        return;
1792    }
1793    map.insert(&unit, build_runner.files().deps_dir(&unit));
1794
1795    for dep in build_runner.unit_deps(unit) {
1796        add_dep_arg(map, build_runner, &dep.unit);
1797    }
1798}
1799
1800/// Adds extra rustc flags and environment variables collected from the output
1801/// of a build-script to the command to execute, include custom environment
1802/// variables and `cfg`.
1803fn add_custom_flags(
1804    cmd: &mut ProcessBuilder,
1805    build_script_outputs: &BuildScriptOutputs,
1806    metadata_vec: Option<Vec<UnitHash>>,
1807) -> CargoResult<()> {
1808    if let Some(metadata_vec) = metadata_vec {
1809        for metadata in metadata_vec {
1810            if let Some(output) = build_script_outputs.get(metadata) {
1811                for cfg in output.cfgs.iter() {
1812                    cmd.arg("--cfg").arg(cfg);
1813                }
1814                for check_cfg in &output.check_cfgs {
1815                    cmd.arg("--check-cfg").arg(check_cfg);
1816                }
1817                for (name, value) in output.env.iter() {
1818                    cmd.env(name, value);
1819                }
1820            }
1821        }
1822    }
1823
1824    Ok(())
1825}
1826
1827/// Generate a list of `-L` arguments
1828pub fn lib_search_paths(
1829    build_runner: &BuildRunner<'_, '_>,
1830    unit: &Unit,
1831) -> CargoResult<Vec<OsString>> {
1832    let mut lib_search_paths = Vec::new();
1833    if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout {
1834        let mut map = BTreeMap::new();
1835
1836        // Recursively add all dependency args to rustc process
1837        add_dep_arg(&mut map, build_runner, unit);
1838
1839        let paths = map.into_iter().map(|(_, path)| path).sorted_unstable();
1840
1841        for path in paths {
1842            let mut deps = OsString::from("dependency=");
1843            deps.push(path);
1844            lib_search_paths.extend(["-L".into(), deps]);
1845        }
1846    } else {
1847        let mut deps = OsString::from("dependency=");
1848        deps.push(build_runner.files().deps_dir(unit));
1849        lib_search_paths.extend(["-L".into(), deps]);
1850    }
1851
1852    // Be sure that the host path is also listed. This'll ensure that proc macro
1853    // dependencies are correctly found (for reexported macros).
1854    if !unit.kind.is_host() {
1855        let mut deps = OsString::from("dependency=");
1856        deps.push(build_runner.files().host_deps(unit));
1857        lib_search_paths.extend(["-L".into(), deps]);
1858    }
1859
1860    Ok(lib_search_paths)
1861}
1862
1863/// Generates a list of `--extern` arguments.
1864pub fn extern_args(
1865    build_runner: &BuildRunner<'_, '_>,
1866    unit: &Unit,
1867    unstable_opts: &mut bool,
1868) -> CargoResult<Vec<OsString>> {
1869    let mut result = Vec::new();
1870    let deps = build_runner.unit_deps(unit);
1871
1872    let no_embed_metadata = build_runner.bcx.gctx.cli_unstable().no_embed_metadata;
1873
1874    // Closure to add one dependency to `result`.
1875    let mut link_to =
1876        |dep: &UnitDep, extern_crate_name: InternedString, noprelude: bool| -> CargoResult<()> {
1877            let mut value = OsString::new();
1878            let mut opts = Vec::new();
1879            let is_public_dependency_enabled = unit
1880                .pkg
1881                .manifest()
1882                .unstable_features()
1883                .require(Feature::public_dependency())
1884                .is_ok()
1885                || build_runner.bcx.gctx.cli_unstable().public_dependency;
1886            if !dep.public && unit.target.is_lib() && is_public_dependency_enabled {
1887                opts.push("priv");
1888                *unstable_opts = true;
1889            }
1890            if noprelude {
1891                opts.push("noprelude");
1892                *unstable_opts = true;
1893            }
1894            if !opts.is_empty() {
1895                value.push(opts.join(","));
1896                value.push(":");
1897            }
1898            value.push(extern_crate_name.as_str());
1899            value.push("=");
1900
1901            let mut pass = |file| {
1902                let mut value = value.clone();
1903                value.push(file);
1904                result.push(OsString::from("--extern"));
1905                result.push(value);
1906            };
1907
1908            let outputs = build_runner.outputs(&dep.unit)?;
1909
1910            if build_runner.only_requires_rmeta(unit, &dep.unit) || dep.unit.mode.is_check() {
1911                // Example: rlib dependency for an rlib, rmeta is all that is required.
1912                let output = outputs
1913                    .iter()
1914                    .find(|output| output.flavor == FileFlavor::Rmeta)
1915                    .expect("failed to find rmeta dep for pipelined dep");
1916                pass(&output.path);
1917            } else {
1918                // Example: a bin needs `rlib` for dependencies, it cannot use rmeta.
1919                for output in outputs.iter() {
1920                    if output.flavor == FileFlavor::Linkable {
1921                        pass(&output.path);
1922                    }
1923                    // If we use -Zembed-metadata=no, we also need to pass the path to the
1924                    // corresponding .rmeta file to the linkable artifact, because the
1925                    // normal dependency (rlib) doesn't contain the full metadata.
1926                    else if no_embed_metadata && output.flavor == FileFlavor::Rmeta {
1927                        pass(&output.path);
1928                    }
1929                }
1930            }
1931            Ok(())
1932        };
1933
1934    for dep in deps {
1935        if dep.unit.target.is_linkable() && !dep.unit.mode.is_doc() {
1936            link_to(dep, dep.extern_crate_name, dep.noprelude)?;
1937        }
1938    }
1939    if unit.target.proc_macro() {
1940        // Automatically import `proc_macro`.
1941        result.push(OsString::from("--extern"));
1942        result.push(OsString::from("proc_macro"));
1943    }
1944
1945    Ok(result)
1946}
1947
1948fn envify(s: &str) -> String {
1949    s.chars()
1950        .flat_map(|c| c.to_uppercase())
1951        .map(|c| if c == '-' { '_' } else { c })
1952        .collect()
1953}
1954
1955/// Configuration of the display of messages emitted by the compiler,
1956/// e.g. diagnostics, warnings, errors, and message caching.
1957struct OutputOptions {
1958    /// What format we're emitting from Cargo itself.
1959    format: MessageFormat,
1960    /// Where to write the JSON messages to support playback later if the unit
1961    /// is fresh. The file is created lazily so that in the normal case, lots
1962    /// of empty files are not created. If this is None, the output will not
1963    /// be cached (such as when replaying cached messages).
1964    cache_cell: Option<(PathBuf, OnceCell<File>)>,
1965    /// If `true`, display any diagnostics.
1966    /// Other types of JSON messages are processed regardless
1967    /// of the value of this flag.
1968    ///
1969    /// This is used primarily for cache replay. If you build with `-vv`, the
1970    /// cache will be filled with diagnostics from dependencies. When the
1971    /// cache is replayed without `-vv`, we don't want to show them.
1972    show_diagnostics: bool,
1973    /// Tracks the number of warnings we've seen so far.
1974    warnings_seen: usize,
1975    /// Tracks the number of errors we've seen so far.
1976    errors_seen: usize,
1977}
1978
1979impl OutputOptions {
1980    fn new(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OutputOptions {
1981        let path = build_runner.files().message_cache_path(unit);
1982        // Remove old cache, ignore ENOENT, which is the common case.
1983        drop(fs::remove_file(&path));
1984        let cache_cell = Some((path, OnceCell::new()));
1985        let show_diagnostics =
1986            build_runner.bcx.gctx.warning_handling().unwrap_or_default() != WarningHandling::Allow;
1987        OutputOptions {
1988            format: build_runner.bcx.build_config.message_format,
1989            cache_cell,
1990            show_diagnostics,
1991            warnings_seen: 0,
1992            errors_seen: 0,
1993        }
1994    }
1995}
1996
1997/// Cloned and sendable context about the manifest file.
1998///
1999/// Sometimes we enrich rustc's errors with some locations in the manifest file; this
2000/// contains a `Send`-able copy of the manifest information that we need for the
2001/// enriched errors.
2002struct ManifestErrorContext {
2003    /// The path to the manifest.
2004    path: PathBuf,
2005    /// The locations of various spans within the manifest.
2006    spans: toml::Spanned<toml::de::DeTable<'static>>,
2007    /// The raw manifest contents.
2008    contents: String,
2009    /// A lookup for all the unambiguous renamings, mapping from the original package
2010    /// name to the renamed one.
2011    rename_table: HashMap<InternedString, InternedString>,
2012    /// A list of targets we're compiling for, to determine which of the `[target.<something>.dependencies]`
2013    /// tables might be of interest.
2014    requested_kinds: Vec<CompileKind>,
2015    /// A list of all the collections of cfg values, one collection for each target, to determine
2016    /// which of the `[target.'cfg(...)'.dependencies]` tables might be of interest.
2017    cfgs: Vec<Vec<Cfg>>,
2018    host_name: InternedString,
2019    /// Cargo's working directory (for printing out a more friendly manifest path).
2020    cwd: PathBuf,
2021    /// Terminal width for formatting diagnostics.
2022    term_width: usize,
2023}
2024
2025fn on_stdout_line(
2026    state: &JobState<'_, '_>,
2027    line: &str,
2028    _package_id: PackageId,
2029    _target: &Target,
2030) -> CargoResult<()> {
2031    state.stdout(line.to_string())?;
2032    Ok(())
2033}
2034
2035fn on_stderr_line(
2036    state: &JobState<'_, '_>,
2037    line: &str,
2038    package_id: PackageId,
2039    manifest: &ManifestErrorContext,
2040    target: &Target,
2041    options: &mut OutputOptions,
2042) -> CargoResult<()> {
2043    if on_stderr_line_inner(state, line, package_id, manifest, target, options)? {
2044        // Check if caching is enabled.
2045        if let Some((path, cell)) = &mut options.cache_cell {
2046            // Cache the output, which will be replayed later when Fresh.
2047            let f = cell.try_borrow_mut_with(|| paths::create(path))?;
2048            debug_assert!(!line.contains('\n'));
2049            f.write_all(line.as_bytes())?;
2050            f.write_all(&[b'\n'])?;
2051        }
2052    }
2053    Ok(())
2054}
2055
2056/// Returns true if the line should be cached.
2057fn on_stderr_line_inner(
2058    state: &JobState<'_, '_>,
2059    line: &str,
2060    package_id: PackageId,
2061    manifest: &ManifestErrorContext,
2062    target: &Target,
2063    options: &mut OutputOptions,
2064) -> CargoResult<bool> {
2065    // We primarily want to use this function to process JSON messages from
2066    // rustc. The compiler should always print one JSON message per line, and
2067    // otherwise it may have other output intermingled (think RUST_LOG or
2068    // something like that), so skip over everything that doesn't look like a
2069    // JSON message.
2070    if !line.starts_with('{') {
2071        state.stderr(line.to_string())?;
2072        return Ok(true);
2073    }
2074
2075    let mut compiler_message: Box<serde_json::value::RawValue> = match serde_json::from_str(line) {
2076        Ok(msg) => msg,
2077
2078        // If the compiler produced a line that started with `{` but it wasn't
2079        // valid JSON, maybe it wasn't JSON in the first place! Forward it along
2080        // to stderr.
2081        Err(e) => {
2082            debug!("failed to parse json: {:?}", e);
2083            state.stderr(line.to_string())?;
2084            return Ok(true);
2085        }
2086    };
2087
2088    let count_diagnostic = |level, options: &mut OutputOptions| {
2089        if level == "warning" {
2090            options.warnings_seen += 1;
2091        } else if level == "error" {
2092            options.errors_seen += 1;
2093        }
2094    };
2095
2096    if let Ok(report) = serde_json::from_str::<FutureIncompatReport>(compiler_message.get()) {
2097        for item in &report.future_incompat_report {
2098            count_diagnostic(&*item.diagnostic.level, options);
2099        }
2100        state.future_incompat_report(report.future_incompat_report);
2101        return Ok(true);
2102    }
2103
2104    let res = serde_json::from_str::<SectionTiming>(compiler_message.get());
2105    if let Ok(timing_record) = res {
2106        state.on_section_timing_emitted(timing_record);
2107        return Ok(false);
2108    }
2109
2110    // Returns `true` if the diagnostic was modified.
2111    let add_pub_in_priv_diagnostic = |diag: &mut String| -> bool {
2112        // We are parsing the compiler diagnostic here, as this information isn't
2113        // currently exposed elsewhere.
2114        // At the time of writing this comment, rustc emits two different
2115        // "exported_private_dependencies" errors:
2116        //  - type `FromPriv` from private dependency 'priv_dep' in public interface
2117        //  - struct `FromPriv` from private dependency 'priv_dep' is re-exported
2118        // This regex matches them both. To see if it needs to be updated, grep the rust
2119        // source for "EXPORTED_PRIVATE_DEPENDENCIES".
2120        static PRIV_DEP_REGEX: LazyLock<Regex> =
2121            LazyLock::new(|| Regex::new("from private dependency '([A-Za-z0-9-_]+)'").unwrap());
2122        if let Some(crate_name) = PRIV_DEP_REGEX.captures(diag).and_then(|m| m.get(1))
2123            && let Some(span) = manifest.find_crate_span(crate_name.as_str())
2124        {
2125            let rel_path = pathdiff::diff_paths(&manifest.path, &manifest.cwd)
2126                .unwrap_or_else(|| manifest.path.clone())
2127                .display()
2128                .to_string();
2129            let report = [Group::with_title(Level::NOTE.secondary_title(format!(
2130                "dependency `{}` declared here",
2131                crate_name.as_str()
2132            )))
2133            .element(
2134                Snippet::source(&manifest.contents)
2135                    .path(rel_path)
2136                    .annotation(AnnotationKind::Context.span(span)),
2137            )];
2138
2139            let rendered = Renderer::styled()
2140                .term_width(manifest.term_width)
2141                .render(&report);
2142            diag.push_str(&rendered);
2143            diag.push('\n');
2144            return true;
2145        }
2146        false
2147    };
2148
2149    // Depending on what we're emitting from Cargo itself, we figure out what to
2150    // do with this JSON message.
2151    match options.format {
2152        // In the "human" output formats (human/short) or if diagnostic messages
2153        // from rustc aren't being included in the output of Cargo's JSON
2154        // messages then we extract the diagnostic (if present) here and handle
2155        // it ourselves.
2156        MessageFormat::Human
2157        | MessageFormat::Short
2158        | MessageFormat::Json {
2159            render_diagnostics: true,
2160            ..
2161        } => {
2162            #[derive(serde::Deserialize)]
2163            struct CompilerMessage<'a> {
2164                // `rendered` contains escape sequences, which can't be
2165                // zero-copy deserialized by serde_json.
2166                // See https://github.com/serde-rs/json/issues/742
2167                rendered: String,
2168                #[serde(borrow)]
2169                message: Cow<'a, str>,
2170                #[serde(borrow)]
2171                level: Cow<'a, str>,
2172                children: Vec<PartialDiagnostic>,
2173                code: Option<DiagnosticCode>,
2174            }
2175
2176            // A partial rustfix::diagnostics::Diagnostic. We deserialize only a
2177            // subset of the fields because rustc's output can be extremely
2178            // deeply nested JSON in pathological cases involving macro
2179            // expansion. Rustfix's Diagnostic struct is recursive containing a
2180            // field `children: Vec<Self>`, and it can cause deserialization to
2181            // hit serde_json's default recursion limit, or overflow the stack
2182            // if we turn that off. Cargo only cares about the 1 field listed
2183            // here.
2184            #[derive(serde::Deserialize)]
2185            struct PartialDiagnostic {
2186                spans: Vec<PartialDiagnosticSpan>,
2187            }
2188
2189            // A partial rustfix::diagnostics::DiagnosticSpan.
2190            #[derive(serde::Deserialize)]
2191            struct PartialDiagnosticSpan {
2192                suggestion_applicability: Option<Applicability>,
2193            }
2194
2195            #[derive(serde::Deserialize)]
2196            struct DiagnosticCode {
2197                code: String,
2198            }
2199
2200            if let Ok(mut msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
2201            {
2202                if msg.message.starts_with("aborting due to")
2203                    || msg.message.ends_with("warning emitted")
2204                    || msg.message.ends_with("warnings emitted")
2205                {
2206                    // Skip this line; we'll print our own summary at the end.
2207                    return Ok(true);
2208                }
2209                // state.stderr will add a newline
2210                if msg.rendered.ends_with('\n') {
2211                    msg.rendered.pop();
2212                }
2213                let mut rendered = msg.rendered;
2214                if options.show_diagnostics {
2215                    let machine_applicable: bool = msg
2216                        .children
2217                        .iter()
2218                        .map(|child| {
2219                            child
2220                                .spans
2221                                .iter()
2222                                .filter_map(|span| span.suggestion_applicability)
2223                                .any(|app| app == Applicability::MachineApplicable)
2224                        })
2225                        .any(|b| b);
2226                    count_diagnostic(&msg.level, options);
2227                    if msg
2228                        .code
2229                        .as_ref()
2230                        .is_some_and(|c| c.code == "exported_private_dependencies")
2231                        && options.format != MessageFormat::Short
2232                    {
2233                        add_pub_in_priv_diagnostic(&mut rendered);
2234                    }
2235                    let lint = msg.code.is_some();
2236                    state.emit_diag(&msg.level, rendered, lint, machine_applicable)?;
2237                }
2238                return Ok(true);
2239            }
2240        }
2241
2242        MessageFormat::Json { ansi, .. } => {
2243            #[derive(serde::Deserialize, serde::Serialize)]
2244            struct CompilerMessage<'a> {
2245                rendered: String,
2246                #[serde(flatten, borrow)]
2247                other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
2248                code: Option<DiagnosticCode<'a>>,
2249            }
2250
2251            #[derive(serde::Deserialize, serde::Serialize)]
2252            struct DiagnosticCode<'a> {
2253                code: String,
2254                #[serde(flatten, borrow)]
2255                other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
2256            }
2257
2258            if let Ok(mut error) =
2259                serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
2260            {
2261                let modified_diag = if error
2262                    .code
2263                    .as_ref()
2264                    .is_some_and(|c| c.code == "exported_private_dependencies")
2265                {
2266                    add_pub_in_priv_diagnostic(&mut error.rendered)
2267                } else {
2268                    false
2269                };
2270
2271                // Remove color information from the rendered string if color is not
2272                // enabled. Cargo always asks for ANSI colors from rustc. This allows
2273                // cached replay to enable/disable colors without re-invoking rustc.
2274                if !ansi {
2275                    error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
2276                }
2277                if !ansi || modified_diag {
2278                    let new_line = serde_json::to_string(&error)?;
2279                    compiler_message = serde_json::value::RawValue::from_string(new_line)?;
2280                }
2281            }
2282        }
2283    }
2284
2285    // We always tell rustc to emit messages about artifacts being produced.
2286    // These messages feed into pipelined compilation, as well as timing
2287    // information.
2288    //
2289    // Look for a matching directive and inform Cargo internally that a
2290    // metadata file has been produced.
2291    #[derive(serde::Deserialize)]
2292    struct ArtifactNotification<'a> {
2293        #[serde(borrow)]
2294        artifact: Cow<'a, str>,
2295    }
2296
2297    if let Ok(artifact) = serde_json::from_str::<ArtifactNotification<'_>>(compiler_message.get()) {
2298        trace!("found directive from rustc: `{}`", artifact.artifact);
2299        if artifact.artifact.ends_with(".rmeta") {
2300            debug!("looks like metadata finished early!");
2301            state.rmeta_produced();
2302        }
2303        return Ok(false);
2304    }
2305
2306    // And failing all that above we should have a legitimate JSON diagnostic
2307    // from the compiler, so wrap it in an external Cargo JSON message
2308    // indicating which package it came from and then emit it.
2309
2310    if !options.show_diagnostics {
2311        return Ok(true);
2312    }
2313
2314    #[derive(serde::Deserialize)]
2315    struct CompilerMessage<'a> {
2316        #[serde(borrow)]
2317        message: Cow<'a, str>,
2318        #[serde(borrow)]
2319        level: Cow<'a, str>,
2320    }
2321
2322    if let Ok(msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get()) {
2323        if msg.message.starts_with("aborting due to")
2324            || msg.message.ends_with("warning emitted")
2325            || msg.message.ends_with("warnings emitted")
2326        {
2327            // Skip this line; we'll print our own summary at the end.
2328            return Ok(true);
2329        }
2330        count_diagnostic(&msg.level, options);
2331    }
2332
2333    let msg = machine_message::FromCompiler {
2334        package_id: package_id.to_spec(),
2335        manifest_path: &manifest.path,
2336        target,
2337        message: compiler_message,
2338    }
2339    .to_json_string();
2340
2341    // Switch json lines from rustc/rustdoc that appear on stderr to stdout
2342    // instead. We want the stdout of Cargo to always be machine parseable as
2343    // stderr has our colorized human-readable messages.
2344    state.stdout(msg)?;
2345    Ok(true)
2346}
2347
2348impl ManifestErrorContext {
2349    fn new(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> ManifestErrorContext {
2350        let mut duplicates = HashSet::new();
2351        let mut rename_table = HashMap::new();
2352
2353        for dep in build_runner.unit_deps(unit) {
2354            let unrenamed_id = dep.unit.pkg.package_id().name();
2355            if duplicates.contains(&unrenamed_id) {
2356                continue;
2357            }
2358            match rename_table.entry(unrenamed_id) {
2359                std::collections::hash_map::Entry::Occupied(occ) => {
2360                    occ.remove_entry();
2361                    duplicates.insert(unrenamed_id);
2362                }
2363                std::collections::hash_map::Entry::Vacant(vac) => {
2364                    vac.insert(dep.extern_crate_name);
2365                }
2366            }
2367        }
2368
2369        let bcx = build_runner.bcx;
2370        ManifestErrorContext {
2371            path: unit.pkg.manifest_path().to_owned(),
2372            spans: unit.pkg.manifest().document().clone(),
2373            contents: unit.pkg.manifest().contents().to_owned(),
2374            requested_kinds: bcx.target_data.requested_kinds().to_owned(),
2375            host_name: bcx.rustc().host,
2376            rename_table,
2377            cwd: path_args(build_runner.bcx.ws, unit).1,
2378            cfgs: bcx
2379                .target_data
2380                .requested_kinds()
2381                .iter()
2382                .map(|k| bcx.target_data.cfg(*k).to_owned())
2383                .collect(),
2384            term_width: bcx
2385                .gctx
2386                .shell()
2387                .err_width()
2388                .diagnostic_terminal_width()
2389                .unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
2390        }
2391    }
2392
2393    fn requested_target_names(&self) -> impl Iterator<Item = &str> {
2394        self.requested_kinds.iter().map(|kind| match kind {
2395            CompileKind::Host => &self.host_name,
2396            CompileKind::Target(target) => target.short_name(),
2397        })
2398    }
2399
2400    /// Find a span for the dependency that specifies this unrenamed crate, if it's unique.
2401    ///
2402    /// rustc diagnostics (at least for public-in-private) mention the un-renamed
2403    /// crate: if you have `foo = { package = "bar" }`, the rustc diagnostic will
2404    /// say "bar".
2405    ///
2406    /// This function does its best to find a span for "bar", but it could fail if
2407    /// there are multiple candidates:
2408    ///
2409    /// ```toml
2410    /// foo = { package = "bar" }
2411    /// baz = { path = "../bar", package = "bar" }
2412    /// ```
2413    fn find_crate_span(&self, unrenamed: &str) -> Option<Range<usize>> {
2414        let orig_name = self.rename_table.get(unrenamed)?.as_str();
2415
2416        if let Some((k, v)) = get_key_value(&self.spans, &["dependencies", orig_name]) {
2417            // We make some effort to find the unrenamed text: in
2418            //
2419            // ```
2420            // foo = { package = "bar" }
2421            // ```
2422            //
2423            // we try to find the "bar", but fall back to "foo" if we can't (which might
2424            // happen if the renaming took place in the workspace, for example).
2425            if let Some(package) = v.get_ref().as_table().and_then(|t| t.get("package")) {
2426                return Some(package.span());
2427            } else {
2428                return Some(k.span());
2429            }
2430        }
2431
2432        // The dependency could also be in a target-specific table, like
2433        // [target.x86_64-unknown-linux-gnu.dependencies] or
2434        // [target.'cfg(something)'.dependencies]. We filter out target tables
2435        // that don't match a requested target or a requested cfg.
2436        if let Some(target) = self
2437            .spans
2438            .as_ref()
2439            .get("target")
2440            .and_then(|t| t.as_ref().as_table())
2441        {
2442            for (platform, platform_table) in target.iter() {
2443                match platform.as_ref().parse::<Platform>() {
2444                    Ok(Platform::Name(name)) => {
2445                        if !self.requested_target_names().any(|n| n == name) {
2446                            continue;
2447                        }
2448                    }
2449                    Ok(Platform::Cfg(cfg_expr)) => {
2450                        if !self.cfgs.iter().any(|cfgs| cfg_expr.matches(cfgs)) {
2451                            continue;
2452                        }
2453                    }
2454                    Err(_) => continue,
2455                }
2456
2457                let Some(platform_table) = platform_table.as_ref().as_table() else {
2458                    continue;
2459                };
2460
2461                if let Some(deps) = platform_table
2462                    .get("dependencies")
2463                    .and_then(|d| d.as_ref().as_table())
2464                {
2465                    if let Some((k, v)) = deps.get_key_value(orig_name) {
2466                        if let Some(package) = v.get_ref().as_table().and_then(|t| t.get("package"))
2467                        {
2468                            return Some(package.span());
2469                        } else {
2470                            return Some(k.span());
2471                        }
2472                    }
2473                }
2474            }
2475        }
2476        None
2477    }
2478}
2479
2480/// Creates a unit of work that replays the cached compiler message.
2481///
2482/// Usually used when a job is fresh and doesn't need to recompile.
2483fn replay_output_cache(
2484    package_id: PackageId,
2485    manifest: ManifestErrorContext,
2486    target: &Target,
2487    path: PathBuf,
2488    format: MessageFormat,
2489    show_diagnostics: bool,
2490) -> Work {
2491    let target = target.clone();
2492    let mut options = OutputOptions {
2493        format,
2494        cache_cell: None,
2495        show_diagnostics,
2496        warnings_seen: 0,
2497        errors_seen: 0,
2498    };
2499    Work::new(move |state| {
2500        if !path.exists() {
2501            // No cached output, probably didn't emit anything.
2502            return Ok(());
2503        }
2504        // We sometimes have gigabytes of output from the compiler, so avoid
2505        // loading it all into memory at once, as that can cause OOM where
2506        // otherwise there would be none.
2507        let file = paths::open(&path)?;
2508        let mut reader = std::io::BufReader::new(file);
2509        let mut line = String::new();
2510        loop {
2511            let length = reader.read_line(&mut line)?;
2512            if length == 0 {
2513                break;
2514            }
2515            let trimmed = line.trim_end_matches(&['\n', '\r'][..]);
2516            on_stderr_line(state, trimmed, package_id, &manifest, &target, &mut options)?;
2517            line.clear();
2518        }
2519        Ok(())
2520    })
2521}
2522
2523/// Provides a package name with descriptive target information,
2524/// e.g., '`foo` (bin "bar" test)', '`foo` (lib doctest)'.
2525fn descriptive_pkg_name(name: &str, target: &Target, mode: &CompileMode) -> String {
2526    let desc_name = target.description_named();
2527    let mode = if mode.is_rustc_test() && !(target.is_test() || target.is_bench()) {
2528        " test"
2529    } else if mode.is_doc_test() {
2530        " doctest"
2531    } else if mode.is_doc() {
2532        " doc"
2533    } else {
2534        ""
2535    };
2536    format!("`{name}` ({desc_name}{mode})")
2537}
2538
2539/// Applies environment variables from config `[env]` to [`ProcessBuilder`].
2540pub(crate) fn apply_env_config(
2541    gctx: &crate::GlobalContext,
2542    cmd: &mut ProcessBuilder,
2543) -> CargoResult<()> {
2544    for (key, value) in gctx.env_config()?.iter() {
2545        // never override a value that has already been set by cargo
2546        if cmd.get_envs().contains_key(key) {
2547            continue;
2548        }
2549        cmd.env(key, value);
2550    }
2551    Ok(())
2552}
2553
2554/// Checks if there are some scrape units waiting to be processed.
2555fn should_include_scrape_units(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
2556    unit.mode.is_doc() && bcx.scrape_units.len() > 0 && bcx.ws.unit_needs_doc_scrape(unit)
2557}
2558
2559/// Gets the file path of function call information output from `rustdoc`.
2560fn scrape_output_path(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<PathBuf> {
2561    assert!(unit.mode.is_doc() || unit.mode.is_doc_scrape());
2562    build_runner
2563        .outputs(unit)
2564        .map(|outputs| outputs[0].path.clone())
2565}
2566
2567/// Gets the dep-info file emitted by rustdoc.
2568fn rustdoc_dep_info_loc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> PathBuf {
2569    let mut loc = build_runner.files().fingerprint_file_path(unit, "");
2570    loc.set_extension("d");
2571    loc
2572}