Merge pull request #250 from betrusted-io/utralib-no-rebuilds

Fix rebuild logic on utralib
This commit is contained in:
bunnie 2022-10-20 15:58:50 +08:00 committed by GitHub
commit c577798f81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 52 deletions

View file

@ -1,7 +1,7 @@
use std::path::PathBuf;
use std::env; use std::env;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::{Read, Write};
use std::path::PathBuf;
fn out_dir() -> PathBuf { fn out_dir() -> PathBuf {
PathBuf::from(env::var_os("OUT_DIR").unwrap()) PathBuf::from(env::var_os("OUT_DIR").unwrap())
@ -71,7 +71,12 @@ fn main() {
allow_single_target_feature!("precursor", "hosted", "renode"); allow_single_target_feature!("precursor", "hosted", "renode");
#[cfg(feature = "precursor")] #[cfg(feature = "precursor")]
allow_single_gitrev_feature!("precursor-c809403", "precursor-c809403-perflib", "precursor-2753c12-dvt", "precursor-a0912d6"); allow_single_gitrev_feature!(
"precursor-c809403",
"precursor-c809403-perflib",
"precursor-2753c12-dvt",
"precursor-a0912d6"
);
// ----- select an SVD file based on a specific revision ----- // ----- select an SVD file based on a specific revision -----
#[cfg(feature = "precursor-c809403")] #[cfg(feature = "precursor-c809403")]
@ -102,18 +107,39 @@ fn main() {
// ----- control file generation and rebuild sequence ----- // ----- control file generation and rebuild sequence -----
// check and see if the configuration has changed since the last build. This should be // check and see if the configuration has changed since the last build. This should be
// passed by the build system (e.g. xtask) if the feature is used. // passed by the build system (e.g. xtask) if the feature is used.
//
// Debug this using:
// $env:CARGO_LOG="cargo::core::compiler::fingerprint=info"
#[cfg(not(feature = "hosted"))] #[cfg(not(feature = "hosted"))]
{ {
let last_config = out_dir().join("../../LAST_CONFIG");
if last_config.exists() {
println!("cargo:rerun-if-changed={}", last_config.canonicalize().unwrap().display());
}
let svd_file_path = std::path::Path::new(&svd_filename); let svd_file_path = std::path::Path::new(&svd_filename);
println!("cargo:rerun-if-changed={}", svd_file_path.canonicalize().unwrap().display()); println!(
"cargo:rerun-if-changed={}",
svd_file_path.canonicalize().unwrap().display()
);
// Regenerate the utra file in RAM.
let src_file = std::fs::File::open(svd_filename).expect("couldn't open src file"); let src_file = std::fs::File::open(svd_filename).expect("couldn't open src file");
let mut dest_file = std::fs::File::create(generated_filename).expect("couldn't open dest file"); let mut dest_vec = vec![];
svd2utra::generate(src_file, &mut dest_file).unwrap(); svd2utra::generate(src_file, &mut dest_vec).unwrap();
// If the file exists, check to see if it is different from what we just generated.
// If not, skip writing the new file.
// If the file doesn't exist, or if it's different, write out a new utra file.
let should_write = if let Ok(mut existing_file) = std::fs::File::open(generated_filename) {
let mut existing_file_contents = vec![];
existing_file.read_to_end(&mut existing_file_contents).expect("couldn't read existing utra generated file");
existing_file_contents != dest_vec
} else {
true
};
if should_write {
let mut dest_file =
std::fs::File::create(generated_filename).expect("couldn't open dest file");
dest_file
.write_all(&dest_vec)
.expect("couldn't write contents to utra file");
}
// ----- feedback SVD path to build framework ----- // ----- feedback SVD path to build framework -----
// pass the computed SVD filename back to the build system, so that we can pass this // pass the computed SVD filename back to the build system, so that we can pass this

View file

@ -433,35 +433,6 @@ impl Builder {
return Err("Target unknown: please check your UTRA target".into()); return Err("Target unknown: please check your UTRA target".into());
}; };
// LAST_CONFIG tracks the last SVD configuration. It's used by utralib to track if it
// should rebuild itself based on a change in SVD configs. Note that for some reason
// it takes two consecutive builds with the same SVD config before the build system
// figures out that it doesn't need to rebuild everything. After then, it behaves as expected.
let last_config = format!("target/{}/{}/build/LAST_CONFIG", TARGET_TRIPLE, self.stream.to_str());
std::fs::create_dir_all(format!("target/{}/{}/build/", TARGET_TRIPLE, self.stream.to_str())).unwrap();
let changed = match OpenOptions::new()
.read(true)
.open(&last_config) {
Ok(mut file) => {
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
if contents != self.utra_target {
true
} else {
false
}
}
_ => true
};
if changed {
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&last_config).unwrap();
write!(file, "{}", self.utra_target).unwrap();
}
// ------ build the services & apps ------ // ------ build the services & apps ------
let mut app_names = Vec::<String>::new(); let mut app_names = Vec::<String>::new();
for app in self.apps.iter() { for app in self.apps.iter() {