@@ -74,6 +74,42 @@ if(${ZMQ_ENABLE_SANITIZER_UNDEFINED})
7474 set (ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED" )
7575endif ()
7676
77+ option (ZMQ_WASM "Build for WebAssembly" OFF )
78+ set_option_from_env(ZMQ_WASM)
79+
80+ if (ZMQ_WASM OR CMAKE_SYSTEM_NAME STREQUAL "Emscripten" )
81+ set (ZMQ_WASM TRUE )
82+
83+ # Compile for WebAssembly
84+ set (VCPKG_HOST_TRIPLET wasm32-emscripten)
85+ set (VCPKG_TARGET_TRIPLET wasm32-emscripten)
86+
87+ include ($ENV{EMSCRIPTEN_ROOT} /cmake/Modules/Platform/Emscripten.cmake)
88+
89+ set (CMAKE_LINKER "$ENV{EMSCRIPTEN_ROOT} /bin/lld${CMAKE_EXECUTABLE_SUFFIX} " CACHE STRING "Linker" FORCE)
90+
91+ # export an indirect function table for napi-wasm
92+ add_link_options ("--export-table" )
93+
94+ # Add WebAssembly-specific exports for napi-wasm
95+ add_link_options ("SHELL:-s EXPORTED_FUNCTIONS=['_malloc','_free','napi_wasm_malloc','napi_register_module_v1']" )
96+ add_link_options ("SHELL:-s EXPORTED_RUNTIME_METHODS=['ccall','cwrap']" )
97+ add_link_options ("SHELL:-s ALLOW_MEMORY_GROWTH=1" )
98+ add_link_options ("SHELL:-s MODULARIZE=1" )
99+ add_link_options ("SHELL:-s EXPORT_NAME='createModule'" )
100+ add_link_options ("SHELL:-s ENVIRONMENT='web,worker'" )
101+ add_link_options ("--no-entry" )
102+ add_link_options ("SHELL:-Wl,--strip-all" )
103+ add_link_options ("SHELL:-fvisibility=hidden" )
104+ add_link_options ("SHELL:-fvisibility-inlines-hidden" )
105+ add_link_options ("SHELL:-Wl,-gc-sections" )
106+ add_compile_options ("-flto=full" )
107+ add_link_options ("-flto=full" )
108+
109+ # Allow undefined symbols on Linux for runtime loading
110+ add_link_options ("SHELL:-Wl,--unresolved-symbols=ignore-all" )
111+ endif ()
112+
77113# Set MacOS deployment target
78114if (APPLE )
79115 option (MACOSX_DEPLOYMENT_TARGET "MacOS deployment target" "10.15" )
@@ -126,8 +162,14 @@ project_options(
126162 ${ENABLE_SANITIZER_UNDEFINED}
127163)
128164
129- file (GLOB_RECURSE SOURCES "./src/*.cc" )
130- add_library (addon SHARED ${SOURCES} )
165+ file (GLOB_RECURSE SOURCES "./src/*.cc" "./src/*.cpp" )
166+
167+ if (ZMQ_WASM)
168+ # For WebAssembly, create an executable instead of a shared library
169+ add_executable (addon ${SOURCES} )
170+ else ()
171+ add_library (addon SHARED ${SOURCES} )
172+ endif ()
131173
132174if (CMAKE_CXX_COMPILER_ID STREQUAL GNU
133175 OR CMAKE_CXX_COMPILER_ID STREQUAL Clang
@@ -145,6 +187,10 @@ if(ZMQ_NO_SYNC_RESOLVE)
145187 target_compile_definitions (addon PRIVATE ZMQ_NO_SYNC_RESOLVE)
146188endif ()
147189
190+ if (ZMQ_WASM)
191+ target_compile_definitions (addon PRIVATE ZMQ_WASM)
192+ endif ()
193+
148194# ZeroMQ
149195find_package (ZeroMQ CONFIG REQUIRED)
150196target_link_system_libraries(addon PRIVATE libzmq libzmq-static )
@@ -168,7 +214,11 @@ if(WIN32)
168214endif ()
169215
170216# Use `.node` for the library without any "lib" prefix
171- set_target_properties (addon PROPERTIES PREFIX "" SUFFIX ".node" )
217+ if (ZMQ_WASM)
218+ set_target_properties (addon PROPERTIES PREFIX "" SUFFIX ".wasm" )
219+ else ()
220+ set_target_properties (addon PROPERTIES PREFIX "" SUFFIX ".node" )
221+ endif ()
172222
173223# Windows delay load node.exe
174224if (WIN32 )
0 commit comments