2024-03-12 13:49:02 +02:00
|
|
|
use std::{fs, path::Path};
|
|
|
|
|
|
|
|
use abi_generator::{
|
|
|
|
abi::{ty::TypeWidth, AbiBuilder},
|
|
|
|
syntax::UnwrapFancy,
|
|
|
|
TargetEnv,
|
|
|
|
};
|
|
|
|
|
2023-12-28 10:39:47 +02:00
|
|
|
fn add_file<'a>(build: &'a mut cc::Build, name: &'a str) -> &'a mut cc::Build {
|
|
|
|
println!("cargo:rerun-if-changed={}", name);
|
|
|
|
build.file(name)
|
|
|
|
}
|
|
|
|
|
2024-03-13 14:25:02 +02:00
|
|
|
fn load_abi_definitions<P: AsRef<Path>>(path: P) -> String {
|
|
|
|
let mut content = String::new();
|
|
|
|
for file in fs::read_dir(path).unwrap() {
|
|
|
|
let file = file.unwrap();
|
|
|
|
if file.path().extension().map(|x| x == "abi").unwrap_or(false) {
|
|
|
|
println!("cargo:rerun-if-changed={}", file.path().display());
|
|
|
|
content.extend(fs::read_to_string(file.path()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content
|
|
|
|
}
|
|
|
|
|
2024-03-12 13:49:02 +02:00
|
|
|
fn build_libm() {
|
2023-12-28 10:39:47 +02:00
|
|
|
println!("cargo:rerun-if-changed=libm/private.h");
|
|
|
|
|
|
|
|
let mut build = cc::Build::new();
|
|
|
|
|
2024-01-04 22:40:07 +02:00
|
|
|
build
|
|
|
|
.compiler("clang")
|
|
|
|
.flag("-ffreestanding")
|
|
|
|
.flag("-nostdlib");
|
2023-12-28 10:39:47 +02:00
|
|
|
|
|
|
|
add_file(&mut build, "libm/s_truncf.c");
|
|
|
|
add_file(&mut build, "libm/s_ceilf.c");
|
|
|
|
add_file(&mut build, "libm/s_floorf.c");
|
2023-12-28 17:18:05 +02:00
|
|
|
add_file(&mut build, "libm/s_fabsf.c");
|
|
|
|
add_file(&mut build, "libm/s_scalbnf.c");
|
|
|
|
add_file(&mut build, "libm/s_copysignf.c");
|
|
|
|
add_file(&mut build, "libm/e_powf.c");
|
|
|
|
add_file(&mut build, "libm/e_sqrtf.c");
|
|
|
|
add_file(&mut build, "libm/e_hypotf.c");
|
|
|
|
add_file(&mut build, "libm/e_fmodf.c");
|
|
|
|
add_file(&mut build, "libm/w_powf.c");
|
|
|
|
add_file(&mut build, "libm/w_hypotf.c");
|
|
|
|
add_file(&mut build, "libm/w_fmodf.c");
|
2023-12-28 10:39:47 +02:00
|
|
|
|
|
|
|
build.compile("m");
|
|
|
|
}
|
2024-03-12 13:49:02 +02:00
|
|
|
|
2024-03-13 14:25:02 +02:00
|
|
|
fn generate_abi<P: AsRef<Path>>(abi_path: P) {
|
2024-03-12 13:49:02 +02:00
|
|
|
let output_dir = std::env::var("OUT_DIR").expect("$OUT_DIR not set");
|
|
|
|
let generated_calls = Path::new(&output_dir).join("generated_calls.rs");
|
|
|
|
|
2024-03-13 14:25:02 +02:00
|
|
|
let abi = load_abi_definitions(abi_path);
|
2024-03-12 13:49:02 +02:00
|
|
|
let abi = AbiBuilder::from_string(
|
2024-03-13 14:25:02 +02:00
|
|
|
&abi,
|
2024-03-12 13:49:02 +02:00
|
|
|
TargetEnv {
|
|
|
|
thin_pointer_width: TypeWidth::U64,
|
|
|
|
fat_pointer_width: TypeWidth::U64,
|
|
|
|
},
|
|
|
|
)
|
2024-03-13 14:25:02 +02:00
|
|
|
.unwrap_fancy(&abi);
|
2024-03-12 13:49:02 +02:00
|
|
|
|
|
|
|
let calls = prettyplease::unparse(
|
|
|
|
&abi.emit_file(false, true)
|
|
|
|
.unwrap_fancy("Could not emit system calls"),
|
|
|
|
);
|
|
|
|
|
|
|
|
fs::write(generated_calls, calls).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
build_libm();
|
2024-03-13 14:25:02 +02:00
|
|
|
generate_abi("../abi/def");
|
2024-03-12 13:49:02 +02:00
|
|
|
}
|