Skip to content

Commit

Permalink
Merge #1871
Browse files Browse the repository at this point in the history
1871: chore: Add C examples r=syrusakbary a=jubianchi

* [x] Add basic examples
* [x] Add command to test the examples
* [x] Update docs.wasmer.io with the new API changes. Namely: 4df7722

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: jubianchi <[email protected]>
Co-authored-by: Syrus <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2020
2 parents 84be06c + 4433e93 commit 3c2815b
Show file tree
Hide file tree
Showing 12 changed files with 695 additions and 39 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,11 @@ jobs:
- name: Build C API
run: |
make build-capi
if: matrix.os != 'windows-latest' && matrix.target != 'aarch64-apple-darwin'
- name: Build C API
if: matrix.target != 'aarch64-apple-darwin'
- name: Build C API (system libffi)
run: |
make build-capi-cranelift-system-libffi
if: matrix.target == 'aarch64-apple-darwin'
- name: Build C API on Windows
run: make build-capi
if: matrix.os == 'windows-latest'
- name: Build Wasmer binary
run: |
make build-wasmer
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ api-docs-repo/
# Generated by tests on Android
/avd
/core
/lib/c-api/examples/exports-function
/lib/c-api/examples/exports-global
/lib/c-api/examples/imports-exports
/lib/c-api/examples/instance
/lib/c-api/examples/memory
70 changes: 36 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ ifeq ($(ARCH), x86_64)
endif
endif

ifeq ($(ARCH), aarch64)
# If it's an aarch64/arm64 chip
# Using filter as a logical OR
# https://stackoverflow.com/questions/7656425/makefile-ifeq-logical-or
ifneq (,$(filter $(ARCH),aarch64 arm64))
test_compilers_engines += cranelift-jit
ifneq (, $(findstring llvm,$(compilers)))
test_compilers_engines += llvm-native
endif
endif

ifeq ($(ARCH), arm64)
test_compilers_engines += cranelift-jit
# if we are in Darwin, we use the system libffi for the capi
ifeq ($(UNAME_S), Darwin)
capi_default_features := --features system-libffi
endif
endif

compilers := $(filter-out ,$(compilers))
Expand Down Expand Up @@ -110,55 +113,55 @@ build-capi: build-capi-cranelift

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

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

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

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

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

build-capi-cranelift-system-libffi:
cargo build --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,native,object-file,cranelift,wasi,system-libffi $(capi_default_features)

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

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

build-capi-cranelift-object-file:
cargo build --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,native,object-file,cranelift,wasi

build-capi-cranelift-system-libffi:
cargo build --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,jit,native,object-file,cranelift,wasi,system-libffi
--no-default-features --features deprecated,wat,native,object-file,cranelift,wasi $(capi_default_features)

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

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

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

build-capi-llvm-object-file:
cargo build --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,object-file,llvm,wasi
--no-default-features --features deprecated,wat,object-file,llvm,wasi $(capi_default_features)

# Headless (we include the minimal to be able to run)

Expand Down Expand Up @@ -223,31 +226,30 @@ test-packages:
# link the tests against. cargo test doesn't know that the tests will be running
# cmake + make to build programs whose dependencies cargo isn't aware of.

test-capi: $(foreach compiler_engine,$(test_compilers_engines),test-capi-$(compiler_engine)) #$(if $(findstring cranelift-jit,$(test_compilers_engines)),test-capi-cranelift-jit-system-libffi)
test-capi: $(foreach compiler_engine,$(test_compilers_engines),test-capi-$(compiler_engine))

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

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

test-capi-cranelift-native: build-capi-cranelift-native
test-capi-cranelift-native: build-capi-cranelift-native test-capi-examples
cargo test --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features deprecated,wat,native,cranelift,wasi -- --nocapture

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

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

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

test-capi-examples: package-capi
cd lib/c-api/examples; WASMER_DIR=`pwd`/../../../package make run

test-wasi-unit:
cargo test --manifest-path lib/wasi/Cargo.toml --release
Expand Down
1 change: 1 addition & 0 deletions lib/c-api/examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.o
47 changes: 47 additions & 0 deletions lib/c-api/examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

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

ifeq (,$(wildcard $(WASMER_DIR)/bin/wasmer))
CFLAGS = -g -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)
LDFLAGS = -Wl,-rpath,$(shell $(WASMER_DIR)/bin/wasmer config --libdir)
LDLIBS = $(shell $(WASMER_DIR)/bin/wasmer config --libs)
endif

$(info * CFLAGS: $(CFLAGS))
$(info * LDFLAGS: $(LDFLAGS))
$(info * LDLIBS: $(LDLIBS))

ALL = instance imports-exports exports-function exports-global memory

.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

.PHONY: all
all: $(ALL)

.PHONY: run
.SILENT: run
run: $(ALL)
set -o errexit; \
$(foreach example,$?,echo Running \"$(example)\" example; ./$(example); echo;)

.SILENT: clean
.PHONY: clean
clean:
$(foreach file,$(ALL),rm -f $(file).o $(file))
84 changes: 84 additions & 0 deletions lib/c-api/examples/exports-function.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <stdio.h>
#include "wasmer_wasm.h"

int main(int argc, const char* argv[]) {
const char *wat_string =
"(module\n"
" (type $sum_t (func (param i32 i32) (result i32)))\n"
" (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)\n"
" local.get $x\n"
" local.get $y\n"
" i32.add)\n"
" (export \"sum\" (func $sum_f)))";

wasm_byte_vec_t wat;
wasm_byte_vec_new(&wat, strlen(wat_string), wat_string);
wasm_byte_vec_t wasm_bytes;
wat2wasm(&wat, &wasm_bytes);

printf("Creating the store...\n");
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);

printf("Compiling module...\n");
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);

if (!module) {
printf("> Error compiling module!\n");

return 1;
}

wasm_byte_vec_delete(&wasm_bytes);

printf("Creating imports...\n");
wasm_extern_vec_t import_object = WASM_EMPTY_VEC;

printf("Instantiating module...\n");
wasm_instance_t* instance = wasm_instance_new(store, module, &import_object, NULL);

if (!instance) {
printf("> Error instantiating module!\n");

return 1;
}

printf("Retrieving exports...\n");
wasm_extern_vec_t exports;
wasm_instance_exports(instance, &exports);

if (exports.size == 0) {
printf("> Error accessing exports!\n");

return 1;
}

printf("Retrieving the `sum` function...\n");
wasm_func_t* sum_func = wasm_extern_as_func(exports.data[0]);

if (sum_func == NULL) {
printf("> Failed to get the `sum` function!\n");

return 1;
}

printf("Calling `sum` function...\n");
wasm_val_t args_val[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
wasm_val_t results_val[1] = { WASM_INIT_VAL };
wasm_val_vec_t args = WASM_ARRAY_VEC(args_val);
wasm_val_vec_t results = WASM_ARRAY_VEC(results_val);

if (wasm_func_call(sum_func, &args, &results)) {
printf("> Error calling the `sum` function!\n");

return 1;
}

printf("Results of `sum`: %d\n", results_val[0].of.i32);

wasm_func_delete(sum_func);
wasm_module_delete(module);
wasm_instance_delete(instance);
wasm_store_delete(store);
wasm_engine_delete(engine);
}
Loading

0 comments on commit 3c2815b

Please sign in to comment.