@@ -12,6 +12,34 @@ use super::{BuildContext, Context, FileFlavor, Kind, Layout, Unit};
12
12
use crate :: core:: { TargetKind , Workspace } ;
13
13
use crate :: util:: { self , CargoResult } ;
14
14
15
+ /// The `Metadata` is a hash used to make unique file names for each unit in a build.
16
+ /// For example:
17
+ /// - A project may depend on crate `A` and crate `B`, so the package name must be in the file name.
18
+ /// - Similarly a project may depend on two versions of `A`, so the version must be in the file name.
19
+ /// In general this must include all things that need to be distinguished in different parts of
20
+ /// the same build. This is absolutely required or we override things before
21
+ /// we get chance to use them.
22
+ ///
23
+ /// We use a hash because it is an easy way to guarantee
24
+ /// that all the inputs can be converted to a valid path.
25
+ ///
26
+ /// This also acts as the main layer of caching provided by Cargo.
27
+ /// For example, we want to cache `cargo build` and `cargo doc` separately, so that running one
28
+ /// does not invalidate the artifacts for the other. We do this by including `CompileMode` in the
29
+ /// hash, thus the artifacts go in different folders and do not override each other.
30
+ /// If we don't add something that we should have, for this reason, we get the
31
+ /// correct output but rebuild more than is needed.
32
+ ///
33
+ /// Some things that need to be tracked to ensure the correct output should definitely *not*
34
+ /// go in the `Metadata`. For example, the modification time of a file, should be tracked to make a
35
+ /// rebuild when the file changes. However, it would be wasteful to include in the `Metadata`. The
36
+ /// old artifacts are never going to be needed again. We can save space by just overwriting them.
37
+ /// If we add something that we should not have, for this reason, we get the correct output but take
38
+ /// more space than needed. This makes not including something in `Metadata`
39
+ /// a form of cache invalidation.
40
+ ///
41
+ /// Note that the `Fingerprint` is in charge of tracking everything needed to determine if a
42
+ /// rebuild is needed.
15
43
#[ derive( Clone , Hash , Eq , PartialEq , Ord , PartialOrd ) ]
16
44
pub struct Metadata ( u64 ) ;
17
45
@@ -465,6 +493,15 @@ fn compute_metadata<'a, 'cfg>(
465
493
args. hash ( & mut hasher) ;
466
494
}
467
495
496
+ // Throw in the rustflags we're compiling with.
497
+ // This helps when the target directory is a shared cache for projects with different cargo configs,
498
+ // or if the user is experimenting with different rustflags manually.
499
+ if unit. mode . is_doc ( ) {
500
+ cx. bcx . rustdocflags_args ( unit) . ok ( ) . hash ( & mut hasher) ;
501
+ } else {
502
+ cx. bcx . rustflags_args ( unit) . ok ( ) . hash ( & mut hasher) ;
503
+ }
504
+
468
505
// Artifacts compiled for the host should have a different metadata
469
506
// piece than those compiled for the target, so make sure we throw in
470
507
// the unit's `kind` as well
0 commit comments