Skip to content

Commit

Permalink
Tweaked Emscripten build flags to make WASM smaller
Browse files Browse the repository at this point in the history
- Defined the compile/link flags as variables and reuse them
- Enabled LTO
- Forced -Os, since build type MinSizeRel doesn't seem to do anything
- Changed build type to Release, since the debug symbols add ~50MB to
  the .wasm files
  • Loading branch information
snej committed Jun 14, 2024
1 parent 1d7d87f commit 0c5efc7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
7 changes: 2 additions & 5 deletions C/tests/cmake/platform_emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ function(setup_build)

target_compile_options(
C4Tests PRIVATE
"-pthread"
"-fwasm-exceptions"
${EMSCRIPTEN_COMPILE_FLAGS}
)

target_link_options(
C4Tests PRIVATE
"-pthread"
"-fwasm-exceptions"
"-lembind"
${EMSCRIPTEN_LINK_FLAGS}
"SHELL:-s EXIT_RUNTIME=1"
"SHELL:-s PTHREAD_POOL_SIZE=24"
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
include("${PROJECT_SOURCE_DIR}/cmake/platform_linux_desktop.cmake")
else()
message(FATAL_ERROR "Unable to determine a supported platform from ${CMAKE_SYSTEM_NAME}")
endif(MSVC)
endif()

check_threading()
setup_globals()
Expand Down
7 changes: 2 additions & 5 deletions LiteCore/tests/cmake/platform_emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ function(setup_build)

target_compile_options(
CppTests PRIVATE
"-pthread"
"-fwasm-exceptions"
${EMSCRIPTEN_COMPILE_FLAGS}
)

target_link_options(
CppTests PRIVATE
"-pthread"
"-fwasm-exceptions"
"-lembind"
${EMSCRIPTEN_LINK_FLAGS}
"SHELL:-s EXIT_RUNTIME=1"
"SHELL:-s PTHREAD_POOL_SIZE=24"
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
Expand Down
8 changes: 8 additions & 0 deletions Xcode/LiteCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,10 @@
275FF6661E42A90C005F90DD /* QueryParserTables.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = QueryParserTables.hh; sourceTree = "<group>"; };
275FF6D11E4947E1005F90DD /* c4BaseTest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = c4BaseTest.cc; sourceTree = "<group>"; };
2760FBC826210AA0000F34C5 /* CollectionImpl.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = CollectionImpl.hh; sourceTree = "<group>"; };
27614AA12C1CDCCB0065FBB5 /* cmake */ = {isa = PBXFileReference; lastKnownFileType = folder; name = cmake; path = ../cmake; sourceTree = "<group>"; };
27614AA52C1CDCEA0065FBB5 /* cmake */ = {isa = PBXFileReference; lastKnownFileType = folder; path = cmake; sourceTree = "<group>"; };
27614AA62C1CDD110065FBB5 /* cmake */ = {isa = PBXFileReference; lastKnownFileType = folder; path = cmake; sourceTree = "<group>"; };
27614AA72C1CDDB00065FBB5 /* cmake */ = {isa = PBXFileReference; lastKnownFileType = folder; path = cmake; sourceTree = "<group>"; };
2761F3EE1EE9CC58006D4BB8 /* CookieStore.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CookieStore.cc; sourceTree = "<group>"; };
2761F3EF1EE9CC58006D4BB8 /* CookieStore.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = CookieStore.hh; sourceTree = "<group>"; };
2761F3F61EEA00C3006D4BB8 /* CookieStoreTest.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CookieStoreTest.cc; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1915,6 +1919,7 @@
2708FE591CF4D0450022F721 /* LiteCoreTest.hh */,
274D040A1BA75E1C00FF7C35 /* main.cpp */,
2750735A1F4B5ECC003D2CCE /* CMakeLists.txt */,
27614AA72C1CDDB00065FBB5 /* cmake */,
2719253B23970F4E0053DDA6 /* replacedb */,
);
name = "C++ Tests";
Expand Down Expand Up @@ -2101,6 +2106,7 @@
children = (
2744B329241854F2005A194D /* README.md */,
2744B30C241854F2005A194D /* CMakeLists.txt */,
27614AA62C1CDD110065FBB5 /* cmake */,
2744B317241854F2005A194D /* BLIPConnection.hh */,
2744B319241854F2005A194D /* BLIP.hh */,
2744B31C241854F2005A194D /* MessageBuilder.hh */,
Expand Down Expand Up @@ -2234,6 +2240,7 @@
2783DF981D27436700F84E6E /* c4ThreadingTest.cc */,
2700BB612170117F00797537 /* EE */,
275073591F4B5E66003D2CCE /* CMakeLists.txt */,
27614AA52C1CDCEA0065FBB5 /* cmake */,
2719253323970C7F0053DDA6 /* data */,
);
path = tests;
Expand All @@ -2260,6 +2267,7 @@
children = (
273AD3CF18F4B23A007D8C23 /* README.md */,
275073581F4B5E13003D2CCE /* CMakeLists.txt */,
27614AA12C1CDCCB0065FBB5 /* cmake */,
2757DE551B9FC34E002EE261 /* API */,
2745B7CA26825F970012A17A /* docs */,
27E4871019217187007D8940 /* LiteCore */,
Expand Down
2 changes: 1 addition & 1 deletion build_cmake/scripts/build_wasm_EE.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ cd $SCRIPT_DIR/..
mkdir -p wasm_EE
cd wasm_EE

emcmake cmake -DBUILD_ENTERPRISE=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo ../..
emcmake cmake -DBUILD_ENTERPRISE=ON -DCMAKE_BUILD_TYPE=Release ../..
emmake make -j
13 changes: 9 additions & 4 deletions cmake/platform_emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ macro(check_threading)
message(STATUS "No threading checks needed for Emscripten")
endmacro()

set(EMSCRIPTEN_COMPILE_FLAGS "-pthread" "-fwasm-exceptions" "-Os")
set(EMSCRIPTEN_LINK_FLAGS "-pthread" "-fwasm-exceptions" "-lembind")

function(setup_globals)
setup_globals_unix()

set(EMSCRIPTEN_COMPILE_FLAGS "-pthread -fwasm-exceptions")
#if(NOT DISABLE_LTO_BUILD)
set(EMSCRIPTEN_COMPILE_FLAGS ${EMSCRIPTEN_COMPILE_FLAGS} "-flto" PARENT_SCOPE)
set(EMSCRIPTEN_LINK_FLAGS ${EMSCRIPTEN_LINK_FLAGS} "-flto" PARENT_SCOPE)
#endif()
endfunction()

function(set_litecore_source)
Expand Down Expand Up @@ -35,12 +41,11 @@ function(setup_litecore_build)
LiteCore/Unix
)

message(STATUS "Emscripten flags are ${EMSCRIPTEN_COMPILE_FLAGS}")
foreach(platform LiteCoreObjects LiteCoreUnitTesting BLIPObjects CouchbaseSqlite3 SQLite3_UnicodeSN)
target_compile_options(
${platform} PRIVATE
#${EMSCRIPTEN_COMPILE_FLAGS}
"-pthread"
"-fwasm-exceptions"
${EMSCRIPTEN_COMPILE_FLAGS}
)
endforeach()

Expand Down
2 changes: 1 addition & 1 deletion vendor/fleece

0 comments on commit 0c5efc7

Please sign in to comment.