Skip to content

Commit

Permalink
chore: Limit exports in wasm library.
Browse files Browse the repository at this point in the history
This cuts down the size from 3MB to 1.2MB.
  • Loading branch information
iphydf committed Feb 10, 2025
1 parent 12f34cd commit 6a22240
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
28 changes: 15 additions & 13 deletions cmake/StrictAbi.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
find_program(SHELL NAMES sh dash bash zsh fish)

macro(make_version_script)
if(STRICT_ABI AND SHELL AND ENABLE_SHARED)
if(STRICT_ABI AND SHELL)
_make_version_script(${ARGN})

if(ENABLE_SHARED)
if(APPLE)
set_target_properties(${target}_shared PROPERTIES
LINK_FLAGS -Wl,-exported_symbols_list,${${target}_VERSION_SCRIPT})
else()
set_target_properties(${target}_shared PROPERTIES
LINK_FLAGS -Wl,--version-script,${${target}_VERSION_SCRIPT})
endif()
endif()
endif()
endmacro()

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

if(NOT APPLE)
if(NOT APPLE AND NOT EMSCRIPTEN)
file(WRITE ${${target}_VERSION_SCRIPT}
"{ global:\n")
endif()
Expand All @@ -40,29 +50,21 @@ function(_make_version_script target)
string(REPLACE "\n" ";" sublib_SYMS ${sublib_SYMS})

foreach(sym ${sublib_SYMS})
if(APPLE)
if(APPLE OR EMSCRIPTEN)
file(APPEND ${${target}_VERSION_SCRIPT} "_")
endif()
file(APPEND ${${target}_VERSION_SCRIPT} "${sym}")
if(NOT APPLE)
if(NOT APPLE AND NOT EMSCRIPTEN)
file(APPEND ${${target}_VERSION_SCRIPT} ";")
endif()
file(APPEND ${${target}_VERSION_SCRIPT} "\n")
endforeach(sym)
endforeach(sublib)

if(NOT APPLE)
if(NOT APPLE AND NOT EMSCRIPTEN)
file(APPEND ${${target}_VERSION_SCRIPT}
"local: *; };\n")
endif()

if(APPLE)
set_target_properties(${target}_shared PROPERTIES
LINK_FLAGS -Wl,-exported_symbols_list,${${target}_VERSION_SCRIPT})
else()
set_target_properties(${target}_shared PROPERTIES
LINK_FLAGS -Wl,--version-script,${${target}_VERSION_SCRIPT})
endif()
endfunction()

option(STRICT_ABI "Enforce strict ABI export in dynamic libraries" OFF)
Expand Down
19 changes: 12 additions & 7 deletions other/docker/wasm/wasm.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \

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

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
WORKDIR /work
Expand Down Expand Up @@ -84,14 +84,13 @@ RUN . "/work/emsdk/emsdk_env.sh" \
RUN . "/work/emsdk/emsdk_env.sh" \
&& mkdir -p /wasm/bin \
&& emcc -O3 -flto \
-s ALLOW_UNIMPLEMENTED_SYSCALLS=1 \
-s EXPORT_NAME=libtoxcore \
-s IGNORE_MISSING_MAIN=1 \
-s MAIN_MODULE=1 \
-s MALLOC=emmalloc \
-s MODULARIZE=1 \
-s STRICT=1 \
-s WEBSOCKET_URL=ws:// \
--no-entry \
/wasm/lib/libopus.a \
/wasm/lib/libsodium.a \
/wasm/lib/libvpx.a \
Expand All @@ -108,28 +107,34 @@ RUN . "/work/emsdk/emsdk_env.sh" \
-GNinja \
-DCMAKE_INSTALL_PREFIX="/wasm" \
-DCMAKE_C_FLAGS="-O3 -flto -fPIC" \
-DCMAKE_UNITY_BUILD=ON \
-DMUST_BUILD_TOXAV=ON \
-DENABLE_SHARED=OFF \
-DBOOTSTRAP_DAEMON=OFF \
-DMIN_LOGGER_LEVEL=TRACE \
-DSTRICT_ABI=ON \
. \
&& emmake cmake --build _build \
&& emmake cmake --install _build

RUN echo _malloc >>/work/c-toxcore/_build/toxcore.ld \
&& echo _free >>/work/c-toxcore/_build/toxcore.ld \
&& cat /work/c-toxcore/_build/toxcore.ld

# Link together all the libraries.
RUN . "/work/emsdk/emsdk_env.sh" \
&& mkdir -p /wasm/bin \
&& emcc -O3 -flto \
-s ALLOW_UNIMPLEMENTED_SYSCALLS=1 \
-s EXPORTED_FUNCTIONS="$(sed -e 's/.*/"&"/' /work/c-toxcore/_build/toxcore.ld | paste -sd "," -)" \
-s EXPORT_NAME=libtoxcore \
-s IGNORE_MISSING_MAIN=1 \
-s MAIN_MODULE=1 \
-s MALLOC=emmalloc \
-s MODULARIZE=1 \
-s STRICT=1 \
-s WEBSOCKET_URL=ws:// \
--no-entry \
/wasm/lib/libopus.a \
/wasm/lib/libsodium.a \
/wasm/lib/libvpx.a \
/wasm/lib/libtoxcore.a \
-o /wasm/bin/libtoxcore.js
RUN ls -lh /wasm/bin/libtoxcore.js /wasm/bin/libtoxcore.wasm
4 changes: 4 additions & 0 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,11 @@ static int sys_getsockopt(void *obj, Socket sock, int level, int optname, void *
non_null()
static int sys_setsockopt(void *obj, Socket sock, int level, int optname, const void *optval, size_t optlen)
{
#ifdef EMSCRIPTEN
return 0;
#else
return setsockopt(net_socket_to_native(sock), level, optname, (const char *)optval, optlen);
#endif
}

// sets and fills an array of addrs for address
Expand Down

0 comments on commit 6a22240

Please sign in to comment.