Skip to content

Commit

Permalink
Merge pull request wasmerio#238 from Hywan/feat-runtime-c-api-headers…
Browse files Browse the repository at this point in the history
…-in-out-dir

feat(runtime-c-api) Generate the C/C++ header files in `OUT_DIR` (+ copy in `CARGO_MANIFEST_DIR`).
  • Loading branch information
Hywan authored Mar 6, 2019
2 parents 28bb264 + d709191 commit ec1a165
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 38 deletions.
8 changes: 5 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ emtests:
WASM_EMSCRIPTEN_GENERATE_EMTESTS=1 cargo build -p wasmer-emscripten

capi:
WASM_EMSCRIPTEN_GENERATE_C_API_HEADERS=1 cargo build --manifest-path lib/runtime-c-api/Cargo.toml --features generate-c-api-headers
cargo build --manifest-path lib/runtime-c-api/Cargo.toml

# clean:
# rm -rf artifacts
Expand Down
11 changes: 3 additions & 8 deletions lib/runtime-c-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wasmer-runtime-c-api"
version = "0.2.1"
description = "Wasmer c-api library"
description = "Wasmer C API library"
license = "MIT"
authors = ["The Wasmer Engineering Team <[email protected]>"]
repository = "https://github.com/wasmerio/wasmer"
Expand All @@ -14,12 +14,7 @@ wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" }
libc = "0.2"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[build-dependencies]
cbindgen = { version = "0.8", optional = true }

[features]
generate-c-api-headers = ["cbindgen"]


cbindgen = "0.8"
53 changes: 33 additions & 20 deletions lib/runtime-c-api/build.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
#[cfg(feature = "generate-c-api-headers")]
extern crate cbindgen;

use std::env;

static CAPI_ENV_VAR: &str = "WASM_EMSCRIPTEN_GENERATE_C_API_HEADERS";
use cbindgen::{Builder, Language};
use std::{env, fs, path::PathBuf};

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 mut crate_wasmer_header_file = PathBuf::from(&crate_dir);
crate_wasmer_header_file.push("wasmer");

use cbindgen::Language;
cbindgen::Builder::new()
let out_dir = env::var("OUT_DIR").unwrap();
let mut out_wasmer_header_file = PathBuf::from(&out_dir);
out_wasmer_header_file.push("wasmer");

// Generate the C bindings in the `OUT_DIR`.
out_wasmer_header_file.set_extension("h");
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");
.write_to_file(out_wasmer_header_file.as_path());

cbindgen::Builder::new()
// Generate the C++ bindings in the `OUT_DIR`.
out_wasmer_header_file.set_extension("hh");
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");
}
.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");

#[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")
// 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");
}
6 changes: 4 additions & 2 deletions lib/runtime-c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,8 @@ fn take_last_error() -> Option<Box<Error>> {
/// bytes needed to store a message.
///
/// # Example
/// ```
///
/// ```c
/// int error_len = wasmer_last_error_length();
/// char *error_str = malloc(error_len);
/// ```
Expand All @@ -1517,7 +1518,8 @@ pub extern "C" fn wasmer_last_error_length() -> c_int {
/// Returns `-1` if an error occurs.
///
/// # Example
/// ```
///
/// ```c
/// int error_len = wasmer_last_error_length();
/// char *error_str = malloc(error_len);
/// wasmer_last_error_message(error_str, error_len);
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime-c-api/wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ wasmer_result_t wasmer_instantiate(wasmer_instance_t **instance,
* This can be used to dynamically allocate a buffer with the correct number of
* bytes needed to store a message.
* # Example
* ```
* ```c
* int error_len = wasmer_last_error_length();
* char *error_str = malloc(error_len);
* ```
Expand All @@ -430,7 +430,7 @@ int wasmer_last_error_length(void);
* Returns the length of the string in bytes.
* Returns `-1` if an error occurs.
* # Example
* ```
* ```c
* int error_len = wasmer_last_error_length();
* char *error_str = malloc(error_len);
* wasmer_last_error_message(error_str, error_len);
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime-c-api/wasmer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ wasmer_result_t wasmer_instantiate(wasmer_instance_t **instance,
/// This can be used to dynamically allocate a buffer with the correct number of
/// bytes needed to store a message.
/// # Example
/// ```
/// ```c
/// int error_len = wasmer_last_error_length();
/// char *error_str = malloc(error_len);
/// ```
Expand All @@ -344,7 +344,7 @@ int wasmer_last_error_length();
/// Returns the length of the string in bytes.
/// Returns `-1` if an error occurs.
/// # Example
/// ```
/// ```c
/// int error_len = wasmer_last_error_length();
/// char *error_str = malloc(error_len);
/// wasmer_last_error_message(error_str, error_len);
Expand Down

0 comments on commit ec1a165

Please sign in to comment.