Skip to content

Commit ab5976a

Browse files
committed
Add automated tests for emscripten builds
1 parent b797dbb commit ab5976a

19 files changed

+326
-77
lines changed

.github/workflows/emscripten.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,11 @@ jobs:
550550
if: ${{ runner.os != 'windows' }}
551551
shell: bash -l {0}
552552
run: |
553+
set -e
553554
./emsdk/emsdk activate ${{matrix.emsdk_ver}}
554555
source ./emsdk/emsdk_env.sh
555556
micromamba create -f environment-wasm.yml --platform=emscripten-wasm32
556-
557+
export SYSROOT_PATH=$PWD/emsdk/upstream/emscripten/cache/sysroot
557558
export PREFIX=$MAMBA_ROOT_PREFIX/envs/CppInterOp-wasm
558559
export CMAKE_PREFIX_PATH=$PREFIX
559560
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
@@ -587,6 +588,7 @@ jobs:
587588
-DCMAKE_INSTALL_PREFIX=$PREFIX \
588589
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
589590
-DLLVM_ENABLE_WERROR=On \
591+
-DSYSROOT_PATH=$SYSROOT_PATH \
590592
../
591593
else
592594
emcmake cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
@@ -599,13 +601,14 @@ jobs:
599601
-DCMAKE_INSTALL_PREFIX=$PREFIX \
600602
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
601603
-DLLVM_ENABLE_WERROR=On \
604+
-DSYSROOT_PATH=$SYSROOT_PATH \
602605
../
603606
fi
604-
605-
emmake make -j ${{ env.ncpus }} install
606607
608+
emmake make -j ${{ env.ncpus }} check-cppinterop
607609
cd ..
608-
610+
611+
echo "SYSROOT_PATH=$SYSROOT_PATH" >> $GITHUB_ENV
609612
echo "CB_PYTHON_DIR=$CB_PYTHON_DIR" >> $GITHUB_ENV
610613
echo "CPPINTEROP_BUILD_DIR=$CPPINTEROP_BUILD_DIR" >> $GITHUB_ENV
611614
echo "CPPINTEROP_DIR=$CPPINTEROP_DIR" >> $GITHUB_ENV
@@ -618,7 +621,6 @@ jobs:
618621
run: |
619622
./emsdk/emsdk activate ${{matrix.emsdk_ver}}
620623
source ./emsdk/emsdk_env.sh
621-
export SYSROOT_PATH=$PWD/emsdk/upstream/emscripten/cache/sysroot
622624
micromamba activate CppInterOp-wasm
623625
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
624626
cd ./xeus-cpp
@@ -633,6 +635,6 @@ jobs:
633635
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON \
634636
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
635637
-DCppInterOp_DIR="${{ env.CPPINTEROP_BUILD_DIR }}/lib/cmake/CppInterOp" \
636-
-DSYSROOT_PATH=$SYSROOT_PATH \
638+
-DSYSROOT_PATH=${{ env.SYSROOT_PATH }} \
637639
..
638640
emmake make -j ${{ env.ncpus }} install

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

Emscripten-build-instructions.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ git clone https://github.com/emscripten-core/emsdk.git
2525
./emsdk/emsdk install 3.1.73
2626
```
2727

28-
and activate the emsdk environment
28+
and activate the emsdk environment (we are defining SYSROOT_PATH for use later)
2929

3030
```bash
3131
./emsdk/emsdk activate 3.1.73
3232
source ./emsdk/emsdk_env.sh
33+
export SYSROOT_PATH=$PWD/emsdk/upstream/emscripten/cache/sysroot
3334
```
3435

3536
Now clone the 19.x release of the LLVM project repository and CppInterOp (the building of the emscripten version of llvm can be
@@ -97,7 +98,7 @@ export CMAKE_PREFIX_PATH=$PREFIX
9798
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
9899
```
99100

100-
Now to build CppInterOp execute the following
101+
Now to build and test your Emscripten build of CppInterOp by executing the following
101102

102103
```bash
103104
mkdir build
@@ -109,7 +110,14 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
109110
-DBUILD_SHARED_LIBS=ON \
110111
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
111112
-DCMAKE_INSTALL_PREFIX=$PREFIX \
113+
-DSYSROOT_PATH=$SYSROOT_PATH \
112114
../
115+
emmake make -j $(nproc --all) check-cppinterop
116+
```
117+
118+
Assuming it passes all test you can install by executing the following
119+
120+
```bash
113121
emmake make -j $(nproc --all) install
114122
```
115123

@@ -126,7 +134,6 @@ the CppInterOp build folder, you can build the wasm version of xeus-cpp by execu
126134

