Files
rust-as/build.rs
2023-04-28 20:41:36 +02:00

92 lines
3.3 KiB
Rust

use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
#[derive(Debug)]
struct ParseCallback();
impl bindgen::callbacks::ParseCallbacks for ParseCallback {
fn enum_variant_name(
&self,
enum_name: Option<&str>,
original_variant_name: &str,
_variant_value: bindgen::callbacks::EnumVariantValue,
) -> Option<String> {
if let Some(name) = enum_name {
if name == "FeReg" {
return Some(original_variant_name.strip_prefix("FE_").unwrap_or(original_variant_name).to_owned());
}
}
None
}
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if name == "FE_ADDR32" || name == "FE_NOREG" || name == "FE_JMPL" {
return bindgen::callbacks::MacroParsingBehavior::Default;
}
if name.starts_with("FE_") {
// prevent instructions from being parsed
return bindgen::callbacks::MacroParsingBehavior::Ignore;
}
return bindgen::callbacks::MacroParsingBehavior::Default;
}
}
fn main() {
let build_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("fadec-build");
let build_path = build_path.to_str().unwrap();
// build faenc
{
let mut config = HashMap::new();
config.insert("wasm_build", "true");
let config = meson_next::config::Config::new().options(config);
println!("cargo:rustc-link-lib=fadec");
println!("cargo:rustc-link-search=native={}", build_path);
meson_next::build("extern/fadec", build_path, config);
}
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=extern/fadec");
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("extern/fadec/fadec-enc.h")
.clang_arg(&format!("-I{}", build_path))
.rustified_enum("FeReg")
.parse_callbacks(Box::new(ParseCallback()))
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
// you wouldn't expect it but bindgen refuses to parse a file that does not end with '.h'
// lmao
std::fs::copy(format!("{}/fadec-encode-public.inc", build_path), format!("{}/fadec-encode-public.h", build_path)).expect("copy failed");
let inst_bindings = bindgen::Builder::default()
.header(format!("{}/fadec-encode-public.h", build_path))
.generate()
.expect("Unable to generate bindings");
inst_bindings.write_to_file(out_path.join("inst_bindings.rs")).expect("Couldn't write inst_bindings!");
}