Skip to content

Commit df0d283

Browse files
authored
[Emscripten port] Improve emcc flags (#6349)
No changes here to binaryen.js/wasm builds. 1. Add a flag to enable pthreads. 2. Use SINGLE_FILE on binaryen.js/.wasm as before, which is nice for library users as they want just a single file to distribute for Binaryen support. For other builds like wasm-opt.js etc. no longer set SINGLE_FILE, as that type of build wants to be a replacement for a normal wasm-opt build as much as possible, so avoid the overhead of SINGLE_FILE. (Previously we disabled SINGLE_FILE also in the case of BUILD_FOR_BROWSER but I don't think we need to special-case that any more.)
1 parent 999781a commit df0d283

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ option(BUILD_FOR_BROWSER "Build binaryen toolchain utilities for the browser" OF
5252
# Turn this on to use the Wasm EH feature instead of emscripten EH in the wasm/BinaryenJS builds
5353
option(EMSCRIPTEN_ENABLE_WASM_EH "Enable Wasm EH feature in emscripten build" OFF)
5454

55+
# Turn this on to use pthreads feature in the wasm/BinaryenJS builds
56+
option(EMSCRIPTEN_ENABLE_PTHREADS "Enable pthreads in emscripten build" OFF)
57+
5558
# For git users, attempt to generate a more useful version string
5659
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
5760
find_package(Git QUIET REQUIRED)
@@ -342,6 +345,17 @@ if(EMSCRIPTEN)
342345
add_compile_flag("-sDISABLE_EXCEPTION_CATCHING=0")
343346
add_link_flag("-sDISABLE_EXCEPTION_CATCHING=0")
344347
endif()
348+
if(EMSCRIPTEN_ENABLE_PTHREADS)
349+
add_compile_flag("-pthread")
350+
add_link_flag("-pthread")
351+
# Use mimalloc to avoid a 5x slowdown:
352+
# https://github.com/emscripten-core/emscripten/issues/15727#issuecomment-1960295018
353+
add_link_flag("-sMALLOC=mimalloc")
354+
# Disable the warning on pthreads+memory growth (we are not much affected by
355+
# it as there is little wasm-JS transfer of data, almost all work is inside
356+
# the wasm).
357+
add_link_flag("-Wno-pthreads-mem-growth")
358+
endif()
345359
# In the browser, there is no natural place to provide commandline arguments
346360
# for a commandline tool, so let the user run the main entry point themselves
347361
# and pass in the arguments there.
@@ -356,7 +370,6 @@ if(EMSCRIPTEN)
356370
else()
357371
# On Node.js, make the tools immediately usable.
358372
add_link_flag("-sNODERAWFS")
359-
add_link_flag("-sSINGLE_FILE")
360373
endif()
361374
# in opt builds, LTO helps so much (>20%) it's worth slow compile times
362375
add_nondebug_compile_flag("-flto")
@@ -461,6 +474,9 @@ if(EMSCRIPTEN)
461474
target_link_libraries(binaryen_wasm "-sFILESYSTEM")
462475
target_link_libraries(binaryen_wasm "-sEXPORT_NAME=Binaryen")
463476
target_link_libraries(binaryen_wasm "-sNODERAWFS=0")
477+
# Emit a single file for convenience of people using binaryen.js as a library,
478+
# so they only need to distribute a single file.
479+
target_link_libraries(binaryen_wasm "-sSINGLE_FILE")
464480
target_link_libraries(binaryen_wasm "-sEXPORT_ES6")
465481
target_link_libraries(binaryen_wasm "-sEXPORTED_RUNTIME_METHODS=stringToUTF8OnStack,stringToAscii")
466482
target_link_libraries(binaryen_wasm "-sEXPORTED_FUNCTIONS=_malloc,_free")
@@ -493,6 +509,7 @@ if(EMSCRIPTEN)
493509
target_link_libraries(binaryen_js "-sFILESYSTEM=1")
494510
endif()
495511
target_link_libraries(binaryen_js "-sNODERAWFS=0")
512+
target_link_libraries(binaryen_js "-sSINGLE_FILE")
496513
target_link_libraries(binaryen_js "-sEXPORT_NAME=Binaryen")
497514
# Currently, js_of_ocaml can only process ES5 code
498515
if(JS_OF_OCAML)

0 commit comments

Comments
 (0)