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-esp32c6",
19 feature = "riscv-slic",
20 ))]
21 $($tokens)*
22 };
23}
24
25with_backend! { mod: [analyze, check, codegen, preprocess, syntax] }
26with_backend! { use std::{fs, env, path::Path}; }
27with_backend! { use proc_macro::TokenStream; }
28
29with_backend! {
30 #[doc(hidden)]
32 #[proc_macro_attribute]
33 pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
34 if let Err(e) = syntax::parse(args, input) {
35 e.to_compile_error().into()
36 } else {
37 "fn main() {}".parse().unwrap()
38 }
39 }
40}
41
42with_backend! {
43 #[proc_macro_attribute]
51 pub fn app(_args: TokenStream, _input: TokenStream) -> TokenStream {
52 let (mut app, analysis) = match syntax::parse(_args, _input) {
53 Err(e) => return e.to_compile_error().into(),
54 Ok(x) => x,
55 };
56
57 if let Err(e) = preprocess::app(&mut app, &analysis) {
59 return e.to_compile_error().into();
60 }
61 let app = app;
62 if let Err(e) = check::app(&app, &analysis) {
65 return e.to_compile_error().into();
66 }
67
68 let analysis = analyze::app(analysis, &app);
69
70 let ts = codegen::app(&app, &analysis);
71
72 let mut out_dir = Path::new("target");
74
75 let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
78
79 if !out_dir.exists() {
80 out_dir = Path::new(&out_str);
82
83 for path in out_dir.ancestors() {
92 if let Some(dir) = path.components().next_back() {
93 let dir = dir.as_os_str().to_str().unwrap();
94
95 if dir.starts_with("thumbv") || dir.starts_with("riscv") {
96 if let Some(out) = path.parent() {
97 out_dir = out;
98 break;
99 }
100 out_dir = path;
102 break;
103 }
104 }
105 }
106 }
107
108 if let Some(out_str) = out_dir.to_str() {
110 fs::write(format!("{out_str}/rtic-expansion.rs"), ts.to_string()).ok();
111 }
112
113 ts.into()
114 }
115}
116
117#[cfg(not(any(
118 feature = "cortex-m-source-masking",
119 feature = "cortex-m-basepri",
120 feature = "test-template",
121 feature = "riscv-esp32c3",
122 feature = "riscv-esp32c6",
123 feature = "riscv-slic",
124)))]
125compile_error!("Cannot compile. No backend feature selected.");