diff options
author | 2022-01-27 23:47:49 +0100 | |
---|---|---|
committer | 2022-01-28 21:21:06 +0100 | |
commit | 9f54b4aca89d77e2a84d7d522b51e3f2bbf8ac74 (patch) | |
tree | 223002ccede01b7fa095e60f0466b71d12fb0839 | |
parent | c3c75f2200ebc32ca35403c528914baa8e025bed (diff) | |
download | rtic-9f54b4aca89d77e2a84d7d522b51e3f2bbf8ac74.tar.gz rtic-9f54b4aca89d77e2a84d7d522b51e3f2bbf8ac74.tar.zst rtic-9f54b4aca89d77e2a84d7d522b51e3f2bbf8ac74.zip |
RTIC macro expansion: Try to find target-dir
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | macros/Cargo.toml | 3 | ||||
-rw-r--r-- | macros/src/lib.rs | 66 |
3 files changed, 67 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 930a3383..ab090a04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- Try to detect `target-dir` for rtic-expansion.rs - Bump RTIC dependencies also updated to v1.0.0 - Edition 2021 - Change default `idle` behaviour to be `NOP` instead of `WFI` diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 8339549a..0c008c88 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -23,3 +23,6 @@ proc-macro-error = "1" quote = "1" syn = "1" rtic-syntax = "1.0.0" + +[features] +debugprint = [] diff --git a/macros/src/lib.rs b/macros/src/lib.rs index adcd7316..13b6a7c2 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -7,7 +7,7 @@ extern crate proc_macro; use proc_macro::TokenStream; -use std::{fs, path::Path}; +use std::{env, fs, path::Path}; use rtic_syntax::Settings; @@ -42,9 +42,69 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { let ts = codegen::app(&app, &analysis, &extra); + // 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()); + + // Assuming we are building for a thumbv* target + let target_triple_prefix = "thumbv"; + + // Check for special scenario where default target/ directory is not present + // + // This is configurable in .cargo/config: + // + // [build] + // target-dir = "target" + #[cfg(feature = "debugprint")] + println!("OUT_DIR\n{:#?}", out_str); + + 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() { + if dir + .as_os_str() + .to_str() + .unwrap() + .starts_with(target_triple_prefix) + //|| path.ends_with(&out_dir_root) + { + if let Some(out) = path.parent() { + out_dir = out; + #[cfg(feature = "debugprint")] + println!("{:#?}\n", out_dir); + break; + } else { + // If no parent, just use it + out_dir = path; + break; + } + } + } + } + } else { + #[cfg(feature = "debugprint")] + println!("\ntarget/ exists\n"); + } + // Try to write the expanded code to disk - if Path::new("target").exists() { - fs::write("target/rtic-expansion.rs", ts.to_string()).ok(); + 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() |