187187//! See the `A-rebuild-detection` flag on the issue tracker for more:
188188//! <https://github.com/rust-lang/cargo/issues?q=is%3Aissue+is%3Aopen+label%3AA-rebuild-detection>
189189
190- use std:: collections:: HashMap ;
190+ use std:: collections:: hash_map :: { Entry , HashMap } ;
191191use std:: env;
192192use std:: fs;
193193use std:: hash:: { self , Hasher } ;
@@ -213,7 +213,7 @@ use super::job::{
213213 Freshness :: { Dirty , Fresh } ,
214214 Job , Work ,
215215} ;
216- use super :: { BuildContext , Context , FileFlavor , Kind , Unit } ;
216+ use super :: { BuildContext , Context , FileFlavor , Unit } ;
217217
218218/// Determines if a `unit` is up-to-date, and if not prepares necessary work to
219219/// update the persisted fingerprint.
@@ -539,6 +539,7 @@ impl LocalFingerprint {
539539 /// file accesses.
540540 fn find_stale_file (
541541 & self ,
542+ mtime_cache : & mut HashMap < PathBuf , FileTime > ,
542543 pkg_root : & Path ,
543544 target_root : & Path ,
544545 ) -> CargoResult < Option < StaleFile > > {
@@ -550,7 +551,7 @@ impl LocalFingerprint {
550551 LocalFingerprint :: CheckDepInfo { dep_info } => {
551552 let dep_info = target_root. join ( dep_info) ;
552553 if let Some ( paths) = parse_dep_info ( pkg_root, target_root, & dep_info) ? {
553- Ok ( find_stale_file ( & dep_info, paths. iter ( ) ) )
554+ Ok ( find_stale_file ( mtime_cache , & dep_info, paths. iter ( ) ) )
554555 } else {
555556 Ok ( Some ( StaleFile :: Missing ( dep_info) ) )
556557 }
@@ -559,6 +560,7 @@ impl LocalFingerprint {
559560 // We need to verify that no paths listed in `paths` are newer than
560561 // the `output` path itself, or the last time the build script ran.
561562 LocalFingerprint :: RerunIfChanged { output, paths } => Ok ( find_stale_file (
563+ mtime_cache,
562564 & target_root. join ( output) ,
563565 paths. iter ( ) . map ( |p| pkg_root. join ( p) ) ,
564566 ) ) ,
@@ -756,7 +758,12 @@ impl Fingerprint {
756758 /// dependencies up to this unit as well. This function assumes that the
757759 /// unit starts out as `FsStatus::Stale` and then it will optionally switch
758760 /// it to `UpToDate` if it can.
759- fn check_filesystem ( & mut self , pkg_root : & Path , target_root : & Path ) -> CargoResult < ( ) > {
761+ fn check_filesystem (
762+ & mut self ,
763+ mtime_cache : & mut HashMap < PathBuf , FileTime > ,
764+ pkg_root : & Path ,
765+ target_root : & Path ,
766+ ) -> CargoResult < ( ) > {
760767 assert ! ( !self . fs_status. up_to_date( ) ) ;
761768
762769 let mut mtimes = HashMap :: new ( ) ;
@@ -840,7 +847,7 @@ impl Fingerprint {
840847 // files for this package itself. If we do find something log a helpful
841848 // message and bail out so we stay stale.
842849 for local in self . local . get_mut ( ) . unwrap ( ) . iter ( ) {
843- if let Some ( file) = local. find_stale_file ( pkg_root, target_root) ? {
850+ if let Some ( file) = local. find_stale_file ( mtime_cache , pkg_root, target_root) ? {
844851 file. log ( ) ;
845852 return Ok ( ( ) ) ;
846853 }
@@ -1014,8 +1021,8 @@ fn calculate<'a, 'cfg>(
10141021
10151022 // After we built the initial `Fingerprint` be sure to update the
10161023 // `fs_status` field of it.
1017- let target_root = target_root ( cx, unit ) ;
1018- fingerprint. check_filesystem ( unit. pkg . root ( ) , & target_root) ?;
1024+ let target_root = target_root ( cx) ;
1025+ fingerprint. check_filesystem ( & mut cx . mtime_cache , unit. pkg . root ( ) , & target_root) ?;
10191026
10201027 let fingerprint = Arc :: new ( fingerprint) ;
10211028 cx. fingerprints . insert ( * unit, Arc :: clone ( & fingerprint) ) ;
@@ -1046,7 +1053,7 @@ fn calculate_normal<'a, 'cfg>(
10461053 // correctly, but otherwise upstream packages like from crates.io or git
10471054 // get bland fingerprints because they don't change without their
10481055 // `PackageId` changing.
1049- let target_root = target_root ( cx, unit ) ;
1056+ let target_root = target_root ( cx) ;
10501057 let local = if use_dep_info ( unit) {
10511058 let dep_info = dep_info_loc ( cx, unit) ;
10521059 let dep_info = dep_info. strip_prefix ( & target_root) . unwrap ( ) . to_path_buf ( ) ;
@@ -1098,13 +1105,10 @@ fn calculate_normal<'a, 'cfg>(
10981105 } )
10991106}
11001107
1101- // We want to use the mtime for files if we're a path source, but if we're a
1102- // git/registry source, then the mtime of files may fluctuate, but they won't
1103- // change so long as the source itself remains constant (which is the
1104- // responsibility of the source)
1108+ /// Whether or not the fingerprint should track the dependencies from the
1109+ /// dep-info file for this unit.
11051110fn use_dep_info ( unit : & Unit < ' _ > ) -> bool {
1106- let path = unit. pkg . summary ( ) . source_id ( ) . is_path ( ) ;
1107- !unit. mode . is_doc ( ) && path
1111+ !unit. mode . is_doc ( )
11081112}
11091113
11101114/// Calculate a fingerprint for an "execute a build script" unit. This is an
@@ -1219,8 +1223,8 @@ fn build_script_local_fingerprints<'a, 'cfg>(
12191223 // package. Remember that the fact that this is an `Option` is a bug, but a
12201224 // longstanding bug, in Cargo. Recent refactorings just made it painfully
12211225 // obvious.
1222- let script_root = cx. files ( ) . build_script_run_dir ( unit) ;
12231226 let pkg_root = unit. pkg . root ( ) . to_path_buf ( ) ;
1227+ let target_dir = target_root ( cx) ;
12241228 let calculate =
12251229 move |deps : & BuildDeps , pkg_fingerprint : Option < & dyn Fn ( ) -> CargoResult < String > > | {
12261230 if deps. rerun_if_changed . is_empty ( ) && deps. rerun_if_env_changed . is_empty ( ) {
@@ -1247,7 +1251,7 @@ fn build_script_local_fingerprints<'a, 'cfg>(
12471251 // Ok so now we're in "new mode" where we can have files listed as
12481252 // dependencies as well as env vars listed as dependencies. Process
12491253 // them all here.
1250- Ok ( Some ( local_fingerprints_deps ( deps, & script_root , & pkg_root) ) )
1254+ Ok ( Some ( local_fingerprints_deps ( deps, & target_dir , & pkg_root) ) )
12511255 } ;
12521256
12531257 // Note that `false` == "not overridden"
@@ -1346,17 +1350,10 @@ pub fn dep_info_loc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> Pa
13461350 . join ( & format ! ( "dep-{}" , filename( cx, unit) ) )
13471351}
13481352
1349- /// Returns an absolute path that the `unit`'s outputs should always be relative
1350- /// to. This `target_root` variable is used to store relative path names in
1351- /// `Fingerprint` instead of absolute pathnames (see module comment).
1352- fn target_root < ' a , ' cfg > ( cx : & mut Context < ' a , ' cfg > , unit : & Unit < ' a > ) -> PathBuf {
1353- if unit. mode . is_run_custom_build ( ) {
1354- cx. files ( ) . build_script_run_dir ( unit)
1355- } else if unit. kind == Kind :: Host {
1356- cx. files ( ) . host_root ( ) . to_path_buf ( )
1357- } else {
1358- cx. files ( ) . target_root ( ) . to_path_buf ( )
1359- }
1353+ /// Returns an absolute path that target directory.
1354+ /// All paths are rewritten to be relative to this.
1355+ fn target_root ( cx : & Context < ' _ , ' _ > ) -> PathBuf {
1356+ cx. bcx . ws . target_dir ( ) . into_path_unlocked ( )
13601357}
13611358
13621359fn compare_old_fingerprint (
@@ -1429,11 +1426,7 @@ pub fn parse_dep_info(
14291426 }
14301427 } )
14311428 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
1432- if paths. is_empty ( ) {
1433- Ok ( None )
1434- } else {
1435- Ok ( Some ( paths) )
1436- }
1429+ Ok ( Some ( paths) )
14371430}
14381431
14391432fn pkg_fingerprint ( bcx : & BuildContext < ' _ , ' _ > , pkg : & Package ) -> CargoResult < String > {
@@ -1446,7 +1439,11 @@ fn pkg_fingerprint(bcx: &BuildContext<'_, '_>, pkg: &Package) -> CargoResult<Str
14461439 source. fingerprint ( pkg)
14471440}
14481441
1449- fn find_stale_file < I > ( reference : & Path , paths : I ) -> Option < StaleFile >
1442+ fn find_stale_file < I > (
1443+ mtime_cache : & mut HashMap < PathBuf , FileTime > ,
1444+ reference : & Path ,
1445+ paths : I ,
1446+ ) -> Option < StaleFile >
14501447where
14511448 I : IntoIterator ,
14521449 I :: Item : AsRef < Path > ,
@@ -1458,9 +1455,15 @@ where
14581455
14591456 for path in paths {
14601457 let path = path. as_ref ( ) ;
1461- let path_mtime = match paths:: mtime ( path) {
1462- Ok ( mtime) => mtime,
1463- Err ( ..) => return Some ( StaleFile :: Missing ( path. to_path_buf ( ) ) ) ,
1458+ let path_mtime = match mtime_cache. entry ( path. to_path_buf ( ) ) {
1459+ Entry :: Occupied ( o) => * o. get ( ) ,
1460+ Entry :: Vacant ( v) => {
1461+ let mtime = match paths:: mtime ( path) {
1462+ Ok ( mtime) => mtime,
1463+ Err ( ..) => return Some ( StaleFile :: Missing ( path. to_path_buf ( ) ) ) ,
1464+ } ;
1465+ * v. insert ( mtime)
1466+ }
14641467 } ;
14651468
14661469 // TODO: fix #5918.
@@ -1547,6 +1550,12 @@ impl DepInfoPathType {
15471550/// The `rustc_cwd` argument is the absolute path to the cwd of the compiler
15481551/// when it was invoked.
15491552///
1553+ /// If the `allow_package` argument is true, then package-relative paths are
1554+ /// included. If it is false, then package-relative paths are skipped and
1555+ /// ignored (typically used for registry or git dependencies where we assume
1556+ /// the source never changes, and we don't want the cost of running `stat` on
1557+ /// all those files).
1558+ ///
15501559/// The serialized Cargo format will contain a list of files, all of which are
15511560/// relative if they're under `root`. or absolute if they're elsewhere.
15521561pub fn translate_dep_info (
@@ -1555,26 +1564,35 @@ pub fn translate_dep_info(
15551564 rustc_cwd : & Path ,
15561565 pkg_root : & Path ,
15571566 target_root : & Path ,
1567+ allow_package : bool ,
15581568) -> CargoResult < ( ) > {
15591569 let target = parse_rustc_dep_info ( rustc_dep_info) ?;
15601570 let deps = & target
15611571 . get ( 0 )
15621572 . ok_or_else ( || internal ( "malformed dep-info format, no targets" . to_string ( ) ) ) ?
15631573 . 1 ;
15641574
1575+ let target_root = target_root. canonicalize ( ) ?;
1576+ let pkg_root = pkg_root. canonicalize ( ) ?;
15651577 let mut new_contents = Vec :: new ( ) ;
15661578 for file in deps {
1567- let file = rustc_cwd. join ( file) ;
1568- let ( ty, path) = if let Ok ( stripped) = file. strip_prefix ( pkg_root) {
1569- ( DepInfoPathType :: PackageRootRelative , stripped)
1570- } else if let Ok ( stripped) = file. strip_prefix ( target_root) {
1579+ // The path may be absolute or relative, canonical or not. Make sure
1580+ // it is canonicalized so we are comparing the same kinds of paths.
1581+ let canon_file = rustc_cwd. join ( file) . canonicalize ( ) ?;
1582+ let abs_file = rustc_cwd. join ( file) ;
1583+
1584+ let ( ty, path) = if let Ok ( stripped) = canon_file. strip_prefix ( & target_root) {
15711585 ( DepInfoPathType :: TargetRootRelative , stripped)
1586+ } else if let Ok ( stripped) = canon_file. strip_prefix ( & pkg_root) {
1587+ if !allow_package {
1588+ continue ;
1589+ }
1590+ ( DepInfoPathType :: PackageRootRelative , stripped)
15721591 } else {
15731592 // It's definitely not target root relative, but this is an absolute path (since it was
15741593 // joined to rustc_cwd) and as such re-joining it later to the target root will have no
15751594 // effect.
1576- assert ! ( file. is_absolute( ) , "{:?} is absolute" , file) ;
1577- ( DepInfoPathType :: TargetRootRelative , & * file)
1595+ ( DepInfoPathType :: TargetRootRelative , & * abs_file)
15781596 } ;
15791597 new_contents. push ( ty as u8 ) ;
15801598 new_contents. extend ( util:: path2bytes ( path) ?) ;
0 commit comments