Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/examples/CMakeLists.txt
3069 views
1
cmake_minimum_required(VERSION 3.10)
2
project(wasmtime-examples)
3
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4
set(CMAKE_CXX_STANDARD 17)
5
6
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../crates/c-api ${CMAKE_CURRENT_BINARY_DIR}/wasmtime)
7
8
function(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 ../..)
38
endfunction()
39
40
function(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()
46
endfunction()
47
48
function(CREATE_RUST_WASM EXAMPLE TARGET)
49
add_custom_target(${EXAMPLE}-wasm-${TARGET} ALL COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET})
50
endfunction()
51
52
# Enable testing
53
enable_testing()
54
55
# Wasm files required by tests, but only built if tests are actually being run.
56
create_rust_wasm(fib-debug wasm32-unknown-unknown)
57
create_rust_wasm(tokio wasm32-wasip1)
58
create_rust_wasm(wasi wasm32-wasip1)
59
create_rust_wasm(wasi wasm32-wasip2)
60
create_rust_wasm(component wasm32-unknown-unknown)
61
create_rust_wasm(resource-component wasm32-wasip2)
62
63
# C/C++ examples/tests
64
create_target(anyref anyref.c)
65
create_target(anyref-cpp anyref.cc)
66
create_target(async async.cc)
67
create_target(externref externref.c)
68
create_target(externref-cpp externref.cc)
69
create_target(fib-debug fib-debug/main.c)
70
create_target(fuel fuel.c)
71
create_target(fuel-cpp fuel.cc)
72
create_target(gcd gcd.c)
73
create_target(gcd-cpp gcd.cc)
74
create_target(hello hello.c)
75
create_target(hello-cpp hello.cc)
76
create_target(interrupt interrupt.c)
77
create_target(interrupt-cpp interrupt.cc)
78
create_target(linking linking.c)
79
create_target(linking-cpp linking.cc)
80
create_target(memory memory.c)
81
create_target(memory-cpp memory.cc)
82
create_target(multi multi.c)
83
create_target(multi-cpp multi.cc)
84
create_target(multimemory multimemory.c)
85
create_target(multimemory-cpp multimemory.cc)
86
create_target(serialize serialize.c)
87
create_target(serialize-cpp serialize.cc)
88
create_target(threads threads.c)
89
create_target(threads-cpp threads.cc)
90
create_target(wasip1 wasip1/main.c)
91
create_target(wasip1-cpp wasip1/main.cc)
92
93
# Rust examples/tests
94
if (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)
117
endif()
118
119
if (BUILD_RUST_PLUGINS_EXAMPLE)
120
add_test(NAME wasip2-plugins-rust COMMAND cargo build --features build-plugins WORKING_DIRECTORY ../../examples/wasip2-plugins)
121
endif()
122
123