1cmake_minimum_required(VERSION 3.10) 2project(wasmtime-examples) 3set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 4set(CMAKE_CXX_STANDARD 17) 5 6add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../crates/c-api ${CMAKE_CURRENT_BINARY_DIR}/wasmtime) 7 8function(CREATE_TARGET TARGET TARGET_PATH) 9 add_executable(wasmtime-${TARGET} ${TARGET_PATH}) 10 11 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 12 target_compile_options(wasmtime-${TARGET} PRIVATE -Wall -Wextra -Wno-deprecated-declarations) 13 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") 14 target_compile_options(wasmtime-${TARGET} PRIVATE /W3) 15 endif() 16 17 # This is only used in fib-debug.c right now and it only works with static 18 # linking because the required symbols aren't exported in the shared library, so 19 # only when shared libs are disabled is this turned on. 20 if (NOT BUILD_SHARED_LIBS) 21 target_compile_definitions(wasmtime-${TARGET} PRIVATE WASMTIME_TEST_ONLY) 22 endif() 23 24 if (CMAKE_C_FLAGS MATCHES ".*-fsanitize=address.*") 25 target_compile_definitions(wasmtime-${TARGET} PRIVATE WASMTIME_ASAN) 26 endif() 27 28 set_target_properties(wasmtime-${TARGET} PROPERTIES 29 OUTPUT_NAME wasmtime-${TARGET} 30 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<0:> 31 CXX_VISIBILITY_PRESET hidden 32 POSITION_INDEPENDENT_CODE ON) 33 34 target_include_directories(wasmtime-${TARGET} PUBLIC wasmtime) 35 target_link_libraries(wasmtime-${TARGET} PUBLIC wasmtime) 36 37 add_test(NAME ${TARGET}-c COMMAND wasmtime-${TARGET} WORKING_DIRECTORY ../..) 38endfunction() 39 40function(CREATE_RUST_TEST EXAMPLE) 41 if(ARGC GREATER 1) 42 add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} --features ${ARGV1} WORKING_DIRECTORY ../..) 43 else() 44 add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} WORKING_DIRECTORY ../..) 45 endif() 46endfunction() 47 48function(CREATE_RUST_WASM EXAMPLE TARGET) 49 add_custom_target(${EXAMPLE}-wasm-${TARGET} ALL COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET}) 50endfunction() 51 52# Enable testing 53enable_testing() 54 55# Wasm files required by tests, but only built if tests are actually being run. 56create_rust_wasm(fib-debug wasm32-unknown-unknown) 57create_rust_wasm(tokio wasm32-wasip1) 58create_rust_wasm(wasi wasm32-wasip1) 59create_rust_wasm(wasi wasm32-wasip2) 60create_rust_wasm(component wasm32-unknown-unknown) 61create_rust_wasm(resource-component wasm32-wasip2) 62 63# C/C++ examples/tests 64create_target(anyref anyref.c) 65create_target(anyref-cpp anyref.cc) 66create_target(async async.cc) 67create_target(externref externref.c) 68create_target(externref-cpp externref.cc) 69create_target(fib-debug fib-debug/main.c) 70create_target(fuel fuel.c) 71create_target(fuel-cpp fuel.cc) 72create_target(gcd gcd.c) 73create_target(gcd-cpp gcd.cc) 74create_target(hello hello.c) 75create_target(hello-cpp hello.cc) 76create_target(interrupt interrupt.c) 77create_target(interrupt-cpp interrupt.cc) 78create_target(linking linking.c) 79create_target(linking-cpp linking.cc) 80create_target(memory memory.c) 81create_target(memory-cpp memory.cc) 82create_target(multi multi.c) 83create_target(multi-cpp multi.cc) 84create_target(multimemory multimemory.c) 85create_target(multimemory-cpp multimemory.cc) 86create_target(serialize serialize.c) 87create_target(serialize-cpp serialize.cc) 88create_target(threads threads.c) 89create_target(threads-cpp threads.cc) 90create_target(wasip1 wasip1/main.c) 91create_target(wasip1-cpp wasip1/main.cc) 92 93# Rust examples/tests 94if (BUILD_RUST_EXAMPLES) 95 add_custom_target(rust-examples ALL COMMAND cargo build --examples WORKING_DIRECTORY ../..) 96 create_rust_test(anyref) 97 create_rust_test(epochs) 98 create_rust_test(externref) 99 create_rust_test(fib-debug) 100 create_rust_test(fuel) 101 create_rust_test(gcd) 102 create_rust_test(hello) 103 create_rust_test(interrupt) 104 create_rust_test(linking) 105 create_rust_test(memory) 106 create_rust_test(multi) 107 create_rust_test(multimemory) 108 create_rust_test(serialize) 109 create_rust_test(threads) 110 create_rust_test(wasip1) 111 create_rust_test(wasip1-async) 112 create_rust_test(wasip2) 113 create_rust_test(wasip2-async) 114 create_rust_test(tokio wasi-common/tokio) 115 create_rust_test(component) 116 create_rust_test(resource-component) 117endif() 118 119if (BUILD_RUST_PLUGINS_EXAMPLE) 120 add_test(NAME wasip2-plugins-rust COMMAND cargo build --features build-plugins WORKING_DIRECTORY ../../examples/wasip2-plugins) 121endif() 122