From f4058ec4cf64e94da8dab621bd06b654f5468454 Mon Sep 17 00:00:00 2001 From: Andreas Wallner <20979738+andreasWallnerIFX@users.noreply.github.com> Date: Thu, 13 Jun 2024 11:46:40 +0200 Subject: [PATCH] Add version information to generated crates --- Cargo.lock | 3 +++ Cargo.toml | 2 +- src/lib.rs | 3 +++ src/rust_gen.rs | 34 ++++++++++++++++++++++++++++++++-- src/rust_gen/ir.rs | 1 + src/rust_gen/xml2ir.rs | 1 + templates/rust/Cargo_toml.tera | 2 ++ templates/rust/aurix_core.tera | 2 ++ templates/rust/common.tera | 2 ++ templates/rust/lib.tera | 2 ++ templates/rust/peri_mod.tera | 2 ++ templates/rust/reg_name.tera | 2 ++ templates/rust/tracing.tera | 2 ++ 13 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d39d057..fbde8f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,7 +143,9 @@ checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", + "wasm-bindgen", "windows-targets", ] @@ -881,6 +883,7 @@ name = "svd2pac" version = "0.1.0" dependencies = [ "anyhow", + "chrono", "clap", "convert_case", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index d58884e..0ab437e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ serde_json = {version = "1.0.96", features = ["preserve_order"]} serde = { version = "1.0.160", features = ["derive","rc"] } syn = "2.0.33" linked-hash-map = {version="0.5",features =["serde_impl"]} - +chrono = "0.4" [dev-dependencies] similar = "2.2" diff --git a/src/lib.rs b/src/lib.rs index e4e2c48..de04f7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -435,6 +435,8 @@ use std::ffi::OsString; use std::fs; use std::path::PathBuf; +const VERSION: &str = env!("CARGO_PKG_VERSION"); + #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)] pub enum SvdValidationLevel { Disabled, @@ -534,6 +536,7 @@ where tracing: args.tracing, package_name: args.package_name, license_file: args.license_file, + svd2pac_version: VERSION.to_owned(), }, ) { error!("Failed to generate code with err {}", err); diff --git a/src/rust_gen.rs b/src/rust_gen.rs index b0e882a..d7f91e0 100644 --- a/src/rust_gen.rs +++ b/src/rust_gen.rs @@ -319,6 +319,7 @@ pub struct GenPkgSettings { pub tracing: bool, pub package_name: Option, pub license_file: Option, + pub svd2pac_version: String, } fn precompile_tera(tera: &mut Tera) { @@ -368,6 +369,9 @@ fn generate_tracing_module( let lib_path = destination_folder.join("src/tracing.rs"); execute_template(tera, "tracing.tera", context, &lib_path) .context("Failed generation of tracing.rs")?; + + let svd2pac_version = context.get("svd2pac_version").unwrap().as_str(); + let now = context.get("now").unwrap().as_str(); // reg_name module // // # Issue @@ -405,6 +409,8 @@ fn generate_tracing_module( let lib_path = destination_folder.join("src/reg_name.rs"); context.insert("register_addresses", &ir.register_addresses); context.insert("ir", &ir); + context.insert("svd2pac_version", &svd2pac_version); + context.insert("now", &now); execute_template(tera, "reg_name.tera", &context, &lib_path) .context("Failed generation of reg_name.rs")?; Ok(()) @@ -427,6 +433,8 @@ fn generate_peripheral_module( ir: &ir::IR, template_name: &str, destination_folder: &Path, + svd2pac_version: &str, + now: &str, ) -> anyhow::Result<()> { // Generate one module for each peripheral for (_, peri) in &ir.device.peripheral_mod { @@ -439,6 +447,8 @@ fn generate_peripheral_module( let mut context = tera::Context::new(); context.insert("peri", peri); context.insert("ir", &ir); + context.insert("svd2pac_version", svd2pac_version); + context.insert("now", now); execute_template( tera, template_name, @@ -461,6 +471,7 @@ fn generate_aurix_core_ir( tracing: _, package_name: _, license_file, + svd2pac_version: _, } = settings; info!("Start generating csfr rust code"); @@ -497,6 +508,7 @@ pub(crate) fn generate_rust_package( tracing, ref package_name, ref license_file, + ref svd2pac_version, } = settings; info!("Start generating rust code"); @@ -518,15 +530,26 @@ pub(crate) fn generate_rust_package( Some(ref package_name) => package_name.clone(), }; + let now = chrono::Utc::now().to_rfc2822(); + let mut context = tera::Context::new(); context.insert("ir", &ir); context.insert("target", &target); context.insert("tracing", &tracing); context.insert("package_name", &package_name); context.insert("description", "Description tests"); + context.insert("svd2pac_version", svd2pac_version); + context.insert("now", &now); // Generate peripheral modules - generate_peripheral_module(&tera, &ir, "peri_mod.tera", destination_folder)?; + generate_peripheral_module( + &tera, + &ir, + "peri_mod.tera", + destination_folder, + svd2pac_version, + &now, + )?; //Generate common module generate_common_module(&tera, &ir, destination_folder, &context)?; @@ -542,7 +565,14 @@ pub(crate) fn generate_rust_package( // Generate cpu peripheral modules if let Some(ref ir) = ir_csfr { - generate_peripheral_module(&tera, ir, "aurix_core.tera", destination_folder)?; + generate_peripheral_module( + &tera, + ir, + "aurix_core.tera", + destination_folder, + svd2pac_version, + &now, + )?; context.insert("ir_csfr", &ir_csfr); } } diff --git a/src/rust_gen/ir.rs b/src/rust_gen/ir.rs index b8e6cd6..933fa5e 100644 --- a/src/rust_gen/ir.rs +++ b/src/rust_gen/ir.rs @@ -185,6 +185,7 @@ pub struct IR { pub device: Device, pub register_addresses: LinkedHashMap>>, pub license_text: String, + pub version: String, /// Interrupt table to be created in the lib.rs. Interrupt table hole has value None pub interrupt_table: Vec>, /// used only for cortex m target diff --git a/src/rust_gen/xml2ir.rs b/src/rust_gen/xml2ir.rs index 2d9d854..c186453 100644 --- a/src/rust_gen/xml2ir.rs +++ b/src/rust_gen/xml2ir.rs @@ -593,6 +593,7 @@ pub(super) fn svd_device2ir( device, register_addresses: entity_db.register_addresses, license_text, + version: svd_device.version.clone(), interrupt_table, nvic_prio_bits: svd_device.cpu.as_ref().map(|x| x.nvic_priority_bits), vendor_systick_config: svd_device.cpu.as_ref().map(|x| x.has_vendor_systick), diff --git a/templates/rust/Cargo_toml.tera b/templates/rust/Cargo_toml.tera index 4984b2b..6a757ae 100644 --- a/templates/rust/Cargo_toml.tera +++ b/templates/rust/Cargo_toml.tera @@ -1,6 +1,8 @@ {% for line in ir.license_text | prepend_lines(prefix="# ") -%} {{line}} {% endfor -%} +# Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}} + [package] name = "{{package_name}}" version = "0.0.1" diff --git a/templates/rust/aurix_core.tera b/templates/rust/aurix_core.tera index 7896114..2535591 100644 --- a/templates/rust/aurix_core.tera +++ b/templates/rust/aurix_core.tera @@ -77,6 +77,8 @@ pub const fn {{cluster.name~"_"~reg.name~"_"~cluster_index~"_"~reg_index| to_fun /* {{ir.license_text}} */ +// Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}} + #![allow(clippy::identity_op)] #![allow(clippy::module_inception)] #![allow(clippy::derivable_impls)] diff --git a/templates/rust/common.tera b/templates/rust/common.tera index 8b341db..11966e1 100644 --- a/templates/rust/common.tera +++ b/templates/rust/common.tera @@ -1,6 +1,8 @@ /* {{ir.license_text}} */ +// Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}} + use ::core::convert::From; use ::core::marker::PhantomData; diff --git a/templates/rust/lib.tera b/templates/rust/lib.tera index 374befb..ef34028 100644 --- a/templates/rust/lib.tera +++ b/templates/rust/lib.tera @@ -2,6 +2,8 @@ /* {{ir.license_text}} */ +// Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}} + {%- if tracing %} #![cfg_attr(not(feature = "tracing"), no_std)] {%- else %} diff --git a/templates/rust/peri_mod.tera b/templates/rust/peri_mod.tera index e15677d..6b86ee1 100644 --- a/templates/rust/peri_mod.tera +++ b/templates/rust/peri_mod.tera @@ -2,6 +2,8 @@ /* {{ir.license_text}} */ +// Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}} + #![allow(clippy::identity_op)] #![allow(clippy::module_inception)] #![allow(clippy::derivable_impls)] diff --git a/templates/rust/reg_name.tera b/templates/rust/reg_name.tera index 2f16213..cd0d1af 100644 --- a/templates/rust/reg_name.tera +++ b/templates/rust/reg_name.tera @@ -1,6 +1,8 @@ /* {{ir.license_text}} */ +// Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}} + //! Contains perfect hash function that maps form raw addresses to //! a string containing the names of all registers that point to an address. //! diff --git a/templates/rust/tracing.tera b/templates/rust/tracing.tera index 328aef5..3d62246 100644 --- a/templates/rust/tracing.tera +++ b/templates/rust/tracing.tera @@ -1,6 +1,8 @@ /* {{ir.license_text}} */ +// Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}} + use std::sync::OnceLock; thread_local! {