wasmer/lib/runtime-c-api/build.rs
Ivan Enderlin 7c56d893c2 feat(runtime-c-api) Generate the C header file in OUT_DIR.
This patch changes the directory where the C header files are
generated, from `CARGO_MANIFEST_DIR` to `OUT_DIR`. The goal is: When
`wasm-runtime-c-api` is used as a dependency of another Rust project,
then the C header files are accessible in the `target/` directory
(i.e. the `OUT_DIR`).

Also, since `rustc` has a `--out-dir` directory, it increases the
flexibility of this approach: The user can generate the C header files
where she wants.
2019-03-06 10:42:06 +01:00

46 lines
1.3 KiB
Rust

#[cfg(feature = "generate-c-api-headers")]
extern crate cbindgen;
use std::{env, path::Path};
static CAPI_ENV_VAR: &str = "WASM_EMSCRIPTEN_GENERATE_C_API_HEADERS";
fn main() {
if env::var(CAPI_ENV_VAR).unwrap_or("0".to_string()) == "1" {
build();
}
}
#[cfg(feature = "generate-c-api-headers")]
fn build() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir);
let mut wasmer_h = out_path.to_path_buf();
wasmer_h.push("wasmer.h");
let mut wasmer_hh = out_path.to_path_buf();
wasmer_hh.push("wasmer.hh");
use cbindgen::Language;
cbindgen::Builder::new()
.with_crate(crate_dir.clone())
.with_language(Language::C)
.with_include_guard("WASMER_H")
.generate()
.expect("Unable to generate C bindings")
.write_to_file(wasmer_h);
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_language(Language::Cxx)
.with_include_guard("WASMER_H")
.generate()
.expect("Unable to generate C++ bindings")
.write_to_file(wasmer_hh);
}
#[cfg(not(feature = "generate-c-api-headers"))]
fn build() {
panic!("environment var set to generate wasmer c API headers but generate-c-api-headers feature not enabled")
}