-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.rs
114 lines (92 loc) · 3.73 KB
/
build.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::str;
use std::{env, error, path::Path};
use memory_regions::MemoryRegions;
#[cfg(feature = "board-nk3xn")]
const MEMORY_REGIONS: &MemoryRegions = &MemoryRegions::NK3XN;
#[cfg(feature = "board-nk3am")]
const MEMORY_REGIONS: &MemoryRegions = &MemoryRegions::NK3AM;
#[derive(Eq, PartialEq)]
enum SocType {
Lpc55,
Nrf52840,
}
fn check_build_triplet() -> SocType {
let target = env::var("TARGET").expect("$TARGET unset");
let soc_is_lpc55 = env::var_os("CARGO_FEATURE_SOC_LPC55").is_some();
let soc_is_nrf52840 = env::var_os("CARGO_FEATURE_SOC_NRF52").is_some();
if soc_is_lpc55 && !soc_is_nrf52840 {
if target != "thumbv8m.main-none-eabi" {
panic!(
"Wrong build triplet for LPC55, expecting thumbv8m.main-none-eabi, got {}",
target
);
}
SocType::Lpc55
} else if soc_is_nrf52840 && !soc_is_lpc55 {
if target != "thumbv7em-none-eabihf" {
panic!(
"Wrong build triplet for NRF52840, expecting thumbv7em-none-eabihf, got {}",
target
);
}
SocType::Nrf52840
} else {
panic!("Multiple or no SOC features set.");
}
}
fn generate_memory_x(outpath: &Path, template: &str, regions: &MemoryRegions) {
let buildrs_caveat = r#"/* DO NOT EDIT THIS FILE */
/* This file was generated by build.rs */
"#;
let template = std::fs::read_to_string(template).expect("cannot read memory.x template file");
let fw_len = regions.firmware.len();
let template = template.replace("##FLASH_LENGTH##", &format!("{fw_len:#X}"));
let fs_len = regions.filesystem.len();
let template = template.replace("##FS_LENGTH##", &format!("{fs_len:#X}"));
let template = template.replace("##FS_BASE##", &format!("{:#X}", regions.filesystem.start));
let template = template.replace("##FLASH_BASE##", &format!("{:#X}", regions.firmware.start));
std::fs::write(outpath, [buildrs_caveat, &template].join("")).expect("cannot write memory.x");
}
fn main() -> Result<(), Box<dyn error::Error>> {
println!(
"cargo:rustc-env=NK3_FIRMWARE_VERSION={}",
utils::version_string("nitrokey-3-firmware", env!("CARGO_PKG_VERSION"))
);
// @todo: add profile 'platform' items and cross-check them here ...
let soc_type = check_build_triplet();
if MEMORY_REGIONS.filesystem.start & 0x3ff != 0 {
panic!("filesystem boundary is not a multiple of the flash block size (1KB)");
}
// @todo: move this decision into 'profile.cfg'
let (memory_x_infix, template_file) = match soc_type {
SocType::Lpc55 => ("ld/lpc55", "../ld/lpc55-memory-template.x"),
SocType::Nrf52840 => ("ld/nrf52", "../ld/nrf52-memory-template.x"),
};
println!("cargo:rerun-if-changed={}", template_file);
println!("cargo:rerun-if-changed={}", template_file);
let memory_x_dir =
Path::new(&env::var("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR not set"))
.join(memory_x_infix);
std::fs::create_dir_all(&memory_x_dir).ok();
let memory_x = memory_x_dir.join("custom_memory.x");
generate_memory_x(&memory_x, template_file, MEMORY_REGIONS);
println!(
"cargo:rustc-link-search={}/../ld",
env!("CARGO_MANIFEST_DIR")
);
println!(
"cargo:rustc-link-search={}/{}",
env!("CARGO_MANIFEST_DIR"),
memory_x_infix
);
let lockfile =
cargo_lock::Lockfile::load(Path::new(env!("CARGO_MANIFEST_DIR")).join("../../Cargo.lock"))?;
let pkg_cortex_m_rt = lockfile
.packages
.iter()
.find(|p| p.name.as_str() == "cortex-m-rt");
if let Some(p) = pkg_cortex_m_rt {
println!("cargo:rustc-link-arg=-Tcortex-m-rt_{}_link.x", p.version);
}
Ok(())
}