Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version information to generated crates #25

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 32 additions & 2 deletions src/rust_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ pub struct GenPkgSettings {
pub tracing: bool,
pub package_name: Option<String>,
pub license_file: Option<PathBuf>,
pub svd2pac_version: String,
}

fn precompile_tera(tera: &mut Tera) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(())
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -461,6 +471,7 @@ fn generate_aurix_core_ir(
tracing: _,
package_name: _,
license_file,
svd2pac_version: _,
} = settings;

info!("Start generating csfr rust code");
Expand Down Expand Up @@ -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");
Expand All @@ -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)?;
Expand All @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/rust_gen/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub struct IR {
pub device: Device,
pub register_addresses: LinkedHashMap<u64, Vec<Vec<PathChunk>>>,
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<Option<Interrupt>>,
/// used only for cortex m target
Expand Down
1 change: 1 addition & 0 deletions src/rust_gen/xml2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions templates/rust/Cargo_toml.tera
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions templates/rust/aurix_core.tera
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions templates/rust/common.tera
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 2 additions & 0 deletions templates/rust/lib.tera
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
2 changes: 2 additions & 0 deletions templates/rust/peri_mod.tera
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions templates/rust/reg_name.tera
Original file line number Diff line number Diff line change
@@ -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.
//!
Expand Down
2 changes: 2 additions & 0 deletions templates/rust/tracing.tera
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
{{ir.license_text}}
*/
// Generated from SVD {{ir.version}}, with svd2pac {{svd2pac_version}} on {{now}}

use std::sync::OnceLock;

thread_local! {
Expand Down
Loading