use std::sync::OnceLock;
use anyhow::{bail, Result};
use strum::IntoEnumIterator;
macro_rules! include_toml {
($type:ty, $file:expr) => {{
static LOADED_TOML: OnceLock<$type> = OnceLock::new();
LOADED_TOML.get_or_init(|| basic_toml::from_str(include_str!($file)).unwrap())
}};
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
serde::Deserialize,
serde::Serialize,
strum::Display,
strum::EnumIter,
strum::EnumString,
strum::AsRefStr,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Arch {
RiscV,
Xtensa,
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
serde::Deserialize,
serde::Serialize,
strum::Display,
strum::EnumIter,
strum::EnumString,
strum::AsRefStr,
)]
pub enum Cores {
#[serde(rename = "single_core")]
#[strum(serialize = "single_core")]
Single,
#[serde(rename = "multi_core")]
#[strum(serialize = "multi_core")]
Multi,
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Deserialize,
serde::Serialize,
strum::Display,
strum::EnumIter,
strum::EnumString,
strum::AsRefStr,
)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum Chip {
Esp32,
Esp32c2,
Esp32c3,
Esp32c6,
Esp32h2,
Esp32s2,
Esp32s3,
}
impl Chip {
pub fn target(&self) -> &str {
use Chip::*;
match self {
Esp32 => "xtensa-esp32-none-elf",
Esp32c2 | Esp32c3 => "riscv32imc-unknown-none-elf",
Esp32c6 | Esp32h2 => "riscv32imac-unknown-none-elf",
Esp32s2 => "xtensa-esp32s2-none-elf",
Esp32s3 => "xtensa-esp32s3-none-elf",
}
}
pub fn has_lp_core(&self) -> bool {
use Chip::*;
matches!(self, Esp32c6 | Esp32s2 | Esp32s3)
}
pub fn lp_target(&self) -> Result<&str> {
use Chip::*;
match self {
Esp32c6 => Ok("riscv32imac-unknown-none-elf"),
Esp32s2 | Esp32s3 => Ok("riscv32imc-unknown-none-elf"),
_ => bail!("Chip does not contain an LP core: '{}'", self),
}
}
pub fn pretty_name(&self) -> &str {
match self {
Chip::Esp32 => "ESP32",
Chip::Esp32c2 => "ESP32-C2",
Chip::Esp32c3 => "ESP32-C3",
Chip::Esp32c6 => "ESP32-C6",
Chip::Esp32h2 => "ESP32-H2",
Chip::Esp32s2 => "ESP32-S2",
Chip::Esp32s3 => "ESP32-S3",
}
}
pub fn is_xtensa(&self) -> bool {
matches!(self, Chip::Esp32 | Chip::Esp32s2 | Chip::Esp32s3)
}
pub fn is_riscv(&self) -> bool {
!self.is_xtensa()
}
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
struct Device {
pub name: String,
pub arch: Arch,
pub cores: Cores,
pub peripherals: Vec<String>,
pub symbols: Vec<String>,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Config {
device: Device,
}
impl Config {
pub fn for_chip(chip: &Chip) -> &Self {
match chip {
Chip::Esp32 => include_toml!(Config, "../devices/esp32.toml"),
Chip::Esp32c2 => include_toml!(Config, "../devices/esp32c2.toml"),
Chip::Esp32c3 => include_toml!(Config, "../devices/esp32c3.toml"),
Chip::Esp32c6 => include_toml!(Config, "../devices/esp32c6.toml"),
Chip::Esp32h2 => include_toml!(Config, "../devices/esp32h2.toml"),
Chip::Esp32s2 => include_toml!(Config, "../devices/esp32s2.toml"),
Chip::Esp32s3 => include_toml!(Config, "../devices/esp32s3.toml"),
}
}
pub fn name(&self) -> String {
self.device.name.clone()
}
pub fn arch(&self) -> Arch {
self.device.arch
}
pub fn cores(&self) -> Cores {
self.device.cores
}
pub fn peripherals(&self) -> &[String] {
&self.device.peripherals
}
pub fn symbols(&self) -> &[String] {
&self.device.symbols
}
pub fn all(&self) -> impl Iterator<Item = &str> + '_ {
[
self.device.name.as_str(),
self.device.arch.as_ref(),
self.device.cores.as_ref(),
]
.into_iter()
.chain(self.device.peripherals.iter().map(|s| s.as_str()))
.chain(self.device.symbols.iter().map(|s| s.as_str()))
}
pub fn contains(&self, item: &str) -> bool {
self.all().any(|i| i == item)
}
pub fn define_symbols(&self) {
define_all_possible_symbols();
for symbol in self.all() {
println!("cargo:rustc-cfg={symbol}");
}
}
}
fn define_all_possible_symbols() {
println!("cargo:rustc-check-cfg=cfg(not_really_docsrs)");
for chip in Chip::iter() {
let config = Config::for_chip(&chip);
for symbol in config.all() {
println!("cargo:rustc-check-cfg=cfg({})", symbol);
}
}
}