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