yggdrasil/kernel/build.rs

75 lines
2.0 KiB
Rust
Raw Normal View History

2023-08-05 16:32:12 +03:00
use std::{
2024-03-12 13:46:24 +02:00
env, fs,
2023-08-05 16:32:12 +03:00
io::{self, Write},
2024-03-12 13:46:24 +02:00
path::{Path, PathBuf},
2023-08-05 16:32:12 +03:00
process::Command,
};
2024-03-12 13:46:24 +02:00
use abi_generator::{
abi::{ty::TypeWidth, AbiBuilder},
syntax::UnwrapFancy,
TargetEnv,
};
2023-08-05 16:32:12 +03:00
fn build_x86_64() {
const DEFAULT_8086_AS: &str = "nasm";
const AP_BOOTSTRAP_S: &str = "src/arch/x86_64/boot/ap_boot.S";
println!("cargo:rerun-if-changed={}", AP_BOOTSTRAP_S);
let out_dir = env::var("OUT_DIR").unwrap();
let assembler = env::var("AS8086").unwrap_or(DEFAULT_8086_AS.to_owned());
let ap_bootstrap_out = PathBuf::from(out_dir).join("__x86_64_ap_boot.bin");
// Assemble the code
let output = Command::new(assembler.as_str())
.args([
"-fbin",
"-o",
ap_bootstrap_out.to_str().unwrap(),
AP_BOOTSTRAP_S,
])
.output()
.unwrap();
if !output.status.success() {
io::stderr().write_all(&output.stderr).ok();
panic!("{}: could not assemble {}", assembler, AP_BOOTSTRAP_S);
}
}
2024-03-12 13:46:24 +02:00
fn generate_syscall_dispatcher<P: AsRef<Path>>(out_dir: P) {
let abi: AbiBuilder = AbiBuilder::from_string(
yggdrasil_abi_def::ABI_FILE,
TargetEnv {
thin_pointer_width: TypeWidth::U64,
fat_pointer_width: TypeWidth::U128,
},
)
.unwrap_fancy(yggdrasil_abi_def::ABI_FILE);
2024-03-12 13:46:24 +02:00
let generated_dispatcher = out_dir.as_ref().join("generated_dispatcher.rs");
let file = prettyplease::unparse(
&abi.emit_syscall_dispatcher("handle_syscall", "impls")
.expect("Could not generate syscall dispatcher"),
2024-03-12 13:46:24 +02:00
);
fs::write(generated_dispatcher, file.as_bytes()).unwrap();
}
2023-08-05 16:32:12 +03:00
fn main() {
2024-03-12 13:46:24 +02:00
let out_dir = env::var("OUT_DIR").unwrap();
2023-08-05 16:32:12 +03:00
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
2024-03-13 01:54:00 +02:00
generate_syscall_dispatcher(out_dir);
2024-03-12 13:46:24 +02:00
2023-08-05 16:32:12 +03:00
println!("cargo:rerun-if-changed=build.rs");
match arch.as_str() {
"x86_64" => build_x86_64(),
"aarch64" => (),
_ => panic!("Unknown target arch: {:?}", arch),
}
}