From 9e445b3583c15c7701f3167eaa8dfe4afd541691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Sat, 4 Feb 2023 16:47:17 +0100 Subject: Move rtic macros to repo root, tune xtask --- rtic-macros/src/lib.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 rtic-macros/src/lib.rs (limited to 'rtic-macros/src/lib.rs') diff --git a/rtic-macros/src/lib.rs b/rtic-macros/src/lib.rs new file mode 100644 index 0000000..3ac2701 --- /dev/null +++ b/rtic-macros/src/lib.rs @@ -0,0 +1,91 @@ +#![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 check; +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 { + if let Err(e) = syntax::parse(args, input) { + 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 (app, analysis) = match syntax::parse(args, input) { + Err(e) => return e.to_compile_error().into(), + Ok(x) => x, + }; + + if let Err(e) = check::app(&app) { + return e.to_compile_error().into(); + } + + let analysis = analyze::app(analysis, &app); + + let ts = codegen::app(&app, &analysis); + + // Default output path: /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/rtic-/out/ + // ///debug/build/rtic-/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, / 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!("{out_str}/rtic-expansion.rs"), ts.to_string()).ok(); + } + + ts.into() +} -- cgit v1.2.3 From 60f0342b697cdddbab9c0e8c6d772bc7aab9de38 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Sat, 11 Feb 2023 08:55:19 +0100 Subject: Break out core specific codegen to bindings --- rtic-macros/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'rtic-macros/src/lib.rs') diff --git a/rtic-macros/src/lib.rs b/rtic-macros/src/lib.rs index 3ac2701..cd2a924 100644 --- a/rtic-macros/src/lib.rs +++ b/rtic-macros/src/lib.rs @@ -8,7 +8,6 @@ use proc_macro::TokenStream; use std::{env, fs, path::Path}; mod analyze; -mod bindings; mod check; mod codegen; mod syntax; @@ -38,7 +37,7 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { Ok(x) => x, }; - if let Err(e) = check::app(&app) { + if let Err(e) = check::app(&app, &analysis) { return e.to_compile_error().into(); } -- cgit v1.2.3