|
| 1 | +// Fizzy: A fast WebAssembly interpreter |
| 2 | +// Copyright 2019-2020 The Fizzy Authors. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +extern crate bindgen; |
| 6 | +extern crate cmake; |
| 7 | + |
| 8 | +use cmake::Config; |
| 9 | + |
| 10 | +use std::env; |
| 11 | +use std::path::PathBuf; |
| 12 | + |
| 13 | +fn main() { |
| 14 | + // This is the root directory. |
| 15 | + let src = "../../"; |
| 16 | + |
| 17 | + let dst = Config::new(src).define("FIZZY_TESTING", "OFF").build(); |
| 18 | + |
| 19 | + println!("cargo:rustc-link-lib=static=fizzy"); |
| 20 | + println!("cargo:rustc-link-search=native={}/lib", dst.display()); |
| 21 | + |
| 22 | + // We need to link against C++ std lib |
| 23 | + if let Some(cpp_stdlib) = get_cpp_stdlib() { |
| 24 | + println!("cargo:rustc-link-lib={}", cpp_stdlib); |
| 25 | + } |
| 26 | + |
| 27 | + let bindings = bindgen::Builder::default() |
| 28 | + .header(format!("{}/include/fizzy/fizzy.h", src)) |
| 29 | + // See https://github.com/rust-lang-nursery/rust-bindgen/issues/947 |
| 30 | + .trust_clang_mangling(false) |
| 31 | + .generate_comments(true) |
| 32 | + // https://github.com/rust-lang-nursery/rust-bindgen/issues/947#issuecomment-327100002 |
| 33 | + .layout_tests(false) |
| 34 | + .whitelist_function("fizzy_.*") |
| 35 | + // TODO: consider removing this |
| 36 | + .size_t_is_usize(true) |
| 37 | + .generate() |
| 38 | + .expect("Unable to generate bindings"); |
| 39 | + |
| 40 | + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 41 | + bindings |
| 42 | + .write_to_file(out_path.join("bindings.rs")) |
| 43 | + .expect("Could not write bindings"); |
| 44 | +} |
| 45 | + |
| 46 | +// See https://github.com/alexcrichton/gcc-rs/blob/88ac58e25/src/lib.rs#L1197 |
| 47 | +fn get_cpp_stdlib() -> Option<String> { |
| 48 | + env::var("TARGET").ok().and_then(|target| { |
| 49 | + if target.contains("msvc") { |
| 50 | + None |
| 51 | + } else if target.contains("darwin") || target.contains("freebsd") { |
| 52 | + Some("c++".to_string()) |
| 53 | + } else if target.contains("musl") { |
| 54 | + Some("static=stdc++".to_string()) |
| 55 | + } else { |
| 56 | + Some("stdc++".to_string()) |
| 57 | + } |
| 58 | + }) |
| 59 | +} |
0 commit comments