46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use std::{fs, path::Path};
|
|
|
|
use abi_generator::{
|
|
abi::{ty::TypeWidth, AbiBuilder},
|
|
syntax::UnwrapFancy,
|
|
TargetEnv,
|
|
};
|
|
|
|
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
|
|
}
|
|
|
|
fn generate_abi<P: AsRef<Path>>(abi_path: P) {
|
|
let output_dir = std::env::var("OUT_DIR").expect("$OUT_DIR not set");
|
|
let generated_types = Path::new(&output_dir).join("generated_types.rs");
|
|
|
|
let abi = load_abi_definitions(abi_path);
|
|
let abi = AbiBuilder::from_string(
|
|
&abi,
|
|
TargetEnv {
|
|
thin_pointer_width: TypeWidth::U64,
|
|
fat_pointer_width: TypeWidth::U64,
|
|
},
|
|
)
|
|
.unwrap_fancy(&abi);
|
|
|
|
let types = prettyplease::unparse(
|
|
&abi.emit_file(true, false)
|
|
.unwrap_fancy("Could not emit types"),
|
|
);
|
|
|
|
fs::write(generated_types, types).unwrap();
|
|
}
|
|
|
|
fn main() {
|
|
generate_abi("def");
|
|
}
|