Skip to content

Commit

Permalink
Add emscripten automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbarton committed Jan 26, 2025
1 parent f4a36bb commit afdfb8c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 56 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/emscripten.yml
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,10 @@ jobs:
-DLLVM_ENABLE_WERROR=On \
../
fi
emmake make -j ${{ env.ncpus }} install
emmake make -j ${{ env.ncpus }}
emmake make check-cppinterop
cd ..
echo "CB_PYTHON_DIR=$CB_PYTHON_DIR" >> $GITHUB_ENV
Expand Down
14 changes: 6 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,15 @@ endif()

# Add appropriate flags for GCC
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
if (APPLE)
if (APPLE OR EMSCRIPTEN)
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")
else()
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")
endif ()
# Needed due an error which occurs when you compile gtest on emscripten
if (EMSCRIPTEN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")
endif()
endif ()

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


if(EMSCRIPTEN)
message("Build with emscripten")
option(CPPINTEROP_ENABLE_TESTING "Enables the testing infrastructure." OFF)
else()
message("Build with cmake")
option(CPPINTEROP_ENABLE_TESTING "Enables the testing infrastructure." ON)
endif()
option(CPPINTEROP_ENABLE_TESTING "Enables the testing infrastructure." ON)

if(MSVC)

Expand Down
49 changes: 40 additions & 9 deletions cmake/modules/GoogleTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ elseif(APPLE)
endif()

include(ExternalProject)
if(EMSCRIPTEN)

ExternalProject_Add(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
Expand All @@ -31,15 +33,10 @@ ExternalProject_Add(
# CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
# -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
# -Dgtest_force_shared_crt=ON
CMAKE_ARGS -G ${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=$<CONFIG>
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_AR=${CMAKE_AR}
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
${EXTRA_GTEST_OPTS}
CONFIGURE_COMMAND emcmake cmake -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-S ${CMAKE_BINARY_DIR}/unittests/googletest-prefix/src/googletest/
-B ${CMAKE_BINARY_DIR}/unittests/googletest-prefix/src/googletest-build/
BUILD_COMMAND emmake make
# Disable install step
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${_gtest_byproducts}
Expand All @@ -50,6 +47,40 @@ ExternalProject_Add(
TIMEOUT 600
)

else()

ExternalProject_Add(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_SHALLOW 1
GIT_TAG v1.15.2
UPDATE_COMMAND ""
# # Force separate output paths for debug and release builds to allow easy
# # identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands
# CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
# -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
# -Dgtest_force_shared_crt=ON
CMAKE_ARGS -G ${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=$<CONFIG>
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_AR=${CMAKE_AR}
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
${EXTRA_GTEST_OPTS}
# Disable install step
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${_gtest_byproducts}
# Wrap download, configure and build steps in a script to log output
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
TIMEOUT 600
)

endif()

# Specify include dirs for gtest and gmock
ExternalProject_Get_Property(googletest source_dir)
set(GTEST_INCLUDE_DIR ${source_dir}/googletest/include)
Expand Down
56 changes: 28 additions & 28 deletions lib/Interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
if(EMSCRIPTEN)
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-s SIDE_MODULE=1")
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-s SIDE_MODULE=1")
set(CMAKE_STRIP FALSE)

add_llvm_library(clangCppInterOp
SHARED

CppInterOp.cpp
CXCppInterOp.cpp
DynamicLibraryManager.cpp
DynamicLibraryManagerSymbol.cpp
Paths.cpp

# Additional libraries from Clang and LLD
LINK_LIBS
clangInterpreter
)
#FIXME: Setting no_soname=1 is needed until https://github.com/emscripten-core/emscripten/blob/ac676d5e437525d15df5fd46bc2c208ec6d376a3/cmake/Modules/Platform/Emscripten.cmake#L36
# 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
#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
# https://github.com/llvm/llvm-project/blob/128e2e446e90c3b1827cfc7d4d19e3c0976beff3/llvm/cmake/modules/HandleLLVMOptions.cmake#L318 . The PR to do try to do this is here
# https://github.com/llvm/llvm-project/pull/123396
set_target_properties(clangCppInterOp
PROPERTIES NO_SONAME 1
)
target_link_options(clangCppInterOp PRIVATE
PUBLIC "SHELL: -s WASM_BIGINT"
)
message(STATUS "LLVM_TARGETS_TO_BUILD ${LLVM_TARGETS_TO_BUILD}")
set(LLVM_LINK_COMPONENTS "")
else()
set(LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD}
Expand All @@ -41,6 +16,7 @@ else()
if ("LLVMFrontendDriver" IN_LIST LLVM_AVAILABLE_LIBS)
list(APPEND LLVM_LINK_COMPONENTS FrontendDriver)
endif()
endif()
if ("LLVMOrcDebugging" IN_LIST LLVM_AVAILABLE_LIBS)
list(APPEND LLVM_LINK_COMPONENTS OrcDebugging)
endif()
Expand All @@ -65,6 +41,11 @@ else()
set(cling_clang_interp clangInterpreter)
endif()

if(EMSCRIPTEN)
set(link_libs
${cling_clang_interp}
)
else()
set(link_libs
${cling_clang_interp}
clangAST
Expand All @@ -73,13 +54,15 @@ else()
clangLex
clangSema
)
endif()

if(NOT WIN32)
list(APPEND link_libs dl)
endif()

# Get rid of libLLVM-X.so which is appended to the list of static libraries.
if (LLVM_LINK_LLVM_DYLIB)
message(STATUS "In here ${LLVM_LINK_LLVM_DYLIB}")
set(new_libs ${link_libs})
set(libs ${new_libs})
while(NOT "${new_libs}" STREQUAL "")
Expand All @@ -103,6 +86,7 @@ else()
endif()
endforeach(transitive_lib)
# Update the target properties with the list of only static libraries.
message(STATUS "static_transitive_libs ${static_transitive_libs}")
set_target_properties(${lib} PROPERTIES INTERFACE_LINK_LIBRARIES "${static_transitive_libs}")
set(static_transitive_libs "")
endif()
Expand All @@ -124,7 +108,8 @@ else()
clangStaticAnalyzerCore
)
endif(LLVM_LINK_LLVM_DYLIB)

message(STATUS "Test DLM ${DLM}")
message(STATUS "Test link_libs ${link_libs}")
add_llvm_library(clangCppInterOp
DISABLE_LLVM_LINK_LLVM_DYLIB
CppInterOp.cpp
Expand All @@ -133,6 +118,21 @@ else()
LINK_LIBS
${link_libs}
)

if(EMSCRIPTEN)

#FIXME: Setting no_soname=1 is needed until https://github.com/emscripten-core/emscripten/blob/ac676d5e437525d15df5fd46bc2c208ec6d376a3/cmake/Modules/Platform/Emscripten.cmake#L36
# 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
#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
# https://github.com/llvm/llvm-project/blob/128e2e446e90c3b1827cfc7d4d19e3c0976beff3/llvm/cmake/modules/HandleLLVMOptions.cmake#L318 . The PR to do try to do this is here
# https://github.com/llvm/llvm-project/pull/123396
set_target_properties(clangCppInterOp PROPERTIES
NO_SONAME 1
COMPILE_FLAGS "-s SIDE_MODULE=1"
LINK_FLAGS "-s LINKABLE=1 -s EXPORT_ALL=1 -s WASM_BIGINT -s SIDE_MODULE=1"
SUFFIX ".wasm"
)

endif()

string(REPLACE ";" "\;" _VER CPPINTEROP_VERSION)
Expand Down
26 changes: 21 additions & 5 deletions unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ if (NOT TARGET gtest)
include(GoogleTest)
endif()

set(gtest_libs gtest gtest_main)
# Clang prior than clang13 (I think) merges both gmock into gtest.
if (TARGET gmock)
list(APPEND gtest_libs gmock gmock_main)
if(EMSCRIPTEN)
# By removing gtest_main and gmock_main you avoid duplicate symbol error (probably not the best solution)
set(gtest_libs gtest_main gtest gmock)
else()
set(gtest_libs gtest gtest_main)
# Clang prior than clang13 (I think) merges both gmock into gtest.
if (TARGET gmock)
list(APPEND gtest_libs gmock gmock_main)
endif()
endif()

add_custom_target(CppInterOpUnitTests)
Expand All @@ -28,7 +33,18 @@ if(WIN32)
target_link_libraries(${name} PUBLIC ${ARG_LIBRARIES} ${gtest_libs})
set_property(TARGET ${name} APPEND_STRING PROPERTY LINK_FLAGS "${MSVC_EXPORTS}")
else()
target_link_libraries(${name} PUBLIC ${ARG_LIBRARIES} ${gtest_libs} pthread)
if(EMSCRIPTEN)
target_link_libraries(${name} PRIVATE ${ARG_LIBRARIES} ${gtest_libs} clangCppInterOp)
target_link_options(${name} PRIVATE
"SHELL: -sERROR_ON_UNDEFINED_SYMBOLS=0 -s WARN_ON_UNDEFINED_SYMBOLS=0"
)
target_link_libraries(CppInterOpTests
PRIVATE
clangCppInterOp
)
else()
target_link_libraries(${name} PUBLIC ${ARG_LIBRARIES} ${gtest_libs} pthread)
endif()
endif()
add_test(NAME cppinterop-${name} COMMAND ${name})
set_tests_properties(cppinterop-${name} PROPERTIES
Expand Down
35 changes: 31 additions & 4 deletions unittests/CppInterOp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@ add_cppinterop_unittest(CppInterOpTests
VariableReflectionTest.cpp
)

target_link_libraries(CppInterOpTests
PRIVATE
clangCppInterOp
)
if(EMSCRIPTEN)
set_target_properties(CppInterOpTests PROPERTIES
LINK_FLAGS "-s MAIN_MODULE=1 --preload-file ${CMAKE_BINARY_DIR}/lib@/lib/libclangCppInterOp.wasm --bind"
)
target_link_options(CppInterOpTests PRIVATE
PUBLIC "SHELL: -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s WARN_ON_UNDEFINED_SYMBOLS=0"
)
target_link_libraries(CppInterOpTests
PRIVATE
clangCppInterOp
)
else()
target_link_libraries(CppInterOpTests
PRIVATE
clangCppInterOp
)
endif()

set_output_directory(CppInterOpTests
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/CppInterOpTests/unittests/bin/$<CONFIG>/
LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/CppInterOpTests/unittests/bin/$<CONFIG>/
)

if(EMSCRIPTEN)
target_link_options(CppInterOpTests PRIVATE
"SHELL: -s LINKABLE=1 -s WASM_BIGINT"
)
endif()

if(NOT WIN32)
set_source_files_properties(VariableReflectionTest.cpp PROPERTIES COMPILE_FLAGS
"-Wno-pedantic"
Expand All @@ -37,16 +56,24 @@ export_executable_symbols(CppInterOpTests)

unset(LLVM_LINK_COMPONENTS)

if(NOT EMSCRIPTEN)
add_cppinterop_unittest(DynamicLibraryManagerTests DynamicLibraryManagerTest.cpp)
target_link_libraries(DynamicLibraryManagerTests
PRIVATE
clangCppInterOp
)

if(EMSCRIPTEN)
target_link_options(DynamicLibraryManagerTests PRIVATE
"SHELL: -s LINKABLE=1 -s WASM_BIGINT -s MAIN_MODULE=1"
)
endif()

set_output_directory(DynamicLibraryManagerTests
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/$<CONFIG>/
LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/$<CONFIG>/
)
add_dependencies(DynamicLibraryManagerTests TestSharedLib)
endif()
#export_executable_symbols_for_plugins(TestSharedLib)
add_subdirectory(TestSharedLib)
19 changes: 19 additions & 0 deletions unittests/CppInterOp/TestSharedLib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
if(EMSCRIPTEN)
# Without this it tries compiling to static library and get a warning
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
set(CMAKE_STRIP FALSE)
endif()

add_llvm_library(TestSharedLib
SHARED
DISABLE_LLVM_LINK_LLVM_DYLIB
Expand All @@ -8,4 +14,17 @@ set_output_directory(TestSharedLib
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/unittests/bin/$<CONFIG>/
LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/unittests/bin/$<CONFIG>/
)

set_target_properties(TestSharedLib
PROPERTIES NO_SONAME 1
)

if(EMSCRIPTEN)
# Without this you get an error that -gc-sections and -r cannot be used together.
# Not sure I have picked the correct one to keep
target_link_options(TestSharedLib PRIVATE
"SHELL: -Wl,--no-gc-sections -s LINKABLE=1 -s WASM_BIGINT -s SIDE_MODULE=1"
)
endif()

set_target_properties(TestSharedLib PROPERTIES FOLDER "Tests")

0 comments on commit afdfb8c

Please sign in to comment.