| 1 |
use std::{env, error::Error, fs, path::PathBuf}; |
| 2 |
|
| 3 |
fn main() -> Result<(), Box<dyn Error>> { |
| 4 |
// Read the linker script |
| 5 |
let mut linker_script = fs::read_to_string("defmt.x.in")?; |
| 6 |
|
| 7 |
// Optionally exclude default panic handler |
| 8 |
if cfg!(feature = "avoid-default-panic") { |
| 9 |
linker_script = avoid_default_panic(linker_script); |
| 10 |
} |
| 11 |
|
| 12 |
// Put the linker script somewhere the linker can find it |
| 13 |
let out = &PathBuf::from(env::var("OUT_DIR")?); |
| 14 |
fs::write(out.join("defmt.x"), linker_script)?; |
| 15 |
println!("cargo:rustc-link-search={}", out.display()); |
| 16 |
let target = env::var("TARGET")?; |
| 17 |
|
| 18 |
// `"atomic-cas": false` in `--print target-spec-json` |
| 19 |
// last updated: rust 1.48.0 |
| 20 |
match &target[..] { |
| 21 |
"avr-gnu-base" |
| 22 |
| "msp430-none-elf" |
| 23 |
| "riscv32i-unknown-none-elf" |
| 24 |
| "riscv32imc-unknown-none-elf" |
| 25 |
| "thumbv4t-none-eabi" |
| 26 |
| "thumbv6m-none-eabi" |
| 27 |
| "xtensa-esp32s2-none-elf" => { |
| 28 |
println!("cargo:rustc-cfg=no_cas"); |
| 29 |
} |
| 30 |
_ => {} |
| 31 |
} |
| 32 |
|
| 33 |
// allow #[cfg(cfg_name)] |
| 34 |
for cfg_name in ["c_variadic", "no_cas"] { |
| 35 |
println!("cargo:rustc-check-cfg=cfg({cfg_name})"); |
| 36 |
} |
| 37 |
|
| 38 |
Ok(()) |
| 39 |
} |
| 40 |
|
| 41 |
fn avoid_default_panic(linker_script: String) -> String { |
| 42 |
linker_script.replacen("PROVIDE(_defmt_panic = __defmt_default_panic);", "", 1) |
| 43 |
} |