rtic_macros/
lib.rs
1#![doc(
2 html_logo_url = "https://raw.githubusercontent.com/rtic-rs/rtic/master/book/en/src/RTIC.svg",
3 html_favicon_url = "https://raw.githubusercontent.com/rtic-rs/rtic/master/book/en/src/RTIC.svg"
4)]
5
6macro_rules! with_backend {
7 (mod: [$($mod:tt),*]) => {
8 $(
9 with_backend!{ mod $mod; }
10 )*
11 };
12 ($($tokens:tt)*) => {
13 #[cfg(any(
14 feature = "cortex-m-source-masking",
15 feature = "cortex-m-basepri",
16 feature = "test-template",
17 feature = "riscv-esp32c3",
18 feature = "riscv-slic",
19 ))]
20 $($tokens)*
21 };
22}
23
24with_backend! { mod: [analyze, check, codegen, preprocess, syntax] }
25with_backend! { use std::{fs, env, path::Path}; }
26with_backend! { use proc_macro::TokenStream; }
27
28with_backend! {
29 #[doc(hidden)]
31 #[proc_macro_attribute]
32 pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
33 if let Err(e) = syntax::parse(args, input) {
34 e.to_compile_error().into()
35 } else {
36 "fn main() {}".parse().unwrap()
37 }
38 }
39}
40
41with_backend! {
42 #[proc_macro_attribute]
50 pub fn app(_args: TokenStream, _input: TokenStream) -> TokenStream {
51 let (mut app, analysis) = match syntax::parse(_args, _input) {
52 Err(e) => return e.to_compile_error().into(),
53 Ok(x) => x,
54 };
55
56 if let Err(e) = preprocess::app(&mut app, &analysis) {
58 return e.to_compile_error().into();
59 }
60 let app = app;
61 if let Err(e) = check::app(&app, &analysis) {
64 return e.to_compile_error().into();
65 }
66
67 let analysis = analyze::app(analysis, &app);
68
69 let ts = codegen::app(&app, &analysis);
70
71 let mut out_dir = Path::new("target");
73
74 let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
77
78 if !out_dir.exists() {
79 out_dir = Path::new(&out_str);
81
82 for path in out_dir.ancestors() {
91 if let Some(dir) = path.components().next_back() {
92 let dir = dir.as_os_str().to_str().unwrap();
93
94 if dir.starts_with("thumbv") || dir.starts_with("riscv") {
95 if let Some(out) = path.parent() {
96 out_dir = out;
97 break;
98 }
99 out_dir = path;
101 break;
102 }
103 }
104 }
105 }
106
107 if let Some(out_str) = out_dir.to_str() {
109 fs::write(format!("{out_str}/rtic-expansion.rs"), ts.to_string()).ok();
110 }
111
112 ts.into()
113 }
114}
115
116#[cfg(not(any(
117 feature = "cortex-m-source-masking",
118 feature = "cortex-m-basepri",
119 feature = "test-template",
120 feature = "riscv-esp32c3",
121 feature = "riscv-slic",
122)))]
123compile_error!("Cannot compile. No backend feature selected.");