-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
39 lines (34 loc) · 1.57 KB
/
build.rs
File metadata and controls
39 lines (34 loc) · 1.57 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
use std::path::PathBuf;
fn main() {
// Only apply bare-metal linker settings when targeting a no_std platform.
// This allows `cargo publish` verification (which builds for the host) to succeed.
let target = std::env::var("TARGET").unwrap_or_default();
if !target.contains("-none") {
return;
}
if std::env::var("CARGO_FEATURE_PAYLOAD").is_ok() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-arg=-T{}/payload/linker.ld", manifest_dir);
println!("cargo:rustc-link-arg=-nostdlib");
println!("cargo:rustc-link-arg=-static");
return;
}
// The linker script is generated by axhal's build.rs at:
// target/<target_triple>/<profile>/linker_<platform>.lds
let out_dir = std::env::var("OUT_DIR").unwrap();
let profile_dir = PathBuf::from(&out_dir).join("../../..");
let profile_dir = std::fs::canonicalize(&profile_dir)
.unwrap_or_else(|_| PathBuf::from(&out_dir).join("../../.."));
let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let platform = match arch.as_str() {
"riscv64" => "riscv64-qemu-virt",
"aarch64" => "aarch64-qemu-virt",
"x86_64" => "x86-pc",
"loongarch64" => "loongarch64-qemu-virt",
other => panic!("Unsupported architecture: {other}"),
};
let lds_path = profile_dir.join(format!("linker_{platform}.lds"));
println!("cargo:rustc-link-arg=-T{}", lds_path.display());
println!("cargo:rustc-link-arg=-no-pie");
println!("cargo:rustc-link-arg=-znostart-stop-gc");
}