Skip to content

Commit e211109

Browse files
committed
Add emscripten automated tests
1 parent f4a36bb commit e211109

File tree

7 files changed

+148
-56
lines changed

7 files changed

+148
-56
lines changed

.github/workflows/emscripten.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,10 @@ jobs:
601601
-DLLVM_ENABLE_WERROR=On \
602602
../
603603
fi
604-
605-
emmake make -j ${{ env.ncpus }} install
606604
605+
emmake make -j ${{ env.ncpus }}
606+
emmake make check-cppinterop
607+
607608
cd ..
608609
609610
echo "CB_PYTHON_DIR=$CB_PYTHON_DIR" >> $GITHUB_ENV

CMakeLists.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,15 @@ endif()
361361

362362
# Add appropriate flags for GCC
363363
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
364-
if (APPLE)
364+
if (APPLE OR EMSCRIPTEN)
365365
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings")
366366
else()
367367
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -pedantic -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings")
368368
endif ()
369+
# Needed due an error which occurs when you compile gtest on emscripten
370+
if (EMSCRIPTEN)
371+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")
372+
endif()
369373
endif ()
370374

371375
# Fixes "C++ exception handler used, but unwind semantics are not enabled" warning Windows
@@ -451,13 +455,7 @@ option(CPPINTEROP_ENABLE_DOXYGEN "Use doxygen to generate CppInterOp interal API
451455
option(CPPINTEROP_ENABLE_SPHINX "Use sphinx to generage CppInterOp user documentation")
452456

453457

454-
if(EMSCRIPTEN)
455-
message("Build with emscripten")
456-
option(CPPINTEROP_ENABLE_TESTING "Enables the testing infrastructure." OFF)
457-
else()
458-
message("Build with cmake")
459-
option(CPPINTEROP_ENABLE_TESTING "Enables the testing infrastructure." ON)
460-
endif()
458+
option(CPPINTEROP_ENABLE_TESTING "Enables the testing infrastructure." ON)
461459

462460
if(MSVC)
463461

cmake/modules/GoogleTest.cmake

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ elseif(APPLE)
2020
endif()
2121

2222
include(ExternalProject)
23+
if(EMSCRIPTEN)
24+
2325
ExternalProject_Add(
2426
googletest
2527
GIT_REPOSITORY https://github.com/google/googletest.git
@@ -31,15 +33,10 @@ ExternalProject_Add(
3133
# CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
3234
# -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
3335
# -Dgtest_force_shared_crt=ON
34-
CMAKE_ARGS -G ${CMAKE_GENERATOR}
35-
-DCMAKE_BUILD_TYPE=$<CONFIG>
36-
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
37-
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
38-
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
39-
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
40-
-DCMAKE_AR=${CMAKE_AR}
41-
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
42-
${EXTRA_GTEST_OPTS}
36+
CONFIGURE_COMMAND emcmake cmake -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
37+
-S ${CMAKE_BINARY_DIR}/unittests/googletest-prefix/src/googletest/
38+
-B ${CMAKE_BINARY_DIR}/unittests/googletest-prefix/src/googletest-build/
39+
BUILD_COMMAND emmake make
4340
# Disable install step
4441
INSTALL_COMMAND ""
4542
BUILD_BYPRODUCTS ${_gtest_byproducts}
@@ -50,6 +47,40 @@ ExternalProject_Add(
5047
TIMEOUT 600
5148
)
5249

50+
else()
51+
52+
ExternalProject_Add(
53+
googletest
54+
GIT_REPOSITORY https://github.com/google/googletest.git
55+
GIT_SHALLOW 1
56+
GIT_TAG v1.15.2
57+
UPDATE_COMMAND ""
58+
# # Force separate output paths for debug and release builds to allow easy
59+
# # identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands
60+
# CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
61+
# -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
62+
# -Dgtest_force_shared_crt=ON
63+
CMAKE_ARGS -G ${CMAKE_GENERATOR}
64+
-DCMAKE_BUILD_TYPE=$<CONFIG>
65+
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
66+
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
67+
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
68+
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
69+
-DCMAKE_AR=${CMAKE_AR}
70+
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
71+
${EXTRA_GTEST_OPTS}
72+
# Disable install step
73+
INSTALL_COMMAND ""
74+
BUILD_BYPRODUCTS ${_gtest_byproducts}
75+
# Wrap download, configure and build steps in a script to log output
76+
LOG_DOWNLOAD ON
77+
LOG_CONFIGURE ON
78+
LOG_BUILD ON
79+
TIMEOUT 600
80+
)
81+
82+
endif()
83+
5384
# Specify include dirs for gtest and gmock
5485
ExternalProject_Get_Property(googletest source_dir)
5586
set(GTEST_INCLUDE_DIR ${source_dir}/googletest/include)

lib/Interpreter/CMakeLists.txt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
11
if(EMSCRIPTEN)
22
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
3-
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-s SIDE_MODULE=1")
4-
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-s SIDE_MODULE=1")
53
set(CMAKE_STRIP FALSE)
6-
7-
add_llvm_library(clangCppInterOp
8-
SHARED
9-
10-
CppInterOp.cpp
11-
CXCppInterOp.cpp
12-
DynamicLibraryManager.cpp
13-
DynamicLibraryManagerSymbol.cpp
14-
Paths.cpp
15-
16-
# Additional libraries from Clang and LLD
17-
LINK_LIBS
18-
clangInterpreter
19-
)
20-
#FIXME: Setting no_soname=1 is needed until https://github.com/emscripten-core/emscripten/blob/ac676d5e437525d15df5fd46bc2c208ec6d376a3/cmake/Modules/Platform/Emscripten.cmake#L36
21-
# is patched out of emsdk, as --soname is not recognised by emscripten. A PR to do this has been done here https://github.com/emscripten-core/emscripten/pull/23453
22-
#FIXME: A patch is needed to llvm to remove -Wl,-z,defs since it is now recognised on emscripten. What needs to be removed is here
23-
# https://github.com/llvm/llvm-project/blob/128e2e446e90c3b1827cfc7d4d19e3c0976beff3/llvm/cmake/modules/HandleLLVMOptions.cmake#L318 . The PR to do try to do this is here
24-
# https://github.com/llvm/llvm-project/pull/123396
25-
set_target_properties(clangCppInterOp
26-
PROPERTIES NO_SONAME 1
27-
)
28-
target_link_options(clangCppInterOp PRIVATE
29-
PUBLIC "SHELL: -s WASM_BIGINT"
30-
)
4+
message(STATUS "LLVM_TARGETS_TO_BUILD ${LLVM_TARGETS_TO_BUILD}")
5+
set(LLVM_LINK_COMPONENTS "")
316
else()
327
set(LLVM_LINK_COMPONENTS
338
${LLVM_TARGETS_TO_BUILD}
@@ -41,6 +16,7 @@ else()
4116
if ("LLVMFrontendDriver" IN_LIST LLVM_AVAILABLE_LIBS)
4217
list(APPEND LLVM_LINK_COMPONENTS FrontendDriver)
4318
endif()
19+
endif()
4420
if ("LLVMOrcDebugging" IN_LIST LLVM_AVAILABLE_LIBS)
4521
list(APPEND LLVM_LINK_COMPONENTS OrcDebugging)
4622
endif()
@@ -65,6 +41,11 @@ else()
6541
set(cling_clang_interp clangInterpreter)
6642
endif()
6743

44+
if(EMSCRIPTEN)
45+
set(link_libs
46+
${cling_clang_interp}
47+
)
48+
else()
6849
set(link_libs
6950
${cling_clang_interp}
7051
clangAST
@@ -73,13 +54,15 @@ else()
7354
clangLex
7455
clangSema
7556
)
57+
endif()
7658

7759
if(NOT WIN32)
7860
list(APPEND link_libs dl)
7961
endif()
8062

8163
# Get rid of libLLVM-X.so which is appended to the list of static libraries.
8264
if (LLVM_LINK_LLVM_DYLIB)
65+
message(STATUS "In here ${LLVM_LINK_LLVM_DYLIB}")
8366
set(new_libs ${link_libs})
8467
set(libs ${new_libs})
8568
while(NOT "${new_libs}" STREQUAL "")
@@ -103,6 +86,7 @@ else()
10386
endif()
10487
endforeach(transitive_lib)
10588
# Update the target properties with the list of only static libraries.
89+
message(STATUS "static_transitive_libs ${static_transitive_libs}")
10690
set_target_properties(${lib} PROPERTIES INTERFACE_LINK_LIBRARIES "${static_transitive_libs}")
10791
set(static_transitive_libs "")
10892
endif()
@@ -124,7 +108,8 @@ else()
124108
clangStaticAnalyzerCore
125109
)
126110
endif(LLVM_LINK_LLVM_DYLIB)
127-
111+
message(STATUS "Test DLM ${DLM}")
112+
message(STATUS "Test link_libs ${link_libs}")
128113
add_llvm_library(clangCppInterOp
129114
DISABLE_LLVM_LINK_LLVM_DYLIB
130115
CppInterOp.cpp
@@ -133,6 +118,21 @@ else()
133118
LINK_LIBS
134119
${link_libs}
135120
)
121+
122+
if(EMSCRIPTEN)
123+
124+
#FIXME: Setting no_soname=1 is needed until https://github.com/emscripten-core/emscripten/blob/ac676d5e437525d15df5fd46bc2c208ec6d376a3/cmake/Modules/Platform/Emscripten.cmake#L36
125+
# is patched out of emsdk, as --soname is not recognised by emscripten. A PR to do this has been done here https://github.com/emscripten-core/emscripten/pull/23453
126+
#FIXME: A patch is needed to llvm to remove -Wl,-z,defs since it is now recognised on emscripten. What needs to be removed is here
127+
# https://github.com/llvm/llvm-project/blob/128e2e446e90c3b1827cfc7d4d19e3c0976beff3/llvm/cmake/modules/HandleLLVMOptions.cmake#L318 . The PR to do try to do this is here
128+
# https://github.com/llvm/llvm-project/pull/123396
129+
set_target_properties(clangCppInterOp PROPERTIES
130+
NO_SONAME 1
131+
COMPILE_FLAGS "-s SIDE_MODULE=1"
132+
LINK_FLAGS "-s LINKABLE=1 -s EXPORT_ALL=1 -s WASM_BIGINT -s SIDE_MODULE=1"
133+
SUFFIX ".wasm"
134+
)
135+
136136
endif()
137137

138138
string(REPLACE ";" "\;" _VER CPPINTEROP_VERSION)

unittests/CMakeLists.txt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ if (NOT TARGET gtest)
77
include(GoogleTest)
88
endif()
99

10-
set(gtest_libs gtest gtest_main)
11-
# Clang prior than clang13 (I think) merges both gmock into gtest.
12-
if (TARGET gmock)
13-
list(APPEND gtest_libs gmock gmock_main)
10+
if(EMSCRIPTEN)
11+
# By removing gtest_main and gmock_main you avoid duplicate symbol error (probably not the best solution)
12+
set(gtest_libs gtest_main gtest gmock)
13+
else()
14+
set(gtest_libs gtest gtest_main)
15+
# Clang prior than clang13 (I think) merges both gmock into gtest.
16+
if (TARGET gmock)
17+
list(APPEND gtest_libs gmock gmock_main)
18+
endif()
1419
endif()
1520

1621
add_custom_target(CppInterOpUnitTests)
@@ -28,7 +33,21 @@ if(WIN32)
2833
target_link_libraries(${name} PUBLIC ${ARG_LIBRARIES} ${gtest_libs})
2934
set_property(TARGET ${name} APPEND_STRING PROPERTY LINK_FLAGS "${MSVC_EXPORTS}")
3035
else()
31-
target_link_libraries(${name} PUBLIC ${ARG_LIBRARIES} ${gtest_libs} pthread)
36+
if(EMSCRIPTEN)
37+
target_link_libraries(${name} PRIVATE ${ARG_LIBRARIES} ${gtest_libs} clangCppInterOp)
38+
set_target_properties(${name} PROPERTIES
39+
LINK_FLAGS "-s MAIN_MODULE=1 -s TOTAL_MEMORY=140MB--embed-file ${CMAKE_BINARY_DIR}/lib@/lib/libclangCppInterOp.wasm --bind --preload-file ${LLVM_DIR}/../../../bin@/bin --bind"
40+
)
41+
target_link_options(${name} PRIVATE
42+
PUBLIC "SHELL: -sERROR_ON_UNDEFINED_SYMBOLS=0"
43+
)
44+
target_link_libraries(CppInterOpTests
45+
PRIVATE
46+
clangCppInterOp
47+
)
48+
else()
49+
target_link_libraries(${name} PUBLIC ${ARG_LIBRARIES} ${gtest_libs} pthread)
50+
endif()
3251
endif()
3352
add_test(NAME cppinterop-${name} COMMAND ${name})
3453
set_tests_properties(cppinterop-${name} PROPERTIES

unittests/CppInterOp/CMakeLists.txt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,32 @@ add_cppinterop_unittest(CppInterOpTests
1414
VariableReflectionTest.cpp
1515
)
1616

17-
target_link_libraries(CppInterOpTests
18-
PRIVATE
19-
clangCppInterOp
20-
)
17+
if(EMSCRIPTEN)
18+
set_target_properties(CppInterOpTests PROPERTIES
19+
LINK_FLAGS "-s MAIN_MODULE=1 -s TOTAL_MEMORY=140MB --embed-file ${CMAKE_BINARY_DIR}/lib@/lib/libclangCppInterOp.wasm --bind --preload-file ${LLVM_DIR}/../../../bin@/bin --bind"
20+
)
21+
target_link_libraries(CppInterOpTests
22+
PRIVATE
23+
clangCppInterOp
24+
)
25+
else()
26+
target_link_libraries(CppInterOpTests
27+
PRIVATE
28+
clangCppInterOp
29+
)
30+
endif()
2131

2232
set_output_directory(CppInterOpTests
2333
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/CppInterOpTests/unittests/bin/$<CONFIG>/
2434
LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/CppInterOpTests/unittests/bin/$<CONFIG>/
2535
)
2636

37+
if(EMSCRIPTEN)
38+
target_link_options(CppInterOpTests PRIVATE
39+
"SHELL: -s LINKABLE=1 -s WASM_BIGINT"
40+
)
41+
endif()
42+
2743
if(NOT WIN32)
2844
set_source_files_properties(VariableReflectionTest.cpp PROPERTIES COMPILE_FLAGS
2945
"-Wno-pedantic"
@@ -37,16 +53,24 @@ export_executable_symbols(CppInterOpTests)
3753

3854
unset(LLVM_LINK_COMPONENTS)
3955

56+
if(NOT EMSCRIPTEN)
4057
add_cppinterop_unittest(DynamicLibraryManagerTests DynamicLibraryManagerTest.cpp)
4158
target_link_libraries(DynamicLibraryManagerTests
4259
PRIVATE
4360
clangCppInterOp
4461
)
4562

63+
if(EMSCRIPTEN)
64+
target_link_options(DynamicLibraryManagerTests PRIVATE
65+
"SHELL: -s LINKABLE=1 -s WASM_BIGINT -s MAIN_MODULE=1"
66+
)
67+
endif()
68+
4669
set_output_directory(DynamicLibraryManagerTests
4770
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/$<CONFIG>/
4871
LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/$<CONFIG>/
4972
)
5073
add_dependencies(DynamicLibraryManagerTests TestSharedLib)
74+
endif()
5175
#export_executable_symbols_for_plugins(TestSharedLib)
5276
add_subdirectory(TestSharedLib)

unittests/CppInterOp/TestSharedLib/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
if(EMSCRIPTEN)
2+
# Without this it tries compiling to static library and get a warning
3+
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
4+
set(CMAKE_STRIP FALSE)
5+
endif()
6+
17
add_llvm_library(TestSharedLib
28
SHARED
39
DISABLE_LLVM_LINK_LLVM_DYLIB
@@ -8,4 +14,17 @@ set_output_directory(TestSharedLib
814
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/unittests/bin/$<CONFIG>/
915
LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/unittests/bin/$<CONFIG>/
1016
)
17+
18+
set_target_properties(TestSharedLib
19+
PROPERTIES NO_SONAME 1
20+
)
21+
22+
if(EMSCRIPTEN)
23+
# Without this you get an error that -gc-sections and -r cannot be used together.
24+
# Not sure I have picked the correct one to keep
25+
target_link_options(TestSharedLib PRIVATE
26+
"SHELL: -Wl,--no-gc-sections -s LINKABLE=1 -s WASM_BIGINT -s SIDE_MODULE=1"
27+
)
28+
endif()
29+
1130
set_target_properties(TestSharedLib PROPERTIES FOLDER "Tests")

0 commit comments

Comments
 (0)