2019-02-02 00:52:22 +00:00
|
|
|
extern crate cbindgen;
|
|
|
|
|
2019-03-06 09:39:34 +00:00
|
|
|
use cbindgen::{Builder, Language};
|
2019-03-06 09:58:40 +00:00
|
|
|
use std::{env, fs, path::PathBuf};
|
2019-02-02 00:52:22 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
2019-03-06 09:58:40 +00:00
|
|
|
let mut crate_wasmer_header_file = PathBuf::from(&crate_dir);
|
|
|
|
crate_wasmer_header_file.push("wasmer");
|
2019-03-06 09:39:34 +00:00
|
|
|
|
2019-03-05 14:52:18 +00:00
|
|
|
let out_dir = env::var("OUT_DIR").unwrap();
|
2019-03-06 09:58:40 +00:00
|
|
|
let mut out_wasmer_header_file = PathBuf::from(&out_dir);
|
|
|
|
out_wasmer_header_file.push("wasmer");
|
2019-02-02 00:52:22 +00:00
|
|
|
|
2019-11-11 22:45:14 +00:00
|
|
|
const WASMER_PRE_HEADER: &str = r#"
|
2019-11-13 14:28:04 +00:00
|
|
|
#if !defined(WASMER_H_MACROS)
|
|
|
|
#define WASMER_H_MACROS
|
|
|
|
#if defined(MSVC)
|
|
|
|
#if defined(_M_AMD64)
|
|
|
|
#define ARCH_X86_64
|
|
|
|
#endif
|
|
|
|
#endif
|
2019-11-11 22:45:14 +00:00
|
|
|
|
2019-11-13 14:28:57 +00:00
|
|
|
#if defined(GCC) || defined(__clang__)
|
2019-11-13 14:28:04 +00:00
|
|
|
#if defined(__x86_64__)
|
|
|
|
#define ARCH_X86_64
|
|
|
|
#endif
|
|
|
|
#endif
|
2019-11-11 22:45:14 +00:00
|
|
|
#endif
|
|
|
|
"#;
|
2019-03-06 09:58:40 +00:00
|
|
|
// Generate the C bindings in the `OUT_DIR`.
|
|
|
|
out_wasmer_header_file.set_extension("h");
|
2019-03-06 09:39:34 +00:00
|
|
|
Builder::new()
|
2019-02-05 01:54:12 +00:00
|
|
|
.with_crate(crate_dir.clone())
|
2019-02-02 04:10:36 +00:00
|
|
|
.with_language(Language::C)
|
2019-02-10 00:07:05 +00:00
|
|
|
.with_include_guard("WASMER_H")
|
2019-11-11 22:45:14 +00:00
|
|
|
.with_header(WASMER_PRE_HEADER)
|
|
|
|
.with_define("target_family", "windows", "_WIN32")
|
|
|
|
.with_define("target_arch", "x86_64", "ARCH_X86_64")
|
2019-02-02 00:52:22 +00:00
|
|
|
.generate()
|
2019-02-05 01:54:12 +00:00
|
|
|
.expect("Unable to generate C bindings")
|
2019-03-06 09:58:40 +00:00
|
|
|
.write_to_file(out_wasmer_header_file.as_path());
|
2019-02-05 01:54:12 +00:00
|
|
|
|
2019-03-06 09:58:40 +00:00
|
|
|
// Generate the C++ bindings in the `OUT_DIR`.
|
|
|
|
out_wasmer_header_file.set_extension("hh");
|
2019-03-06 09:39:34 +00:00
|
|
|
Builder::new()
|
2019-02-05 01:54:12 +00:00
|
|
|
.with_crate(crate_dir)
|
|
|
|
.with_language(Language::Cxx)
|
2019-02-10 00:07:05 +00:00
|
|
|
.with_include_guard("WASMER_H")
|
2019-11-11 22:45:14 +00:00
|
|
|
.with_header(WASMER_PRE_HEADER)
|
|
|
|
.with_define("target_family", "windows", "_WIN32")
|
|
|
|
.with_define("target_arch", "x86_64", "ARCH_X86_64")
|
2019-02-05 01:54:12 +00:00
|
|
|
.generate()
|
|
|
|
.expect("Unable to generate C++ bindings")
|
2019-03-06 09:58:40 +00:00
|
|
|
.write_to_file(out_wasmer_header_file.as_path());
|
|
|
|
|
|
|
|
// Copy the generated C bindings from `OUT_DIR` to
|
|
|
|
// `CARGO_MANIFEST_DIR`.
|
|
|
|
crate_wasmer_header_file.set_extension("h");
|
|
|
|
out_wasmer_header_file.set_extension("h");
|
|
|
|
fs::copy(
|
|
|
|
out_wasmer_header_file.as_path(),
|
|
|
|
crate_wasmer_header_file.as_path(),
|
|
|
|
)
|
|
|
|
.expect("Unable to copy the generated C bindings");
|
|
|
|
|
|
|
|
// Copy the generated C++ bindings from `OUT_DIR` to
|
|
|
|
// `CARGO_MANIFEST_DIR`.
|
|
|
|
crate_wasmer_header_file.set_extension("h");
|
|
|
|
crate_wasmer_header_file.set_extension("hh");
|
|
|
|
out_wasmer_header_file.set_extension("hh");
|
|
|
|
fs::copy(out_wasmer_header_file, crate_wasmer_header_file)
|
|
|
|
.expect("Unable to copy the generated C++ bindings");
|
2019-02-02 00:52:22 +00:00
|
|
|
}
|