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