Hello again! I just wanted to drop a note detailing my experiences building Telegram's Database Library (
) with the new WASM backend. Overall the experience was a bit rough, mainly due to errors that did not occur using fastcomp, primarily the new strict pthreads checking and lack of support for Position Independent Code.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3ec8c456..96736d4f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -36,7 +36,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" "${CMAKE_MODULE_PATH}"
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
-set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+# set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
@@ -98,8 +98,8 @@ if (EMSCRIPTEN)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s WASM=0 -Wno-almost-asm")
else()
set(TD_EMSCRIPTEN td_wasm)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s WASM=1")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0")
endif()
endif()
diff --git a/example/web/build-openssl.sh b/example/web/build-openssl.sh
index faf4f657..0031bef8 100755
--- a/example/web/build-openssl.sh
+++ b/example/web/build-openssl.sh
@@ -12,7 +12,7 @@ echo "Unpacking OpenSSL sources..."
tar xzf $OPENSSL.tar.gz || exit 1
cd openssl-$OPENSSL
-emconfigure ./Configure linux-generic32 no-shared no-dso no-engine no-unit-test no-ui || exit 1
+emconfigure ./Configure linux-generic32 no-threads no-pic no-asm no-shared no-dso no-engine no-unit-test no-ui || exit 1
sed -i.bak 's/CROSS_COMPILE=.*/CROSS_COMPILE=/g' Makefile || exit 1
sed -i.bak 's/-ldl //g' Makefile || exit 1
echo "Building OpenSSL..."
These changes disabled threading and PIC when building OpenSSL, and also disabled the error on undefined symbols. The latter change was necessary as I was running into "error: undefined symbol: __cxa_uncaught_exception".
Once those changes were made, I was able to successfully compile TdLib to WASM! Sadly I didn't see any significant size changes: the original fastcomp WASM module was 7.7 MB, and the new module was 7.5 MB. While the module seems to load and perform some actions correctly, the WebSocket connections are closed before a connection is established, so I wasn't able to make any calls to the Telegram API. I'm not a C++ developer, so I probably can't debug that particular issue and will have to wait for the Telegram developers to take a look.
I just wanted to share my experiences trying to compile a non-trivial WASM library with the new upstream backend. The only suggestions I can make are to add warnings to the fastcomp compiler for things that won't be supported in the new backend, such as linking a library built with threads support with a library that doesn't support it.