From 7c56d893c2604c3b6917885d19d084c2a5a77b59 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 5 Mar 2019 15:52:18 +0100 Subject: [PATCH 1/6] 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. --- lib/runtime-c-api/build.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/runtime-c-api/build.rs b/lib/runtime-c-api/build.rs index f78c5cf69..5b05670ba 100644 --- a/lib/runtime-c-api/build.rs +++ b/lib/runtime-c-api/build.rs @@ -1,7 +1,7 @@ #[cfg(feature = "generate-c-api-headers")] extern crate cbindgen; -use std::env; +use std::{env, path::Path}; static CAPI_ENV_VAR: &str = "WASM_EMSCRIPTEN_GENERATE_C_API_HEADERS"; @@ -14,6 +14,12 @@ fn main() { #[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() @@ -22,7 +28,7 @@ fn build() { .with_include_guard("WASMER_H") .generate() .expect("Unable to generate C bindings") - .write_to_file("wasmer.h"); + .write_to_file(wasmer_h); cbindgen::Builder::new() .with_crate(crate_dir) @@ -30,7 +36,7 @@ fn build() { .with_include_guard("WASMER_H") .generate() .expect("Unable to generate C++ bindings") - .write_to_file("wasmer.hh"); + .write_to_file(wasmer_hh); } #[cfg(not(feature = "generate-c-api-headers"))] From f2997357fce942a6e9c810371b44c98ba3525e65 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 6 Mar 2019 10:39:34 +0100 Subject: [PATCH 2/6] feat(runtime-c-api) Remove the flag `WASM_EMSCRIPTEN_GENERATE_C_API_HEADERS`. This patch removes the `WASM_EMSCRIPTEN_GENERATE_C_API_HEADERS` flag. Consequently, the C header files will be generated for each build. The `generate-c-api-headers` feature is also removed, since it becomes useless. --- Makefile | 2 +- lib/runtime-c-api/Cargo.toml | 9 ++------- lib/runtime-c-api/build.rs | 24 ++++++------------------ 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 74e3428fc..cf38c43fd 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/lib/runtime-c-api/Cargo.toml b/lib/runtime-c-api/Cargo.toml index 6541d482e..077b2a226 100644 --- a/lib/runtime-c-api/Cargo.toml +++ b/lib/runtime-c-api/Cargo.toml @@ -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 "] repository = "https://github.com/wasmerio/wasmer" @@ -17,9 +17,4 @@ libc = "0.2" crate-type = ["cdylib"] [build-dependencies] -cbindgen = { version = "0.8", optional = true } - -[features] -generate-c-api-headers = ["cbindgen"] - - +cbindgen = "0.8" \ No newline at end of file diff --git a/lib/runtime-c-api/build.rs b/lib/runtime-c-api/build.rs index 5b05670ba..8b4e8f8ee 100644 --- a/lib/runtime-c-api/build.rs +++ b/lib/runtime-c-api/build.rs @@ -1,28 +1,21 @@ -#[cfg(feature = "generate-c-api-headers")] extern crate cbindgen; +use cbindgen::{Builder, Language}; 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() + Builder::new() .with_crate(crate_dir.clone()) .with_language(Language::C) .with_include_guard("WASMER_H") @@ -30,7 +23,7 @@ fn build() { .expect("Unable to generate C bindings") .write_to_file(wasmer_h); - cbindgen::Builder::new() + Builder::new() .with_crate(crate_dir) .with_language(Language::Cxx) .with_include_guard("WASMER_H") @@ -38,8 +31,3 @@ fn build() { .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") -} From 0f74133be3a3997567496f1f9b2b33fd1e5998a2 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 6 Mar 2019 10:58:40 +0100 Subject: [PATCH 3/6] feat(runtime-c-api) Build: Copy the C header files from `OUT_DIR` to `CARGO_MANIFEST_DIR`. This patch copies the generated C and C++ header files from the `OUT_DIR` to the root of the crate. --- lib/runtime-c-api/build.rs | 39 ++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/runtime-c-api/build.rs b/lib/runtime-c-api/build.rs index 8b4e8f8ee..1db19a197 100644 --- a/lib/runtime-c-api/build.rs +++ b/lib/runtime-c-api/build.rs @@ -1,33 +1,52 @@ extern crate cbindgen; use cbindgen::{Builder, Language}; -use std::{env, path::Path}; +use std::{env, fs, path::PathBuf}; fn main() { 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"); 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"); + 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()); + // 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"); + + // 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"); } From 76caebebb4ab56cbd17d815e020aea5cad99bfde Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 6 Mar 2019 11:02:14 +0100 Subject: [PATCH 4/6] fix(runtime-c-api) Provide a link target. When compiling `wasmer-runtime-c-api` as a dependency of another crate, `rustc` emits this warning: ``` warning: The package `wasmer_runtime_c_api` provides no linkable target. The compiler might raise an error while compiling `foo`. Consider adding 'dylib' or 'rlib' to key `crate-type` in `wasmer_runtime_c_api`'s Cargo.toml. This warning might turn into a hard error in the future. ``` To solve this issue, the `rlib` type has been added to the `crate-type` list. --- lib/runtime-c-api/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime-c-api/Cargo.toml b/lib/runtime-c-api/Cargo.toml index 077b2a226..f4908f29a 100644 --- a/lib/runtime-c-api/Cargo.toml +++ b/lib/runtime-c-api/Cargo.toml @@ -14,7 +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 = "0.8" \ No newline at end of file From 365d00979baeb1ae9bf454edba82dc2fe7030ac7 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 6 Mar 2019 11:03:38 +0100 Subject: [PATCH 5/6] chore(runtime-c-api) Update the `Cargo.lock` file. --- Cargo.lock | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6940f529c..bd5fbd81e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,11 +115,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cbindgen" -version = "0.7.1" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1271,7 +1273,7 @@ dependencies = [ name = "wasmer-runtime-c-api" version = "0.2.1" dependencies = [ - "cbindgen 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cbindgen 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)", "wasmer-runtime 0.2.1", "wasmer-runtime-core 0.2.1", @@ -1401,7 +1403,7 @@ dependencies = [ "checksum blake2b_simd 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce2571a6cd634670daa2977cc894c1cc2ba57c563c498e5a82c35446f34d056e" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" -"checksum cbindgen 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "32e01024aaf5390d6a8145047371a4f5b0063a14c1e411bc731353bd2278ca44" +"checksum cbindgen 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "93aabdea4371364a6b05c72a64d26417ad7eae2b06ddf3c7eaf62b3699701ebe" "checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749" "checksum cexpr 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "644d693ecfa91955ed32dcc7eda4914e1be97a641fb6f0645a37348e20b230da" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" From d709191be13a0e70d3bd5271d17fa1ae646a8e0d Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 6 Mar 2019 12:02:20 +0100 Subject: [PATCH 6/6] doc(runtime-c-api) Declare the example as C, so that they are not tested. Those examples contain C code. They must not be run by `rustdoc` as tests. --- lib/runtime-c-api/src/lib.rs | 6 ++++-- lib/runtime-c-api/wasmer.h | 4 ++-- lib/runtime-c-api/wasmer.hh | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index 3b08d9103..591a03a54 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -1498,7 +1498,8 @@ fn take_last_error() -> Option> { /// bytes needed to store a message. /// /// # Example -/// ``` +/// +/// ```c /// int error_len = wasmer_last_error_length(); /// char *error_str = malloc(error_len); /// ``` @@ -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); diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 9681d5dce..8ecdd1d7d 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -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); * ``` @@ -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); diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index 3bd59bd89..b093b2b64 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -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); /// ``` @@ -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);