#
cargo/core/compiler/
mod.rs

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