1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn main() {
let out = PathBuf::from(env::var_os("OUT_DIR").unwrap());
// 1. Write the custom memory mappings into the compiler path
File::create(out.join("memory.x")).unwrap().write_all(include_bytes!("memory.x")).unwrap();
// 2. FORCE GENERATION: Generate a clean defmt template locally.
// This provides the structural mapping sections defmt needs,
// ensuring rust-lld never fails with "cannot find linker script defmt.x".
File::create(out.join("defmt.x"))
.unwrap()
.write_all(
b"SECTIONS {
.defmt 1 (INFO) : {
. = 1;
*(.defmt.prim.*);
*(.defmt.trace.*);
*(.defmt.debug.*);
*(.defmt.info.*);
*(.defmt.warn.*);
*(.defmt.error.*);
*(.defmt.struct.*);
}
}
EXTERN(_defmt_panic);",
)
.unwrap();
// 3. Point Cargo to look in OUT_DIR for both memory.x and defmt.x
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rerun-if-changed=build.rs");
}