diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..2e8f364 --- /dev/null +++ b/build.rs @@ -0,0 +1,18 @@ +use vergen_gitcl::{BuildBuilder, Emitter, GitclBuilder}; + +fn main() -> Result<(), Box> { + // Get build metadata + let build = BuildBuilder::default().build_timestamp(true).build()?; + let gitcl = GitclBuilder::default() + .commit_date(true) + .describe(false, true, None) + .commit_timestamp(true) + .sha(false) + .build()?; + Emitter::default() + .add_instructions(&build)? + .add_instructions(&gitcl)? + .emit()?; + + Ok(()) +} diff --git a/src/converter.rs b/src/converter.rs index 426ba4d..3ccc699 100644 --- a/src/converter.rs +++ b/src/converter.rs @@ -7,6 +7,7 @@ use std::{num, str}; use roxmltree::{Node, TextPos}; +use sha2::{Digest, Sha256}; use thiserror::Error; use crate::parser::{ParseCustomBool, ParseEnumError, ParsePrefixedU32, ParsePrefixedU32Error}; @@ -210,6 +211,7 @@ impl Module { desc: node.attribute("desc").map(str::to_string), elements_bitstring: child_bitstrings, elements_other: child_other, + xmlhash: Sha256::digest(node.document().input_text()).into(), }) } } diff --git a/src/generator.rs b/src/generator.rs index a4c6d95..7e9c2bc 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -11,9 +11,14 @@ use std::{ }; use crate::{ + meta::{ + GENERATOR_BUILD_TIMESTAMP, GENERATOR_GIT_COMMIT_TIMESTAMP, GENERATOR_GIT_DESCRIBE, + GENERATOR_GIT_SHA, + }, type_traits::GetName, types::{Block, Module, ModuleBlockElements, Register}, }; +use chrono::Local; use heck::{ToSnakeCase, ToUpperCamelCase}; use itertools::Itertools; use quote::quote; @@ -123,11 +128,50 @@ mod util { impl Module { pub fn generate_code(self) -> Result, CodeGenError> { + let build_metadata = format!( + " +# Build metadata + +- timestamp: {} + +## CSR XML + +- sha256: {} +- git describe: TODO (after building step is fixed) +- git commit timestamp: TODO +- git SHA: TODO + +## Generator + +- build timestamp: {} +- git describe: {} +- git commit timestamp: {} +- git SHA: {} +", + Local::now().to_rfc3339_opts(chrono::SecondsFormat::Nanos, false), + hex::encode(self.xmlhash), + GENERATOR_BUILD_TIMESTAMP, + GENERATOR_GIT_DESCRIBE, + GENERATOR_GIT_COMMIT_TIMESTAMP, + GENERATOR_GIT_SHA, + ); let files = self.generate_register_interface(None, None, HashMap::new())?; Ok(files .into_iter() .map( |(path, tokens)| -> Result<(PathBuf, syn::File), syn::Error> { + let tokens = if path + .file_name() + .is_some_and(|file| file == "register_interface.rs") + { + quote! { + #![doc = #build_metadata] + + #tokens + } + } else { + tokens + }; let file: syn::File = syn::parse2(tokens)?; Ok((path, file)) }, diff --git a/src/lib.rs b/src/lib.rs index 4129c60..8430a7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ pub mod converter; pub mod generator; pub mod io; +pub mod meta; mod parser; mod type_traits; pub mod types; diff --git a/src/main.rs b/src/main.rs index a3a63bf..41b0963 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ fn main() -> Result<()> { for (path, code) in &files { log::debug!("path: {:?}", path); log::debug!("{}", prettyplease::unparse(code)); + log::trace!("{}", prettyplease::unparse(code)); } } if log::log_enabled!(log::Level::Info) { diff --git a/src/meta.rs b/src/meta.rs new file mode 100644 index 0000000..c9d5d66 --- /dev/null +++ b/src/meta.rs @@ -0,0 +1,9 @@ +pub(crate) const GENERATOR_BUILD_TIMESTAMP: &str = + env!("VERGEN_BUILD_TIMESTAMP", "Failed to get build timestamp"); +pub(crate) const GENERATOR_GIT_DESCRIBE: &str = + env!("VERGEN_GIT_DESCRIBE", "Failed to get git describe"); +pub(crate) const GENERATOR_GIT_COMMIT_TIMESTAMP: &str = env!( + "VERGEN_GIT_COMMIT_TIMESTAMP", + "Failed to get git commit timestamp" +); +pub(crate) const GENERATOR_GIT_SHA: &str = env!("VERGEN_GIT_SHA", "Failed to get git sha"); diff --git a/src/types.rs b/src/types.rs index 65cc783..e074786 100644 --- a/src/types.rs +++ b/src/types.rs @@ -10,6 +10,7 @@ pub struct Module { pub desc: Option, pub elements_bitstring: Vec, pub elements_other: Vec, + pub xmlhash: [u8; 32], } #[derive(Debug)]