Skip to main content

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
6mod analyze;
7mod check;
8mod codegen;
9mod preprocess;
10mod syntax;
11use proc_macro::TokenStream;
12use std::{env, fs, path::Path};
13
14// Used for mocking the API in testing
15#[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/// Attribute used to declare a RTIC application
26///
27/// For user documentation see the [RTIC book](https://rtic.rs)
28///
29/// # Panics
30///
31/// Should never panic, cargo feeds a path which is later converted to a string
32#[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    // Modify app based on backend before continuing
40    if let Err(e) = preprocess::app(&mut app, &analysis) {
41        return e.to_compile_error().into();
42    }
43    let app = app;
44    // App is not mutable after this point
45
46    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    // Default output path: <project_dir>/target/
55    let mut out_dir = Path::new("target");
56
57    // Get output directory from Cargo environment
58    // TODO don't want to break builds if OUT_DIR is not set, is this ever the case?
59    let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
60
61    if !out_dir.exists() {
62        // Set out_dir to OUT_DIR
63        out_dir = Path::new(&out_str);
64
65        // Default build path, annotated below:
66        // $(pwd)/target/thumbv7em-none-eabihf/debug/build/rtic-<HASH>/out/
67        // <project_dir>/<target-dir>/<TARGET>/debug/build/rtic-<HASH>/out/
68        //
69        // traverse up to first occurrence of TARGET, approximated with starts_with("thumbv")
70        // and use the parent() of this path
71        //
72        // If no "target" directory is found, <project_dir>/<out_dir_root> is used
73        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                    // If no parent, just use it
83                    out_dir = path;
84                    break;
85                }
86            }
87        }
88    }
89
90    // Try to write the expanded code to disk
91    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}