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
6mod analyze;
7mod check;
8mod codegen;
9mod preprocess;
10mod syntax;
11use proc_macro::TokenStream;
12use std::{env, fs, path::Path};
13
14#[doc(hidden)]
16#[proc_macro_attribute]
17pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
18 if let Err(e) = syntax::parse(args, input) {
19 e.to_compile_error().into()
20 } else {
21 "fn main() {}".parse().unwrap()
22 }
23}
24
25#[proc_macro_attribute]
33pub fn app(_args: TokenStream, _input: TokenStream) -> TokenStream {
34 let (mut app, analysis) = match syntax::parse(_args, _input) {
35 Err(e) => return e.to_compile_error().into(),
36 Ok(x) => x,
37 };
38
39 if let Err(e) = preprocess::app(&mut app, &analysis) {
41 return e.to_compile_error().into();
42 }
43 let app = app;
44 if let Err(e) = check::app(&app, &analysis) {
47 return e.to_compile_error().into();
48 }
49
50 let analysis = analyze::app(analysis, &app);
51
52 let ts = codegen::app(&app, &analysis);
53
54 let mut out_dir = Path::new("target");
56
57 let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
60
61 if !out_dir.exists() {
62 out_dir = Path::new(&out_str);
64
65 for path in out_dir.ancestors() {
74 if let Some(dir) = path.components().next_back() {
75 let dir = dir.as_os_str().to_str().unwrap();
76
77 if dir.starts_with("thumbv") || dir.starts_with("riscv") {
78 if let Some(out) = path.parent() {
79 out_dir = out;
80 break;
81 }
82 out_dir = path;
84 break;
85 }
86 }
87 }
88 }
89
90 if let Some(out_str) = out_dir.to_str() {
92 fs::write(format!("{out_str}/rtic-expansion.rs"), ts.to_string()).ok();
93 }
94
95 ts.into()
96}