From 2eccdc13623a66e988c714baa724910b52bd4901 Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 18 Jul 2024 00:10:10 +0800 Subject: [PATCH 1/3] add `rerun-if-changed` args Signed-off-by: HouXiaoxuan --- neptune/build.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/neptune/build.rs b/neptune/build.rs index 79fb09248..ad339afd4 100644 --- a/neptune/build.rs +++ b/neptune/build.rs @@ -45,7 +45,7 @@ fn npm_build_agent_ui() { } } -fn parse_args_to_rustc(dst: &Path) { +fn parse_link_args_to_rustc(dst: &Path) { // ** `cargo:rustc-*` format is used to pass information to the cargo build system // parse to `rustc` to look for dynamic library, used in running @@ -190,9 +190,13 @@ fn main() { panic!("Please run `git submodule update --init --recursive` to get the submodule"); } + /* set return if changed to reduce build times, ref: `https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection`` */ + println!("cargo:rerun-if-changed=mega"); + println!("cargo:rerun-if-changed=src"); + copy_mega_apps(); let dst = build(); - parse_args_to_rustc(&dst); + parse_link_args_to_rustc(&dst); copy_lib_to_target(&dst); // optional, didn't work in all cases } From 332c9b45356027f123b1ea4680db826a26f9dadb Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 18 Jul 2024 01:28:46 +0800 Subject: [PATCH 2/3] update ztm Signed-off-by: HouXiaoxuan --- neptune/libs/ztm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neptune/libs/ztm b/neptune/libs/ztm index f3ba7aa56..2e0ac3ed1 160000 --- a/neptune/libs/ztm +++ b/neptune/libs/ztm @@ -1 +1 @@ -Subproject commit f3ba7aa56814c5e23b12bb3f7d951b86977fd260 +Subproject commit 2e0ac3ed15b8374aeb8227b6a65a83dc6f2a97dc From 42330762efc27d98aaf6918923857abaa572d8df Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 18 Jul 2024 01:29:30 +0800 Subject: [PATCH 3/3] fix: didn't rebuild after update submodule Signed-off-by: HouXiaoxuan --- neptune/build.rs | 49 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/neptune/build.rs b/neptune/build.rs index ad339afd4..d0a686095 100644 --- a/neptune/build.rs +++ b/neptune/build.rs @@ -2,6 +2,8 @@ use core::panic; use std::{ env::current_dir, fs, + hash::Hasher, + io::Read, path::{Path, PathBuf}, }; @@ -16,7 +18,10 @@ fn copy_mega_apps() { fs::remove_dir_all(dst).expect("failed to remove origin agent/apps"); } // std::fs::copy(src, dst).expect("failed to copy mega to agent/apps"); - copy_dir_all(src, dst).expect("failed to copy mega to agent/apps"); + copy_dir_all(src, dst).unwrap_or_else(|e| { + fs::remove_dir(dst).expect("failed to remove agent/apps"); + panic!("failed to copy mega to agent/apps: {}", e); + }); } /// use npm to build agent ui in `libs/ztm/agent/gui` @@ -183,16 +188,31 @@ fn build() -> PathBuf { config.build() } +/// file/path list to detect change +/// list files because cargo didn't dupport exclude path +/// we didn't want to detect changes such as node_modules +fn return_if_change() { + /* set return if changed to reduce build times, ref: `https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection`` */ + println!("cargo:rerun-if-changed=mega"); + println!("cargo:rerun-if-changed=src"); + + println!("cargo:rerun-if-changed=libs/ztm/agent"); + println!("cargo:rerun-if-changed=libs/ztm/hub"); + println!("cargo:rerun-if-changed=libs/ztm/ca"); + + println!("cargo:rerun-if-changed=libs/ztm/pipy/src"); + println!("cargo:rerun-if-changed=libs/ztm/pipy/CMakeLists.txt"); + println!("cargo:rerun-if-changed=libs/ztm/pipy/include"); + println!("cargo:rerun-if-changed=libs/ztm/pipy/deps"); +} + fn main() { // check submodule exists let check_file = Path::new("libs/ztm/pipy/CMakeLists.txt"); if !check_file.exists() { panic!("Please run `git submodule update --init --recursive` to get the submodule"); } - - /* set return if changed to reduce build times, ref: `https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection`` */ - println!("cargo:rerun-if-changed=mega"); - println!("cargo:rerun-if-changed=src"); + return_if_change(); copy_mega_apps(); let dst = build(); @@ -201,6 +221,14 @@ fn main() { } fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Result<()> { + fn hash_file(file: PathBuf) -> std::io::Result { + let mut file = fs::File::open(file)?; + let mut buf = Vec::new(); + file.read_to_end(&mut buf)?; + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + std::hash::Hash::hash(&buf, &mut hasher); + Ok(hasher.finish()) + } fs::create_dir_all(&dst)?; for entry in fs::read_dir(src)? { let entry = entry?; @@ -208,7 +236,16 @@ fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Result if ty.is_dir() { copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; } else { - fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; + // check content hash, skip if same, so didn't change timestamp of file, in case cargo rebuild + let dts_file = dst.as_ref().join(entry.file_name()); + if dts_file.exists() { + let src_hash = hash_file(entry.path())?; + let dst_hash = hash_file(dts_file.clone())?; + if src_hash == dst_hash { + continue; + } + } + fs::copy(entry.path(), dts_file)?; } } Ok(())