-
Notifications
You must be signed in to change notification settings - Fork 13
/
CMakeLists.txt
executable file
·368 lines (315 loc) · 15.2 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
cmake_minimum_required(VERSION 3.13)
cmake_policy(VERSION 3.16)
project(rlbox_wasm2c
VERSION 0.1
DESCRIPTION "RLBox integration with WASM modules compiled with wasm2c")
# Project Settings ###################
# set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
option(DEV "Use settings suitable for dev contributions to rlbox" OFF)
file(GLOB_RECURSE
ALL_CXX_SOURCE_FILES
${CMAKE_SOURCE_DIR}/include/*.[chi]pp
${CMAKE_SOURCE_DIR}/include/*.[chi]xx
${CMAKE_SOURCE_DIR}/include/*.cc
${CMAKE_SOURCE_DIR}/include/*.hh
${CMAKE_SOURCE_DIR}/include/*.ii
${CMAKE_SOURCE_DIR}/include/*.[CHI]
${CMAKE_SOURCE_DIR}/test/*.[chi]pp
${CMAKE_SOURCE_DIR}/test/*.[chi]xx
${CMAKE_SOURCE_DIR}/test/*.cc
${CMAKE_SOURCE_DIR}/test/*.hh
${CMAKE_SOURCE_DIR}/test/*.ii
${CMAKE_SOURCE_DIR}/test/*.[CHI]
${CMAKE_SOURCE_DIR}/c_src/*.[chi]pp
${CMAKE_SOURCE_DIR}/c_src/*.[chi]xx
${CMAKE_SOURCE_DIR}/c_src/*.cc
${CMAKE_SOURCE_DIR}/c_src/*.hh
${CMAKE_SOURCE_DIR}/c_src/*.ii
${CMAKE_SOURCE_DIR}/c_src/*.[CHI])
# Dev Tools ###################
if(DEV)
find_program(CLANG_TIDY "clang-tidy")
if(CLANG_TIDY)
# Config in .clang-tidy
set(CMAKE_CXX_CLANG_TIDY clang-tidy)
endif()
find_program(CLANG_FORMAT "clang-format")
if(CLANG_FORMAT)
# Config in .clang-format
add_custom_target(format-source
COMMAND clang-format
-i
-style=file
${ALL_CXX_SOURCE_FILES})
endif()
endif()
# Dependencies ###################
find_program(CARGO "cargo")
if(!CARGO)
message(
FATAL_ERROR
"Could not find cargo. Please instal/l cargo as it is needed to build rust libraries."
)
endif()
find_program(EMCC "emcc")
if(!EMCC)
message(
FATAL_ERROR
"Could not find emcc. Please install emcc as it is needed to generate the wasm2c sandbox."
)
endif()
include(FetchContent)
FetchContent_Declare(
rlbox
GIT_REPOSITORY https://github.com/PLSysSec/rlbox_api_cpp17.git)
FetchContent_GetProperties(rlbox)
if(NOT rlbox_POPULATED)
FetchContent_Populate(rlbox)
endif()
FetchContent_Declare(catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.9)
FetchContent_GetProperties(catch2)
if(NOT catch2_POPULATED)
FetchContent_Populate(catch2)
endif()
add_subdirectory("${catch2_SOURCE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${catch2_SOURCE_DIR}/contrib")
if (WIN32)
FetchContent_Declare(
wasiclang
URL
https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-mingw.tar.gz
)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
FetchContent_Declare(
wasiclang
URL
https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-macos.tar.gz
)
else()
FetchContent_Declare(
wasiclang
URL
https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz
)
endif()
FetchContent_GetProperties(wasiclang)
if(NOT wasiclang_POPULATED)
FetchContent_Populate(wasiclang)
endif()
FetchContent_Declare(
mod_wasm2c
GIT_REPOSITORY https://github.com/WebAssembly/wabt/
GIT_TAG main)
FetchContent_GetProperties(mod_wasm2c)
if(NOT mod_wasm2c_POPULATED)
FetchContent_Populate(mod_wasm2c)
endif()
# set (mod_wasm2c_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../wabt/")
if(DEV)
if(MSVC)
set(RLBOX_SANITIZER_COMPILE_FLAGS)
set(RLBOX_SANITIZER_LINK_FLAGS "")
else()
set(RLBOX_SANITIZER_COMPILE_FLAGS -fsanitize=address -fsanitize=undefined)
set(RLBOX_SANITIZER_LINK_FLAGS -fsanitize=address -fsanitize=undefined)
endif()
else()
set(RLBOX_SANITIZER_COMPILE_FLAGS "")
set(RLBOX_SANITIZER_LINK_FLAGS "")
endif()
if(DEV)
set(WASM2C_BUILD_DIR_SUFFIX "debug")
set (WASM2C_BUILD_TYPE Debug)
else()
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(WASM2C_BUILD_DIR_SUFFIX "debug")
set (WASM2C_BUILD_TYPE Debug)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
set(WASM2C_BUILD_DIR_SUFFIX "relwithdebinfo")
set (WASM2C_BUILD_TYPE RelWithDebInfo)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
set(WASM2C_BUILD_DIR_SUFFIX "minsizerel")
set (WASM2C_BUILD_TYPE MinSizeRel)
else()
set(WASM2C_BUILD_DIR_SUFFIX "release")
set (WASM2C_BUILD_TYPE Release)
endif()
endif()
# wasm2c ###################
set(WASM2C_RUNTIME_SOURCE_DIR "${mod_wasm2c_SOURCE_DIR}/wasm2c/")
set(WASM2C_RUNTIME_DIR "${mod_wasm2c_SOURCE_DIR}/build_${WASM2C_BUILD_DIR_SUFFIX}/")
set(C_SOURCE_FILES "${CMAKE_SOURCE_DIR}/c_src/wasm2c_sandbox_wrapper.c")
set(GLUE_LIB_WASM_IMPORTED_DIR "${CMAKE_BINARY_DIR}/wasm_imported/")
set(GLUE_LIB_IMPORTED_WASM "${GLUE_LIB_WASM_IMPORTED_DIR}/glue_lib_wasm2c.wasm")
set(GLUE_LIB_IMPORTED_H "${GLUE_LIB_WASM_IMPORTED_DIR}/glue_lib_wasm2c.h")
set(GLUE_LIB_IMPORTED_C "${GLUE_LIB_WASM_IMPORTED_DIR}/glue_lib_wasm2c.c")
set(GLUE_LIB_WASM_EXPORTED_DIR "${CMAKE_BINARY_DIR}/wasm_exported/")
set(GLUE_LIB_EXPORTED_WASM "${GLUE_LIB_WASM_EXPORTED_DIR}/glue_lib_wasm2c.wasm")
set(GLUE_LIB_EXPORTED_H "${GLUE_LIB_WASM_EXPORTED_DIR}/glue_lib_wasm2c.h")
set(GLUE_LIB_EXPORTED_C "${GLUE_LIB_WASM_EXPORTED_DIR}/glue_lib_wasm2c.c")
if(MSVC)
set(WASM2C_PATH ${WASM2C_RUNTIME_DIR}/${WASM2C_BUILD_TYPE}/wasm2c.exe)
else()
set(WASM2C_PATH ${WASM2C_RUNTIME_DIR}/wasm2c)
endif()
add_custom_command(OUTPUT "${WASM2C_PATH}"
WORKING_DIRECTORY "${mod_wasm2c_SOURCE_DIR}"
COMMAND ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=${WASM2C_BUILD_TYPE}
-DCMAKE_C_FLAGS="${RLBOX_SANITIZER_COMPILE_FLAGS}"
-DCMAKE_CXX_FLAGS="${RLBOX_SANITIZER_COMPILE_FLAGS}"
-DLINK_FLAGS="${RLBOX_SANITIZER_LINK_FLAGS}"
-S .
-B ${WASM2C_RUNTIME_DIR}
COMMAND ${CMAKE_COMMAND}
--build ${WASM2C_RUNTIME_DIR}
--config ${WASM2C_BUILD_TYPE}
--parallel
COMMENT "Building wasm2c compiler and runtime")
add_custom_command(OUTPUT "${GLUE_LIB_IMPORTED_H}" "${GLUE_LIB_IMPORTED_C}" "${GLUE_LIB_IMPORTED_WASM}"
DEPENDS ${C_SOURCE_FILES} ${WASM2C_PATH}
COMMAND ${CMAKE_COMMAND} -E make_directory ${GLUE_LIB_WASM_IMPORTED_DIR}
COMMAND ${wasiclang_SOURCE_DIR}/bin/clang
--sysroot ${wasiclang_SOURCE_DIR}/share/wasi-sysroot/
-O3
-Wl,--export-all -Wl,--no-entry -Wl,--growable-table -Wl,--stack-first -Wl,-z,stack-size=1048576 -Wl,--import-memory -Wl,--import-table
-o ${GLUE_LIB_IMPORTED_WASM}
${CMAKE_SOURCE_DIR}/c_src/wasm2c_sandbox_wrapper.c
${rlbox_SOURCE_DIR}/code/tests/rlbox_glue/lib/libtest.c
COMMAND ${WASM2C_PATH}
-o ${GLUE_LIB_IMPORTED_C}
${GLUE_LIB_IMPORTED_WASM}
COMMENT "Building wasm sandboxed library with imported memory and imported table")
add_custom_command(OUTPUT "${GLUE_LIB_EXPORTED_H}" "${GLUE_LIB_EXPORTED_C}" "${GLUE_LIB_EXPORTED_WASM}"
DEPENDS ${C_SOURCE_FILES} ${WASM2C_PATH}
COMMAND ${CMAKE_COMMAND} -E make_directory ${GLUE_LIB_WASM_EXPORTED_DIR}
COMMAND ${wasiclang_SOURCE_DIR}/bin/clang
--sysroot ${wasiclang_SOURCE_DIR}/share/wasi-sysroot/
-O3
-Wl,--export-all -Wl,--no-entry -Wl,--growable-table -Wl,--stack-first -Wl,-z,stack-size=1048576 -Wl,--export-table
-o ${GLUE_LIB_EXPORTED_WASM}
${CMAKE_SOURCE_DIR}/c_src/wasm2c_sandbox_wrapper.c
${rlbox_SOURCE_DIR}/code/tests/rlbox_glue/lib/libtest.c
COMMAND ${WASM2C_PATH}
-o ${GLUE_LIB_EXPORTED_C}
${GLUE_LIB_EXPORTED_WASM}
COMMENT "Building wasm sandboxed library with exported memory and exported table")
# Tests ###################
if(DEV)
if(NOT MSVC)
add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers ${RLBOX_SANITIZER_COMPILE_FLAGS})
set_source_files_properties(${GLUE_LIB_IMPORTED_C} PROPERTIES COMPILE_OPTIONS "-Wno-cast-function-type;-Wno-unused-function;-Wno-unused-value;-Wno-unused-but-set-variable")
set_source_files_properties(${GLUE_LIB_EXPORTED_C} PROPERTIES COMPILE_OPTIONS "-Wno-cast-function-type;-Wno-unused-function;-Wno-unused-value;-Wno-unused-but-set-variable")
add_link_options(${RLBOX_SANITIZER_LINK_FLAGS})
endif()
endif()
set(WASM2C_RUNTIME_CODE ${WASM2C_RUNTIME_SOURCE_DIR}/wasm-rt-impl.c
${WASM2C_RUNTIME_SOURCE_DIR}/wasm-rt-mem-impl.c
${CMAKE_SOURCE_DIR}/src/wasm2c_rt_minwasi.c
${CMAKE_SOURCE_DIR}/src/wasm2c_rt_mem.c)
add_library(glue_lib_imported STATIC ${GLUE_LIB_IMPORTED_C} ${WASM2C_RUNTIME_CODE})
target_include_directories(glue_lib_imported PRIVATE ${mod_wasm2c_SOURCE_DIR}/wasm2c
PUBLIC ${mod_wasm2c_SOURCE_DIR}/third_party/simde
PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_compile_definitions(glue_lib_imported PUBLIC WASM_RT_USE_MMAP=1
PUBLIC WASM_RT_SKIP_SIGNAL_RECOVERY=1
PUBLIC WASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION=1)
add_library(glue_lib_exported STATIC ${GLUE_LIB_EXPORTED_C} ${WASM2C_RUNTIME_CODE})
target_include_directories(glue_lib_exported PRIVATE ${mod_wasm2c_SOURCE_DIR}/wasm2c
PUBLIC ${mod_wasm2c_SOURCE_DIR}/third_party/simde
PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_compile_definitions(glue_lib_exported PUBLIC WASM_RT_USE_MMAP=1
PUBLIC WASM_RT_SKIP_SIGNAL_RECOVERY=1
PUBLIC WASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION=1)
# Hack to avoid multiple concurrent calls to the custom command that builds wasm2c
add_dependencies(glue_lib_imported glue_lib_exported)
include(CTest)
include(Catch)
find_package(Threads REQUIRED)
# Tests executables ###################
add_executable(test_rlbox_glue test/test_wasm2c_sandbox_glue_main.cpp
test/test_wasm2c_sandbox_glue.cpp)
target_include_directories(test_rlbox_glue PUBLIC ${CMAKE_SOURCE_DIR}/include
PUBLIC ${rlbox_SOURCE_DIR}/code/include
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue/lib
PUBLIC ${mod_wasm2c_SOURCE_DIR}/wasm2c
PUBLIC ${GLUE_LIB_WASM_IMPORTED_DIR}
)
target_link_libraries(test_rlbox_glue Catch2::Catch2
${CMAKE_THREAD_LIBS_INIT}
${CMAKE_DL_LIBS}
glue_lib_imported
)
if(UNIX AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
target_link_libraries(test_rlbox_glue rt)
endif()
catch_discover_tests(test_rlbox_glue)
####
add_executable(test_rlbox_glue_exported test/test_wasm2c_sandbox_glue_main.cpp
test/test_wasm2c_sandbox_glue.cpp)
target_include_directories(test_rlbox_glue_exported PUBLIC ${CMAKE_SOURCE_DIR}/include
PUBLIC ${rlbox_SOURCE_DIR}/code/include
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue/lib
PUBLIC ${mod_wasm2c_SOURCE_DIR}/wasm2c
PUBLIC ${GLUE_LIB_WASM_EXPORTED_DIR}
)
target_link_libraries(test_rlbox_glue_exported Catch2::Catch2
${CMAKE_THREAD_LIBS_INIT}
${CMAKE_DL_LIBS}
glue_lib_exported
)
if(UNIX AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
target_link_libraries(test_rlbox_glue_exported rt)
endif()
catch_discover_tests(test_rlbox_glue_exported)
####
add_executable(test_rlbox_glue_smallheap test/test_wasm2c_sandbox_glue_main.cpp
test/test_wasm2c_sandbox_glue_smallheap.cpp)
target_include_directories(test_rlbox_glue_smallheap PUBLIC ${CMAKE_SOURCE_DIR}/include
PUBLIC ${rlbox_SOURCE_DIR}/code/include
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue/lib
PUBLIC ${mod_wasm2c_SOURCE_DIR}/wasm2c
PUBLIC ${GLUE_LIB_WASM_IMPORTED_DIR}
)
target_link_libraries(test_rlbox_glue_smallheap Catch2::Catch2
${CMAKE_THREAD_LIBS_INIT}
${CMAKE_DL_LIBS}
glue_lib_imported
)
if(UNIX AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
target_link_libraries(test_rlbox_glue_smallheap rt)
endif()
catch_discover_tests(test_rlbox_glue_smallheap)
####
add_executable(test_rlbox_glue_embed test/test_wasm2c_sandbox_glue_main.cpp
test/test_wasm2c_sandbox_glue_embedder_vars.cpp)
target_include_directories(test_rlbox_glue_embed PUBLIC ${CMAKE_SOURCE_DIR}/include
PUBLIC ${rlbox_SOURCE_DIR}/code/include
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue
PUBLIC ${rlbox_SOURCE_DIR}/code/tests/rlbox_glue/lib
PUBLIC ${mod_wasm2c_SOURCE_DIR}/wasm2c
PUBLIC ${GLUE_LIB_WASM_IMPORTED_DIR}
)
target_link_libraries(test_rlbox_glue_embed Catch2::Catch2
${CMAKE_THREAD_LIBS_INIT}
${CMAKE_DL_LIBS}
glue_lib_imported
)
if(UNIX AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
target_link_libraries(test_rlbox_glue_embed rt)
endif()
catch_discover_tests(test_rlbox_glue_embed)
# Shortcuts ###################
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -V)
add_dependencies(check test_rlbox_glue)
add_dependencies(check test_rlbox_glue_exported)
add_dependencies(check test_rlbox_glue_smallheap)
add_dependencies(check test_rlbox_glue_embed)