Skip to main content

Module compiler

Module compiler 

Source
Expand description

Β§Interact with the compiler

If you consider ops::cargo_compile::compile as a rustc driver but on Cargo side, this module is kinda the rustc_interface for that merits. It contains all the interaction between Cargo and the rustc compiler, from preparing the context for the entire build process, to scheduling and executing each unit of work (e.g. running rustc), to managing and caching the output artifact of a build.

However, it hasn’t yet exposed a clear definition of each phase or session, like what rustc has done. Also, no one knows if Cargo really needs that. To be pragmatic, here we list a handful of items you may want to learn:

  • BuildContext is a static context containing all information you need before a build gets started.
  • BuildRunner is the center of the world, coordinating a running build and collecting information from it.
  • custom_build is the home of build script executions and output parsing.
  • fingerprint not only defines but also executes a set of rules to determine if a re-compile is needed.
  • job_queue is where the parallelism, job scheduling, and communication machinery happen between Cargo and the compiler.
  • layout defines and manages output artifacts of a build in the filesystem.
  • unit_dependencies is for building a dependency graph for compilation from a result of dependency resolution.
  • Unit contains sufficient information to build something, usually turning into a compiler invocation in a later phase.

Re-exportsΒ§

use cargo_util::collections::HashMap; πŸ”’
use cargo_util::collections::HashSet; πŸ”’
use std::borrow::Cow; πŸ”’
use std::cell::OnceCell; πŸ”’
use std::env; πŸ”’
use std::env; πŸ”’
use std::ffi::OsStr; πŸ”’
use std::ffi::OsString; πŸ”’
use std::fmt::Display; πŸ”’
use std::fs; πŸ”’
use std::fs::File; πŸ”’
use std::io::BufRead; πŸ”’
use std::io::BufWriter; πŸ”’
use std::io::Write; πŸ”’
use std::ops::Deref; πŸ”’
use std::ops::Range; πŸ”’
use std::path::Path; πŸ”’
use std::path::PathBuf; πŸ”’
use std::sync::Arc; πŸ”’
use std::sync::LazyLock; πŸ”’
use anyhow::Context as _; πŸ”’
use anyhow::Error; πŸ”’
use cargo_platform::Cfg; πŸ”’
use cargo_platform::Platform; πŸ”’
use cargo_util_schemas::core::CrateType; πŸ”’
use cargo_util_terminal::report::AnnotationKind; πŸ”’
use cargo_util_terminal::report::Group; πŸ”’
use cargo_util_terminal::report::Level; πŸ”’
use cargo_util_terminal::report::Renderer; πŸ”’
use cargo_util_terminal::report::Snippet; πŸ”’
use regex::Regex; πŸ”’
use tracing::debug; πŸ”’
use tracing::instrument; πŸ”’
use tracing::instrument; πŸ”’
use tracing::trace; πŸ”’
pub use self::build_config::UserIntent;
pub use self::build_config::BuildConfig;
pub use self::build_config::CompileMode;
pub use self::build_config::MessageFormat;
pub use self::build_context::BuildContext;
pub use self::build_context::DepKindSet;
pub use self::build_context::FileFlavor;
pub use self::build_context::FileType;
pub use self::build_context::RustcTargetData;
pub use self::build_context::TargetInfo;
pub use self::build_runner::BuildRunner;
pub use self::build_runner::Metadata;
pub use self::build_runner::UnitHash;
pub use self::compilation::Compilation;
pub use self::compilation::Doctest;
pub use self::compilation::UnitOutput;
pub use self::compile_kind::CompileKind;
pub use self::compile_kind::CompileKindFallback;
pub use self::compile_kind::CompileTarget;
pub use self::crate_type::CrateTypeExt;
pub use self::custom_build::LinkArgTarget;
pub use self::custom_build::BuildOutput;
pub use self::custom_build::BuildScriptOutputs;
pub use self::custom_build::BuildScripts;
pub use self::custom_build::LibraryPath;
pub(crate) use self::fingerprint::DirtyReason; πŸ”’
pub use self::fingerprint::RustdocFingerprint;
pub use self::job_queue::Freshness;
use self::job_queue::Job; πŸ”’
use self::job_queue::JobQueue; πŸ”’
use self::job_queue::JobState; πŸ”’
use self::job_queue::Work; πŸ”’
pub(crate) use self::layout::Layout; πŸ”’
pub use self::lto::Lto;
use self::output_depinfo::output_depinfo; πŸ”’
use self::output_sbom::build_sbom; πŸ”’
use self::trim_paths::trim_paths_args; πŸ”’
use self::trim_paths::trim_paths_args_rustdoc; πŸ”’
use self::unit_graph::UnitDep; πŸ”’
use crate::compiler::future_incompat::FutureIncompatReport; πŸ”’
use crate::compiler::locking::LockKey; πŸ”’
use crate::compiler::timings::SectionTiming; πŸ”’
pub use crate::compiler::unit::Unit;
pub use crate::compiler::unit::UnitIndex;
pub use crate::compiler::unit::UnitInterner;
use crate::context::FingerprintMethod; πŸ”’
use crate::diagnostics::get_key_value; πŸ”’
use crate::util::OnceExt; πŸ”’
use crate::util::errors::CargoResult; πŸ”’
use crate::util::errors::VerboseError; πŸ”’
use crate::util::interning::InternedString; πŸ”’
use crate::util::machine_message; πŸ”’
use crate::util::machine_message::Message; πŸ”’
use crate::util::add_path_args; πŸ”’
use crate::util::internal; πŸ”’
use crate::util::path_args; πŸ”’
use crate::workspace::Feature; πŸ”’
use crate::workspace::PackageId; πŸ”’
use crate::workspace::Target; πŸ”’
use crate::workspace::TargetKind; πŸ”’
use crate::workspace::manifest::TargetSourcePath; πŸ”’
use crate::workspace::profiles::PanicStrategy; πŸ”’
use crate::workspace::profiles::Profile; πŸ”’
use crate::workspace::profiles::StripInner; πŸ”’
use cargo_util::ExecStats; πŸ”’
use cargo_util::ProcessBuilder; πŸ”’
use cargo_util::ProcessError; πŸ”’
use cargo_util::paths; πŸ”’
use cargo_util_schemas::manifest::TomlDebugInfo; πŸ”’
use cargo_util_terminal::Verbosity; πŸ”’
use rustfix::diagnostics::Applicability; πŸ”’

