Skip to content

Commit

Permalink
chore: split logic around is_newer_than; print debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Jan 20, 2025
1 parent e746699 commit 77b9074
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
6 changes: 3 additions & 3 deletions cargo-near-build/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn copy(from: &Utf8Path, out_dir: &Utf8Path) -> eyre::Result<Utf8PathBuf> {
/// Copy a file to a file destination.
///
/// Does nothing if the destination is the same as the source to avoid truncating the file.
pub fn copy_to_file(from: &Utf8Path, to: &Utf8Path) -> eyre::Result<Utf8PathBuf> {
pub fn copy_to_file(from: &Utf8Path, to: &Utf8Path) -> eyre::Result<()> {
tracing::debug!("Copying file `{}` -> `{}`", from, to,);

if !from.is_file() {
Expand All @@ -40,14 +40,14 @@ pub fn copy_to_file(from: &Utf8Path, to: &Utf8Path) -> eyre::Result<Utf8PathBuf>
from,
to,
);
return Ok(to.to_path_buf());
return Ok(());
}
}
if from != to {
std::fs::copy(from, to)
.wrap_err_with(|| format!("failed to copy `{}` to `{}`", from, to))?;
}
Ok(to.to_path_buf())
Ok(())
}

/// Create the directory if it doesn't exist, and return the absolute path to it.
Expand Down
58 changes: 42 additions & 16 deletions cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,23 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
)?;

wasm_artifact.path = {
let target_path = out_dir.join(wasm_artifact.path.file_name().expect("has filename"));
if std::fs::metadata(&wasm_artifact.path)
.and_then(|m| m.modified())
.unwrap_or_else(|_| std::time::SystemTime::now())
> std::fs::metadata(&target_path)
.and_then(|m| m.modified())
.unwrap_or_else(|_| std::time::SystemTime::UNIX_EPOCH)
{
let prev_artifact_path = wasm_artifact.path;
let target_path = out_dir.join(prev_artifact_path.file_name().expect("has filename"));

// target file does not yet exist `!target_path.is_file()` condition is implied by
// `is_newer_than(...)` predicate, but it's redundantly added here for readability 🙏
if !target_path.is_file() || is_newer_than(&prev_artifact_path, &target_path) {
let (from_path, _maybe_tmpfile) =
maybe_wasm_opt_step(&wasm_artifact.path, args.no_wasmopt)?;
crate::fs::copy_to_file(&from_path, &target_path)?
maybe_wasm_opt_step(&prev_artifact_path, args.no_wasmopt)?;
crate::fs::copy_to_file(&from_path, &target_path)?;
} else {
target_path
println!();
pretty_print::step(
"Skipped running wasm-opt as final target exists and is newer than wasm produced by cargo",
);
println!();
}
target_path
};

wasm_artifact.builder_version_info = Some(builder_version_info);
Expand Down Expand Up @@ -211,6 +214,34 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
Ok(wasm_artifact)
}

fn is_newer_than(prev: &Utf8PathBuf, next: &Utf8PathBuf) -> bool {
// (1) if `next` does not yet exist, `metadata_of_prev.modified()` will be greater than
// `std::time::SystemTime::UNIX_EPOCH`;
// (2) if `m.modified()` isn't available on current platform, the predicate will always
// return true
// (3) non-monotonic nature of `std::time::SystemTime` won't be a problem:
// if the next_time and prev_time are too close in time so that next_time registers
// before prev_time, it will only affect that skipping build won't occur, but doesn't
// affect correctnes, as the build will run next time due to prev_time > next_time
let prev_time = std::fs::metadata(prev)
.and_then(|m| m.modified())
.unwrap_or_else(|_| std::time::SystemTime::now());
let next_time = std::fs::metadata(next)
.and_then(|m| m.modified())
.unwrap_or(std::time::SystemTime::UNIX_EPOCH);
let debug_msg = format!(
"{:?} = {:?}\n\
{:?} = {:?}",
prev, prev_time, next, next_time
);
println!();
println!(
"Modification times of:\n{}",
pretty_print::indent_payload(&debug_msg)
);
prev_time > next_time
}

fn maybe_wasm_opt_step(
input_path: &Utf8PathBuf,
no_wasmopt: bool,
Expand All @@ -225,11 +256,6 @@ fn maybe_wasm_opt_step(
"Running an optimize for size post-step with wasm-opt...",
|| {
let start = std::time::Instant::now();
println!(
"{} -> {}",
format!("{}", input_path).cyan(),
format!("{}", opt_destination.path().to_string_lossy()).cyan()
);
wasm_opt::OptimizationOptions::new_optimize_for_size()
.run(input_path, opt_destination.path())?;
pretty_print::duration_millis(start, "wasm-opt -O");
Expand Down
2 changes: 1 addition & 1 deletion cargo-near-build/src/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn duration_millis(start: Instant, activity: &str) {
let duration = std::time::Duration::from_millis(start.elapsed().as_millis() as u64);
println!(
" {} {} in {}",
"Finished".bold().cyan(),
"Finished".bold().truecolor(90, 90, 90),
activity,
humantime::format_duration(duration)
);
Expand Down

0 comments on commit 77b9074

Please sign in to comment.