Skip to content

Reduce CppInterOp Emscripten shared library size #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions .github/workflows/emscripten.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ jobs:
-DLLVM_EXTERNAL_CLING_SOURCE_DIR=../../cling \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" \
-DLLVM_ENABLE_LIBEDIT=OFF \
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" \
Expand All @@ -194,6 +193,9 @@ jobs:
-DLLVM_ENABLE_LIBPFM=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DLLVM_ENABLE_LTO=Full \
../llvm
emmake ninja clang cling lld gtest_main
else
Expand All @@ -206,7 +208,6 @@ jobs:
cd build
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" \
-DLLVM_ENABLE_LIBEDIT=OFF \
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" \
Expand All @@ -225,6 +226,9 @@ jobs:
-DCLANG_BUILD_TOOLS=OFF \
-G Ninja \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DLLVM_ENABLE_LTO=Full \
../llvm
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
fi
Expand Down Expand Up @@ -284,7 +288,6 @@ jobs:
-DLLVM_EXTERNAL_CLING_SOURCE_DIR=../../cling `
-DCMAKE_BUILD_TYPE=Release `
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
-DLLVM_ENABLE_ASSERTIONS=ON `
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" `
-DLLVM_ENABLE_LIBEDIT=OFF `
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" `
Expand All @@ -302,6 +305,9 @@ jobs:
-DLLVM_BUILD_TOOLS=OFF `
-DLLVM_ENABLE_LIBPFM=OFF `
-DCLANG_BUILD_TOOLS=OFF `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DLLVM_ENABLE_LTO=Full `
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
..\llvm
emmake make clang cling lld gtest_main
Expand All @@ -325,7 +331,6 @@ jobs:
echo "Apply clang${{ matrix.clang-runtime }}-*.patch patches:"
emcmake cmake -DCMAKE_BUILD_TYPE=Release `
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
-DLLVM_ENABLE_ASSERTIONS=ON `
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" `
-DLLVM_ENABLE_LIBEDIT=OFF `
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" `
Expand All @@ -342,6 +347,9 @@ jobs:
-DLLVM_BUILD_TOOLS=OFF `
-DLLVM_ENABLE_LIBPFM=OFF `
-DCLANG_BUILD_TOOLS=OFF `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DLLVM_ENABLE_LTO=Full `
-G Ninja `
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
..\llvm
Expand Down Expand Up @@ -527,6 +535,15 @@ jobs:
fi

emmake make -j ${{ env.ncpus }} check-cppinterop
os="${{ matrix.os }}"
if [[ "${os}" != macos* ]] ; then
actual_size=$(stat -c%s "./lib/libclangCppInterOp.so")
max_size=$((40 * 1024 * 1024))
if [[ "$actual_size" -gt "$max_size" ]]; then
echo "Error: libclangCppInterOp.so is larger than 40 MB."
exit 1
fi
fi
cd ./unittests/CppInterOp/
# Fresh install browsers, and run Emscripten tests in them
# This is to match the Emscripten build instructions, where
Expand Down Expand Up @@ -689,7 +706,12 @@ jobs:
-DSYSROOT_PATH=$SYSROOT_PATH \
../
fi
emmake make -j ${{ env.ncpus }} check-cppinterop
os="${{ matrix.os }}"
if [[ "${os}" != macos* ]] ; then
EMCC_CORES=1 emmake make -j 1 check-cppinterop
else
EMCC_CORES=2 emmake make -j 2 check-cppinterop
fi
cd ./unittests/CppInterOp/
# Explaination of options for emrun
# --browser (name of browser on path)
Expand Down
8 changes: 6 additions & 2 deletions Emscripten-build-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ mkdir build
cd build
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_TARGETS_TO_BUILD="WebAssembly" \
-DLLVM_ENABLE_LIBEDIT=OFF \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
Expand All @@ -98,6 +97,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_LIBPFM=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DLLVM_ENABLE_LTO=Full \
../llvm
emmake make libclang -j $(nproc --all)
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
Expand All @@ -118,7 +120,6 @@ mkdir build
cd build
emcmake cmake -DCMAKE_BUILD_TYPE=Release `
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
-DLLVM_ENABLE_ASSERTIONS=ON `
-DLLVM_TARGETS_TO_BUILD="WebAssembly" `
-DLLVM_ENABLE_LIBEDIT=OFF `
-DLLVM_ENABLE_PROJECTS="clang;lld" `
Expand All @@ -137,6 +138,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release `
-DCLANG_BUILD_TOOLS=OFF `
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
-G Ninja `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DLLVM_ENABLE_LTO=Full `
..\llvm
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
```
Expand Down
8 changes: 6 additions & 2 deletions docs/Emscripten-build-instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ and osx
cd build
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_TARGETS_TO_BUILD="WebAssembly" \
-DLLVM_ENABLE_LIBEDIT=OFF \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
Expand All @@ -118,6 +117,9 @@ and osx
-DLLVM_ENABLE_LIBPFM=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
-DLLVM_ENABLE_LTO=Full \
../llvm
emmake make libclang -j $(nproc --all)
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
Expand All @@ -138,7 +140,6 @@ or executing
cd build
emcmake cmake -DCMAKE_BUILD_TYPE=Release `
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
-DLLVM_ENABLE_ASSERTIONS=ON `
-DLLVM_TARGETS_TO_BUILD="WebAssembly" `
-DLLVM_ENABLE_LIBEDIT=OFF `
-DLLVM_ENABLE_PROJECTS="clang;lld" `
Expand All @@ -157,6 +158,9 @@ or executing
-DCLANG_BUILD_TOOLS=OFF `
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
-G Ninja `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
-DLLVM_ENABLE_LTO=Full `
..\llvm
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm

Expand Down
6 changes: 6 additions & 0 deletions lib/CppInterOp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,16 @@ if(EMSCRIPTEN)
set_target_properties(clangCppInterOp
PROPERTIES NO_SONAME 1
)
target_compile_options(clangCppInterOp
PRIVATE "SHELL: -Oz"
PRIVATE "SHELL: -flto"
Comment on lines +131 to +132
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't these become part of CMAKE_X_FLAGS_RELEASE flags, too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will give it a go and find out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this out and couldn't get it to work. For some reason if I do it via cmake options then Ubuntu both x86 and arm cannot pass xeus-cpps Emscripten tests for llvm 20. I have reverted back to the original solution now, and waiting to see the ci pass

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's that even possible...

Copy link
Collaborator Author

@mcbarton mcbarton Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not supposed to he possible according to the WebAssembly/Emscripten standard. The result should be independent of the platform you cross compile from, but I have come accross a few examples where this is found not to be true in reality (and shows exactly why we should be cross compiling from multiple platforms in the ci to tests these assumptions). In the process of trying to get the cmake approach to work I found out that the symbols you need to export on each platform manually to get the CppInterOps tests to pass are not the same (e.g. you can not export _ZN4llvm15SmallVectorBaseIjE8set_sizeEm , and the Emscripten tests will build and pass on Ubuntu, but wont build on MacOS or Windows)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vgvassilev Here is a workflow run where Ubuntu arm and x86 fail for llvm 20 Emscripten builds using cmake options https://github.com/compiler-research/CppInterOp/actions/runs/16596946952 despite the other platforms passing

)
target_link_options(clangCppInterOp
PRIVATE "SHELL: -s WASM_BIGINT"
PRIVATE "SHELL: -s SIDE_MODULE=1"
PRIVATE "SHELL: ${SYMBOLS_LIST}"
PRIVATE "SHELL: -Oz"
PRIVATE "SHELL: -flto"
)
else()
target_link_options(clangCppInterOp
Expand Down
3 changes: 2 additions & 1 deletion lib/CppInterOp/exports.ld
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@
-Wl,--export=_ZNK5clang4Type14isFloatingTypeEv
-Wl,--export=_ZNK5clang12FunctionDecl12getNumParamsEv
-Wl,--export=__clang_Interpreter_SetValueNoAlloc
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
-Wl,--export=_ZN4llvm15SmallVectorBaseIjE8set_sizeEm
Loading