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
111
112
#![doc(
html_logo_url = "https://raw.githubusercontent.com/rtic-rs/cortex-m-rtic/master/book/en/src/RTIC.svg",
html_favicon_url = "https://raw.githubusercontent.com/rtic-rs/cortex-m-rtic/master/book/en/src/RTIC.svg"
)]
extern crate proc_macro;
use proc_macro::TokenStream;
use std::{env, fs, path::Path};
use rtic_syntax::Settings;
mod analyze;
mod check;
mod codegen;
#[cfg(test)]
mod tests;
#[proc_macro_attribute]
pub fn app(args: TokenStream, input: TokenStream) -> TokenStream {
let mut settings = Settings::default();
settings.optimize_priorities = false;
settings.parse_binds = true;
settings.parse_extern_interrupt = true;
let (app, analysis) = match rtic_syntax::parse(args, input, settings) {
Err(e) => return e.to_compile_error().into(),
Ok(x) => x,
};
let extra = match check::app(&app, &analysis) {
Err(e) => return e.to_compile_error().into(),
Ok(x) => x,
};
let analysis = analyze::app(analysis, &app);
let ts = codegen::app(&app, &analysis, &extra);
let mut out_dir = Path::new("target");
let out_str = env::var("OUT_DIR").unwrap_or_else(|_| "".to_string());
let target_triple_prefix = "thumbv";
#[cfg(feature = "debugprint")]
println!("OUT_DIR\n{:#?}", out_str);
if out_dir.exists() {
#[cfg(feature = "debugprint")]
println!("\ntarget/ exists\n");
} else {
out_dir = Path::new(&out_str);
for path in out_dir.ancestors() {
if let Some(dir) = path.components().last() {
if dir
.as_os_str()
.to_str()
.unwrap()
.starts_with(target_triple_prefix)
{
if let Some(out) = path.parent() {
out_dir = out;
#[cfg(feature = "debugprint")]
println!("{:#?}\n", out_dir);
break;
}
out_dir = path;
break;
}
}
}
}
if let Some(out_str) = out_dir.to_str() {
#[cfg(feature = "debugprint")]
println!("Write file:\n{}/rtic-expansion.rs\n", out_str);
fs::write(format!("{}/rtic-expansion.rs", out_str), ts.to_string()).ok();
}
ts.into()
}