Skip to content

Commit

Permalink
Merge pull request #238 from kinode-dao/hf/build-fix-up-to-date-detec…
Browse files Browse the repository at this point in the history
…tion

build: fix up to date detection
  • Loading branch information
nick1udwig authored Sep 16, 2024
2 parents 1d8ac77 + 90ae3ea commit 32e9d6a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ jobs:
rustup update
cargo install wasm-tools
rustup install nightly
rustup target add wasm32-wasi
rustup target add wasm32-wasi --toolchain nightly
rustup target add wasm32-wasip1
rustup target add wasm32-wasip1 --toolchain nightly
cargo install cargo-wasi
#- name: Get latest release from foundry-rs/foundry
# id: get-latest-foundry-release
Expand Down
99 changes: 74 additions & 25 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,78 @@ fn get_file_modified_time(file_path: &Path) -> Result<SystemTime> {
Ok(metadata.modified()?)
}

#[instrument(level = "trace", skip_all)]
fn get_cargo_package_path(package: &cargo_metadata::Package) -> Result<PathBuf> {
match package.manifest_path.parent().map(|p| p.as_std_path().to_path_buf()) {
Some(p) => Ok(p),
None => Err(eyre!("Cargo manifest path {} has no parent", package.manifest_path)),
}
}

#[instrument(level = "trace", skip_all)]
fn is_up_to_date(
build_with_features_path: &Path,
features: &str,
package_dir: &Path,
) -> Result<bool> {
let old_features = fs::read_to_string(&build_with_features_path).ok();
if old_features == Some(features.to_string())
&& package_dir.join("Cargo.lock").exists()
&& package_dir.join("pkg").exists()
&& package_dir.join("pkg").join("api.zip").exists()
&& file_with_extension_exists(&package_dir.join("pkg"), "wasm")
{
let (mut source_time, build_time) = get_most_recent_modified_time(
package_dir,
&HashSet::from(["Cargo.lock", "api.zip"]),
&HashSet::from(["wasm"]),
&HashSet::from(["target"]),
)?;
let Some(build_time) = build_time else {
return Ok(false);
};

// update source to most recent of package_dir
// or package_dir's local deps
let metadata = cargo_metadata::MetadataCommand::new()
.manifest_path(package_dir.join("Cargo.toml"))
.exec()?;
for package in metadata.packages.iter().filter(|p| p.source.is_none()) {
let dep_package_dir = get_cargo_package_path(&package)?;
let (dep_source_time, _) = get_most_recent_modified_time(
&dep_package_dir,
&HashSet::from(["Cargo.lock", "api.zip"]),
&HashSet::from(["wasm"]),
&HashSet::from(["target"]),
)?;
match source_time {
None => source_time = dep_source_time,
Some(ref st) => {
if let Some(ref dst) = dep_source_time {
if dst.duration_since(st.clone()).is_ok() {
// dep has more recent changes than source
// -> update source_time to dep_source_time
source_time = dep_source_time;
}
}
}
}
}

if let Some(source_time) = source_time {
if build_time.duration_since(source_time).is_ok() {
// build_time - source_time >= 0
// -> current build is up-to-date: don't rebuild
info!("Build up-to-date.");
return Ok(true);
}
}
}
Ok(false)
}



#[instrument(level = "trace", skip_all)]
async fn compile_javascript_wasm_process(
process_dir: &Path,
Expand Down Expand Up @@ -1331,31 +1403,8 @@ pub async fn execute(
.with_suggestion(|| "Please re-run targeting a package."));
}
let build_with_features_path = package_dir.join("target").join("build_with_features.txt");
if !force {
let old_features = fs::read_to_string(&build_with_features_path).ok();
if old_features == Some(features.to_string())
&& package_dir.join("Cargo.lock").exists()
&& package_dir.join("pkg").exists()
&& package_dir.join("pkg").join("api.zip").exists()
&& file_with_extension_exists(&package_dir.join("pkg"), "wasm")
{
let (source_time, build_time) = get_most_recent_modified_time(
package_dir,
&HashSet::from(["Cargo.lock", "api.zip"]),
&HashSet::from(["wasm"]),
&HashSet::from(["target"]),
)?;
if let Some(source_time) = source_time {
if let Some(build_time) = build_time {
if build_time.duration_since(source_time).is_ok() {
// build_time - source_time >= 0
// -> current build is up-to-date: don't rebuild
info!("Build up-to-date.");
return Ok(());
}
}
}
}
if !force && is_up_to_date(&build_with_features_path, features, package_dir)? {
return Ok(());
}
fs::create_dir_all(package_dir.join("target"))?;
fs::write(&build_with_features_path, features)?;
Expand Down

0 comments on commit 32e9d6a

Please sign in to comment.