Add cmake compatibility to c-api (#4369)

* Add cmake compatibility to c-api

* Add CMake documentation to wasmtime.h

* Add CMake instructions in examples

* Modify CI for CMake support

* Use correct rust in CI

* Trigger build

* Refactor run-examples

* Reintroduce example_to_run in run-examples

* Replace run-examples crate with cmake

* Fix markdown formatting in examples readme

* Fix cmake test quotes

* Build rust wasm before cmake tests

* Pass CTEST_OUTPUT_ON_FAILURE

* Another cmake test

* Handle os differences in cmake test

* Fix bugs in memory and multimemory examples
This commit is contained in:
TheGreatRambler
2022-07-22 11:22:36 -06:00
committed by GitHub
parent 35b750ab9a
commit 2ba3025e67
25 changed files with 244 additions and 161 deletions

73
examples/CMakeLists.txt Normal file
View File

@@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.10)
project(wasmtime-examples)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../crates/c-api ${CMAKE_CURRENT_BINARY_DIR}/wasmtime)
function(CREATE_TARGET TARGET TARGET_PATH)
add_executable(wasmtime-${TARGET} ${TARGET_PATH})
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(wasmtime-${TARGET} PRIVATE -Wall -Wextra -Wno-deprecated-declarations)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(wasmtime-${TARGET} PRIVATE /W3)
endif()
set_target_properties(wasmtime-${TARGET} PROPERTIES
OUTPUT_NAME wasmtime-${TARGET}
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<0:>
CXX_VISIBILITY_PRESET hidden
POSITION_INDEPENDENT_CODE ON)
target_include_directories(wasmtime-${TARGET} PUBLIC wasmtime)
target_link_libraries(wasmtime-${TARGET} PUBLIC wasmtime)
add_test(NAME ${TARGET}-c COMMAND $<TARGET_FILE:wasmtime-${TARGET}> WORKING_DIRECTORY ../..)
endfunction()
function(CREATE_RUST_TEST EXAMPLE)
if(ARGC GREATER 1)
add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} --features ${ARGV1} WORKING_DIRECTORY ../..)
else()
add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} WORKING_DIRECTORY ../..)
endif()
endfunction()
function(CREATE_RUST_WASM EXAMPLE TARGET)
execute_process(COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET})
endfunction()
# Enable testing
enable_testing()
# Add all examples
create_target(externref externref.c)
create_target(fib-debug fib-debug/main.c)
create_target(fuel fuel.c)
create_target(gcd gcd.c)
create_target(hello hello.c)
create_target(interrupt interrupt.c)
create_target(linking linking.c)
create_target(memory memory.c)
create_target(multi multi.c)
create_target(multimemory multimemory.c)
create_target(serialize serialize.c)
create_target(threads threads.c)
create_target(wasi wasi/main.c)
# Add rust tests
create_rust_wasm(fib-debug wasm32-unknown-unknown)
create_rust_wasm(tokio wasm32-wasi)
create_rust_wasm(wasi wasm32-wasi)
create_rust_test(epochs)
create_rust_test(externref)
create_rust_test(fib-debug)
create_rust_test(fuel)
create_rust_test(gcd)
create_rust_test(hello)
create_rust_test(interrupt)
create_rust_test(linking)
create_rust_test(memory)
create_rust_test(multi)
create_rust_test(multimemory)
create_rust_test(serialize)
create_rust_test(threads)
create_rust_test(wasi)
create_rust_test(tokio wasmtime-wasi/tokio)

View File

@@ -8,7 +8,10 @@ Each example is available in both C and in Rust. Examples are accompanied with a
`*.wat` file which is the wasm input, or a Rust project in a `wasm` folder which
is the source code for the original wasm file.
Rust examples can be executed with `cargo run --example $name`, and C examples
need to be compiled using your system compiler and appropriate header files.
Rust examples can be executed with `cargo run --example $name`. C examples can
be built with `mkdir build && cd build && cmake ..`. You can run
`cmake --build .` to build all examples or
`cmake --build . --target wasmtime-$name`, replacing the name as you wish. They
can also be [built manually](https://docs.wasmtime.dev/c-api/).
For more information see the examples themselves!

View File

@@ -15,6 +15,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations as well as the name of the
`libwasmtime.a` file on Windows.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-externref
*/
#include <assert.h>

View File

@@ -15,6 +15,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-fuel
*/
#include <assert.h>

View File

@@ -15,6 +15,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-gcd
*/
#include <assert.h>

View File

@@ -16,6 +16,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations as well as the name of the
`libwasmtime.a` file on Windows.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-hello
*/
#include <assert.h>

View File

@@ -16,6 +16,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations as well as the name of the
`libwasmtime.a` file on Windows.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-interrupt
*/
#include <assert.h>

View File

@@ -15,6 +15,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-linking
*/
#include <assert.h>

View File

@@ -16,6 +16,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-memory
Also note that this example was taken from
https://github.com/WebAssembly/wasm-c-api/blob/master/example/memory.c
originally
@@ -220,7 +224,7 @@ int main(int argc, const char* argv[]) {
// Grow memory.
printf("Growing memory...\n");
uint32_t old_size;
uint64_t old_size;
error = wasmtime_memory_grow(context, &memory, 1, &old_size);
if (error != NULL)
exit_with_error("failed to grow memory", error, trap);

View File

@@ -16,6 +16,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-multi
Also note that this example was taken from
https://github.com/WebAssembly/wasm-c-api/blob/master/example/multi.c
originally

View File

@@ -14,6 +14,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-multimemory
*/
#include <inttypes.h>
@@ -264,7 +268,7 @@ int main(int argc, const char* argv[]) {
// Grow memory.
printf("Growing memory...\n");
uint32_t old_size;
uint64_t old_size;
error = wasmtime_memory_grow(context, &memory0, 1, &old_size);
if (error != NULL)
exit_with_error("failed to grow memory", error, trap);

View File

@@ -16,6 +16,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations as well as the name of the
`libwasmtime.a` file on Windows.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-serialize
*/
#include <assert.h>

View File

@@ -1,3 +1,27 @@
/*
Example of instantiating of the WebAssembly module and invoking its exported
function in a separate thread.
You can compile and run this example on Linux with:
cargo build --release -p wasmtime-c-api
cc examples/threads.c \
-I crates/c-api/include \
-I crates/c-api/wasm-c-api/include \
target/release/libwasmtime.a \
-lpthread -ldl -lm \
-o threads
./threads
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations as well as the name of the
`libwasmtime.a` file on Windows.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-threads
*/
#ifndef _WIN32
#include <inttypes.h>

View File

@@ -1,5 +0,0 @@
int main(int argc, char *argv[]) {
// This example is specific to integrating with Rust's tokio ecosystem, so
// it isnt applicable to C/C++.
return 0;
}

View File

@@ -14,6 +14,10 @@ You can compile and run this example on Linux with:
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-wasi
*/
#include <assert.h>