-
Notifications
You must be signed in to change notification settings - Fork 8
/
CMakeLists.txt
351 lines (303 loc) · 14.1 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
cmake_minimum_required(VERSION 3.16)
include(cmake/LLVM.cmake)
project(toywasm LANGUAGES C)
# We use C11 features like _Atomic, _Static_assert
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED YES)
set(CMAKE_C_EXTENSIONS NO)
# we need timespec (199309L), flockfile (199506L), etc
add_compile_definitions(_POSIX_C_SOURCE=199506L)
include(CTest)
include(CheckIPOSupported)
if(BUILD_TESTING)
enable_testing()
endif()
include(cmake/ToywasmConfig.cmake)
add_subdirectory(lib)
if(TOYWASM_ENABLE_WASI)
add_subdirectory(libwasi)
endif()
if(TOYWASM_ENABLE_WASI_THREADS)
add_subdirectory(libwasi_threads)
endif()
if(TOYWASM_ENABLE_WASI_LITTLEFS)
add_subdirectory(libwasi_littlefs)
endif()
if(TOYWASM_ENABLE_DYLD)
add_subdirectory(libdyld)
endif()
if(CMAKE_C_COMPILER_TARGET MATCHES "wasm")
set(TOYWASM_CLI "${CMAKE_CURRENT_SOURCE_DIR}/test/toywasm-on-toywasm.py")
if(NOT DEFINED ENV{TOYWASM_NATIVE})
list(APPEND TEST_ENV "TOYWASM_NATIVE=${CMAKE_CURRENT_SOURCE_DIR}/build.native/toywasm")
endif()
if(NOT DEFINED ENV{TOYWASM_WASM})
list(APPEND TEST_ENV "TOYWASM_WASM=${CMAKE_BINARY_DIR}/toywasm")
endif()
else()
set(TOYWASM_CLI "${CMAKE_BINARY_DIR}/toywasm")
endif()
# for wasi-testsuite-adapter.py
list(APPEND TEST_ENV "TEST_RUNTIME_EXE=${TOYWASM_CLI}")
# cli
if(TOYWASM_BUILD_CLI)
set(cli_sources
"cli/main.c"
"cli/repl.c"
"cli/str_to_uint.c"
)
add_executable(toywasm-cli ${cli_sources})
target_link_libraries(toywasm-cli toywasm-lib-core
$<$<BOOL:${TOYWASM_ENABLE_WASI}>:toywasm-lib-wasi>
$<$<BOOL:${TOYWASM_ENABLE_WASI_THREADS}>:toywasm-lib-wasi-threads>
$<$<BOOL:${TOYWASM_ENABLE_WASI_LITTLEFS}>:toywasm-lib-wasi-littlefs>
$<$<BOOL:${TOYWASM_ENABLE_DYLD}>:toywasm-lib-dyld>
m)
set_target_properties(toywasm-cli PROPERTIES OUTPUT_NAME toywasm)
add_test(NAME toywasm-cli-simple-module COMMAND
${TOYWASM_CLI} --load=spectest.wasm "--invoke=print_i32 123"
)
set_tests_properties(toywasm-cli-simple-module PROPERTIES ENVIRONMENT "${TEST_ENV}")
add_test(NAME toywasm-cli-timeout COMMAND
${TOYWASM_CLI} --timeout=100 infiniteloop.wasm
)
set_tests_properties(toywasm-cli-timeout PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-timeout PROPERTIES LABELS "timeout")
set_tests_properties(toywasm-cli-timeout PROPERTIES WILL_FAIL ON)
add_test(NAME toywasm-cli-start-timeout COMMAND
${TOYWASM_CLI} --timeout=100 infiniteloop_in_start.wasm
)
set_tests_properties(toywasm-cli-start-timeout PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-start-timeout PROPERTIES LABELS "timeout")
set_tests_properties(toywasm-cli-start-timeout PROPERTIES WILL_FAIL ON)
if(TOYWASM_ENABLE_WASI_THREADS)
add_test(NAME toywasm-cli-timeout-wasi-threads COMMAND
${TOYWASM_CLI} --wasi --timeout=100 infiniteloops.wasm
)
set_tests_properties(toywasm-cli-timeout-wasi-threads PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-timeout-wasi-threads PROPERTIES LABELS "timeout")
set_tests_properties(toywasm-cli-timeout-wasi-threads PROPERTIES WILL_FAIL ON)
endif()
# some tests are not compatible with multi-memory
# (binary.wast, imports.wast, memory.wast)
# REVISIT: disable only those tests.
if(NOT TOYWASM_ENABLE_WASM_MULTI_MEMORY)
add_test(NAME toywasm-cli-wasm3-spec-test
# Note: arbitrary limits for stack overflow tests in call.wast.
# (--max-frames and --max-stack-cells)
COMMAND ./test/run-wasm3-spec-test-opam-2.0.0.sh --exec "${TOYWASM_CLI} --max-frames=201 --max-stack-cells=1000 --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test PROPERTIES LABELS "spec")
endif()
if(TOYWASM_ENABLE_WASM_SIMD)
add_test(NAME toywasm-cli-wasm3-spec-test-simd
COMMAND ./test/run-wasm3-spec-test-simd.sh --exec "${TOYWASM_CLI} --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test-simd PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test-simd PROPERTIES LABELS "spec;simd")
endif()
if(TOYWASM_ENABLE_WASM_EXTENDED_CONST)
add_test(NAME toywasm-cli-wasm3-spec-test-extended-const
COMMAND ./test/run-wasm3-spec-test-extended-const.sh --exec "${TOYWASM_CLI} --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test-extended-const PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test-extended-const PROPERTIES LABELS "spec")
endif()
# Note: some of EH tests require tail-call.
# the spec interpreter in the EH proposal repo has
# cherry-picked tail-call support for it.
if(TOYWASM_ENABLE_WASM_EXCEPTION_HANDLING)
if(TOYWASM_ENABLE_WASM_TAILCALL)
add_test(NAME toywasm-cli-wasm3-spec-test-exception-handling
COMMAND ./test/run-wasm3-spec-test-exception-handling.sh --exec "${TOYWASM_CLI} --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test-exception-handling PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test-exception-handling PROPERTIES LABELS "spec")
endif()
endif()
if(TOYWASM_ENABLE_WASM_MULTI_MEMORY)
add_test(NAME toywasm-cli-wasm3-spec-test-multi-memory
COMMAND ./test/run-wasm3-spec-test-multi-memory.sh --exec "${TOYWASM_CLI} --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test-multi-memory PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test-multi-memory PROPERTIES LABELS "spec")
endif()
if(TOYWASM_ENABLE_WASM_TAILCALL)
add_test(NAME toywasm-cli-wasm3-spec-test-tailcall
COMMAND ./test/run-wasm3-spec-test-tailcall.sh --exec "${TOYWASM_CLI} --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test-tailcall PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test-tailcall PROPERTIES LABELS "spec")
endif()
if(TOYWASM_ENABLE_WASM_THREADS)
add_test(NAME toywasm-cli-wasm3-spec-test-threads
COMMAND ./test/run-wasm3-spec-test-threads.sh --exec "${TOYWASM_CLI} --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test-threads PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test-threads PROPERTIES LABELS "spec:wasm-threads")
endif()
if(TOYWASM_ENABLE_WASM_EXCEPTION_HANDLING)
add_test(NAME toywasm-cli-exception-handling-test
COMMAND ./test.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wat/eh
)
set_tests_properties(toywasm-cli-exception-handling-test PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-exception-handling-test PROPERTIES LABELS "exception-handling")
add_test(NAME toywasm-cli-exception-handling-test-disable-jump-table
COMMAND ./test.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wat/eh
)
set_tests_properties(toywasm-cli-exception-handling-test-disable-jump-table PROPERTIES ENVIRONMENT "${TEST_ENV};TEST_RUNTIME_EXE=${TOYWASM_CLI} --disable-jump-table")
set_tests_properties(toywasm-cli-exception-handling-test-disable-jump-table PROPERTIES LABELS "exception-handling")
endif()
if(TOYWASM_ENABLE_WASI)
add_test(NAME toywasm-cli-wasi-testsuite
COMMAND ./test/run-wasi-testsuite.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasi-testsuite PROPERTIES ENVIRONMENT "${TEST_ENV};TOYWASM=${TOYWASM_CLI};$<$<BOOL:${TOYWASM_ENABLE_WASI_THREADS}>:TESTS=proposals/wasi-threads/>")
set_tests_properties(toywasm-cli-wasi-testsuite PROPERTIES LABELS "wasi-testsuite")
if(TOYWASM_ENABLE_WASI_LITTLEFS)
add_test(NAME toywasm-cli-wasi-testsuite-littlefs
COMMAND ./test/run-wasi-testsuite.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasi-testsuite-littlefs PROPERTIES ENVIRONMENT "${TEST_ENV};TOYWASM=${TOYWASM_CLI};$<$<BOOL:${TOYWASM_ENABLE_WASI_THREADS}>:TESTS=proposals/wasi-threads/>;TOYWASM_WASI_FILESYSTEM_TYPE=littlefs")
set_tests_properties(toywasm-cli-wasi-testsuite-littlefs PROPERTIES LABELS "wasi-testsuite;littlefs")
endif()
add_test(NAME toywasm-cli-wasmtime-wasi-tests
COMMAND ./test/run-wasmtime-wasi-tests.sh "${TOYWASM_CLI} --wasi --wasi-dir=."
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasmtime-wasi-tests PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasmtime-wasi-tests PROPERTIES LABELS "wasmtime-wasi-tests")
add_test(NAME toywasm-cli-js-wasm
COMMAND ./test/js-wasm.sh test/pi2.js
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-js-wasm PROPERTIES ENVIRONMENT "${TEST_ENV};TEST_RUNTIME_EXE=${TOYWASM_CLI} --wasi --wasi-dir=test --")
set_tests_properties(toywasm-cli-js-wasm PROPERTIES LABELS "app")
add_test(NAME toywasm-cli-spidermonkey
COMMAND ./test/run-spidermonkey.sh ${TOYWASM_CLI} --wasi
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-spidermonkey PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-spidermonkey PROPERTIES LABELS "app")
add_test(NAME toywasm-cli-ffmpeg
COMMAND ./test/run-ffmpeg.sh ${TOYWASM_CLI} --wasi --wasi-dir=.video --
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-ffmpeg PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-ffmpeg PROPERTIES LABELS "slow;app")
add_test(NAME toywasm-cli-wcpl
COMMAND ./test/run-wcpl.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wcpl PROPERTIES ENVIRONMENT "${TEST_ENV};TEST_RUNTIME_EXE=${TOYWASM_CLI} --wasi --wasi-dir=. --")
set_tests_properties(toywasm-cli-wcpl PROPERTIES LABELS "slow;app")
endif() # TOYWASM_ENABLE_WASI
if(NOT TOYWASM_ENABLE_WASM_MULTI_MEMORY)
add_test(NAME toywasm-cli-wasm3-spec-test-disable-optimizations
# Note: arbitrary limits for stack overflow tests in call.wast.
# (--max-frames and --max-stack-cells)
COMMAND ./test/run-wasm3-spec-test-opam-2.0.0.sh --exec "${TOYWASM_CLI} --disable-jump-table --disable-resulttype-cellidx --disable-localtype-cellidx --max-frames=201 --max-stack-cells=1000 --repl --repl-prompt=wasm3" --timeout 60 --spectest ${CMAKE_BINARY_DIR}/spectest.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-spec-test-disable-optimizations PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-spec-test-disable-optimizations PROPERTIES LABELS "spec")
endif()
if(TOYWASM_ENABLE_WASI)
add_test(NAME toywasm-cli-wasm3-wasi-test
COMMAND ./test/run-wasm3-wasi-test.sh --exec "${TOYWASM_CLI} --wasi --wasi-dir=." --separate-args --timeout 1200
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(toywasm-cli-wasm3-wasi-test PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-wasm3-wasi-test PROPERTIES LABELS "slow")
endif() # TOYWASM_ENABLE_WASI
if(TOYWASM_ENABLE_WASI_LITTLEFS)
add_test(NAME toywasm-cli-littlefs-test
COMMAND ./test.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test/littlefs
)
set_tests_properties(toywasm-cli-littlefs-test PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-littlefs-test PROPERTIES LABELS "wasi;littlefs")
endif()
if(TOYWASM_ENABLE_WASI)
if(TOYWASM_ENABLE_DYLD_DLFCN)
add_test(NAME toywasm-cli-dyld-test
COMMAND ./fetch-and-run-test.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test/dyld
)
set_tests_properties(toywasm-cli-dyld-test PROPERTIES ENVIRONMENT "${TEST_ENV}")
set_tests_properties(toywasm-cli-dyld-test PROPERTIES LABELS "wasi;dyld")
endif()
endif()
endif() # if(TOYWASM_BUILD_CLI)
# sample wasm files
if(BUILD_TESTING)
set(wat_files
test/spectest.wat
wat/infiniteloop.wat
wat/infiniteloop_in_start.wat
wat/wasi-threads/infiniteloops.wat
)
find_program(WAT2WASM wat2wasm REQUIRED)
foreach(wat ${wat_files})
get_filename_component(f ${wat} NAME_WLE)
set(wasm "${f}.wasm")
add_custom_command(OUTPUT ${wasm}
COMMAND ${WAT2WASM} --enable-all -o ${wasm} ${CMAKE_CURRENT_SOURCE_DIR}/${wat}
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${wat}
COMMENT "Building ${wasm}")
add_custom_target(build-${wasm} ALL DEPENDS ${wasm})
endforeach()
endif()
# unit test
if(BUILD_TESTING)
if(TOYWASM_BUILD_UNITTEST)
find_package(cmocka REQUIRED)
# compat with cmocka <1.1.6
# https://gitlab.com/cmocka/cmocka/-/commit/e4c51ce9a4792d7d301d49e4e962036bfaa7f75e
if(NOT TARGET cmocka::cmocka)
add_library(cmocka::cmocka STATIC IMPORTED)
set_target_properties(cmocka::cmocka
PROPERTIES
IMPORTED_LOCATION "${CMOCKA_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${CMOCKA_INCLUDE_DIR}")
endif()
set(test_sources
"test/test.c"
)
add_executable(toywasm-test ${test_sources})
# Force native build because cmocka from homebrew is not universal binary.
set_target_properties(toywasm-test
PROPERTIES OSX_ARCHITECTURES ${CMAKE_HOST_SYSTEM_PROCESSOR})
target_link_libraries(toywasm-test toywasm-lib-core m ${CMOCKA_LIBRARY})
add_test(NAME toywasm-test COMMAND toywasm-test)
set_tests_properties(toywasm-test PROPERTIES ENVIRONMENT "${TEST_ENV}")
target_link_libraries(toywasm-test cmocka::cmocka)
endif() # TOYWASM_BUILD_UNITTEST
endif()
# XXX Is there a way to create the file list from install() commands?
# for now, we assume we only have toywasm-installed files in
# CMAKE_INSTALL_PREFIX.
# XXX Is there a way to express dependency on "install" target?
find_program(PAX pax)
add_custom_target(tar
COMMAND ${PAX} -wzf toywasm-${TOYWASM_VERSION}${TOYWASM_TARBALL_SUFFIX}.tgz
-s!${CMAKE_INSTALL_PREFIX}/!!
${CMAKE_INSTALL_PREFIX}/*
)
if(TOYWASM_BUILD_CLI)
install(TARGETS toywasm-cli DESTINATION bin)
endif()
install(FILES LICENSE README.md DESTINATION share/doc/toywasm)
install(FILES doc/annotations.md doc/check_interrupt.md DESTINATION share/doc/toywasm/doc)