#
cargo/core/compiler/
mod.rs

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