Skip to content

Commit de7101c

Browse files
committed
Use Oz and flto flags to reduce Emscripten shared library size to 44/45 Mb
1 parent 2585d0d commit de7101c

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

.github/workflows/emscripten.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ jobs:
195195
-DCLANG_BUILD_TOOLS=OFF \
196196
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
197197
../llvm
198-
emmake ninja clang cling lld gtest_main
198+
EMCC_CFLAGS="-Oz -flto" emmake ninja clang cling lld gtest_main
199199
else
200200
# Apply patches
201201
llvm_vers=$(echo "${{ matrix.clang-runtime }}" | tr '[:lower:]' '[:upper:]')
@@ -226,7 +226,7 @@ jobs:
226226
-G Ninja \
227227
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
228228
../llvm
229-
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
229+
EMCC_CFLAGS="-Oz -flto" emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
230230
fi
231231
cd ../
232232
rm -rf $(find . -maxdepth 1 ! -name "build" ! -name "llvm" ! -name "clang" ! -name ".")
@@ -296,6 +296,7 @@ jobs:
296296
-DLLVM_ENABLE_LIBPFM=OFF `
297297
-DCLANG_BUILD_TOOLS=OFF `
298298
..\llvm
299+
$env:EMCC_CFLAGS="-Oz -flto"
299300
emmake make clang cling lld gtest_main
300301
}
301302
else
@@ -337,6 +338,7 @@ jobs:
337338
-DCLANG_BUILD_TOOLS=OFF `
338339
-G Ninja `
339340
..\llvm
341+
$env:EMCC_CFLAGS="-Oz -flto"
340342
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
341343
}
342344
cd ..\

Emscripten-build-instructions.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
100100
-DCLANG_BUILD_TOOLS=OFF \
101101
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
102102
../llvm
103-
emmake make libclang -j $(nproc --all)
104-
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
105-
emmake make lldWasm -j $(nproc --all)
103+
EMCC_CFLAGS="-Oz -flto" emmake make libclang -j $(nproc --all)
104+
EMCC_CFLAGS="-Oz -flto" emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
105+
EMCC_CFLAGS="-Oz -flto" emmake make lldWasm -j $(nproc --all)
106106
```
107107

108108
or executing
@@ -131,7 +131,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release `
131131
-DCLANG_BUILD_TOOLS=OFF `
132132
-G Ninja `
133133
..\llvm
134+
$env:EMCC_CFLAGS="-Oz -flto"
134135
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
136+
$env:EMCC_CFLAGS=""
135137
```
136138

137139
on Windows. Once this finishes building we need to take note of where we built our llvm build. This can be done by executing the following on Linux and osx

docs/Emscripten-build-instructions.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ and osx
120120
-DCLANG_BUILD_TOOLS=OFF \
121121
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
122122
../llvm
123-
emmake make libclang -j $(nproc --all)
124-
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
125-
emmake make lldWasm -j $(nproc --all)
123+
EMCC_CFLAGS="-Oz -flto" emmake make libclang -j $(nproc --all)
124+
EMCC_CFLAGS="-Oz -flto" emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
125+
EMCC_CFLAGS="-Oz -flto" emmake make lldWasm -j $(nproc --all)
126126
127127
or executing
128128

@@ -151,7 +151,9 @@ or executing
151151
-DCLANG_BUILD_TOOLS=OFF `
152152
-G Ninja `
153153
..\llvm
154+
$env:EMCC_CFLAGS="-Oz -flto"
154155
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
156+
$env:EMCC_CFLAGS=""
155157
156158
on Windows. Once this finishes building we need to take note of where we built our llvm build.
157159
This can be done by executing the following on Linux and osx

lib/CppInterOp/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,16 @@ if(EMSCRIPTEN)
127127
set_target_properties(clangCppInterOp
128128
PROPERTIES NO_SONAME 1
129129
)
130+
target_compile_options(clangCppInterOp
131+
PRIVATE "SHELL: -Oz"
132+
PRIVATE "SHELL: -flto"
133+
)
130134
target_link_options(clangCppInterOp
131135
PRIVATE "SHELL: -s WASM_BIGINT"
132136
PRIVATE "SHELL: -s SIDE_MODULE=1"
133137
PRIVATE "SHELL: ${SYMBOLS_LIST}"
138+
PRIVATE "SHELL: -Oz"
139+
PRIVATE "SHELL: -flto"
134140
)
135141
else()
136142
target_link_options(clangCppInterOp

lib/CppInterOp/exports.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@
5050
-Wl,--export=_ZNK5clang4Type14isFloatingTypeEv
5151
-Wl,--export=_ZNK5clang12FunctionDecl12getNumParamsEv
5252
-Wl,--export=__clang_Interpreter_SetValueNoAlloc
53-
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
53+
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
54+
-Wl,--export=_ZN4llvm15SmallVectorBaseIjE8set_sizeEm

0 commit comments

Comments
 (0)