Final fixes for make test-capi on windows-msvc

- .wasm files should always be opened in "rb" mode
- open_memstream doesn't exist on Windows, use tempfile() instead
- remove .obj and .exe files when the test finish
This commit is contained in:
Felix Schütt
2022-09-14 22:31:19 +02:00
parent fd390f6160
commit 7441fade9c
8 changed files with 196 additions and 169 deletions

8
Cargo.lock generated
View File

@ -3054,6 +3054,14 @@ dependencies = [
"wasmer-compiler-singlepass",
]
[[package]]
name = "wasmer-capi-examples-runner"
version = "0.1.0"
dependencies = [
"cc",
"target-lexicon 0.11.2",
]
[[package]]
name = "wasmer-cli"
version = "3.0.0-beta"

View File

@ -47,6 +47,7 @@ members = [
"lib/wasi-experimental-io-devices",
"lib/wasi-local-networking",
"lib/c-api/tests/wasmer-c-api-test-runner",
"lib/c-api/examples/wasmer-capi-examples-runner",
"lib/types",
"tests/wasi-wast",
"tests/lib/wast",

View File

@ -4,14 +4,26 @@ $(info Using provided WASMER_DIR=$(WASMER_DIR))
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
MSVC_CFLAGS:=""
MSVC_LDFLAGS:=""
MSVC_LDLIBS:=""
ifeq (,$(wildcard $(WASMER_DIR)/bin/wasmer))
CFLAGS = -g -I$(ROOT_DIR)/../tests -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
MSVC_CFLAGS:= /DEBUG /I $(ROOT_DIR)/ /I $(WASMER_DIR)/include
MSVC_LDFLAGS:= ""
MSVC_LDLIBS:= /LIBPATH:$(WASMER_DIR)/lib wasmer.dll.lib
else
CFLAGS = -g -I$(ROOT_DIR)/../tests -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)
MSVC_CFLAGS:= /DEBUG /I $(ROOT_DIR)/ /I $(shell $(WASMER_DIR)/bin/wasmer config --includedir)
MSVC_LDFLAGS:= ""
MSVC_LDLIBS:= /LIBPATH:$(shell $(WASMER_DIR)/bin/wasmer config --libs) wasmer.dll.lib
endif
$(info * CFLAGS: $(CFLAGS))
@ -20,44 +32,10 @@ $(info * LDLIBS: $(LDLIBS))
ALL = deprecated-header early-exit instance imports-exports exports-function exports-global memory memory2 features wasi
.SILENT: deprecated-header deprecated-header.o
deprecated-header: deprecated-header.o
.SILENT: early-exit early-exit.o
early-exit: early-exit.o
.SILENT: instance instance.o
instance: instance.o
.SILENT: imports-exports imports-exports.o
imports-exports: imports-exports.o
.SILENT: exports-function exports-function.o
exports-function: exports-function.o
.SILENT: exports-global exports-global.o
exports-global: exports-global.o
.SILENT: memory memory.o
memory: memory.o
.SILENT: memory2 memory2.o
memory2: memory2.o
.SILENT: features features.o
features: features.o
.SILENT: wasi wasi.o
wasi: wasi.o
.PHONY: all
all: $(ALL)
.PHONY: run
.SILENT: run
run: $(ALL)
set -o errexit; \
$(foreach example,$?,echo Running \"$(example)\" example; ./$(example); echo;)
run:
WASMER_DIR="$(WASMER_DIR)" ROOT_DIR="$(ROOT_DIR)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LDLIBS="$(LDLIBS)" cargo test --manifest-path="./wasmer-capi-examples-runner/Cargo.toml" -- --nocapture
.SILENT: clean
.PHONY: clean

View File

@ -45,7 +45,7 @@ int main(int argc, const char *argv[]) {
// Load binary.
printf("Loading binary...\n");
FILE *file = fopen("assets/call_trap.wasm", "r");
FILE *file = fopen("assets/call_trap.wasm", "rb");
if (!file) {
printf("> Error loading module!\n");
return 1;

View File

@ -38,7 +38,7 @@ int main(int argc, const char* argv[]) {
// Load binary.
printf("Loading binary...\n");
FILE* file = fopen("assets/qjs.wasm", "r");
FILE* file = fopen("assets/qjs.wasm", "rb");
if (!file) {
printf("> Error loading module!\n");
return 1;
@ -49,7 +49,7 @@ int main(int argc, const char* argv[]) {
wasm_byte_vec_t binary;
wasm_byte_vec_new_uninitialized(&binary, file_size);
if (fread(binary.data, file_size, 1, file) != 1) {
printf("> Error loading module!\n");
printf("> Error initializing module!\n");
return 1;
}
fclose(file);
@ -137,12 +137,12 @@ int main(int argc, const char* argv[]) {
}
printf("Call completed\n");
{
FILE *memory_stream;
char* stdout;
size_t stdout_size = 0;
if(true) {
memory_stream = open_memstream(&stdout, &stdout_size);
// NOTE: previously, this used open_memstream,
// which is not cross-platform
FILE *memory_stream = NULL;
memory_stream = tmpfile(); // stdio.h
if (NULL == memory_stream) {
printf("> Error creating a memory stream.\n");
@ -161,15 +161,20 @@ int main(int argc, const char* argv[]) {
}
if (data_read_size > 0) {
stdout_size += data_read_size;
fwrite(buffer, sizeof(char), data_read_size, memory_stream);
}
} while (BUF_SIZE == data_read_size);
// print memory_stream
rewind(memory_stream);
fputs("WASI Stdout: ", stdout);
char buffer2[256];
while (!feof(memory_stream)) {
if (fgets(buffer2, 256, memory_stream) == NULL) break;
fputs(buffer2, stdout);
}
fputs("\n", stdout);
fclose(memory_stream);
printf("WASI Stdout: `%.*s`\n", (int) stdout_size, stdout);
free(stdout);
}

View File

@ -35,7 +35,7 @@ $(info * LDFLAGS: $(LDFLAGS))
$(info * LDLIBS: $(LDLIBS))
test:
cargo test --manifest-path="./wasmer-c-api-test-runner/Cargo.toml" -- --nocapture
WASMER_DIR="$(WASMER_DIR)" ROOT_DIR="$(ROOT_DIR)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LDLIBS="$(LDLIBS)" cargo test --manifest-path="./wasmer-c-api-test-runner/Cargo.toml" -- --nocapture
.SILENT: clean
.PHONY: clean

View File

@ -1 +0,0 @@
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.33.31629\\bin\\HostX64\\x64\\cl.exe" -nologo -MD -O1 -Brepro -W4 "C:\\Users\\felix\\Development\\wasmer\\lib\\c-api\\tests\\wasmer-c-api-test-runner/../wasm-c-api/example/callback.c" /I "C:/Users/felix/Development/wasmer/package/include" /link /LIBPATH:"C:/Users/felix/Development/wasmer/package/lib" "C:/Users/felix/Development/wasmer/package/lib/wasmer.dll.lib" /OUT:"C:\\Users\\felix\\Development\\wasmer\\lib\\c-api\\tests\\wasmer-c-api-test-runner/../wasm-c-api/example/callback.exe"

View File

@ -30,6 +30,36 @@ impl Config {
}
}
#[derive(Default)]
pub struct RemoveTestsOnDrop { }
impl Drop for RemoveTestsOnDrop {
fn drop(&mut self) {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
for entry in std::fs::read_dir(&manifest_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let extension = path.extension().and_then(|s| s.to_str());
if extension == Some("obj") || extension == Some("exe") {
println!("removing {}", path.display());
let _ = std::fs::remove_file(&path);
}
}
if let Some(parent) = std::path::Path::new(&manifest_dir).parent() {
for entry in std::fs::read_dir(&parent).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let extension = path.extension().and_then(|s| s.to_str());
if extension == Some("obj") || extension == Some("exe") {
println!("removing {}", path.display());
let _ = std::fs::remove_file(&path);
}
}
}
}
}
const CAPI_BASE_TESTS: &[&str] = &[
"wasm-c-api/example/callback",
"wasm-c-api/example/memory",
@ -56,15 +86,15 @@ const CAPI_BASE_TESTS_NOT_WORKING: &[&str] = &[
#[test]
fn test_ok() {
let _drop = RemoveTestsOnDrop::default();
let config = Config::get();
println!("config: {:#?}", config);
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let host = target_lexicon::HOST.to_string();
let target = &host;
#[cfg(target_os = "windows")]
if target.contains("msvc") {
for test in CAPI_BASE_TESTS.iter() {
let mut build = cc::Build::new();
@ -157,8 +187,7 @@ fn test_ok() {
// -o wasm-c-api/example/callback
}
#[cfg(not(target_os = "windows"))]
} else {
for test in CAPI_BASE_TESTS.iter() {
let mut command = std::process::Command::new("cc");
@ -184,11 +213,18 @@ fn test_ok() {
if !output.status.success() {
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stdout: {}", String::from_utf8_lossy(&output.stderr));
panic!("failed to compile {test}");
panic!("failed to execute {test}");
}
}
}
for test in CAPI_BASE_TESTS.iter() {
let _ = std::fs::remove_file(&format!("{manifest_dir}/{test}.obj"));
let _ = std::fs::remove_file(&format!("{manifest_dir}/../{test}.exe"));
let _ = std::fs::remove_file(&format!("{manifest_dir}/../{test}"));
}
}
#[cfg(test)]
fn find_vcvars64(compiler: &cc::Tool) -> Option<String> {