cortex_m_rtic_macros/
lib.rs1#![doc(
2    html_logo_url = "https://raw.githubusercontent.com/rtic-rs/cortex-m-rtic/master/book/en/src/RTIC.svg",
3    html_favicon_url = "https://raw.githubusercontent.com/rtic-rs/cortex-m-rtic/master/book/en/src/RTIC.svg"
4)]
5extern crate proc_macro;
8
9use proc_macro::TokenStream;
10use std::{env, fs, path::Path};
11
12use rtic_syntax::Settings;
13
14mod analyze;
15mod check;
16mod codegen;
17#[cfg(test)]
18mod tests;
19
20#[proc_macro_attribute]
28pub fn app(args: TokenStream, input: TokenStream) -> TokenStream {
29    let mut settings = Settings::default();
30    settings.optimize_priorities = false;
31    settings.parse_binds = true;
32    settings.parse_extern_interrupt = true;
33
34    let (app, analysis) = match rtic_syntax::parse(args, input, settings) {
35        Err(e) => return e.to_compile_error().into(),
36        Ok(x) => x,
37    };
38
39    let extra = match check::app(&app, &analysis) {
40        Err(e) => return e.to_compile_error().into(),
41        Ok(x) => x,
42    };
43
44    let analysis = analyze::app(analysis, &app);
45
46    let ts = codegen::app(&app, &analysis, &extra);
47
48    let mut out_dir = Path::new("target");
50
51    let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
54
55    let target_triple_prefix = "thumbv";
57
58    #[cfg(feature = "debugprint")]
65    println!("OUT_DIR\n{:#?}", out_str);
66
67    if out_dir.exists() {
68        #[cfg(feature = "debugprint")]
69        println!("\ntarget/ exists\n");
70    } else {
71        out_dir = Path::new(&out_str);
73
74        for path in out_dir.ancestors() {
83            if let Some(dir) = path.components().last() {
84                if dir
85                    .as_os_str()
86                    .to_str()
87                    .unwrap()
88                    .starts_with(target_triple_prefix)
89                {
90                    if let Some(out) = path.parent() {
91                        out_dir = out;
92                        #[cfg(feature = "debugprint")]
93                        println!("{:#?}\n", out_dir);
94                        break;
95                    }
96                    out_dir = path;
98                    break;
99                }
100            }
101        }
102    }
103
104    if let Some(out_str) = out_dir.to_str() {
106        #[cfg(feature = "debugprint")]
107        println!("Write file:\n{}/rtic-expansion.rs\n", out_str);
108        fs::write(format!("{}/rtic-expansion.rs", out_str), ts.to_string()).ok();
109    }
110
111    ts.into()
112}