Browse Source

Improve target gen

* Show an error message when the build script is unable to run
  rustfmt.

* Don't generate a struct without values for the JEP106 code in the
  target generation. The whole struct should be `None` in that case.
adi-overhaul
Dominik Boehi 2 years ago
committed by Yatekii
parent
commit
6c33eb368a
  1. 20
      probe-rs-t2rust/src/lib.rs
  2. 13
      probe-rs/build.rs

20
probe-rs-t2rust/src/lib.rs

@ -3,6 +3,9 @@ use std::fs::{read_dir, read_to_string};
use std::io;
use std::path::{Path, PathBuf};
/// Parse all target description files in the input directory and create
/// a single output file with the Rust source code
/// for all targets.
pub fn run(input_dir: impl AsRef<Path>, output_file: impl AsRef<Path>) {
// Determine all config files to parse.
let mut files = vec![];
@ -375,15 +378,20 @@ fn extract_chip_family(
/// Extracts the jep code token stream from a yaml value.
fn extract_manufacturer(chip: &serde_yaml::Value) -> Option<proc_macro2::TokenStream> {
chip.get("manufacturer").map(|manufacturer| {
chip.get("manufacturer").and_then(|manufacturer| {
let cc = manufacturer.get("cc").map(|v| v.as_u64().unwrap() as u8);
let id = manufacturer.get("id").map(|v| v.as_u64().unwrap() as u8);
quote::quote! {
JEP106Code {
cc: #cc,
id: #id,
}
// For a valid JEP106 Code we need both cc and id
if cc.is_some() && id.is_some() {
Some(quote::quote! {
JEP106Code {
cc: #cc,
id: #id,
}
})
} else {
None
}
})
}

13
probe-rs/build.rs

@ -16,7 +16,16 @@ fn main() {
let mut rustfmt = Command::new("rustfmt");
rustfmt.arg("--emit").arg("files").arg(dest_path);
rustfmt.arg("--emit").arg("files").arg(&dest_path);
rustfmt.status().expect("Failed to run rustfmt");
let fmt_result = rustfmt.status().expect("Failed to run rustfmt");
if !fmt_result.success() {
println!("cargo:warning=Failed to formated generated target file.",);
println!(
"cargo:warning='rustfmt --emit files {}' failed with {}",
dest_path.display(),
fmt_result
);
}
}

Loading…
Cancel
Save