Skip to content

Commit 78a4423

Browse files
swap to bindgen cli for no_std
1 parent 7915db9 commit 78a4423

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

wasm3-sys/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ wasi = []
1313
libc = "^0.2"
1414

1515
[build-dependencies]
16-
bindgen = "0.52"
1716
cc = "1"

wasm3-sys/build.rs

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,66 @@ use std::env;
22
use std::ffi::{OsStr, OsString};
33
use std::fs;
44
use std::io;
5+
use std::io::BufWriter;
6+
use std::io::Write;
57
use std::path::PathBuf;
68

79
fn gen_bindings() -> io::Result<()> {
810
let whitelist_regex =
911
"((?:I|c_)?[Mm]3.*)|.*Page.*|Module_.*|EmitWord_impl|op_CallRawFunction|Compile_Function";
10-
let bindgen = bindgen::builder()
11-
.use_core()
12-
.ctypes_prefix("libc")
13-
.layout_tests(false)
14-
.generate_comments(false)
15-
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
16-
.whitelist_function(whitelist_regex)
17-
.whitelist_type(whitelist_regex)
18-
.whitelist_var(whitelist_regex)
19-
.derive_debug(false);
20-
let bindgen = fs::read_dir("wasm3/source")?
21-
.filter_map(Result::ok)
22-
.map(|entry| entry.path())
23-
.filter(|path| path.extension().and_then(OsStr::to_str) == Some("h"))
24-
.fold(bindgen, |bindgen, path| {
25-
bindgen.header(path.to_str().unwrap())
26-
});
12+
13+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
14+
15+
let wrapper_file = out_path.join("wrapper.h");
16+
{
17+
let file = fs::File::create(wrapper_file.clone()).unwrap();
18+
let mut file = BufWriter::new(file);
19+
for path in fs::read_dir("wasm3/source")
20+
.unwrap()
21+
.filter_map(Result::ok)
22+
.map(|entry| entry.path())
23+
.filter(|path| path.extension().and_then(OsStr::to_str) == Some("h"))
24+
{
25+
writeln!(file, "#include <{}>", path.to_str().unwrap()).unwrap();
26+
}
27+
}
28+
29+
let mut bindgen = std::process::Command::new("bindgen");
30+
31+
let bindgen = bindgen
32+
.arg(wrapper_file.to_str().unwrap())
33+
.arg("--use-core")
34+
.arg("--ctypes-prefix")
35+
.arg("libc")
36+
.arg("--no-layout-tests")
37+
.arg("--no-doc-comments")
38+
.arg("--whitelist-function")
39+
.arg(whitelist_regex)
40+
.arg("--whitelist-type")
41+
.arg(whitelist_regex)
42+
.arg("--whitelist-var")
43+
.arg(whitelist_regex)
44+
.arg("--no-derive-debug");
45+
2746
let bindgen = [
2847
"f64", "f32", "u64", "i64", "u32", "i32", "u16", "i16", "u8", "i8",
2948
]
3049
.iter()
31-
.fold(bindgen, |bindgen, &ty| bindgen.blacklist_type(ty));
50+
.fold(bindgen, |bindgen, &ty| {
51+
bindgen.arg("--blacklist-type").arg(ty)
52+
});
3253

33-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
34-
bindgen
35-
.generate()
36-
.expect("Unable to generate bindings")
37-
.write_to_file(out_path.join("bindings.rs"))
54+
let status = bindgen
55+
.arg("-o")
56+
.arg(out_path.join("bindings.rs").to_str().unwrap())
57+
.status()
58+
.expect("Unable to generate bindings");
59+
60+
if !status.success() {
61+
panic!("Failed to run bindgen: {:?}", status);
62+
}
63+
64+
Ok(())
3865
}
3966

4067
fn main() -> io::Result<()> {

0 commit comments

Comments
 (0)