Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

touch some files when we use them #6477

Merged
merged 5 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/cargo/core/compiler/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs;
use std::hash::{self, Hasher};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use filetime::FileTime;
use log::{debug, info};
Expand Down Expand Up @@ -88,6 +89,7 @@ pub fn prepare_target<'a, 'cfg>(

let root = cx.files().out_dir(unit);
let missing_outputs = {
let t = FileTime::from_system_time(SystemTime::now());
if unit.mode.is_doc() {
!root
.join(unit.target.crate_name())
Expand All @@ -98,8 +100,15 @@ pub fn prepare_target<'a, 'cfg>(
.outputs(unit)?
.iter()
.filter(|output| output.flavor != FileFlavor::DebugInfo)
.find(|output| !output.path.exists())
{
.find(|output| {
if output.path.exists() {
// update the mtime so other cleaners know we used it
let _ = filetime::set_file_times(&output.path, t, t);
false
} else {
true
}
}) {
None => false,
Some(output) => {
info!("missing output path {:?}", output.path);
Expand Down Expand Up @@ -681,6 +690,11 @@ pub fn dep_info_loc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> Pa

fn compare_old_fingerprint(loc: &Path, new_fingerprint: &Fingerprint) -> CargoResult<()> {
let old_fingerprint_short = paths::read(loc)?;

// update the mtime so other cleaners know we used it
let t = FileTime::from_system_time(SystemTime::now());
filetime::set_file_times(loc, t, t)?;

let new_hash = new_fingerprint.hash();

if util::to_hex(new_hash) == old_fingerprint_short {
Expand Down
172 changes: 172 additions & 0 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fs::{self, File, OpenOptions};
use std::io::prelude::*;
use std::net::TcpListener;
use std::path::PathBuf;
use std::thread;
use std::time::SystemTime;

use crate::support::paths::CargoPathExt;
use crate::support::registry::Package;
Expand Down Expand Up @@ -1178,6 +1180,176 @@ fn changing_rustflags_is_cached() {
.run();
}

fn simple_deps_cleaner(mut dir: PathBuf, timestamp: filetime::FileTime) {
// Cargo is experimenting with letting outside projects develop some
// limited forms of GC for target_dir. This is one of the forms.
// Specifically, Cargo is updating the mtime of files in
// target/profile/deps each time it uses the file.
// So a cleaner can remove files older then a time stamp without
// effecting any builds that happened since that time stamp.
let mut cleand = false;
dir.push("deps");
for dep in fs::read_dir(&dir).unwrap() {
let dep = dep.unwrap();
if filetime::FileTime::from_last_modification_time(&dep.metadata().unwrap()) <= timestamp {
fs::remove_file(dep.path()).unwrap();
println!("remove: {:?}", dep.path());
cleand = true;
}
}
assert!(
cleand,
"called simple_deps_cleaner, but there was nothing to remove"
);
}

#[test]
fn simple_deps_cleaner_dose_not_rebuild() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"

[dependencies]
bar = { path = "bar" }
"#,
)
.file("src/lib.rs", "")
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("bar/src/lib.rs", "")
.build();

p.cargo("build").run();
p.cargo("build")
.env("RUSTFLAGS", "-C target-cpu=native")
.with_stderr(
"\
[COMPILING] bar v0.0.1 ([..])
[COMPILING] foo v0.0.1 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();
if is_coarse_mtime() {
sleep_ms(1000);
}
let timestamp = filetime::FileTime::from_system_time(SystemTime::now());
if is_coarse_mtime() {
sleep_ms(1000);
}
// This dose not make new files, but it dose update the mtime.
p.cargo("build")
.env("RUSTFLAGS", "-C target-cpu=native")
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
.run();
simple_deps_cleaner(p.target_debug_dir(), timestamp);
// This should not recompile!
p.cargo("build")
.env("RUSTFLAGS", "-C target-cpu=native")
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
.run();
// But this should be cleaned and so need a rebuild
p.cargo("build")
.with_stderr(
"\
[COMPILING] bar v0.0.1 ([..])
[COMPILING] foo v0.0.1 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();
}

fn fingerprint_cleaner(mut dir: PathBuf, timestamp: filetime::FileTime) {
// Cargo is experimenting with letting outside projects develop some
// limited forms of GC for target_dir. This is one of the forms.
// Specifically, Cargo is updating the mtime of a file in
// target/profile/.fingerprint each time it uses the fingerprint.
// So a cleaner can remove files associated with a fingerprint
// if all the files in the fingerprint's folder are older then a time stamp without
// effecting any builds that happened since that time stamp.
let mut cleand = false;
dir.push(".fingerprint");
for fing in fs::read_dir(&dir).unwrap() {
let fing = fing.unwrap();

if fs::read_dir(fing.path()).unwrap().all(|f| {
filetime::FileTime::from_last_modification_time(&f.unwrap().metadata().unwrap())
<= timestamp
}) {
fs::remove_dir_all(fing.path()).unwrap();
println!("remove: {:?}", fing.path());
// a real cleaner would remove the big files in deps and build as well
// but fingerprint is sufficient for our tests
cleand = true;
} else {
}
}
assert!(
cleand,
"called fingerprint_cleaner, but there was nothing to remove"
);
}

#[test]
fn fingerprint_cleaner_dose_not_rebuild() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"

[dependencies]
bar = { path = "bar" }
"#,
)
.file("src/lib.rs", "")
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("bar/src/lib.rs", "")
.build();

p.cargo("build").run();
p.cargo("build")
.env("RUSTFLAGS", "-C target-cpu=native")
.with_stderr(
"\
[COMPILING] bar v0.0.1 ([..])
[COMPILING] foo v0.0.1 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();
if is_coarse_mtime() {
sleep_ms(1000);
}
let timestamp = filetime::FileTime::from_system_time(SystemTime::now());
if is_coarse_mtime() {
sleep_ms(1000);
}
// This dose not make new files, but it dose update the mtime.
p.cargo("build")
.env("RUSTFLAGS", "-C target-cpu=native")
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
.run();
fingerprint_cleaner(p.target_debug_dir(), timestamp);
// This should not recompile!
p.cargo("build")
.env("RUSTFLAGS", "-C target-cpu=native")
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
.run();
// But this should be cleaned and so need a rebuild
p.cargo("build")
.with_stderr(
"\
[COMPILING] bar v0.0.1 ([..])
[COMPILING] foo v0.0.1 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();
}

#[test]
fn reuse_panic_build_dep_test() {
let p = project()
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,9 @@ pub fn sleep_ms(ms: u64) {

/// Returns true if the local filesystem has low-resolution mtimes.
pub fn is_coarse_mtime() -> bool {
// If the filetime crate is being used to emulate HFS then
// return true, without looking at the actual hardware.
cfg!(emulate_second_only_system) ||
// This should actually be a test that $CARGO_TARGET_DIR is on an HFS
// filesystem, (or any filesystem with low-resolution mtimes). However,
// that's tricky to detect, so for now just deal with CI.
Expand Down