diff --git a/utralib/build.rs b/utralib/build.rs index 22de1c41b..cfdd0850e 100644 --- a/utralib/build.rs +++ b/utralib/build.rs @@ -1,7 +1,7 @@ -use std::path::PathBuf; use std::env; use std::fs::OpenOptions; -use std::io::Write; +use std::io::{Read, Write}; +use std::path::PathBuf; fn out_dir() -> PathBuf { PathBuf::from(env::var_os("OUT_DIR").unwrap()) @@ -70,50 +70,73 @@ fn main() { allow_single_target_feature!("precursor", "hosted", "renode"); - #[cfg(feature="precursor")] - allow_single_gitrev_feature!("precursor-c809403", "precursor-c809403-perflib", "precursor-2753c12-dvt", "precursor-a0912d6"); + #[cfg(feature = "precursor")] + allow_single_gitrev_feature!( + "precursor-c809403", + "precursor-c809403-perflib", + "precursor-2753c12-dvt", + "precursor-a0912d6" + ); // ----- select an SVD file based on a specific revision ----- - #[cfg(feature="precursor-c809403")] + #[cfg(feature = "precursor-c809403")] let svd_filename = "precursor/soc-c809403.svd"; - #[cfg(feature="precursor-c809403")] + #[cfg(feature = "precursor-c809403")] let generated_filename = "src/generated/precursor_c809403.rs"; - #[cfg(feature="precursor-a0912d6")] + #[cfg(feature = "precursor-a0912d6")] let svd_filename = "precursor/soc-a0912d6.svd"; - #[cfg(feature="precursor-a0912d6")] + #[cfg(feature = "precursor-a0912d6")] let generated_filename = "src/generated/precursor_a0912d6.rs"; - #[cfg(feature="precursor-c809403-perflib")] + #[cfg(feature = "precursor-c809403-perflib")] let svd_filename = "precursor/soc-perf-c809403.svd"; - #[cfg(feature="precursor-c809403-perflib")] + #[cfg(feature = "precursor-c809403-perflib")] let generated_filename = "src/generated/precursor_perf_c809403.rs"; - #[cfg(feature="renode")] + #[cfg(feature = "renode")] let svd_filename = "renode/renode.svd"; - #[cfg(feature="renode")] + #[cfg(feature = "renode")] let generated_filename = "src/generated/renode.rs"; - #[cfg(feature="precursor-2753c12-dvt")] + #[cfg(feature = "precursor-2753c12-dvt")] let svd_filename = "precursor/soc-dvt-2753c12.svd"; - #[cfg(feature="precursor-2753c12-dvt")] + #[cfg(feature = "precursor-2753c12-dvt")] let generated_filename = "src/generated/precursor_dvt_2753c12.rs"; // ----- control file generation and rebuild sequence ----- // 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. - #[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); - 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 mut dest_file = std::fs::File::create(generated_filename).expect("couldn't open dest file"); - svd2utra::generate(src_file, &mut dest_file).unwrap(); + let mut dest_vec = vec![]; + 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 ----- // pass the computed SVD filename back to the build system, so that we can pass this @@ -132,7 +155,7 @@ fn main() { .unwrap(); write!(svd_file, "utralib/{}", svd_filename).unwrap(); } - #[cfg(feature="hosted")] + #[cfg(feature = "hosted")] { let svd_path = out_dir().join("../../SVD_PATH"); let mut svd_file = OpenOptions::new()