forked from arceos-org/arceos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
66 lines (59 loc) · 1.81 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
use std::io::Result;
const BUILTIN_PLATFORMS: &[&str] = &[
"aarch64-bsta1000b",
"aarch64-qemu-virt",
"aarch64-raspi4",
"riscv64-qemu-virt",
"x86_64-pc-oslab",
"x86_64-qemu-q35",
];
const BUILTIN_PLATFORM_FAMILIES: &[&str] = &[
"aarch64-bsta1000b",
"aarch64-qemu-virt",
"aarch64-raspi",
"riscv64-qemu-virt",
"x86-pc",
];
fn make_cfg_values(str_list: &[&str]) -> String {
str_list
.iter()
.map(|s| format!("{:?}", s))
.collect::<Vec<_>>()
.join(", ")
}
fn main() {
let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let platform = axconfig::PLATFORM;
if platform != "dummy" {
gen_linker_script(&arch, platform).unwrap();
}
println!("cargo:rustc-cfg=platform=\"{}\"", platform);
println!("cargo:rustc-cfg=platform_family=\"{}\"", axconfig::FAMILY);
println!(
"cargo::rustc-check-cfg=cfg(platform, values({}))",
make_cfg_values(BUILTIN_PLATFORMS)
);
println!(
"cargo::rustc-check-cfg=cfg(platform_family, values({}))",
make_cfg_values(BUILTIN_PLATFORM_FAMILIES)
);
}
fn gen_linker_script(arch: &str, platform: &str) -> Result<()> {
let fname = format!("linker_{}.lds", platform);
let output_arch = if arch == "x86_64" {
"i386:x86-64"
} else if arch.contains("riscv") {
"riscv" // OUTPUT_ARCH of both riscv32/riscv64 is "riscv"
} else {
arch
};
let ld_content = std::fs::read_to_string("linker.lds.S")?;
let ld_content = ld_content.replace("%ARCH%", output_arch);
let ld_content = ld_content.replace(
"%KERNEL_BASE%",
&format!("{:#x}", axconfig::KERNEL_BASE_VADDR),
);
let ld_content = ld_content.replace("%SMP%", &format!("{}", axconfig::SMP));
std::fs::write(fname, ld_content)?;
Ok(())
}