-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbuild.rs
More file actions
68 lines (59 loc) · 2.43 KB
/
build.rs
File metadata and controls
68 lines (59 loc) · 2.43 KB
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
//! SPDX-License-Identifier: MIT OR Apache-2.0
//!
//! Copyright (c) 2021–2024 The rp-rs Developers
//! Copyright (c) 2021 rp-rs organization
//! Copyright (c) 2025 Raspberry Pi Ltd.
//!
//! Set up linker scripts
use std::fs::{ File, read_to_string };
use std::io::Write;
use std::path::PathBuf;
use regex::Regex;
fn main() {
println!("cargo::rustc-check-cfg=cfg(rp2040)");
println!("cargo::rustc-check-cfg=cfg(rp2350)");
// Put the linker script somewhere the linker can find it
let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=.pico-rs");
let contents = read_to_string(".pico-rs")
.map(|s| s.trim().to_string().to_lowercase())
.unwrap_or_else(|e| {
eprintln!("Failed to read file: {}", e);
String::new()
});
// The file `memory.x` is loaded by cortex-m-rt's `link.x` script, which
// is what we specify in `.cargo/config.toml` for Arm builds
let target;
if contents == "rp2040" {
target = "thumbv6m-none-eabi";
let memory_x = include_bytes!("rp2040.x");
let mut f = File::create(out.join("memory.x")).unwrap();
f.write_all(memory_x).unwrap();
println!("cargo::rustc-cfg=rp2040");
println!("cargo:rerun-if-changed=rp2040.x");
} else {
if contents.contains("riscv") {
target = "riscv32imac-unknown-none-elf";
} else {
target = "thumbv8m.main-none-eabihf";
}
let memory_x = include_bytes!("rp2350.x");
let mut f = File::create(out.join("memory.x")).unwrap();
f.write_all(memory_x).unwrap();
println!("cargo::rustc-cfg=rp2350");
println!("cargo:rerun-if-changed=rp2350.x");
}
let re = Regex::new(r"target = .*").unwrap();
let config_toml = include_str!(".cargo/config.toml");
let result = re.replace(config_toml, format!("target = \"{}\"", target));
let mut f = File::create(".cargo/config.toml").unwrap();
f.write_all(result.as_bytes()).unwrap();
// The file `rp2350_riscv.x` is what we specify in `.cargo/config.toml` for
// RISC-V builds
let rp2350_riscv_x = include_bytes!("rp2350_riscv.x");
let mut f = File::create(out.join("rp2350_riscv.x")).unwrap();
f.write_all(rp2350_riscv_x).unwrap();
println!("cargo:rerun-if-changed=rp2350_riscv.x");
println!("cargo:rerun-if-changed=build.rs");
}