|
1 | 1 | use std::env;
|
2 | 2 | use std::ffi::{OsStr, OsString};
|
3 | 3 | use std::fs;
|
4 |
| -use std::io; |
| 4 | +use std::io::{BufWriter, Result, Write}; |
5 | 5 | use std::path::PathBuf;
|
6 | 6 |
|
7 |
| -fn gen_bindings() -> io::Result<()> { |
| 7 | +fn gen_bindings() -> Result<()> { |
8 | 8 | let whitelist_regex =
|
9 | 9 | "((?: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 | + |
27 | 47 | let bindgen = [
|
28 | 48 | "f64", "f32", "u64", "i64", "u32", "i32", "u16", "i16", "u8", "i8",
|
29 | 49 | ]
|
30 | 50 | .iter()
|
31 |
| - .fold(bindgen, |bindgen, &ty| bindgen.blacklist_type(ty)); |
| 51 | + .fold(bindgen, |bindgen, &ty| { |
| 52 | + bindgen.arg("--blacklist-type").arg(ty) |
| 53 | + }); |
32 | 54 |
|
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(()) |
38 | 66 | }
|
39 | 67 |
|
40 |
| -fn main() -> io::Result<()> { |
| 68 | +fn main() -> Result<()> { |
41 | 69 | gen_bindings()?;
|
42 | 70 | // build
|
43 | 71 | let mut cfg = cc::Build::new();
|
|
0 commit comments