@@ -74,6 +74,42 @@ if(${ZMQ_ENABLE_SANITIZER_UNDEFINED})
74
74
set (ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED" )
75
75
endif ()
76
76
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
+
77
113
# Set MacOS deployment target
78
114
if (APPLE )
79
115
option (MACOSX_DEPLOYMENT_TARGET "MacOS deployment target" "10.15" )
@@ -126,8 +162,14 @@ project_options(
126
162
${ENABLE_SANITIZER_UNDEFINED}
127
163
)
128
164
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 ()
131
173
132
174
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU
133
175
OR CMAKE_CXX_COMPILER_ID STREQUAL Clang
@@ -145,6 +187,10 @@ if(ZMQ_NO_SYNC_RESOLVE)
145
187
target_compile_definitions (addon PRIVATE ZMQ_NO_SYNC_RESOLVE )
146
188
endif ()
147
189
190
+ if (ZMQ_WASM )
191
+ target_compile_definitions (addon PRIVATE ZMQ_WASM )
192
+ endif ()
193
+
148
194
# ZeroMQ
149
195
find_package (ZeroMQ CONFIG REQUIRED )
150
196
target_link_system_libraries (addon PRIVATE libzmq libzmq-static )
@@ -168,7 +214,11 @@ if(WIN32)
168
214
endif ()
169
215
170
216
# 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 ()
172
222
173
223
# Windows delay load node.exe
174
224
if (WIN32 )
0 commit comments