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 authored and Veykril committed Jan 28, 2020
1 parent 56fdcf5 commit 6e53656
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/target
Cargo.lock
wrapper.h
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"
80 changes: 54 additions & 26 deletions wasm3-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,71 @@
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io;
use std::io::{BufWriter, Result, Write};
use std::path::PathBuf;

fn gen_bindings() -> io::Result<()> {
fn gen_bindings() -> 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 root_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

let wrapper_file = root_path.join("wrapper.h");
let wrapper_file = wrapper_file.to_str().unwrap();

{
let file = fs::File::create(wrapper_file).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)
.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<()> {
fn main() -> Result<()> {
gen_bindings()?;
// build
let mut cfg = cc::Build::new();
Expand Down

0 comments on commit 6e53656

Please sign in to comment.