-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbuild.rs
151 lines (114 loc) · 5.54 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::{error, env, fs::File, io::Write, path::Path};
use std::process::Command;
use std::str;
/// Waiting on cargo fix!
// #[derive(serde::Deserialize)]
struct Config {
parameters: Parameters,
}
// #[derive(serde::Deserialize)]
struct Parameters {
filesystem_boundary: u32,
}
macro_rules! add_build_variable{
($file:expr, $name:literal, u8) => {
let value = env!($name);
let value: u8 = str::parse(value).expect("Version components must be able to fit in a u8.");
writeln!($file, "pub const {}: u8 = {};", $name, value)
.expect("Could not write build_constants.rs file");
};
($file:expr, $name:literal, u16) => {
let value = env!($name);
let value: u16 = str::parse(value).expect("Version components must be able to fit in a u16.");
writeln!($file, "pub const {}: u16 = {};", $name, value)
.expect("Could not write build_constants.rs file");
};
($file:expr, $name:literal, $value:expr, u32) => {
writeln!($file, "pub const {}: u32 = {};", $name, $value)
.expect("Could not write build_constants.rs file");
};
($file:expr, $name:literal, $value:expr, usize) => {
writeln!($file, "pub const {}: usize= {};", $name, $value)
.expect("Could not write build_constants.rs file");
};
($file:expr, $name:literal, $value:expr) => {
writeln!($file, "pub const {}: &'static str = \"{}\";", $name, $value)
.expect("Could not write build_constants.rs file");
}
}
fn write_memory_x(linker_script_file: &mut File, parameters: &Parameters){
writeln!(linker_script_file,
"
/* DO NOT EDIT THIS FILE */
/* This file was generated by build.rs */
MEMORY
{{
FLASH : ORIGIN = 0x00000000, LENGTH = {}K
FILESYSTEM: ORIGIN = 0x{:08X}, LENGTH = {}K
/* for use with standard link.x */
RAM : ORIGIN = 0x20000000, LENGTH = 256K
/* would be used with proper link.x */
/* needs changes to r0 (initialization code) */
/* SRAM0 : ORIGIN = 0x20000000, LENGTH = 64K */
/* SRAM1 : ORIGIN = 0x20010000, LENGTH = 64K */
/* SRAM2 : ORIGIN = 0x20020000, LENGTH = 64K */
/* SRAM3 : ORIGIN = 0x20030000, LENGTH = 64K */
/* CASPER SRAM regions */
/* SRAMX0: ORIGIN = 0x1400_0000, LENGTH = 4K /1* to 0x1400_0FFF *1/ */
/* SRAMX1: ORIGIN = 0x1400_4000, LENGTH = 4K /1* to 0x1400_4FFF *1/ */
/* USB1 SRAM regin */
/* USB1_SRAM : ORIGIN = 0x40100000, LENGTH = 16K */
/* To define our own USB RAM section in one regular */
/* RAM, probably easiest to shorten length of RAM */
/* above, and use this freed RAM section */
}}
",
parameters.filesystem_boundary/1024, // code size in KB
parameters.filesystem_boundary, // Start address of filesystem
630 - parameters.filesystem_boundary/1024, // filesystem size in KB
).expect("Could not write memory.x");
}
fn main() -> Result<(), Box<dyn error::Error>> {
println!("cargo:rerun-if-changed=config/src/lib.rs");
println!("cargo:rerun-if-changed=cfg.toml");
let out_dir = env::var("OUT_DIR").expect("No out dir");
// We would like to put configuration variables in cfg.toml, but due to a Cargo bug,
// the serde feature flags will get merged with solo-bee's serde, causing build issues.
// So this will remain here until the Cargo bug gets fixed.
// let config = fs::read_to_string("cfg.toml")?;
// let config: Config = toml::from_str(&config)?;
// Hardcode until cargo issue gets fixed.
let config = Config {parameters: Parameters{filesystem_boundary: 0x8_0000}};
let linker_script_file = env::var("CARGO_MANIFEST_DIR").expect("No out dir");
let linker_script_file = Path::new(&linker_script_file).join("memory.x");
let mut linker_script_file = File::create(&linker_script_file).expect("Could not create file");
let dest_path = Path::new(&out_dir).join("build_constants.rs");
let mut f = File::create(&dest_path).expect("Could not create file");
let hash_long_cmd = Command::new("git").args(&["rev-parse", "HEAD"]).output().unwrap().stdout;
let hash_short_cmd = Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output().unwrap().stdout;
let hash_long =
str::from_utf8(&hash_long_cmd).unwrap();
let hash_short =
str::from_utf8(&hash_short_cmd).unwrap();
writeln!(&mut f, "pub mod build_constants {{").expect("Could not write build_constants.rs.");
add_build_variable!(&mut f, "CARGO_PKG_VERSION_MAJOR", u8);
add_build_variable!(&mut f, "CARGO_PKG_VERSION_MINOR", u16);
add_build_variable!(&mut f, "CARGO_PKG_VERSION_PATCH", u8);
add_build_variable!(&mut f, "CARGO_PKG_HASH", hash_long);
add_build_variable!(&mut f, "CARGO_PKG_HASH_SHORT", hash_short);
// Add integer version of the version number
let major: u32 = str::parse(env!("CARGO_PKG_VERSION_MAJOR")).unwrap();
let minor: u32 = str::parse(env!("CARGO_PKG_VERSION_MINOR")).unwrap();
let patch: u32 = str::parse(env!("CARGO_PKG_VERSION_PATCH")).unwrap();
if major >= 1024 || minor > 9999 || patch >= 64 {
panic!("config.firmware.product can at most be 1023.9999.63 for versions in customer data");
}
let version_to_check: u32 =
(major << 22) |
(minor << 6) | patch;
add_build_variable!(&mut f, "CARGO_PKG_VERSION", version_to_check, u32);
add_build_variable!(&mut f, "CONFIG_FILESYSTEM_BOUNDARY", config.parameters.filesystem_boundary, usize);
writeln!(&mut f, "}}").expect("Could not write build_constants.rs.");
write_memory_x(&mut linker_script_file, &config.parameters);
Ok(())
}