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