Skip to content

Commit 2bf35da

Browse files
committed
docs(fingerprint): Track each use of Metadata separately
1 parent cdf805e commit 2bf35da

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

src/cargo/core/compiler/build_runner/compilation_files.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,25 @@ impl fmt::Debug for UnitHash {
4141
}
4242
}
4343

44-
/// The `Metadata` is a hash used to make unique file names for each unit in a
45-
/// build. It is also used for symbol mangling.
44+
/// [`Metadata`] tracks several [`UnitHash`]s, including
45+
/// [`Metadata::unit_id`], [`Metadata::c_metadata`], and [`Metadata::c_extra_filename`].
4646
///
47-
/// For example:
47+
/// We use a hash because it is an easy way to guarantee
48+
/// that all the inputs can be converted to a valid path.
49+
///
50+
/// [`Metadata::unit_id`] is used to uniquely identify a unit in the build graph.
51+
/// This serves as a similar role as [`Metadata::c_extra_filename`] in that it uniquely identifies output
52+
/// on the filesystem except that its always present.
53+
///
54+
/// [`Metadata::c_extra_filename`] is needed for cases like:
4855
/// - A project may depend on crate `A` and crate `B`, so the package name must be in the file name.
4956
/// - Similarly a project may depend on two versions of `A`, so the version must be in the file name.
5057
///
51-
/// In general this must include all things that need to be distinguished in different parts of
58+
/// This also acts as the main layer of caching provided by Cargo
59+
/// so this must include all things that need to be distinguished in different parts of
5260
/// the same build. This is absolutely required or we override things before
5361
/// we get chance to use them.
5462
///
55-
/// It is also used for symbol mangling, because if you have two versions of
56-
/// the same crate linked together, their symbols need to be differentiated.
57-
///
58-
/// We use a hash because it is an easy way to guarantee
59-
/// that all the inputs can be converted to a valid path.
60-
///
61-
/// This also acts as the main layer of caching provided by Cargo.
6263
/// For example, we want to cache `cargo build` and `cargo doc` separately, so that running one
6364
/// does not invalidate the artifacts for the other. We do this by including [`CompileMode`] in the
6465
/// hash, thus the artifacts go in different folders and do not override each other.
@@ -73,17 +74,20 @@ impl fmt::Debug for UnitHash {
7374
/// more space than needed. This makes not including something in `Metadata`
7475
/// a form of cache invalidation.
7576
///
76-
/// You should also avoid anything that would interfere with reproducible
77+
/// Note that the `Fingerprint` is in charge of tracking everything needed to determine if a
78+
/// rebuild is needed.
79+
///
80+
/// [`Metadata::c_metadata`] is used for symbol mangling, because if you have two versions of
81+
/// the same crate linked together, their symbols need to be differentiated.
82+
///
83+
/// You should avoid anything that would interfere with reproducible
7784
/// builds. For example, *any* absolute path should be avoided. This is one
78-
/// reason that `RUSTFLAGS` is not in `Metadata`, because it often has
85+
/// reason that `RUSTFLAGS` is not in [`Metadata::c_metadata`], because it often has
7986
/// absolute paths (like `--remap-path-prefix` which is fundamentally used for
8087
/// reproducible builds and has absolute paths in it). Also, in some cases the
8188
/// mangled symbols need to be stable between different builds with different
8289
/// settings. For example, profile-guided optimizations need to swap
8390
/// `RUSTFLAGS` between runs, but needs to keep the same symbol names.
84-
///
85-
/// Note that the `Fingerprint` is in charge of tracking everything needed to determine if a
86-
/// rebuild is needed.
8791
#[derive(Copy, Clone, Debug)]
8892
pub struct Metadata {
8993
unit_id: UnitHash,

src/cargo/core/compiler/fingerprint/mod.rs

+36-30
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,48 @@
4747
//! reliable and reproducible builds at the cost of being complex, slow, and
4848
//! platform-dependent.
4949
//!
50-
//! ## Fingerprints and Metadata
50+
//! ## Fingerprints and [`UnitHash`]s
51+
//!
52+
//! [`Metadata`] tracks several [`UnitHash`]s, including
53+
//! [`Metadata::unit_id`], [`Metadata::c_metadata`], and [`Metadata::c_extra_filename`].
54+
//! See its documentation for more details.
5155
//!
52-
//! The [`Metadata`] hash is a hash added to the output filenames to isolate
53-
//! each unit. See its documentationfor more details.
5456
//! NOTE: Not all output files are isolated via filename hashes (like dylibs).
5557
//! The fingerprint directory uses a hash, but sometimes units share the same
5658
//! fingerprint directory (when they don't have Metadata) so care should be
5759
//! taken to handle this!
5860
//!
59-
//! Fingerprints and Metadata are similar, and track some of the same things.
60-
//! The Metadata contains information that is required to keep Units separate.
61+
//! Fingerprints and [`UnitHash`]s are similar, and track some of the same things.
62+
//! [`UnitHash`]s contains information that is required to keep Units separate.
6163
//! The Fingerprint includes additional information that should cause a
6264
//! recompile, but it is desired to reuse the same filenames. A comparison
6365
//! of what is tracked:
6466
//!
65-
//! Value | Fingerprint | Metadata
66-
//! -------------------------------------------|-------------|----------
67-
//! rustc | ✓ | ✓
68-
//! [`Profile`] | ✓ | ✓
69-
//! `cargo rustc` extra args | ✓ |
70-
//! [`CompileMode`] | ✓ | ✓
71-
//! Target Name | ✓ | ✓
72-
//! `TargetKind` (bin/lib/etc.) | ✓ | ✓
73-
//! Enabled Features | ✓ | ✓
74-
//! Declared Features | ✓ |
75-
//! Immediate dependency’s hashes | ✓[^1] | ✓
76-
//! [`CompileKind`] (host/target) | ✓ | ✓
77-
//! `__CARGO_DEFAULT_LIB_METADATA`[^4] | | ✓
78-
//! `package_id` | | ✓
79-
//! authors, description, homepage, repo | ✓ |
80-
//! Target src path relative to ws | ✓ |
81-
//! Target flags (test/bench/for_host/edition) | ✓ |
82-
//! -C incremental=… flag | ✓ |
83-
//! mtime of sources | ✓[^3] |
84-
//! RUSTFLAGS/RUSTDOCFLAGS | ✓ |
85-
//! [`Lto`] flags | ✓ | ✓
86-
//! config settings[^5] | ✓ |
87-
//! `is_std` | | ✓
88-
//! `[lints]` table[^6] | ✓ |
89-
//! `[lints.rust.unexpected_cfgs.check-cfg]` | ✓ |
67+
//! Value | Fingerprint | `Metadata::unit_id` | `Metadata::c_metadata` | `Metadata::c_extra_filename`
68+
//! -------------------------------------------|-------------|---------------------|------------------------|----------
69+
//! rustc | ✓ | ✓ | ✓ | ✓
70+
//! [`Profile`] | ✓ | ✓ | ✓ | ✓
71+
//! `cargo rustc` extra args | ✓ | | |
72+
//! [`CompileMode`] | ✓ | ✓ | ✓ | ✓
73+
//! Target Name | ✓ | ✓ | ✓ | ✓
74+
//! `TargetKind` (bin/lib/etc.) | ✓ | ✓ | ✓ | ✓
75+
//! Enabled Features | ✓ | ✓ | ✓ | ✓
76+
//! Declared Features | ✓ | | |
77+
//! Immediate dependency’s hashes | ✓[^1] | ✓ | ✓ | ✓
78+
//! [`CompileKind`] (host/target) | ✓ | ✓ | ✓ | ✓
79+
//! `__CARGO_DEFAULT_LIB_METADATA`[^4] | | ✓ | ✓ | ✓
80+
//! `package_id` | | ✓ | ✓ | ✓
81+
//! authors, description, homepage, repo | ✓ | | |
82+
//! Target src path relative to ws | ✓ | | |
83+
//! Target flags (test/bench/for_host/edition) | ✓ | | |
84+
//! -C incremental=… flag | ✓ | | |
85+
//! mtime of sources | ✓[^3] | | |
86+
//! RUSTFLAGS/RUSTDOCFLAGS | ✓ | | |
87+
//! [`Lto`] flags | ✓ | ✓ | ✓ | ✓
88+
//! config settings[^5] | ✓ | | |
89+
//! `is_std` | | ✓ | ✓ | ✓
90+
//! `[lints]` table[^6] | ✓ | | |
91+
//! `[lints.rust.unexpected_cfgs.check-cfg]` | ✓ | | |
9092
//!
9193
//! [^1]: Build script and bin dependencies are not included.
9294
//!
@@ -348,6 +350,10 @@
348350
//!
349351
//! [`check_filesystem`]: Fingerprint::check_filesystem
350352
//! [`Metadata`]: crate::core::compiler::Metadata
353+
//! [`Metadata::unit_id`]: crate::core::compiler::Metadata::unit_id
354+
//! [`Metadata::c_metadata`]: crate::core::compiler::Metadata::c_metadata
355+
//! [`Metadata::c_extra_filename`]: crate::core::compiler::Metadata::c_extra_filename
356+
//! [`UnitHash`]: crate::core::compiler::UnitHash
351357
//! [`Profile`]: crate::core::profiles::Profile
352358
//! [`CompileMode`]: crate::core::compiler::CompileMode
353359
//! [`Lto`]: crate::core::compiler::Lto

0 commit comments

Comments
 (0)