From bdcc95925cacb8538158fa25f03308886827ab8f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 12 Feb 2021 10:29:58 +0100 Subject: [PATCH] test(c-api) Add tests for the `wasmer_is_*_available` API. --- Cargo.lock | 19 ++-- lib/c-api/Cargo.toml | 5 +- lib/c-api/build.rs | 6 +- lib/c-api/src/wasm_c_api/unstable/engine.rs | 104 ++++++++++++++++++++ 4 files changed, 121 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b9d113b0..0327bf433 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "abort_on_panic" version = "2.0.0" @@ -723,9 +725,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e" +checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" dependencies = [ "atty", "humantime", @@ -984,9 +986,9 @@ dependencies = [ [[package]] name = "inline-c" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5db7d6323d3b700eec996bc5d60ffed2249f6bc63d88dec0681d2158115e5ed7" +checksum = "194a0c4736432e499db196f6e18e9988935a88f69b2881b5ce5f486f603839ea" dependencies = [ "assert_cmd", "cc", @@ -1001,12 +1003,13 @@ dependencies = [ [[package]] name = "inline-c-macro" -version = "0.1.0" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18417ebfdcd2ffc446b55558a66ce4e1d3a7127af7889a1014288aabc8dcbf2d" +checksum = "17f5621ec7adacda881d7c2826c064f5c29c72fd44333f97df61b458a583ae15" dependencies = [ "proc-macro2", "quote", + "rustc_version 0.3.3", ] [[package]] @@ -1504,9 +1507,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.8" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ "proc-macro2", ] diff --git a/lib/c-api/Cargo.toml b/lib/c-api/Cargo.toml index 000744350..d3dc1ef48 100644 --- a/lib/c-api/Cargo.toml +++ b/lib/c-api/Cargo.toml @@ -36,12 +36,9 @@ serde = { version = "1", optional = true, features = ["derive"] } thiserror = "1" typetag = { version = "0.1", optional = true } paste = "1.0" -# for generating code in the same way thot the wasm-c-api does -# Commented out for now until we can find a solution to the exported function problem -# wasmer-wasm-c-api = { version = "1.0.2", path = "crates/wasm-c-api" } [dev-dependencies] -inline-c = "0.1.4" +inline-c = "0.1.5" [features] default = [ diff --git a/lib/c-api/build.rs b/lib/c-api/build.rs index ab0683f58..8b600a75e 100644 --- a/lib/c-api/build.rs +++ b/lib/c-api/build.rs @@ -525,6 +525,7 @@ fn build_inline_c_env_vars() { let target = env::var("TARGET").unwrap(); assert_eq!(shared_object_dir.file_name(), Some(OsStr::new(&target))); } + shared_object_dir.push(env::var("PROFILE").unwrap()); let shared_object_dir = shared_object_dir.as_path().to_string_lossy(); @@ -535,8 +536,11 @@ fn build_inline_c_env_vars() { // * `-I`, add `include_dir` to include search path, // * `-L`, add `shared_object_dir` to library search path, // * `-D_DEBUG`, enable debug mode to enable `assert.h`. + // * `-D_CRT_SECURE_NO_WARNINGS`, disable security features in the + // Windows C runtime, which allows to use `getenv` without any + // warnings. println!( - "cargo:rustc-env=INLINE_C_RS_CFLAGS=-I{I} -L{L} -D_DEBUG", + "cargo:rustc-env=INLINE_C_RS_CFLAGS=-I{I} -L{L} -D_DEBUG -D_CRT_SECURE_NO_WARNINGS", I = include_dir, L = shared_object_dir.clone(), ); diff --git a/lib/c-api/src/wasm_c_api/unstable/engine.rs b/lib/c-api/src/wasm_c_api/unstable/engine.rs index 495efce52..dab7f931a 100644 --- a/lib/c-api/src/wasm_c_api/unstable/engine.rs +++ b/lib/c-api/src/wasm_c_api/unstable/engine.rs @@ -58,6 +58,11 @@ pub extern "C" fn wasmer_is_compiler_available(compiler: wasmer_compiler_t) -> b } } +#[no_mangle] +pub extern "C" fn wasmer_is_headless() -> bool { + !cfg!(feature = "compiler") +} + #[no_mangle] pub extern "C" fn wasmer_is_engine_available(engine: wasmer_engine_t) -> bool { match engine { @@ -67,3 +72,102 @@ pub extern "C" fn wasmer_is_engine_available(engine: wasmer_engine_t) -> bool { _ => false, } } + +#[cfg(test)] +mod tests { + use inline_c::assert_c; + use std::env::{remove_var, set_var}; + + #[test] + fn test_wasmer_is_headless() { + set_var( + "COMPILER", + if cfg!(feature = "compiler") { "0" } else { "1" }, + ); + + (assert_c! { + #include "tests/wasmer_wasm.h" + #include + + int main() { + assert(wasmer_is_headless() == (getenv("COMPILER")[0] == '1')); + + return 0; + } + }) + .success(); + + remove_var("COMPILER"); + } + + #[test] + fn test_wasmer_is_compiler_available() { + set_var( + "CRANELIFT", + if cfg!(feature = "cranelift") { + "1" + } else { + "0" + }, + ); + set_var("LLVM", if cfg!(feature = "llvm") { "1" } else { "0" }); + set_var( + "SINGLEPASS", + if cfg!(feature = "singlepass") { + "1" + } else { + "0" + }, + ); + + (assert_c! { + #include "tests/wasmer_wasm.h" + #include + + int main() { + assert(wasmer_is_compiler_available(CRANELIFT) == (getenv("CRANELIFT")[0] == '1')); + assert(wasmer_is_compiler_available(LLVM) == (getenv("LLVM")[0] == '1')); + assert(wasmer_is_compiler_available(SINGLEPASS) == (getenv("SINGLEPASS")[0] == '1')); + + return 0; + } + }) + .success(); + + remove_var("CRANELIFT"); + remove_var("LLVM"); + remove_var("SINGLEPASS"); + } + + #[test] + fn test_wasmer_is_engine_available() { + set_var("JIT", if cfg!(feature = "jit") { "1" } else { "0" }); + set_var("NATIVE", if cfg!(feature = "native") { "1" } else { "0" }); + set_var( + "OBJECT_FILE", + if cfg!(feature = "object-file") { + "1" + } else { + "0" + }, + ); + + (assert_c! { + #include "tests/wasmer_wasm.h" + #include + + int main() { + assert(wasmer_is_engine_available(JIT) == (getenv("JIT")[0] == '1')); + assert(wasmer_is_engine_available(NATIVE) == (getenv("NATIVE")[0] == '1')); + assert(wasmer_is_engine_available(OBJECT_FILE) == (getenv("OBJECT_FILE")[0] == '1')); + + return 0; + } + }) + .success(); + + remove_var("JIT"); + remove_var("NATIVE"); + remove_var("OBJECT_FILE"); + } +}