rpk_config/
builder.rs

1use std::{env, fs, io::Write, path::PathBuf};
2
3/// This build script copies the `memory.x` file from the crate root into
4/// a directory where the linker can always find it at build time.
5/// You may instead place the `memory.x` in the `default-layout.rpk.conf` file.
6pub fn build_rs() {
7    let manifest =
8        PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("missing CARGO_MANIFEST_DIR"));
9    let memoryx = manifest.join("memory.x");
10    if !fs::exists(&memoryx).unwrap() {
11        panic!("Missing memory.x file!");
12    }
13
14    let out = &PathBuf::from(env::var_os("OUT_DIR").expect("missing OUT_DIR"));
15    fs::File::create(out.join("memory.x"))
16        .and_then(|mut f| f.write_all(fs::read(memoryx)?.as_slice()))
17        .expect("can't create memory.x");
18
19    println!("cargo:rustc-link-search={}", out.display());
20
21    println!("cargo:rerun-if-changed=memory.x");
22    println!("cargo:rustc-link-arg-bins=--nmagic");
23    println!("cargo:rustc-link-arg-bins=-Tlink.x");
24    println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");
25
26    if env::var_os("CARGO_FEATURE_DEFMT").is_some() {
27        println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
28    }
29}