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  target_compile_definitions(wasmtime-${TARGET} PRIVATE WASMTIME_TEST_ONLY)
18
19  set_target_properties(wasmtime-${TARGET} PROPERTIES
20    OUTPUT_NAME wasmtime-${TARGET}
21    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<0:>
22    CXX_VISIBILITY_PRESET hidden
23    POSITION_INDEPENDENT_CODE ON)
24
25  target_include_directories(wasmtime-${TARGET} PUBLIC wasmtime)
26  target_link_libraries(wasmtime-${TARGET} PUBLIC wasmtime)
27  if(APPLE)
28    target_link_libraries(wasmtime-${TARGET} PRIVATE "-framework CoreFoundation")
29  endif()
30  add_test(NAME ${TARGET}-c COMMAND wasmtime-${TARGET} WORKING_DIRECTORY ../..)
31endfunction()
32
33function(CREATE_RUST_TEST EXAMPLE)
34  if(ARGC GREATER 1)
35    add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} --features ${ARGV1} WORKING_DIRECTORY ../..)
36  else()
37    add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} WORKING_DIRECTORY ../..)
38  endif()
39endfunction()
40function(CREATE_RUST_WASM EXAMPLE TARGET)
41  add_custom_target(${EXAMPLE}-wasm-${TARGET} ALL COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET})
42endfunction()
43
44# Enable testing
45enable_testing()
46
47# Wasm files required by tests
48create_rust_wasm(fib-debug wasm32-unknown-unknown)
49create_rust_wasm(tokio wasm32-wasip1)
50create_rust_wasm(wasi wasm32-wasip1)
51create_rust_wasm(wasi wasm32-wasip2)
52create_rust_wasm(component wasm32-unknown-unknown)
53
54# C/C++ examples/tests
55create_target(anyref anyref.c)
56create_target(async async.cpp)
57create_target(externref externref.c)
58create_target(externref-cpp externref.cc)
59create_target(fib-debug fib-debug/main.c)
60create_target(fuel fuel.c)
61create_target(fuel-cpp fuel.cc)
62create_target(gcd gcd.c)
63create_target(gcd-cpp gcd.cc)
64create_target(hello hello.c)
65create_target(hello-cpp hello.cc)
66create_target(interrupt interrupt.c)
67create_target(interrupt-cpp interrupt.cc)
68create_target(linking linking.c)
69create_target(linking-cpp linking.cc)
70create_target(memory memory.c)
71create_target(memory-cpp memory.cc)
72create_target(multi multi.c)
73create_target(multimemory multimemory.c)
74create_target(serialize serialize.c)
75create_target(threads threads.c)
76create_target(wasip1 wasip1/main.c)
77
78# Rust examples/tests
79create_rust_test(anyref)
80create_rust_test(epochs)
81create_rust_test(externref)
82create_rust_test(fib-debug)
83create_rust_test(fuel)
84create_rust_test(gcd)
85create_rust_test(hello)
86create_rust_test(interrupt)
87create_rust_test(linking)
88create_rust_test(memory)
89create_rust_test(multi)
90create_rust_test(multimemory)
91create_rust_test(serialize)
92create_rust_test(threads)
93create_rust_test(wasip1)
94create_rust_test(wasip1-async)
95create_rust_test(wasip2)
96create_rust_test(wasip2-async)
97create_rust_test(tokio wasi-common/tokio)
98create_rust_test(component)
99