2021-11-25 10:04:37 +01:00
|
|
|
#![doc(
|
2022-12-31 14:45:13 +01:00
|
|
|
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"
|
2021-11-25 10:04:37 +01:00
|
|
|
)]
|
2022-12-31 14:45:13 +01:00
|
|
|
//deny_warnings_placeholder_for_ci
|
2017-07-04 18:26:11 +02:00
|
|
|
|
2017-07-15 01:54:54 +02:00
|
|
|
use proc_macro::TokenStream;
|
2022-01-27 23:47:49 +01:00
|
|
|
use std::{env, fs, path::Path};
|
2019-05-08 14:08:09 +02:00
|
|
|
|
2017-07-15 01:54:54 +02:00
|
|
|
mod analyze;
|
2023-01-02 14:58:37 +01:00
|
|
|
mod check;
|
2018-11-03 17:02:41 +01:00
|
|
|
mod codegen;
|
2022-12-31 14:45:13 +01:00
|
|
|
mod syntax;
|
|
|
|
|
|
|
|
// Used for mocking the API in testing
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
|
2023-01-03 07:51:35 +01:00
|
|
|
if let Err(e) = syntax::parse(args, input) {
|
2022-12-31 14:45:13 +01:00
|
|
|
e.to_compile_error().into()
|
|
|
|
} else {
|
|
|
|
"fn main() {}".parse().unwrap()
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 06:44:54 +02:00
|
|
|
|
2020-06-11 19:18:29 +02:00
|
|
|
/// Attribute used to declare a RTIC application
|
2019-08-21 12:19:38 +02:00
|
|
|
///
|
2020-11-19 19:49:39 +01:00
|
|
|
/// For user documentation see the [RTIC book](https://rtic.rs)
|
2022-02-18 19:38:48 +01:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Should never panic, cargo feeds a path which is later converted to a string
|
2018-11-03 17:02:41 +01:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn app(args: TokenStream, input: TokenStream) -> TokenStream {
|
2023-01-03 07:51:35 +01:00
|
|
|
let (app, analysis) = match syntax::parse(args, input) {
|
2019-06-13 23:56:59 +02:00
|
|
|
Err(e) => return e.to_compile_error().into(),
|
|
|
|
Ok(x) => x,
|
|
|
|
};
|
2017-07-15 01:54:54 +02:00
|
|
|
|
2023-02-11 08:55:19 +01:00
|
|
|
if let Err(e) = check::app(&app, &analysis) {
|
2023-01-08 21:30:53 +01:00
|
|
|
return e.to_compile_error().into();
|
2023-01-02 14:58:37 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
let analysis = analyze::app(analysis, &app);
|
2017-07-04 18:26:11 +02:00
|
|
|
|
2022-12-31 14:45:13 +01:00
|
|
|
let ts = codegen::app(&app, &analysis);
|
2019-05-08 14:08:09 +02:00
|
|
|
|
2022-01-27 23:47:49 +01:00
|
|
|
// 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());
|
|
|
|
|
2022-12-31 14:45:13 +01:00
|
|
|
if !out_dir.exists() {
|
2022-01-27 23:47:49 +01:00
|
|
|
// Set out_dir to OUT_DIR
|
|
|
|
out_dir = Path::new(&out_str);
|
|
|
|
|
|
|
|
// Default build path, annotated below:
|
2023-01-28 20:26:50 +01:00
|
|
|
// $(pwd)/target/thumbv7em-none-eabihf/debug/build/rtic-<HASH>/out/
|
|
|
|
// <project_dir>/<target-dir>/<TARGET>/debug/build/rtic-<HASH>/out/
|
2022-01-27 23:47:49 +01:00
|
|
|
//
|
|
|
|
// 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() {
|
2022-12-31 14:45:13 +01:00
|
|
|
let dir = dir.as_os_str().to_str().unwrap();
|
|
|
|
|
|
|
|
if dir.starts_with("thumbv") || dir.starts_with("riscv") {
|
2022-01-27 23:47:49 +01:00
|
|
|
if let Some(out) = path.parent() {
|
|
|
|
out_dir = out;
|
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:38:48 +01:00
|
|
|
// If no parent, just use it
|
|
|
|
out_dir = path;
|
|
|
|
break;
|
2022-01-27 23:47:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:08:09 +02:00
|
|
|
// Try to write the expanded code to disk
|
2022-01-27 23:47:49 +01:00
|
|
|
if let Some(out_str) = out_dir.to_str() {
|
2023-01-08 21:30:53 +01:00
|
|
|
fs::write(format!("{out_str}/rtic-expansion.rs"), ts.to_string()).ok();
|
2019-05-08 14:08:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ts.into()
|
2017-07-04 18:26:11 +02:00
|
|
|
}
|