use std::{ env, fs, io::{self, Write}, path::{Path, PathBuf}, process::Command, }; use abi_generator::{ abi::{ty::TypeWidth, AbiBuilder}, syntax::UnwrapFancy, TargetEnv, }; 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); } } fn generate_syscall_dispatcher>(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(""); let generated_dispatcher = out_dir.as_ref().join("generated_dispatcher.rs"); let file = prettyplease::unparse( &abi.emit_syscall_dispatcher("handle_syscall", "impls") .unwrap_fancy(""), ); fs::write(generated_dispatcher, file.as_bytes()).unwrap(); } fn main() { let out_dir = env::var("OUT_DIR").unwrap(); let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); generate_syscall_dispatcher(&out_dir); println!("cargo:rerun-if-changed=build.rs"); match arch.as_str() { "x86_64" => build_x86_64(), "aarch64" => (), _ => panic!("Unknown target arch: {:?}", arch), } }