Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature to stop linking the default panic #812

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- [#821]: Cleanup
- [#813]: doc: add note for the alloc feature flag
- [#812]: `defmt`: Add a feature to stop linking a default panic handler
- [#811]: `book`: Add some examples for byte slice/array hints as well
- [#800]: `defmt-macros`: Fix generic trait bounds in Format derive macro

[#821]: https://github.com/knurling-rs/defmt/pull/821
[#813]: https://github.com/knurling-rs/defmt/pull/813
[#812]: https://github.com/knurling-rs/defmt/pull/812
[#811]: https://github.com/knurling-rs/defmt/pull/811
[#800]: https://github.com/knurling-rs/defmt/pull/800

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,9 +1,16 @@
use std::{env, error::Error, fs, path::PathBuf};

fn main() -> Result<(), Box<dyn Error>> {
// Read the linker script
let mut linker_script = fs::read_to_string("defmt.x.in")?;

// Optionally exclude default panic handler
if cfg!(feature = "avoid-default-panic") {
linker_script = avoid_default_panic(linker_script);
}

// 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)?;
println!("cargo:rustc-link-search={}", out.display());
let target = env::var("TARGET")?;
Expand All @@ -23,3 +30,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}
Ok(())
}

fn avoid_default_panic(linker_script: String) -> String {
linker_script.replacen("PROVIDE(_defmt_panic = __defmt_default_panic);", "", 1)
}
Loading