rtic-macros: wrap everything in macro_rules! to improve the error experience and

royally **** up the DX experience (for lib.rs in rtic-macros, at least :P)
This commit is contained in:
datdenkikniet 2023-04-13 22:13:40 +02:00
parent 68ccf0423d
commit 2ba09dd68d
2 changed files with 86 additions and 70 deletions

View file

@ -1,10 +1,3 @@
#[cfg(not(any(
feature = "cortex-m-source-masking",
feature = "cortex-m-basepri",
feature = "test-template"
)))]
compile_error!("No backend selected");
#[cfg(any(feature = "cortex-m-source-masking", feature = "cortex-m-basepri"))] #[cfg(any(feature = "cortex-m-source-masking", feature = "cortex-m-basepri"))]
pub use cortex::*; pub use cortex::*;

View file

@ -4,87 +4,110 @@
)] )]
//deny_warnings_placeholder_for_ci //deny_warnings_placeholder_for_ci
use proc_macro::TokenStream; macro_rules! with_backend {
use std::{env, fs, path::Path}; (mod: [$($mod:tt),*]) => {
$(
with_backend!{ mod $mod; }
)*
};
($($tokens:tt)*) => {
#[cfg(any(
feature = "cortex-m-source-masking",
feature = "cortex-m-basepri",
feature = "test-template"
))]
$($tokens)*
};
}
mod analyze; with_backend! { mod: [analyze, check, codegen, syntax] }
mod check; with_backend! { use std::{fs, env, path::Path}; }
mod codegen; with_backend! { use proc_macro::TokenStream; }
mod syntax;
// Used for mocking the API in testing with_backend! {
#[doc(hidden)] // Used for mocking the API in testing
#[proc_macro_attribute] #[doc(hidden)]
pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream { #[proc_macro_attribute]
if let Err(e) = syntax::parse(args, input) { pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
e.to_compile_error().into() if let Err(e) = syntax::parse(args, input) {
} else { e.to_compile_error().into()
"fn main() {}".parse().unwrap() } else {
"fn main() {}".parse().unwrap()
}
} }
} }
/// Attribute used to declare a RTIC application with_backend! {
/// /// Attribute used to declare a RTIC application
/// For user documentation see the [RTIC book](https://rtic.rs) ///
/// /// For user documentation see the [RTIC book](https://rtic.rs)
/// # Panics ///
/// /// # Panics
/// Should never panic, cargo feeds a path which is later converted to a string ///
#[proc_macro_attribute] /// Should never panic, cargo feeds a path which is later converted to a string
pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { #[proc_macro_attribute]
let (app, analysis) = match syntax::parse(args, input) { pub fn app(_args: TokenStream, _input: TokenStream) -> TokenStream {
Err(e) => return e.to_compile_error().into(), let (app, analysis) = match syntax::parse(_args, _input) {
Ok(x) => x, Err(e) => return e.to_compile_error().into(),
}; Ok(x) => x,
};
if let Err(e) = check::app(&app, &analysis) { if let Err(e) = check::app(&app, &analysis) {
return e.to_compile_error().into(); return e.to_compile_error().into();
} }
let analysis = analyze::app(analysis, &app); let analysis = analyze::app(analysis, &app);
let ts = codegen::app(&app, &analysis); let ts = codegen::app(&app, &analysis);
// Default output path: <project_dir>/target/ // Default output path: <project_dir>/target/
let mut out_dir = Path::new("target"); let mut out_dir = Path::new("target");
// Get output directory from Cargo environment // Get output directory from Cargo environment
// TODO don't want to break builds if OUT_DIR is not set, is this ever the case? // TODO don't want to break builds if OUT_DIR is not set, is this ever the case?
let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string()); let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
if !out_dir.exists() { if !out_dir.exists() {
// Set out_dir to OUT_DIR // Set out_dir to OUT_DIR
out_dir = Path::new(&out_str); out_dir = Path::new(&out_str);
// Default build path, annotated below: // Default build path, annotated below:
// $(pwd)/target/thumbv7em-none-eabihf/debug/build/rtic-<HASH>/out/ // $(pwd)/target/thumbv7em-none-eabihf/debug/build/rtic-<HASH>/out/
// <project_dir>/<target-dir>/<TARGET>/debug/build/rtic-<HASH>/out/ // <project_dir>/<target-dir>/<TARGET>/debug/build/rtic-<HASH>/out/
// //
// traverse up to first occurrence of TARGET, approximated with starts_with("thumbv") // traverse up to first occurrence of TARGET, approximated with starts_with("thumbv")
// and use the parent() of this path // and use the parent() of this path
// //
// If no "target" directory is found, <project_dir>/<out_dir_root> is used // If no "target" directory is found, <project_dir>/<out_dir_root> is used
for path in out_dir.ancestors() { for path in out_dir.ancestors() {
if let Some(dir) = path.components().last() { if let Some(dir) = path.components().last() {
let dir = dir.as_os_str().to_str().unwrap(); let dir = dir.as_os_str().to_str().unwrap();
if dir.starts_with("thumbv") || dir.starts_with("riscv") { if dir.starts_with("thumbv") || dir.starts_with("riscv") {
if let Some(out) = path.parent() { if let Some(out) = path.parent() {
out_dir = out; out_dir = out;
break;
}
// If no parent, just use it
out_dir = path;
break; break;
} }
// If no parent, just use it
out_dir = path;
break;
} }
} }
} }
}
// Try to write the expanded code to disk // Try to write the expanded code to disk
if let Some(out_str) = out_dir.to_str() { if let Some(out_str) = out_dir.to_str() {
fs::write(format!("{out_str}/rtic-expansion.rs"), ts.to_string()).ok(); fs::write(format!("{out_str}/rtic-expansion.rs"), ts.to_string()).ok();
} }
ts.into() ts.into()
}
} }
#[cfg(not(any(
feature = "cortex-m-source-masking",
feature = "cortex-m-basepri",
feature = "test-template"
)))]
compile_error!("Cannot compile. No backend feature selected.");