new: embed generation metadata (generator & xml)

This commit is contained in:
Wataru Otsubo 2025-02-07 17:49:34 +09:00
parent d41bc7c1b0
commit b0ceb39278
7 changed files with 76 additions and 0 deletions

18
build.rs Normal file
View file

@ -0,0 +1,18 @@
use vergen_gitcl::{BuildBuilder, Emitter, GitclBuilder};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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(())
}

View file

@ -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(),
})
}
}

View file

@ -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<HashMap<path::PathBuf, syn::File>, 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))
},

View file

@ -39,6 +39,7 @@
pub mod converter;
pub mod generator;
pub mod io;
pub mod meta;
mod parser;
mod type_traits;
pub mod types;

View file

@ -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) {

9
src/meta.rs Normal file
View file

@ -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");

View file

@ -10,6 +10,7 @@ pub struct Module {
pub desc: Option<String>,
pub elements_bitstring: Vec<BitString>,
pub elements_other: Vec<ModuleBlockElements>,
pub xmlhash: [u8; 32],
}
#[derive(Debug)]