Skip to content

Commit ee98f1b

Browse files
committed
Create CPPINTEROP_COMMON_WASM_TEST_COMPILE_FLAGS and CPPINTEROP_COMMON_WASM_TEST_LINK_FLAGS cmake variables to avoid repetition when creating tests
1 parent 4e3e4c2 commit ee98f1b

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

unittests/CppInterOp/CMakeLists.txt

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,49 @@ if (EMSCRIPTEN)
55
# For Emscripten builds linking to gtest_main will not suffice for gtest to run
66
# the tests and an explicitly main.cpp is needed
77
set(EXTRA_TEST_SOURCE_FILES main.cpp)
8+
9+
string(REPLACE "@" "@@" ESCAPED_SYSROOT_PATH "${SYSROOT_PATH}")
10+
# Explanation of Emscripten-specific link flags for CppInterOpTests:
11+
#
12+
# MAIN_MODULE=1:
13+
# Enables building CppInterOpTests.js as the main WebAssembly module, allowing dynamic linking of side modules.
14+
#
15+
# WASM_BIGINT:
16+
# Ensures support for 64-bit integer types by enabling JavaScript BigInt integration in WASM.
17+
#
18+
# ALLOW_MEMORY_GROWTH=1:
19+
# Allows the WebAssembly memory to grow dynamically at runtime to accommodate increasing memory needs.
20+
# Would lead to an abortOnCannotGrowMemory error if memory cannot be grown while running the tests.
21+
#
22+
# STACK_SIZE=32mb: Allocates 32MB of stack space to handle deep recursion or large stack-allocated objects safely.
23+
# INITIAL_MEMORY=128mb: Sets the initial linear memory size to 128MB to reduce the likelihood of early memory expansion and improve performance.
24+
# The STACK_SIZE and INITIAL_MEMORY values are chosen based on what has been put to use for running xeus-cpp-lite.
25+
# Check https://github.com/jupyter-xeus/xeus/blob/main/cmake/WasmBuildOptions.cmake#L35-L36 for more details.
26+
# Not setting these flags would lead to a memory access out of bounds error while running the tests.
27+
#
28+
# --preload-file ${SYSROOT_PATH}/include@/include:
29+
# Preloads the system include directory into the Emscripten virtual filesystem to make headers accessible at runtime.
30+
#
31+
# --emrun
32+
# Makes it so that we run the html file created by this target, that we can capture the standard output
33+
# and output to the terminal
34+
35+
# Common compile flags for WASM tests
36+
set(CPPINTEROP_COMMON_WASM_TEST_COMPILE_FLAGS
37+
"SHELL:${CPPINTEROP_EXTRA_WASM_FLAGS}"
38+
)
39+
40+
# Common link flags for WASM tests
41+
set(CPPINTEROP_COMMON_WASM_TEST_LINK_FLAGS
42+
"SHELL:${CPPINTEROP_EXTRA_WASM_FLAGS}"
43+
"SHELL:-s MAIN_MODULE=1"
44+
"SHELL:-s WASM_BIGINT"
45+
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
46+
"SHELL:-s STACK_SIZE=32mb"
47+
"SHELL:-s INITIAL_MEMORY=64mb"
48+
"SHELL:--preload-file ${ESCAPED_SYSROOT_PATH}/include@/include"
49+
"SHELL:--emrun"
50+
)
851
else()
952
# Do not need main.cpp for native builds, but we do have GPU support for native builds
1053
set(EXTRA_TEST_SOURCE_FILES CUDATest.cpp)
@@ -35,43 +78,11 @@ set_source_files_properties(InterpreterTest.cpp PROPERTIES COMPILE_DEFINITIONS
3578
)
3679

