88 lines
3.3 KiB
Rust
88 lines
3.3 KiB
Rust
//! This build script copies the `memory.x` file from the crate root into
|
|
//! a directory where the linker can always find it at build time.
|
|
//! For many projects this is optional, as the linker always searches the
|
|
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
|
//! are using a workspace or have a more complicated build setup, this
|
|
//! build script becomes required. Additionally, by requesting that
|
|
//! Cargo re-run the build script whenever `memory.x` is changed,
|
|
//! updating `memory.x` ensures a rebuild of the application with the
|
|
//! new memory settings.
|
|
//!
|
|
//! The build script also sets the linker flags to tell it which link script to use.
|
|
|
|
use const_gen::*;
|
|
use std::fs::File;
|
|
use std::io::{Read, Write};
|
|
use std::path::{Path, PathBuf};
|
|
use std::{env, fs};
|
|
use xz2::read::XzEncoder;
|
|
|
|
fn main() {
|
|
// Generate vial config at the root of project
|
|
println!("cargo:rerun-if-changed=vial.json");
|
|
println!("cargo:rerun-if-changed=keyboard.toml");
|
|
|
|
generate_vial_config();
|
|
|
|
// Put `memory.x` in our output directory and ensure it's
|
|
// on the linker search path.
|
|
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
File::create(out.join("memory.x"))
|
|
.unwrap()
|
|
.write_all(include_bytes!("memory.x"))
|
|
.unwrap();
|
|
println!("cargo:rustc-link-search={}", out.display());
|
|
|
|
// By default, Cargo will re-run a build script whenever
|
|
// any file in the project changes. By specifying `memory.x`
|
|
// here, we ensure the build script is only re-run when
|
|
// `memory.x` is changed.
|
|
println!("cargo:rerun-if-changed=memory.x");
|
|
|
|
// Specify linker arguments.
|
|
|
|
// `--nmagic` is required if memory section addresses are not aligned to 0x10000,
|
|
// for example the FLASH and RAM sections in your `memory.x`.
|
|
// See https://github.com/rust-embedded/cortex-m-quickstart/pull/95
|
|
println!("cargo:rustc-link-arg=--nmagic");
|
|
|
|
// Set the linker script to the one provided by cortex-m-rt.
|
|
println!("cargo:rustc-link-arg=-Tlink.x");
|
|
|
|
// Set the extra linker script from defmt
|
|
println!("cargo:rustc-link-arg=-Tdefmt.x");
|
|
|
|
// Use flip-link overflow check: https://github.com/knurling-rs/flip-link
|
|
println!("cargo:rustc-linker=flip-link");
|
|
}
|
|
|
|
fn generate_vial_config() {
|
|
// Generated vial config file
|
|
let out_file = Path::new(&env::var_os("OUT_DIR").unwrap()).join("config_generated.rs");
|
|
|
|
let p = Path::new("vial.json");
|
|
let mut content = String::new();
|
|
match File::open(p) {
|
|
Ok(mut file) => {
|
|
file.read_to_string(&mut content)
|
|
.expect("Cannot read vial.json");
|
|
}
|
|
Err(e) => println!("Cannot find vial.json {:?}: {}", p, e),
|
|
};
|
|
|
|
let vial_cfg = json::stringify(json::parse(&content).unwrap());
|
|
let mut keyboard_def_compressed: Vec<u8> = Vec::new();
|
|
XzEncoder::new(vial_cfg.as_bytes(), 6)
|
|
.read_to_end(&mut keyboard_def_compressed)
|
|
.unwrap();
|
|
|
|
let keyboard_id: Vec<u8> = vec![0xB9, 0xBC, 0x09, 0xB2, 0x9D, 0x37, 0x4C, 0xEA];
|
|
let const_declarations = [
|
|
const_declaration!(pub VIAL_KEYBOARD_DEF = keyboard_def_compressed),
|
|
const_declaration!(pub VIAL_KEYBOARD_ID = keyboard_id),
|
|
]
|
|
.map(|s| "#[allow(clippy::redundant_static_lifetimes)]\n".to_owned() + s.as_str())
|
|
.join("\n");
|
|
fs::write(out_file, const_declarations).unwrap();
|
|
}
|