Skip to content

Commit

Permalink
Add a feature to stop linking the default panic
Browse files Browse the repository at this point in the history
When end-user crates provide a panic handler, this symbol is not needed
... but the linker isn't smart enough to optimize it away, even though
the PROVIDE line isn't used (the symbol is provided by the rust function
tagged with #[panic_handler]).  Those crates can enable this feature to
remove the PROVIDE line from the linker script, to stop linking this
default panic handler.
  • Loading branch information
BryanKadzban committed Feb 20, 2024
1 parent f149fa8 commit fae2227
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions defmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
13 changes: 12 additions & 1 deletion defmt/build.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
// 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")?;

Expand Down

0 comments on commit fae2227

Please sign in to comment.