-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathbuild.rs
145 lines (126 loc) · 3.96 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
use std::{
env,
fs::File,
io::{self, prelude::*},
path::PathBuf,
};
fn main() -> Result<(), Error> {
let target = Target::read();
copy_memory_config(target)?;
println!("cargo:rerun-if-changed=build.rs");
Ok(())
}
/// Make `memory.x` available to dependent crates
fn copy_memory_config(target: Target) -> Result<(), Error> {
let memory_x = match target.sub_family {
SubFamily::Stm32f722
| SubFamily::Stm32f723
| SubFamily::Stm32f732
| SubFamily::Stm32f733 => include_bytes!("memory_512_176.x").as_ref(),
SubFamily::Stm32f745 | SubFamily::Stm32f746 | SubFamily::Stm32f756 => {
include_bytes!("memory_1024_320.x").as_ref()
}
SubFamily::Stm32f765
| SubFamily::Stm32f767
| SubFamily::Stm32f769
| SubFamily::Stm32f777
| SubFamily::Stm32f778
| SubFamily::Stm32f779 => include_bytes!("memory_2048_368.x").as_ref(),
SubFamily::Stm32f730 => include_bytes!("memory_64_176.x").as_ref(),
};
let out_dir = env::var("OUT_DIR")?;
let out_dir = PathBuf::from(out_dir);
File::create(out_dir.join("memory.x"))?.write_all(memory_x)?;
// Tell Cargo where to find the file.
println!("cargo:rustc-link-search={}", out_dir.display());
println!("cargo:rerun-if-changed=memory_1024_320.x");
println!("cargo:rerun-if-changed=memory_2048_368.x");
println!("cargo:rerun-if-changed=memory_512_176.x");
println!("cargo:rerun-if-changed=memory_64_176.x");
println!("cargo:rerun-if-changed=memory_64_240.x");
Ok(())
}
#[derive(Clone, Copy)]
struct Target {
sub_family: SubFamily,
}
impl Target {
fn read() -> Self {
let sub_family = SubFamily::read();
Self { sub_family }
}
}
#[derive(Clone, Copy)]
enum SubFamily {
Stm32f722,
Stm32f723,
Stm32f730,
Stm32f732,
Stm32f733,
Stm32f745,
Stm32f746,
Stm32f756,
Stm32f765,
Stm32f767,
Stm32f769,
Stm32f777,
Stm32f778,
Stm32f779,
}
impl SubFamily {
fn read() -> Self {
if cfg!(feature = "stm32f722") {
SubFamily::Stm32f722
} else if cfg!(feature = "stm32f723") {
SubFamily::Stm32f723
} else if cfg!(feature = "stm32f730") || cfg!(feature = "stm32f730-lpc") {
SubFamily::Stm32f730
} else if cfg!(feature = "stm32f732") {
SubFamily::Stm32f732
} else if cfg!(feature = "stm32f733") {
SubFamily::Stm32f733
} else if cfg!(feature = "stm32f745") {
SubFamily::Stm32f745
} else if cfg!(feature = "stm32f746") {
SubFamily::Stm32f746
} else if cfg!(feature = "stm32f756") {
SubFamily::Stm32f756
} else if cfg!(feature = "stm32f765") {
SubFamily::Stm32f765
} else if cfg!(feature = "stm32f767") {
SubFamily::Stm32f767
} else if cfg!(feature = "stm32f769") {
SubFamily::Stm32f769
} else if cfg!(feature = "stm32f777") {
SubFamily::Stm32f777
} else if cfg!(feature = "stm32f778") {
SubFamily::Stm32f778
} else if cfg!(feature = "stm32f779") {
SubFamily::Stm32f779
} else {
error("You must select a target.
If you added Stm32f7xx HAL as a dependency to your crate, you can select a target by enabling the respective feature in `Cargo.toml`.
If you're running an example from the repository, select a target by passing the desired target as a command-line argument, for example `--features=stm32f746`.
Please refer to the documentation for more details."
)
}
}
}
#[derive(Debug)]
enum Error {
Env(env::VarError),
Io(io::Error),
}
impl From<env::VarError> for Error {
fn from(error: env::VarError) -> Self {
Self::Env(error)
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
fn error(message: &str) -> ! {
panic!("\n\n\n{}\n\n\n", message);
}