3780
if(EMSCRIPTEN)
38-
string(REPLACE "@" "@@" ESCAPED_SYSROOT_PATH "${SYSROOT_PATH}")
39-
# Explanation of Emscripten-specific link flags for CppInterOpTests:
40-
#
41-
# MAIN_MODULE=1:
42-
# Enables building CppInterOpTests.js as the main WebAssembly module, allowing dynamic linking of side modules.
43-
#
44-
# WASM_BIGINT:
45-
# Ensures support for 64-bit integer types by enabling JavaScript BigInt integration in WASM.
46-
#
47-
# ALLOW_MEMORY_GROWTH=1:
48-
# Allows the WebAssembly memory to grow dynamically at runtime to accommodate increasing memory needs.
49-
# Would lead to an abortOnCannotGrowMemory error if memory cannot be grown while running the tests.
50-
#
51-
# STACK_SIZE=32mb: Allocates 32MB of stack space to handle deep recursion or large stack-allocated objects safely.
52-
# INITIAL_MEMORY=128mb: Sets the initial linear memory size to 128MB to reduce the likelihood of early memory expansion and improve performance.
53-
# The STACK_SIZE and INITIAL_MEMORY values are chosen based on what has been put to use for running xeus-cpp-lite.
54-
# Check https://github.com/jupyter-xeus/xeus/blob/main/cmake/WasmBuildOptions.cmake#L35-L36 for more details.
55-
# Not setting these flags would lead to a memory access out of bounds error while running the tests.
56-
#
57-
# --preload-file ${SYSROOT_PATH}/include@/include:
58-
# Preloads the system include directory into the Emscripten virtual filesystem to make headers accessible at runtime.
59-
#
60-
# --emrun
61-
# Makes it so that we run the html file created by this target, that we can capture the standard output
62-
# and output to the terminal
6381
target_compile_options(CppInterOpTests
64-
PUBLIC "SHELL: ${CPPINTEROP_EXTRA_WASM_FLAGS}"
82+
PUBLIC ${CPPINTEROP_COMMON_WASM_TEST_COMPILE_FLAGS}
6583
)
6684
target_link_options(CppInterOpTests
67-
PUBLIC "SHELL: ${CPPINTEROP_EXTRA_WASM_FLAGS}"
68-
PUBLIC "SHELL: -s MAIN_MODULE=1"
69-
PUBLIC "SHELL: -s WASM_BIGINT"
70-
PUBLIC "SHELL: -s ALLOW_MEMORY_GROWTH=1"
71-
PUBLIC "SHELL: -s STACK_SIZE=32mb"
72-
PUBLIC "SHELL: -s INITIAL_MEMORY=64mb"
73-
PUBLIC "SHELL: --preload-file ${ESCAPED_SYSROOT_PATH}/include@/include"
74-
PUBLIC "SHELL: --emrun"
85+
PUBLIC ${CPPINTEROP_COMMON_WASM_TEST_LINK_FLAGS}
7586
)
7687
endif()
7788

@@ -90,20 +101,13 @@ add_cppinterop_unittest(DynamicLibraryManagerTests
90101
if(EMSCRIPTEN)
91102
set(TEST_SHARED_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/bin/$<CONFIG>/")
92103
string(REPLACE "@" "@@" ESCAPED_TEST_SHARED_LIBRARY_PATH "${TEST_SHARED_LIBRARY_PATH}")
93-
# Check explanation of Emscripten-specific link flags for CppInterOpTests above for DynamicLibraryManagerTests as well.
94104
target_compile_options(DynamicLibraryManagerTests
95-
PUBLIC "SHELL: ${CPPINTEROP_EXTRA_WASM_FLAGS}"
105+
PUBLIC ${CPPINTEROP_COMMON_WASM_TEST_COMPILE_FLAGS}
96106
)
97107
target_link_options(DynamicLibraryManagerTests
98-
PUBLIC "SHELL: ${CPPINTEROP_EXTRA_WASM_FLAGS}"
99-
PUBLIC "SHELL: -s MAIN_MODULE=1"
100-
PUBLIC "SHELL: -s WASM_BIGINT"
101-
PUBLIC "SHELL: -s ALLOW_MEMORY_GROWTH=1"
102-
PUBLIC "SHELL: -s STACK_SIZE=32mb"
103-
PUBLIC "SHELL: -s INITIAL_MEMORY=64mb"
104-
PUBLIC "SHELL: --preload-file ${ESCAPED_SYSROOT_PATH}/include@/include"
105-
PUBLIC "SHELL: --emrun"
106-
PUBLIC "SHELL: --preload-file ${ESCAPED_TEST_SHARED_LIBRARY_PATH}/libTestSharedLib.so@/libTestSharedLib.so"
108+
PUBLIC
109+
${CPPINTEROP_COMMON_WASM_TEST_LINK_FLAGS}
110+
"SHELL:--preload-file ${ESCAPED_TEST_SHARED_LIBRARY_PATH}/libTestSharedLib.so@/libTestSharedLib.so"
107111
)
108112
endif()
109113

0 commit comments

Comments
 (0)