From 2ba3025e6763b5c27c7a6048db045dfa6e8dc96d Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Fri, 22 Jul 2022 11:22:36 -0600 Subject: [PATCH] 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 --- .github/workflows/main.yml | 16 +++- .gitignore | 1 + Cargo.lock | 8 -- Cargo.toml | 1 - README.md | 2 +- crates/c-api/CMakeLists.txt | 64 ++++++++++++++ crates/c-api/include/wasmtime.h | 18 +++- crates/misc/run-examples/Cargo.toml | 10 --- crates/misc/run-examples/build.rs | 7 -- crates/misc/run-examples/src/main.rs | 121 --------------------------- examples/CMakeLists.txt | 73 ++++++++++++++++ examples/README.md | 7 +- examples/externref.c | 4 + examples/fuel.c | 4 + examples/gcd.c | 4 + examples/hello.c | 4 + examples/interrupt.c | 4 + examples/linking.c | 4 + examples/memory.c | 6 +- examples/multi.c | 4 + examples/multimemory.c | 6 +- examples/serialize.c | 4 + examples/threads.c | 24 ++++++ examples/tokio/main.c | 5 -- examples/wasi/main.c | 4 + 25 files changed, 244 insertions(+), 161 deletions(-) create mode 100644 crates/c-api/CMakeLists.txt delete mode 100644 crates/misc/run-examples/Cargo.toml delete mode 100644 crates/misc/run-examples/build.rs delete mode 100644 crates/misc/run-examples/src/main.rs create mode 100644 examples/CMakeLists.txt delete mode 100644 examples/tokio/main.c diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 75f84238e38f..fa069b142151 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -273,11 +273,21 @@ jobs: touch ${{ runner.tool_cache }}/qemu/built if: matrix.gcc != '' - # Ensure all our examples build and execute - - run: cargo run -p run-examples + # Prepare tests in CMake + - run: cmake -Sexamples -Bexamples/build -DBUILD_SHARED_LIBS=OFF + if: matrix.target == '' + # Build tests + - run: cmake --build examples/build --config Debug + if: matrix.target == '' + # Run tests + - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target RUN_TESTS env: RUST_BACKTRACE: 1 - if: matrix.target == '' + if: matrix.target == '' && matrix.os == 'windows-2019' + - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target test + env: + RUST_BACKTRACE: 1 + if: matrix.target == '' && matrix.os != 'windows-2019' # Build and test all features - run: ./ci/run-tests.sh --locked diff --git a/.gitignore b/.gitignore index 41bec43a188d..9cad237bc54f 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ foo .cargo publish vendor +examples/build diff --git a/Cargo.lock b/Cargo.lock index 46e9074f03bd..a5398f234778 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2439,14 +2439,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "run-examples" -version = "0.19.0" -dependencies = [ - "anyhow", - "cc", -] - [[package]] name = "rustc-demangle" version = "0.1.21" diff --git a/Cargo.toml b/Cargo.toml index e1e19a871f1f..ef7dcca4307d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,7 +81,6 @@ members = [ "crates/bench-api", "crates/c-api", "crates/cli-flags", - "crates/misc/run-examples", "examples/fib-debug/wasm", "examples/wasi/wasm", "examples/tokio/wasm", diff --git a/README.md b/README.md index cd9d3790565a..74daa0aeced1 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ You can use Wasmtime from a variety of different languages through embeddings of the implementation: * **[Rust]** - the [`wasmtime` crate] -* **[C]** - the [`wasm.h`, `wasi.h`, and `wasmtime.h` headers][c-headers] or use [`wasmtime` Conan package] +* **[C]** - the [`wasm.h`, `wasi.h`, and `wasmtime.h` headers][c-headers], [CMake](crates/c-api/CMakeLists.txt) or [`wasmtime` Conan package] * **C++** - the [`wasmtime-cpp` repository][wasmtime-cpp] or use [`wasmtime-cpp` Conan package] * **[Python]** - the [`wasmtime` PyPI package] * **[.NET]** - the [`Wasmtime` NuGet package] diff --git a/crates/c-api/CMakeLists.txt b/crates/c-api/CMakeLists.txt new file mode 100644 index 000000000000..1e2ddd50f723 --- /dev/null +++ b/crates/c-api/CMakeLists.txt @@ -0,0 +1,64 @@ +cmake_minimum_required(VERSION 3.10) + +option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) + +if (CMAKE_BUILD_TYPE STREQUAL "Release") + set(WASMTIME_BUILD_TYPE_FLAG "--release") + set(WASMTIME_BUILD_TYPE "release") +else() + set(WASMTIME_BUILD_TYPE "debug") +endif() + +if (BUILD_SHARED_LIBS) + # Copy shared library into build directory + if(WIN32) + set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.dll + ${CMAKE_BINARY_DIR}) + elseif(APPLE) + set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.dylib + ${CMAKE_BINARY_DIR}) + else() + set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.so + ${CMAKE_BINARY_DIR}) + endif() +endif() + +include(ExternalProject) +ExternalProject_Add( + wasmtime-crate + DOWNLOAD_COMMAND "" + CONFIGURE_COMMAND "" + INSTALL_COMMAND "${WASMTIME_INSTALL_COMMAND}" + BUILD_COMMAND cargo build ${WASMTIME_BUILD_TYPE_FLAG} + BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR} + BUILD_ALWAYS ON) +add_library(wasmtime INTERFACE) +add_dependencies(wasmtime wasmtime-crate) + +if (BUILD_SHARED_LIBS) + if(WIN32) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.dll.lib) + elseif(APPLE) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.dylib) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'") + else() + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.so) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'") + endif() +else() + if(WIN32) + target_compile_options(wasmtime INTERFACE -DWASM_API_EXTERN= -DWASI_API_EXTERN=) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.lib + ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt) + elseif(APPLE) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.a) + else() + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.a + pthread dl m) + endif() +endif() + +target_include_directories(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/include) \ No newline at end of file diff --git a/crates/c-api/include/wasmtime.h b/crates/c-api/include/wasmtime.h index 1f20beac2000..c70fd8b71339 100644 --- a/crates/c-api/include/wasmtime.h +++ b/crates/c-api/include/wasmtime.h @@ -28,6 +28,22 @@ * as a `lib` directory with both a static archive and a dynamic library of * Wasmtime. You can link to either of them as you see fit. * + * ## Installing the C API through CMake + * + * CMake can be used to make the process of linking and compiling easier. An + * example of this if you have wasmtime as a git submodule at + * `third_party/wasmtime`: + * ``` + * add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/wasmtime/crates/c-api + * ${CMAKE_CURRENT_BINARY_DIR}/wasmtime) + * ... + * target_include_directories(YourProject PUBLIC wasmtime) + * target_link_libraries(YourProject PUBLIC wasmtime) + * ``` + * `BUILD_SHARED_LIBS` is provided as a define if you would like to build a + * shared library instead. You must distribute the appropriate shared library + * for your platform if you do this. + * * ## Linking against the C API * * You'll want to arrange the `include` directory of the C API to be in your @@ -166,8 +182,8 @@ #include #include -#include #include +#include #include #include #include diff --git a/crates/misc/run-examples/Cargo.toml b/crates/misc/run-examples/Cargo.toml deleted file mode 100644 index 01167683d698..000000000000 --- a/crates/misc/run-examples/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "run-examples" -version = "0.19.0" -authors = ["The Wasmtime Project Developers"] -edition = "2021" -publish = false - -[dependencies] -anyhow = "1.0.31" -cc = "1.0" diff --git a/crates/misc/run-examples/build.rs b/crates/misc/run-examples/build.rs deleted file mode 100644 index a2c6171e4a49..000000000000 --- a/crates/misc/run-examples/build.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - println!( - "cargo:rustc-env=TARGET={}", - std::env::var("TARGET").unwrap() - ); - println!("cargo:rerun-if-changed=build.rs"); -} diff --git a/crates/misc/run-examples/src/main.rs b/crates/misc/run-examples/src/main.rs deleted file mode 100644 index fe54be0184d6..000000000000 --- a/crates/misc/run-examples/src/main.rs +++ /dev/null @@ -1,121 +0,0 @@ -use anyhow::Context; -use std::collections::BTreeSet; -use std::process::Command; - -fn main() -> anyhow::Result<()> { - let example_to_run = std::env::args().nth(1); - let mut examples = BTreeSet::new(); - for e in std::fs::read_dir("examples")? { - let e = e?; - let path = e.path(); - let dir = e.metadata()?.is_dir(); - if let Some("wat") = path.extension().and_then(|s| s.to_str()) { - continue; - } - - examples.insert((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir)); - } - - println!("======== Building libwasmtime.a ==========="); - run(Command::new("cargo") - .args(&["build"]) - .current_dir("crates/c-api"))?; - - for (example, is_dir) in examples { - if example == "README" { - continue; - } - if let Some(example_to_run) = &example_to_run { - if !example.contains(&example_to_run[..]) { - continue; - } - } - if is_dir { - println!("======== Rust wasm file `{}` ============", example); - let target = if example == "fib-debug" { - "wasm32-unknown-unknown" - } else { - "wasm32-wasi" - }; - run(Command::new("cargo") - .arg("build") - .arg("-p") - .arg(format!("example-{}-wasm", example)) - .arg("--target") - .arg(target))?; - } - println!("======== Rust example `{}` ============", example); - let mut cargo_cmd = Command::new("cargo"); - cargo_cmd.arg("run").arg("--example").arg(&example); - - if example.contains("tokio") { - cargo_cmd.arg("--features").arg("wasmtime-wasi/tokio"); - } - run(&mut cargo_cmd)?; - - println!("======== C/C++ example `{}` ============", example); - for extension in ["c", "cc"].iter() { - let mut cmd = cc::Build::new() - .opt_level(0) - .cargo_metadata(false) - .target(env!("TARGET")) - .host(env!("TARGET")) - .include("crates/c-api/include") - .include("crates/c-api/wasm-c-api/include") - .define("WASM_API_EXTERN", Some("")) // static linkage, not dynamic - .warnings(false) - .get_compiler() - .to_command(); - - let file = if is_dir { - format!("examples/{}/main.{}", example, extension) - } else { - format!("examples/{}.{}", example, extension) - }; - - if !std::path::Path::new(&file).exists() { - // C and C++ files are optional so we can skip them. - continue; - } - - cmd.arg(file); - let exe = if cfg!(windows) { - cmd.arg("target/debug/wasmtime.lib") - .arg("ws2_32.lib") - .arg("advapi32.lib") - .arg("userenv.lib") - .arg("ntdll.lib") - .arg("shell32.lib") - .arg("ole32.lib") - .arg("bcrypt.lib"); - if is_dir { - "./main.exe".to_string() - } else { - format!("./{}.exe", example) - } - } else { - cmd.arg("target/debug/libwasmtime.a").arg("-o").arg("foo"); - "./foo".to_string() - }; - if cfg!(target_os = "linux") { - cmd.arg("-lpthread").arg("-ldl").arg("-lm"); - } - run(&mut cmd)?; - - run(&mut Command::new(exe))?; - } - } - - Ok(()) -} - -fn run(cmd: &mut Command) -> anyhow::Result<()> { - (|| -> anyhow::Result<()> { - let s = cmd.status()?; - if !s.success() { - anyhow::bail!("Exited with failure status: {}", s); - } - Ok(()) - })() - .with_context(|| format!("failed to run `{:?}`", cmd)) -} diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 000000000000..8e61cff315c7 --- /dev/null +++ b/examples/CMakeLists.txt @@ -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 $ 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) \ No newline at end of file diff --git a/examples/README.md b/examples/README.md index b888c322acd8..e37175bf24b1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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! diff --git a/examples/externref.c b/examples/externref.c index f240cdfb4a0b..b0107c8db437 100644 --- a/examples/externref.c +++ b/examples/externref.c @@ -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 diff --git a/examples/fuel.c b/examples/fuel.c index 3c5558cb3f8b..9d0fb3bc4610 100644 --- a/examples/fuel.c +++ b/examples/fuel.c @@ -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 diff --git a/examples/gcd.c b/examples/gcd.c index 714eb96263a1..cac7f6bb0dd4 100644 --- a/examples/gcd.c +++ b/examples/gcd.c @@ -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 diff --git a/examples/hello.c b/examples/hello.c index 5c24e18db370..5adf97b9b9ad 100644 --- a/examples/hello.c +++ b/examples/hello.c @@ -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 diff --git a/examples/interrupt.c b/examples/interrupt.c index f09e47ebe41e..900bd2b64cd5 100644 --- a/examples/interrupt.c +++ b/examples/interrupt.c @@ -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 diff --git a/examples/linking.c b/examples/linking.c index 330102c5c3c8..b32d46890c0a 100644 --- a/examples/linking.c +++ b/examples/linking.c @@ -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 diff --git a/examples/memory.c b/examples/memory.c index e9cb221cde2c..37eaf0e13744 100644 --- a/examples/memory.c +++ b/examples/memory.c @@ -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); diff --git a/examples/multi.c b/examples/multi.c index b4b962ef2eaf..4039e2a89eb8 100644 --- a/examples/multi.c +++ b/examples/multi.c @@ -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 diff --git a/examples/multimemory.c b/examples/multimemory.c index 5d4ea982e30a..3f1823d6d326 100644 --- a/examples/multimemory.c +++ b/examples/multimemory.c @@ -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 @@ -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); diff --git a/examples/serialize.c b/examples/serialize.c index e4f444764603..19052571d432 100644 --- a/examples/serialize.c +++ b/examples/serialize.c @@ -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 diff --git a/examples/threads.c b/examples/threads.c index 0fbef5a4c3b3..707601be53cf 100644 --- a/examples/threads.c +++ b/examples/threads.c @@ -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 diff --git a/examples/tokio/main.c b/examples/tokio/main.c deleted file mode 100644 index f6920d8bae4a..000000000000 --- a/examples/tokio/main.c +++ /dev/null @@ -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; -} diff --git a/examples/wasi/main.c b/examples/wasi/main.c index b44e44308991..a52a0afc6830 100644 --- a/examples/wasi/main.c +++ b/examples/wasi/main.c @@ -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