Skip to content

Commit 6a22240

Browse files
committed
chore: Limit exports in wasm library.
This cuts down the size from 3MB to 1.2MB.
1 parent 12f34cd commit 6a22240

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

cmake/StrictAbi.cmake

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@
1212
find_program(SHELL NAMES sh dash bash zsh fish)
1313

1414
macro(make_version_script)
15-
if(STRICT_ABI AND SHELL AND ENABLE_SHARED)
15+
if(STRICT_ABI AND SHELL)
1616
_make_version_script(${ARGN})
17+
18+
if(ENABLE_SHARED)
19+
if(APPLE)
20+
set_target_properties(${target}_shared PROPERTIES
21+
LINK_FLAGS -Wl,-exported_symbols_list,${${target}_VERSION_SCRIPT})
22+
else()
23+
set_target_properties(${target}_shared PROPERTIES
24+
LINK_FLAGS -Wl,--version-script,${${target}_VERSION_SCRIPT})
25+
endif()
26+
endif()
1727
endif()
1828
endmacro()
1929

2030
function(_make_version_script target)
2131
set(${target}_VERSION_SCRIPT "${CMAKE_BINARY_DIR}/${target}.ld")
2232

23-
if(NOT APPLE)
33+
if(NOT APPLE AND NOT EMSCRIPTEN)
2434
file(WRITE ${${target}_VERSION_SCRIPT}
2535
"{ global:\n")
2636
endif()
@@ -40,29 +50,21 @@ function(_make_version_script target)
4050
string(REPLACE "\n" ";" sublib_SYMS ${sublib_SYMS})
4151

4252
foreach(sym ${sublib_SYMS})
43-
if(APPLE)
53+
if(APPLE OR EMSCRIPTEN)
4454
file(APPEND ${${target}_VERSION_SCRIPT} "_")
4555
endif()
4656
file(APPEND ${${target}_VERSION_SCRIPT} "${sym}")
47-
if(NOT APPLE)
57+
if(NOT APPLE AND NOT EMSCRIPTEN)
4858
file(APPEND ${${target}_VERSION_SCRIPT} ";")
4959
endif()
5060
file(APPEND ${${target}_VERSION_SCRIPT} "\n")
5161
endforeach(sym)
5262
endforeach(sublib)
5363

54-
if(NOT APPLE)
64+
if(NOT APPLE AND NOT EMSCRIPTEN)
5565
file(APPEND ${${target}_VERSION_SCRIPT}
5666
"local: *; };\n")
5767
endif()
58-
59-
if(APPLE)
60-
set_target_properties(${target}_shared PROPERTIES
61-
LINK_FLAGS -Wl,-exported_symbols_list,${${target}_VERSION_SCRIPT})
62-
else()
63-
set_target_properties(${target}_shared PROPERTIES
64-
LINK_FLAGS -Wl,--version-script,${${target}_VERSION_SCRIPT})
65-
endif()
6668
endfunction()
6769

6870
option(STRICT_ABI "Enforce strict ABI export in dynamic libraries" OFF)

other/docker/wasm/wasm.Dockerfile

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
2323

2424
WORKDIR /work/emsdk
2525
RUN git clone --depth=1 https://github.com/emscripten-core/emsdk /work/emsdk \
26-
&& ./emsdk install 4.0.1 \
27-
&& ./emsdk activate 4.0.1
26+
&& ./emsdk install 4.0.3 \
27+
&& ./emsdk activate 4.0.3
2828

2929
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
3030
WORKDIR /work
@@ -84,14 +84,13 @@ RUN . "/work/emsdk/emsdk_env.sh" \
8484
RUN . "/work/emsdk/emsdk_env.sh" \
8585
&& mkdir -p /wasm/bin \
8686
&& emcc -O3 -flto \
87-
-s ALLOW_UNIMPLEMENTED_SYSCALLS=1 \
8887
-s EXPORT_NAME=libtoxcore \
89-
-s IGNORE_MISSING_MAIN=1 \
9088
-s MAIN_MODULE=1 \
9189
-s MALLOC=emmalloc \
9290
-s MODULARIZE=1 \
9391
-s STRICT=1 \
9492
-s WEBSOCKET_URL=ws:// \
93+
--no-entry \
9594
/wasm/lib/libopus.a \
9695
/wasm/lib/libsodium.a \
9796
/wasm/lib/libvpx.a \
@@ -108,28 +107,34 @@ RUN . "/work/emsdk/emsdk_env.sh" \
108107
-GNinja \
109108
-DCMAKE_INSTALL_PREFIX="/wasm" \
110109
-DCMAKE_C_FLAGS="-O3 -flto -fPIC" \
110+
-DCMAKE_UNITY_BUILD=ON \
111111
-DMUST_BUILD_TOXAV=ON \
112112
-DENABLE_SHARED=OFF \
113113
-DBOOTSTRAP_DAEMON=OFF \
114114
-DMIN_LOGGER_LEVEL=TRACE \
115+
-DSTRICT_ABI=ON \
115116
. \
116117
&& emmake cmake --build _build \
117118
&& emmake cmake --install _build
118119

120+
RUN echo _malloc >>/work/c-toxcore/_build/toxcore.ld \
121+
&& echo _free >>/work/c-toxcore/_build/toxcore.ld \
122+
&& cat /work/c-toxcore/_build/toxcore.ld
123+
119124
# Link together all the libraries.
120125
RUN . "/work/emsdk/emsdk_env.sh" \
121126
&& mkdir -p /wasm/bin \
122127
&& emcc -O3 -flto \
123-
-s ALLOW_UNIMPLEMENTED_SYSCALLS=1 \
128+
-s EXPORTED_FUNCTIONS="$(sed -e 's/.*/"&"/' /work/c-toxcore/_build/toxcore.ld | paste -sd "," -)" \
124129
-s EXPORT_NAME=libtoxcore \
125-
-s IGNORE_MISSING_MAIN=1 \
126-
-s MAIN_MODULE=1 \
127130
-s MALLOC=emmalloc \
128131
-s MODULARIZE=1 \
129132
-s STRICT=1 \
130133
-s WEBSOCKET_URL=ws:// \
134+
--no-entry \
131135
/wasm/lib/libopus.a \
132136
/wasm/lib/libsodium.a \
133137
/wasm/lib/libvpx.a \
134138
/wasm/lib/libtoxcore.a \
135139
-o /wasm/bin/libtoxcore.js
140+
RUN ls -lh /wasm/bin/libtoxcore.js /wasm/bin/libtoxcore.wasm

toxcore/network.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,11 @@ static int sys_getsockopt(void *obj, Socket sock, int level, int optname, void *
592592
non_null()
593593
static int sys_setsockopt(void *obj, Socket sock, int level, int optname, const void *optval, size_t optlen)
594594
{
595+
#ifdef EMSCRIPTEN
596+
return 0;
597+
#else
595598
return setsockopt(net_socket_to_native(sock), level, optname, (const char *)optval, optlen);
599+
#endif
596600
}
597601

598602
// sets and fills an array of addrs for address

0 commit comments

Comments
 (0)