ModulesΒ§

artifact
Generate artifact information from unit dependencies for configuring the compiler environment.
build_config πŸ”’
build_context πŸ”’
BuildContext is a (mostly) static information about a build task.
build_runner πŸ”’
BuildRunner is the mutable state used during the build process.
compilation πŸ”’
Type definitions for the result of a compilation.
compile_kind πŸ”’
Type definitions for cross-compilation.
crate_type πŸ”’
custom_build πŸ”’
How to execute a build script and parse its output.
fingerprint πŸ”’
Tracks changes to determine if something needs to be recompiled.
future_incompat
Support for future-incompatible warning reporting.
job_queue πŸ”’
Management of the interaction between the main cargo and all spawned jobs.
layout πŸ”’
Management of the directory layout of a build
links πŸ”’
locking πŸ”’
This module handles the locking logic during compilation.
lto πŸ”’
output_depinfo πŸ”’
dep-info files for external build system integration. See output_depinfo for more.
output_sbom πŸ”’
cargo-sbom precursor files for external tools to create SBOM files from. See build_sbom_graph for more.
rustdoc
Utilities for building with rustdoc.
standard_lib
Code for building the standard library.
timings
Timing tracking.
trim_paths πŸ”’
Path prefix remapping for RFC 3127 trim-paths.
unit πŸ”’
Types and impls for Unit.
unit_dependencies
Constructs the dependency graph for compilation.
unit_graph
Serialization of UnitGraph for unstable option --unit-graph.
unused_deps

