test(c-api) Add tests for the wasmer_is_*_available API.

This commit is contained in:
Ivan Enderlin
2021-02-12 10:29:58 +01:00
parent 2a93fbd717
commit bdcc95925c
4 changed files with 121 additions and 13 deletions

19
Cargo.lock generated
View File

@@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "abort_on_panic" name = "abort_on_panic"
version = "2.0.0" version = "2.0.0"
@@ -723,9 +725,9 @@ dependencies = [
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.8.2" version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e" checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f"
dependencies = [ dependencies = [
"atty", "atty",
"humantime", "humantime",
@@ -984,9 +986,9 @@ dependencies = [
[[package]] [[package]]
name = "inline-c" name = "inline-c"
version = "0.1.4" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5db7d6323d3b700eec996bc5d60ffed2249f6bc63d88dec0681d2158115e5ed7" checksum = "194a0c4736432e499db196f6e18e9988935a88f69b2881b5ce5f486f603839ea"
dependencies = [ dependencies = [
"assert_cmd", "assert_cmd",
"cc", "cc",
@@ -1001,12 +1003,13 @@ dependencies = [
[[package]] [[package]]
name = "inline-c-macro" name = "inline-c-macro"
version = "0.1.0" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18417ebfdcd2ffc446b55558a66ce4e1d3a7127af7889a1014288aabc8dcbf2d" checksum = "17f5621ec7adacda881d7c2826c064f5c29c72fd44333f97df61b458a583ae15"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"rustc_version 0.3.3",
] ]
[[package]] [[package]]
@@ -1504,9 +1507,9 @@ dependencies = [
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.8" version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]

View File

@@ -36,12 +36,9 @@ serde = { version = "1", optional = true, features = ["derive"] }
thiserror = "1" thiserror = "1"
typetag = { version = "0.1", optional = true } typetag = { version = "0.1", optional = true }
paste = "1.0" 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] [dev-dependencies]
inline-c = "0.1.4" inline-c = "0.1.5"
[features] [features]
default = [ default = [

View File

@@ -525,6 +525,7 @@ fn build_inline_c_env_vars() {
let target = env::var("TARGET").unwrap(); let target = env::var("TARGET").unwrap();
assert_eq!(shared_object_dir.file_name(), Some(OsStr::new(&target))); assert_eq!(shared_object_dir.file_name(), Some(OsStr::new(&target)));
} }
shared_object_dir.push(env::var("PROFILE").unwrap()); shared_object_dir.push(env::var("PROFILE").unwrap());
let shared_object_dir = shared_object_dir.as_path().to_string_lossy(); 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, // * `-I`, add `include_dir` to include search path,
// * `-L`, add `shared_object_dir` to library search path, // * `-L`, add `shared_object_dir` to library search path,
// * `-D_DEBUG`, enable debug mode to enable `assert.h`. // * `-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!( 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, I = include_dir,
L = shared_object_dir.clone(), L = shared_object_dir.clone(),
); );

View File

@@ -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] #[no_mangle]
pub extern "C" fn wasmer_is_engine_available(engine: wasmer_engine_t) -> bool { pub extern "C" fn wasmer_is_engine_available(engine: wasmer_engine_t) -> bool {
match engine { match engine {
@@ -67,3 +72,102 @@ pub extern "C" fn wasmer_is_engine_available(engine: wasmer_engine_t) -> bool {
_ => false, _ => 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 <stdlib.h>
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 <stdlib.h>
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 <stdlib.h>
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");
}
}