Skip to content

Commit

Permalink
Unified C API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed May 15, 2021
1 parent b145d71 commit 71176cc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 78 deletions.
88 changes: 19 additions & 69 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,6 @@ comma := ,
# Define the compiler Cargo features for all crates.
compiler_features := --features $(subst $(space),$(comma),$(compilers))

# Define the compiler Cargo features for the C API. It always excludes
# LLVM for the moment.
capi_compiler_features := --features $(subst $(space),$(comma),$(filter-out llvm, $(compilers)))


#####
#
# Display information.
Expand Down Expand Up @@ -333,7 +328,6 @@ $(info Enabled Compilers: $(bold)$(green)$(subst $(space),$(reset)$(comma)$(spac
$(info Compilers + engines pairs (for testing): $(bold)$(green)${compilers_engines}$(reset))
$(info Cargo features:)
$(info   - Compilers for all crates: `$(bold)$(green)${compiler_features}$(reset)`.)
$(info   - Compilers for the C API: `$(bold)$(green)${capi_compiler_features}$(reset)`.)
$(info   - Default for the C API: `$(bold)$(green)${capi_default_features}$(reset)`.)
$(info )
$(info )
Expand Down Expand Up @@ -410,7 +404,7 @@ build-docs-capi: capi-setup

build-capi: capi-setup
RUSTFLAGS="${RUSTFLAGS}" cargo build --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,native,object-file,wasi,middlewares $(capi_default_features) $(capi_compiler_features)
--no-default-features --features deprecated,wat,jit,native,object-file,wasi,middlewares $(capi_default_features) $(compiler_features)

build-capi-singlepass: capi-setup
RUSTFLAGS="${RUSTFLAGS}" cargo build --manifest-path lib/c-api/Cargo.toml --release \
Expand Down Expand Up @@ -489,22 +483,22 @@ build-capi-headless-all: capi-setup
test: $(foreach compiler,$(compilers),test-$(compiler)) test-packages test-examples test-deprecated

test-singlepass-native:
cargo test --release --tests $(compiler_features) --features "test-singlepass test-native"
cargo test --release --tests $(compiler_features) -- singlepass::native

test-singlepass-jit:
cargo test --release --tests $(compiler_features) --features "test-singlepass test-jit"
cargo test --release --tests $(compiler_features) -- singlepass::jit

test-cranelift-native:
cargo test --release --tests $(compiler_features) --features "test-cranelift test-native"
cargo test --release --tests $(compiler_features) -- cranelift::native

test-cranelift-jit:
cargo test --release --tests $(compiler_features) --features "test-cranelift test-jit"
cargo test --release --tests $(compiler_features) -- cranelift::jit

test-llvm-native:
cargo test --release --tests $(compiler_features) --features "test-llvm test-native"
cargo test --release --tests $(compiler_features) -- llvm::native

test-llvm-jit:
cargo test --release --tests $(compiler_features) --features "test-llvm test-jit"
cargo test --release --tests $(compiler_features) -- llvm::jit

test-singlepass: $(foreach singlepass_engine,$(filter singlepass-%,$(compilers_engines)),test-$(singlepass_engine))

Expand All @@ -513,22 +507,10 @@ test-cranelift: $(foreach cranelift_engine,$(filter cranelift-%,$(compilers_engi
test-llvm: $(foreach llvm_engine,$(filter llvm-%,$(compilers_engines)),test-$(llvm_engine))

test-packages:
cargo test -p wasmer --release
cargo test -p wasmer-vm --release
cargo test -p wasmer-types --release
cargo test -p wasmer-wasi --release
cargo test -p wasmer-object --release
cargo test -p wasmer-engine-native --release --no-default-features
cargo test -p wasmer-engine-jit --release --no-default-features
cargo test -p wasmer-compiler --release
cargo test --all
cargo test --manifest-path lib/compiler-cranelift/Cargo.toml --release --no-default-features --features=std
cargo test --manifest-path lib/compiler-singlepass/Cargo.toml --release --no-default-features --features=std
cargo test --manifest-path lib/cli/Cargo.toml $(compiler_features) --release
cargo test -p wasmer-cache --release
cargo test -p wasmer-engine --release
cargo test -p wasmer-derive --release
cargo check --manifest-path fuzz/Cargo.toml $(compiler_features) --release

cargo test --manifest-path lib/cli/Cargo.toml $(compiler_features)

# We want to run all the tests for all available compilers. The C API
# and the tests rely on the fact that one and only one default
Expand All @@ -551,37 +533,17 @@ test-packages:
# the library built with `build-capi`, which is the one we will
# deliver to the users, i.e. the one that may include multiple
# compilers.
test-capi: $(foreach compiler_engine,$(compilers_engines),test-capi-$(compiler_engine)) test-capi-all

test-capi-all: build-capi
cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,native,object-file,wasi,middlewares $(capi_default_features) $(capi_compiler_features) -- --nocapture

test-capi-singlepass-jit: build-capi-singlepass-jit test-capi-tests
cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,singlepass,wasi,middlewares $(capi_default_features) -- --nocapture

test-capi-cranelift-jit: build-capi-cranelift-jit test-capi-tests
cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,cranelift,wasi,middlewares $(capi_default_features) -- --nocapture

test-capi-cranelift-native: build-capi-cranelift-native test-capi-tests
cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,native,cranelift,wasi,middlewares $(capi_default_features) -- --nocapture

test-capi-llvm-jit: build-capi-llvm-jit test-capi-tests
cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,llvm,wasi,middlewares $(capi_default_features) -- --nocapture
test-capi: $(foreach compiler_engine,$(compilers_engines),test-capi-crate-$(compiler_engine) test-capi-integration-$(compiler_engine))

test-capi-llvm-native: build-capi-llvm-native test-capi-tests
cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,native,llvm,wasi,middlewares $(capi_default_features) -- --nocapture
test-capi-crate-%:
TEST=$(shell echo $@ | sed -e s/test-capi-crate-//) cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,native,object-file,wasi,middlewares $(capi_default_features) $(compiler_features) -- --nocapture

test-capi-tests: package-capi
test-capi-integration-%: package-capi
# Test the Wasmer C API tests for C
cd lib/c-api/tests; WASMER_DIR=`pwd`/../../../package make test
cd lib/c-api/tests; TEST=$(shell echo $@ | sed -e s/test-capi-integration-//) WASMER_DIR=`pwd`/../../../package make test
# Test the Wasmer C API examples
cd lib/c-api/examples; WASMER_DIR=`pwd`/../../../package make run
cd lib/c-api/examples; TEST=$(shell echo $@ | sed -e s/test-capi-integration-//) WASMER_DIR=`pwd`/../../../package make run

test-wasi-unit:
cargo test --manifest-path lib/wasi/Cargo.toml --release
Expand Down Expand Up @@ -614,7 +576,7 @@ else
cp wapm-cli/$(TARGET_DIR)/wapm package/bin/ ;\
fi
ifeq ($(IS_DARWIN), 1)
codesign -s - package/bin/wapm
# codesign -s - package/bin/wapm || true
endif
endif

Expand All @@ -636,7 +598,7 @@ ifeq ($(IS_WINDOWS), 1)
else
cp $(TARGET_DIR)/wasmer package/bin/
ifeq ($(IS_DARWIN), 1)
codesign -s - package/bin/wasmer
# codesign -s - package/bin/wasmer || true
endif
endif

Expand Down Expand Up @@ -738,20 +700,8 @@ update-testsuite:

lint-packages: RUSTFLAGS += -D dead-code -D nonstandard-style -D unused-imports -D unused-mut -D unused-variables -D unused-unsafe -D unreachable-patterns -D bad-style -D improper-ctypes -D unused-allocation -D unused-comparisons -D while-true -D unconditional-recursion -D bare-trait-objects # TODO: add `-D missing-docs` # TODO: add `-D function_item_references` (not available on Rust 1.47, try when upgrading)
lint-packages:
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-c-api
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-vm
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-types
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-wasi
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-object
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-engine-native
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-engine-jit
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-compiler
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-compiler-cranelift
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-compiler-singlepass
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --all
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path lib/cli/Cargo.toml $(compiler_features)
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-cache
RUSTFLAGS="${RUSTFLAGS}" cargo clippy -p wasmer-engine
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path fuzz/Cargo.toml $(compiler_features)

lint-formatting:
Expand Down
7 changes: 7 additions & 0 deletions lib/c-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@ fn build_inline_c_env_vars() {
L = shared_object_dir.clone(),
);

if let Ok(compiler_engine) = env::var("TEST") {
println!(
"cargo:rustc-env=INLINE_C_RS_TEST={test}",
test = compiler_engine
);
}

println!(
"cargo:rustc-env=INLINE_C_RS_LDFLAGS={shared_object_dir}/{lib}",
shared_object_dir = shared_object_dir,
Expand Down
8 changes: 6 additions & 2 deletions lib/c-api/examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
WASMER_DIR:=$(realpath $(WASMER_DIR))

$(info Using provided WASMER_DIR=$(WASMER_DIR))

ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

ifeq (,$(wildcard $(WASMER_DIR)/bin/wasmer))
CFLAGS = -g -I$(WASMER_DIR)/include
CFLAGS = -g -I$(ROOT_DIR)/../tests -I$(WASMER_DIR)/include
LDFLAGS = -Wl,-rpath,$(WASMER_DIR)/lib
LDLIBS = -L$(WASMER_DIR)/lib -lwasmer
else
CFLAGS = -g -I$(shell $(WASMER_DIR)/bin/wasmer config --includedir)
CFLAGS = -g -I$(ROOT_DIR)/../tests -I$(shell $(WASMER_DIR)/bin/wasmer config --includedir)
LDFLAGS = -Wl,-rpath,$(shell $(WASMER_DIR)/bin/wasmer config --libdir)
LDLIBS = $(shell $(WASMER_DIR)/bin/wasmer config --libs)
endif
Expand Down
18 changes: 11 additions & 7 deletions lib/c-api/tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
WASMER_DIR:=$(realpath $(WASMER_DIR))

$(info Using provided WASMER_DIR=$(WASMER_DIR))

ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

ifeq (,$(wildcard $(WASMER_DIR)/bin/wasmer))
CFLAGS = -g -I$(WASMER_DIR)/include
CFLAGS = -g -I$(ROOT_DIR)/ -I$(WASMER_DIR)/include
LDFLAGS = -Wl,-rpath,$(WASMER_DIR)/lib
LDLIBS = -L$(WASMER_DIR)/lib -lwasmer
else
CFLAGS = -g -I$(shell $(WASMER_DIR)/bin/wasmer config --includedir)
CFLAGS = -g -I$(ROOT_DIR)/ -I$(shell $(WASMER_DIR)/bin/wasmer config --includedir)
LDFLAGS = -Wl,-rpath,$(shell $(WASMER_DIR)/bin/wasmer config --libdir)
LDLIBS = $(shell $(WASMER_DIR)/bin/wasmer config --libs)
endif
Expand All @@ -18,13 +22,13 @@ CAPI_WASMER_TESTS = \
test-early-exit test-memory test-wasi \

CAPI_BASE_TESTS = \
wasm-c-api/example/callback wasm-c-api/example/global wasm-c-api/example/hello \
wasm-c-api/example/memory wasm-c-api/example/reflect wasm-c-api/example/serialize \
wasm-c-api/example/start wasm-c-api/example/trap wasm-c-api/example/multi
wasm-c-api/example/callback wasm-c-api/example/global wasm-c-api/example/hello \
wasm-c-api/example/memory wasm-c-api/example/reflect wasm-c-api/example/serialize \
wasm-c-api/example/start wasm-c-api/example/trap wasm-c-api/example/multi

CAPI_BASE_TESTS_NOT_WORKING = \
wasm-c-api/example/finalize wasm-c-api/example/hostref \
wasm-c-api/example/table wasm-c-api/example/threads
wasm-c-api/example/finalize wasm-c-api/example/hostref wasm-c-api/example/threads \
wasm-c-api/example/table

DEPRECATED_TESTS = \
deprecated/test-context deprecated/test-import-object deprecated/test-module-imports \
Expand Down
49 changes: 49 additions & 0 deletions lib/c-api/tests/wasm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// This header file is used only for test purposes! It is used by unit
// test inside the `src/` directory for the moment.

#ifndef TEST_WASM
#define TEST_WASM

#include "../wasm.h"
#include "../wasmer_wasm.h"
#include <stdio.h>
#include <string.h>

wasm_engine_t *wasm_engine_new() {
wasm_config_t *config = wasm_config_new();

char *wasmer_test_compiler = getenv("TEST");
char *wasmer_test_engine;

strtok_r(wasmer_test_compiler, "-", &wasmer_test_engine);
printf("Using compiler: %s, engine: %s\n", wasmer_test_compiler,
wasmer_test_engine);
if (strcmp(wasmer_test_compiler, "cranelift") == 0) {
assert(wasmer_is_compiler_available(CRANELIFT));
wasm_config_set_compiler(config, CRANELIFT);
} else if (strcmp(wasmer_test_compiler, "llvm") == 0) {
assert(wasmer_is_compiler_available(LLVM));
wasm_config_set_compiler(config, LLVM);
} else if (strcmp(wasmer_test_compiler, "singlepass") == 0) {
assert(wasmer_is_compiler_available(SINGLEPASS));
wasm_config_set_compiler(config, SINGLEPASS);
} else if (wasmer_test_compiler) {
printf("Compiler %s not recognized\n", wasmer_test_compiler);
abort();
}
if (strcmp(wasmer_test_engine, "jit") == 0) {
assert(wasmer_is_engine_available(JIT));
wasm_config_set_engine(config, JIT);
} else if (strcmp(wasmer_test_engine, "native") == 0) {
assert(wasmer_is_engine_available(NATIVE));
wasm_config_set_engine(config, NATIVE);
} else if (wasmer_test_engine) {
printf("Engine %s not recognized\n", wasmer_test_engine);
abort();
}

wasm_engine_t *engine = wasm_engine_new_with_config(config);
return engine;
}

#endif
1 change: 1 addition & 0 deletions lib/c-api/tests/wasmer_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define TEST_WASMER_WASM

#include "../wasmer_wasm.h"
#include "wasm.h"
#include <stdio.h>
#include <string.h>

Expand Down

0 comments on commit 71176cc

Please sign in to comment.