Skip to content

Commit 21982bb

Browse files
swap to bindgen cli for no_std
1 parent 7915db9 commit 21982bb

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/target
22
Cargo.lock
3+
wrapper.h

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: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,71 @@
11
use std::env;
22
use std::ffi::{OsStr, OsString};
33
use std::fs;
4-
use std::io;
4+
use std::io::{BufWriter, Result, Write};
55
use std::path::PathBuf;
66

7-
fn gen_bindings() -> io::Result<()> {
7+
fn gen_bindings() -> Result<()> {
88
let whitelist_regex =
99
"((?: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-
});
10+
11+
let root_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
12+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
13+
14+
let wrapper_file = root_path.join("wrapper.h");
15+
let wrapper_file = wrapper_file.to_str().unwrap();
16+
17+
{
18+
let file = fs::File::create(wrapper_file).unwrap();
19+
let mut file = BufWriter::new(file);
20+
for path in fs::read_dir("wasm3/source")
21+
.unwrap()
22+
.filter_map(Result::ok)
23+
.map(|entry| entry.path())
24+
.filter(|path| path.extension().and_then(OsStr::to_str) == Some("h"))
25+
{
26+
writeln!(file, "#include <{}>", path.to_str().unwrap()).unwrap();
27+
}
28+
}
29+
30+
let mut bindgen = std::process::Command::new("bindgen");
31+
32+
let bindgen = bindgen
33+
.arg(wrapper_file)
34+
.arg("--use-core")
35+
.arg("--ctypes-prefix")
36+
.arg("libc")
37+
.arg("--no-layout-tests")
38+
.arg("--no-doc-comments")
39+
.arg("--whitelist-function")
40+
.arg(whitelist_regex)
41+
.arg("--whitelist-type")
42+
.arg(whitelist_regex)
43+
.arg("--whitelist-var")
44+
.arg(whitelist_regex)
45+
.arg("--no-derive-debug");
46+
2747
let bindgen = [
2848
"f64", "f32", "u64", "i64", "u32", "i32", "u16", "i16", "u8", "i8",
2949
]
3050
.iter()
31-
.fold(bindgen, |bindgen, &ty| bindgen.blacklist_type(ty));
51+
.fold(bindgen, |bindgen, &ty| {
52+
bindgen.arg("--blacklist-type").arg(ty)
53+
});
3254

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"))
55+
let status = bindgen
56+
.arg("-o")
57+
.arg(out_path.join("bindings.rs").to_str().unwrap())
58+
.status()
59+
.expect("Unable to generate bindings");
60+
61+
if !status.success() {
62+
panic!("Failed to run bindgen: {:?}", status);
63+
}
64+
65+
Ok(())
3866
}
3967

40-
fn main() -> io::Result<()> {
68+
fn main() -> Result<()> {
4169
gen_bindings()?;
4270
// build
4371
let mut cfg = cc::Build::new();

0 commit comments

Comments
 (0)