diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cf5eb91..6b5a9caa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- [#812]: `defmt`: Add a feature to stop linking a default panic handler + +[#812]: https://github.com/knurling-rs/defmt/pull/812 + ## [v0.3.6] - 2024-02-05 - [#804]: `CI`: Remove mdbook strategy diff --git a/defmt/Cargo.toml b/defmt/Cargo.toml index f68e795b..112f577e 100644 --- a/defmt/Cargo.toml +++ b/defmt/Cargo.toml @@ -20,6 +20,7 @@ version = "0.3.6" [features] alloc = [] ip_in_core = [] +avoid-default-panic = [] # Encoding feature flags. These should only be set by end-user crates, not by library crates. # diff --git a/defmt/build.rs b/defmt/build.rs index 5456cb36..269c2ec8 100644 --- a/defmt/build.rs +++ b/defmt/build.rs @@ -1,10 +1,21 @@ use std::{env, error::Error, fs, path::PathBuf}; +fn sub(linker_script: String) -> String { + #[cfg(not(feature = "avoid-default-panic"))] + { + linker_script + } + #[cfg(feature = "avoid-default-panic")] + { + linker_script.replacen("PROVIDE(_defmt_panic = __defmt_default_panic);", "", 1) + } +} + fn main() -> Result<(), Box> { // Put the linker script somewhere the linker can find it let out = &PathBuf::from(env::var("OUT_DIR")?); let linker_script = fs::read_to_string("defmt.x.in")?; - fs::write(out.join("defmt.x"), linker_script)?; + fs::write(out.join("defmt.x"), sub(linker_script))?; println!("cargo:rustc-link-search={}", out.display()); let target = env::var("TARGET")?;