127135
```bash
128136
cd ../..
129-
export SYSROOT_PATH=$PWD/emsdk/upstream/emscripten/cache/sysroot
130137
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
131138
cd ./xeus-cpp
132139
mkdir build

cmake/CppInterOp/CppInterOpConfig.cmake.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ endif()
1717

1818
### build/install workaround
1919
if (@BUILD_SHARED_LIBS@)
20-
set(_lib_suffix ${CMAKE_SHARED_LIBRARY_SUFFIX})
20+
if(EMSCRIPTEN)
21+
set(_lib_suffix ".wasm")
22+
else()
23+
set(_lib_suffix ${CMAKE_SHARED_LIBRARY_SUFFIX})
24+
endif()
2125
set(_lib_prefix ${CMAKE_SHARED_LIBRARY_PREFIX})
2226
else()
2327
set(_lib_suffix ${CMAKE_STATIC_LIBRARY_SUFFIX})

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)

docs/Emscripten-build-instructions.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ This can be installed by executing (we only currently support version
3535
git clone https://github.com/emscripten-core/emsdk.git
3636
./emsdk/emsdk install 3.1.73
3737
38-
and activate the emsdk environment
38+
and activate the emsdk environment (we are defining SYSROOT_PATH for use later)
3939

4040
.. code:: bash
4141
4242
./emsdk/emsdk activate 3.1.73
4343
source ./emsdk/emsdk_env.sh
44+
export SYSROOT_PATH=$PWD/emsdk/upstream/emscripten/cache/sysroot
4445
4546
Now clone the 19.x release of the LLVM project repository and CppInterOp
4647
(the building of the emscripten version of llvm can be avoided by
@@ -115,7 +116,7 @@ You will also want to set a few environment variables
115116
export CMAKE_PREFIX_PATH=$PREFIX
116117
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
117118
118-
Now to build CppInterOp execute the following
119+
Now to build and test your Emscripten build of CppInterOp by executing the following
119120

120121
.. code:: bash
121122
@@ -128,7 +129,14 @@ Now to build CppInterOp execute the following
128129
-DBUILD_SHARED_LIBS=ON \
129130
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
130131
-DCMAKE_INSTALL_PREFIX=$PREFIX \
132+
-DSYSROOT_PATH=$SYSROOT_PATH \
131133
../
134+
emmake make -j $(nproc --all) check-cppinterop
135+
136+
Assuming it passes all test you can install by executing the following.
137+
138+
.. code:: bash
139+
132140
emmake make -j $(nproc --all) install
133141
134142
Once this finishes building we need to take note of where we built
@@ -147,7 +155,6 @@ build folder, you can build the wasm version of xeus-cpp by executing
147155
.. code:: bash
148156
149157
cd ../..
150-
export SYSROOT_PATH=$PWD/emsdk/upstream/emscripten/cache/sysroot
151158
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
152159
cd ./xeus-cpp
153160
mkdir build

lib/Interpreter/CMakeLists.txt

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
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+
set(LLVM_LINK_COMPONENTS "")
315
else()
326
set(LLVM_LINK_COMPONENTS
337
${LLVM_TARGETS_TO_BUILD}
@@ -41,6 +15,7 @@ else()
4115
if ("LLVMFrontendDriver" IN_LIST LLVM_AVAILABLE_LIBS)
4216
list(APPEND LLVM_LINK_COMPONENTS FrontendDriver)
4317
endif()
18+
endif()
4419
if ("LLVMOrcDebugging" IN_LIST LLVM_AVAILABLE_LIBS)
4520
list(APPEND LLVM_LINK_COMPONENTS OrcDebugging)
4621
endif()
@@ -65,6 +40,11 @@ else()
6540
set(cling_clang_interp clangInterpreter)
6641
endif()
6742

43+
if(EMSCRIPTEN)
44+
set(link_libs
45+
${cling_clang_interp}
46+
)
47+
else()
6848
set(link_libs
6949
${cling_clang_interp}
7050
clangAST
@@ -73,6 +53,7 @@ else()
7353
clangLex
7454
clangSema
7555
)
56+
endif()
7657

7758
if(NOT WIN32)
7859
list(APPEND link_libs dl)
@@ -124,7 +105,6 @@ else()
124105
clangStaticAnalyzerCore
125106
)
126107
endif(LLVM_LINK_LLVM_DYLIB)
127-
128108
add_llvm_library(clangCppInterOp
129109
DISABLE_LLVM_LINK_LLVM_DYLIB
130110
CppInterOp.cpp
@@ -133,6 +113,28 @@ else()
133113
LINK_LIBS
134114
${link_libs}
135115
)
116+
117+
if(EMSCRIPTEN)
118+
# Read the undefined symbols from the text file
119+
file(READ "${CMAKE_SOURCE_DIR}/lib/Interpreter/undefined_symbols.txt" SYMBOLS_LIST)
120+
121+
# Replace newlines with spaces
122+
string(REPLACE "\n" " " SYMBOLS_LIST "${SYMBOLS_LIST}")
123+
#FIXME: Setting no_soname=1 is needed until https://github.com/emscripten-core/emscripten/blob/ac676d5e437525d15df5fd46bc2c208ec6d376a3/cmake/Modules/Platform/Emscripten.cmake#L36
124+
# 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
125+
#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
126+
# https://github.com/llvm/llvm-project/blob/128e2e446e90c3b1827cfc7d4d19e3c0976beff3/llvm/cmake/modules/HandleLLVMOptions.cmake#L318 . The PR to do try to do this is here
127+
# https://github.com/llvm/llvm-project/pull/123396
128+
set_target_properties(clangCppInterOp PROPERTIES
129+
NO_SONAME 1
130+
COMPILE_FLAGS "-s SIDE_MODULE=1"
131+
LINK_FLAGS "-s WASM_BIGINT -s SIDE_MODULE=1 ${SYMBOLS_LIST}"
132+
SUFFIX ".wasm"
133+
)
134+
135+
add_custom_command(TARGET clangCppInterOp POST_BUILD
136+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:clangCppInterOp> ${CMAKE_BINARY_DIR}/unittests/CppInterOp/
137+
)
136138
endif()
137139

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

0 commit comments

Comments
 (0)