aboutsummaryrefslogtreecommitdiff
path: root/macros/src/lib.rs
blob: 7729dcbed013c7cdb5e9a4a72091e215db408a81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/rtic-rs/rtic/master/book/en/src/RTIC.svg",
    html_favicon_url = "https://raw.githubusercontent.com/rtic-rs/rtic/master/book/en/src/RTIC.svg"
)]

//deny_warnings_placeholder_for_ci

use proc_macro::TokenStream;
use std::{env, fs, path::Path};

mod analyze;
mod bindings;
mod codegen;
mod syntax;

// Used for mocking the API in testing
#[doc(hidden)]
#[proc_macro_attribute]
pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
    let mut settings = syntax::Settings::default();
    let mut rtic_args = vec![];
    for arg in args.to_string().split(',') {
        if arg.trim() == "parse_binds" {
            settings.parse_binds = true;
        } else if arg.trim() == "parse_extern_interrupt" {
            settings.parse_extern_interrupt = true;
        } else {
            rtic_args.push(arg.to_string());
        }
    }

    // rtic_args.push("device = mock".into());

    let args = rtic_args.join(", ").parse();

    println!("args: {:?}", args);

    if let Err(e) = syntax::parse(args.unwrap(), input, settings) {
        e.to_compile_error().into()
    } else {
        "fn main() {}".parse().unwrap()
    }
}

/// Attribute used to declare a RTIC application
///
/// For user documentation see the [RTIC book](https://rtic.rs)
///
/// # Panics
///
/// Should never panic, cargo feeds a path which is later converted to a string
#[proc_macro_attribute]
pub fn app(args: TokenStream, input: TokenStream) -> TokenStream {
    let mut settings = syntax::Settings::default();
    settings.optimize_priorities = false;
    settings.parse_binds = true;
    settings.parse_extern_interrupt = true;

    let (app, analysis) = match syntax::parse(args, input, settings) {
        Err(e) => return e.to_compile_error().into(),
        Ok(x) => x,
    };

    let analysis = analyze::app(analysis, &app);

    let ts = codegen::app(&app, &analysis);

    // Default output path: <project_dir>/target/
    let mut out_dir = Path::new("target");

    // Get output directory from Cargo environment
    // TODO don't want to break builds if OUT_DIR is not set, is this ever the case?
    let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());

    if !out_dir.exists() {
        // Set out_dir to OUT_DIR
        out_dir = Path::new(&out_str);

        // Default build path, annotated below:
        // $(pwd)/target/thumbv7em-none-eabihf/debug/build/cortex-m-rtic-<HASH>/out/
        // <project_dir>/<target-dir>/<TARGET>/debug/build/cortex-m-rtic-<HASH>/out/
        //
        // traverse up to first occurrence of TARGET, approximated with starts_with("thumbv")
        // and use the parent() of this path
        //
        // If no "target" directory is found, <project_dir>/<out_dir_root> is used
        for path in out_dir.ancestors() {
            if let Some(dir) = path.components().last() {
                let dir = dir.as_os_str().to_str().unwrap();

                if dir.starts_with("thumbv") || dir.starts_with("riscv") {
                    if let Some(out) = path.parent() {
                        out_dir = out;
                        break;
                    }
                    // If no parent, just use it
                    out_dir = path;
                    break;
                }
            }
        }
    }

    // Try to write the expanded code to disk
    if let Some(out_str) = out_dir.to_str() {
        fs::write(format!("{}/rtic-expansion.rs", out_str), ts.to_string()).ok();
    }

    ts.into()
}