Skip to content

Commit

Permalink
swap to bindgen cli for no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobrosenthal committed Jan 28, 2020
1 parent 7915db9 commit 78a4423
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
1 change: 0 additions & 1 deletion wasm3-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ wasi = []
libc = "^0.2"

[build-dependencies]
bindgen = "0.52"
cc = "1"
73 changes: 50 additions & 23 deletions wasm3-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,66 @@ use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io;
use std::io::BufWriter;
use std::io::Write;
use std::path::PathBuf;

fn gen_bindings() -> io::Result<()> {
let whitelist_regex =
"((?:I|c_)?[Mm]3.*)|.*Page.*|Module_.*|EmitWord_impl|op_CallRawFunction|Compile_Function";
let bindgen = bindgen::builder()
.use_core()
.ctypes_prefix("libc")
.layout_tests(false)
.generate_comments(false)
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.whitelist_function(whitelist_regex)
.whitelist_type(whitelist_regex)
.whitelist_var(whitelist_regex)
.derive_debug(false);
let bindgen = fs::read_dir("wasm3/source")?
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().and_then(OsStr::to_str) == Some("h"))
.fold(bindgen, |bindgen, path| {
bindgen.header(path.to_str().unwrap())
});

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

let wrapper_file = out_path.join("wrapper.h");
{
let file = fs::File::create(wrapper_file.clone()).unwrap();
let mut file = BufWriter::new(file);
for path in fs::read_dir("wasm3/source")
.unwrap()
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().and_then(OsStr::to_str) == Some("h"))
{
writeln!(file, "#include <{}>", path.to_str().unwrap()).unwrap();
}
}

let mut bindgen = std::process::Command::new("bindgen");

let bindgen = bindgen
.arg(wrapper_file.to_str().unwrap())
.arg("--use-core")
.arg("--ctypes-prefix")
.arg("libc")
.arg("--no-layout-tests")
.arg("--no-doc-comments")
.arg("--whitelist-function")
.arg(whitelist_regex)
.arg("--whitelist-type")
.arg(whitelist_regex)
.arg("--whitelist-var")
.arg(whitelist_regex)
.arg("--no-derive-debug");

let bindgen = [
"f64", "f32", "u64", "i64", "u32", "i32", "u16", "i16", "u8", "i8",
]
.iter()
.fold(bindgen, |bindgen, &ty| bindgen.blacklist_type(ty));
.fold(bindgen, |bindgen, &ty| {
bindgen.arg("--blacklist-type").arg(ty)
});

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindgen
.generate()
.expect("Unable to generate bindings")
.write_to_file(out_path.join("bindings.rs"))
let status = bindgen
.arg("-o")
.arg(out_path.join("bindings.rs").to_str().unwrap())
.status()
.expect("Unable to generate bindings");

if !status.success() {
panic!("Failed to run bindgen: {:?}", status);
}

Ok(())
}

fn main() -> io::Result<()> {
Expand Down

0 comments on commit 78a4423

Please sign in to comment.