forked from esp-rs/esp-flasher-stub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
73 lines (57 loc) · 2.04 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
use std::{env, error::Error, fs, path::PathBuf};
fn main() -> Result<(), Box<dyn Error>> {
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-search)={}", out.display());
let chip = if cfg!(feature = "esp32") {
"esp32"
} else if cfg!(feature = "esp32c2") {
"esp32c2"
} else if cfg!(feature = "esp32c3") {
"esp32c3"
} else if cfg!(feature = "esp32c6") {
"esp32c6"
} else if cfg!(feature = "esp32h2") {
"esp32h2"
} else if cfg!(feature = "esp32s2") {
"esp32s2"
} else if cfg!(feature = "esp32s3") {
"esp32s3"
} else {
panic!("Must select exactly one chip feature!")
};
let arch = match chip {
"esp32" | "esp32s2" | "esp32s3" => "xtensa",
_ => "riscv",
};
// Define configuration symbols:
println!("cargo:rustc-cfg={chip}");
println!("cargo:rustc-cfg={arch}");
// Define any USB-related configuration symbols, if required:
if cfg!(feature = "esp32c3")
|| cfg!(feature = "esp32c6")
|| cfg!(feature = "esp32h2")
|| cfg!(feature = "esp32s3")
{
println!("cargo:rustc-cfg=usb_device");
}
if cfg!(feature = "esp32s2") || cfg!(feature = "esp32s3") {
println!("cargo:rustc-cfg=usb0");
}
// Copy required linker scripts to the `out` path:
let ld_path = PathBuf::from("ld");
let stub_x = format!("{}_stub.x", chip);
fs::copy(ld_path.join(&stub_x), out.join(&stub_x))?;
println!("cargo:rerun-if-changed=ld/{stub_x}");
println!("cargo:rustc-link-arg=-Tld/{stub_x}");
let rom_x = format!("{}_rom.x", chip);
fs::copy(ld_path.join(&rom_x), out.join(&rom_x))?;
println!("cargo:rerun-if-changed=ld/{rom_x}");
println!("cargo:rustc-link-arg=-Tld/{rom_x}");
// The RISC-V devices additionally require the `hal-defaults.x` linker
// script from `esp-hal`, to avoid interrupt-related linker errors:
if arch == "riscv" {
println!("cargo:rustc-link-arg=-Thal-defaults.x");
}
// Done!
Ok(())
}