Add stm32-metapac crate, with codegen in rust

This commit is contained in:
Dario Nieuwenhuis
2021-05-25 04:17:24 +02:00
parent fb85850492
commit d8e4421fc6
534 changed files with 2386 additions and 376357 deletions

View File

@ -1,34 +1,32 @@
use regex::Regex;
use std::fmt::Write as _;
use std::fs::File;
use std::io::Write;
use std::env;
use std::path::PathBuf;
use std::{env, fs};
use std::process::Command;
fn main() {
let chip = env::vars_os()
let chip_name = env::vars_os()
.map(|(a, _)| a.to_string_lossy().to_string())
.find(|x| x.starts_with("CARGO_FEATURE_STM32"))
.expect("No stm32xx Cargo feature enabled")
.strip_prefix("CARGO_FEATURE_")
.unwrap()
.to_ascii_lowercase();
.to_ascii_uppercase();
let mut device_x = String::new();
let out_dir = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
let out_file = out_dir.join("generated.rs").to_string_lossy().to_string();
let chip_rs = fs::read_to_string(format!("src/pac/{}.rs", chip)).unwrap();
let re = Regex::new("declare!\\(([a-zA-Z0-9_]+)\\)").unwrap();
for c in re.captures_iter(&chip_rs) {
let name = c.get(1).unwrap().as_str();
write!(&mut device_x, "PROVIDE({} = DefaultHandler);\n", name).unwrap();
let exit_code = Command::new("python3")
.args(&["gen.py", &chip_name, &out_file])
.status()
.expect("failed to execute gen.py");
if !exit_code.success() {
panic!("gen.py exited with {:?}", exit_code)
}
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("device.x"))
.unwrap()
.write_all(device_x.as_bytes())
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=src/pac/{}.rs", chip);
for s in env::var("DEP_STM32_METAPAC_V0.1_CFGS").unwrap().split(",") {
println!("cargo:rustc-cfg={}", s);
}
println!("cargo:rerun-if-env-changed=DEP_STM32_METAPAC_V0.1_CFGS");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=gen.py");
}