StructsΒ§

DefaultExecutor
A DefaultExecutor calls rustc without doing anything else. It is Cargo’s default behaviour.
ManifestErrorContext πŸ”’
Cloned and sendable context about the manifest file.
OutputOptions πŸ”’
Configuration of the display of messages emitted by the compiler, e.g. diagnostics, warnings, errors, and message caching.

EnumsΒ§

CapLints πŸ”’

ConstantsΒ§

RUSTDOC_CRATE_VERSION_FLAG πŸ”’

TraitsΒ§

Executor
A glorified callback for executing calls to rustc. Rather than calling rustc directly, we’ll use an Executor, giving clients an opportunity to intercept the build calls.

FunctionsΒ§

add_allow_features πŸ”’
Forwards -Zallow-features if it is set for cargo.
add_cap_lints πŸ”’
Adds --cap-lints to the command to execute.
add_codegen_incremental πŸ”’
Adds -C incremental=<path>.
add_codegen_linker πŸ”’
Adds -C linker=<path> if specified.
add_custom_flags πŸ”’
Adds extra rustc flags and environment variables collected from the output of a build-script to the command to execute, include custom environment variables and cfg.
add_error_format_and_color πŸ”’
Adds --error-format to the command to execute.
add_plugin_deps πŸ”’
append_crate_version_flag πŸ”’
apply_env_config πŸ”’
Applies environment variables from config [env] to ProcessBuilder.
build_base_args πŸ”’
Adds essential rustc flags and environment variables to the command to execute.
build_deps_args πŸ”’
Adds dependency-relevant rustc flags and environment variables to the command to execute, such as -L and --extern.
check_cfg_args πŸ”’
Generates the --check-cfg arguments for the unit.
compile πŸ”’
Builds up and enqueue a list of pending jobs onto the job queue.
compute_cap_lints πŸ”’
crate_version_flag_already_present πŸ”’
dep_paths_for_args πŸ”’
Generate a list of paths for the rustc -L args.
descriptive_pkg_name πŸ”’
Provides a package name with descriptive target information, e.g., β€˜foo (bin β€œbar” test)’, β€˜foo (lib doctest)’.
downgrade_lock_to_shared πŸ”’
envify πŸ”’
extern_args
Generates a list of --extern arguments.
features_args πŸ”’
All active features for the unit passed as --cfg features=<feature-name>.
filter_dynamic_search_path πŸ”’
get_dynamic_search_path πŸ”’
is_public_dependency_enabled πŸ”’
lib_search_paths
Generate a list of -L arguments
link_targets πŸ”’
Link the compiled target (often of form foo-{metadata_hash}) to the final target. This must happen during both β€œFresh” and β€œCompile”.
lto_args πŸ”’
Adds LTO related codegen flags.
make_failed_scrape_diagnostic πŸ”’
Generates the warning message used when fallible doc-scrape units fail, either for rustdoc or rustc.
on_stderr_line πŸ”’
on_stderr_line_inner πŸ”’
Returns true if the line should be cached.
on_stdout_line πŸ”’
prebuild_lock_exclusive πŸ”’
prepare_rustc πŸ”’
Prepares flags and environments we can compute for a rustc invocation before the job queue starts compiling any unit.
prepare_rustdoc πŸ”’
Prepares flags and environments we can compute for a rustdoc invocation before the job queue starts compiling any unit.
replay_output_cache πŸ”’
Creates a unit of work that replays the cached compiler message.
rustc πŸ”’
Creates a unit of work invoking rustc for building the unit.
rustdoc πŸ”’
Creates a unit of work invoking rustdoc for documenting the unit.
rustdoc_dep_info_loc πŸ”’
Gets the dep-info file emitted by rustdoc.
scrape_output_path πŸ”’
Gets the file path of function call information output from rustdoc.
should_include_scrape_units πŸ”’
Checks if there are some scrape units waiting to be processed.
verbose_if_simple_exit_code πŸ”’