stm32: rustfmt generated files in build.rs

This commit is contained in:
Dario Nieuwenhuis 2024-04-29 23:51:33 +02:00
parent 6f44d7a9df
commit 7fb74ff756

View File

@ -1,6 +1,8 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt::Write as _; use std::fmt::Write as _;
use std::path::PathBuf; use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs}; use std::{env, fs};
use proc_macro2::{Ident, TokenStream}; use proc_macro2::{Ident, TokenStream};
@ -1536,13 +1538,15 @@ fn main() {
let out_dir = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); let out_dir = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
let out_file = out_dir.join("_macros.rs").to_string_lossy().to_string(); let out_file = out_dir.join("_macros.rs").to_string_lossy().to_string();
fs::write(out_file, m).unwrap(); fs::write(&out_file, m).unwrap();
rustfmt(&out_file);
// ======== // ========
// Write generated.rs // Write generated.rs
let out_file = out_dir.join("_generated.rs").to_string_lossy().to_string(); let out_file = out_dir.join("_generated.rs").to_string_lossy().to_string();
fs::write(out_file, g.to_string()).unwrap(); fs::write(&out_file, g.to_string()).unwrap();
rustfmt(&out_file);
// ======== // ========
// Multicore // Multicore
@ -1657,3 +1661,23 @@ fn get_flash_region_type_name(name: &str) -> String {
.replace("REGION", "Region") .replace("REGION", "Region")
.replace('_', "") .replace('_', "")
} }
/// rustfmt a given path.
/// Failures are logged to stderr and ignored.
fn rustfmt(path: impl AsRef<Path>) {
let path = path.as_ref();
match Command::new("rustfmt").args([path]).output() {
Err(e) => {
eprintln!("failed to exec rustfmt {:?}: {:?}", path, e);
}
Ok(out) => {
if !out.status.success() {
eprintln!("rustfmt {:?} failed:", path);
eprintln!("=== STDOUT:");
std::io::stderr().write_all(&out.stdout).unwrap();
eprintln!("=== STDERR:");
std::io::stderr().write_all(&out.stderr).unwrap();
}
}
}
}