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-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    // Used for mocking the API in testing
31    #[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    /// Attribute used to declare a RTIC application
44    ///
45    /// For user documentation see the [RTIC book](https://rtic.rs)
46    ///
47    /// # Panics
48    ///
49    /// Should never panic, cargo feeds a path which is later converted to a string
50    #[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        // Modify app based on backend before continuing
58        if let Err(e) = preprocess::app(&mut app, &analysis) {
59            return e.to_compile_error().into();
60        }
61        let app = app;
62        // App is not mutable after this point
63
64        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        // Default output path: <project_dir>/target/
73        let mut out_dir = Path::new("target");
74
75        // Get output directory from Cargo environment
76        // TODO don't want to break builds if OUT_DIR is not set, is this ever the case?
77        let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
78
79        if !out_dir.exists() {
80            // Set out_dir to OUT_DIR
81            out_dir = Path::new(&out_str);
82
83            // Default build path, annotated below:
84            // $(pwd)/target/thumbv7em-none-eabihf/debug/build/rtic-<HASH>/out/
85            // <project_dir>/<target-dir>/<TARGET>/debug/build/rtic-<HASH>/out/
86            //
87            // traverse up to first occurrence of TARGET, approximated with starts_with("thumbv")
88            // and use the parent() of this path
89            //
90            // If no "target" directory is found, <project_dir>/<out_dir_root> is used
91            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                        // If no parent, just use it
101                        out_dir = path;
102                        break;
103                    }
104                }
105            }
106        }
107
108        // Try to write the expanded code to disk
109        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.");