Skip to content

Commit 3286b6e

Browse files
authored
Use --whole-archive to build binaryen.js. NFC (#7994)
We were previously including `binaryen-c.cpp` twice. Once in the libbinaryen library and once again as part of the binaryen_js/binarn_wasm targets. This previous method worked but pointlessly compiled binaryen-c.cpp twice. This new method as that advantage that I can also build with `-sMAIN_MODULE=1` without getting duplicate symbol warnings. An alternative to this technique would adding any single symbols from `binaryen-c.cpp` to `EXPORTED_FUNCTIONS`.
1 parent 23f7af1 commit 3286b6e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,11 @@ endif()
532532
# declared earlier, so we create binaryen_wasm/js.js, which must then be copied.
533533
if(EMSCRIPTEN)
534534
# binaryen.js WebAssembly variant
535-
add_executable(binaryen_wasm ${binaryen_SOURCES})
536-
target_link_libraries(binaryen_wasm PRIVATE binaryen)
535+
add_executable(binaryen_wasm ${binaryen_HEADERS})
536+
# For the emscripten build we link against libbinaryen wih `--whole-archive`.
537+
# Without this, the EMSCRIPTEN_KEEPALIVE symbols within the library would
538+
# be ignored.
539+
target_link_libraries(binaryen_wasm PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,binaryen>")
537540
target_link_libraries(binaryen_wasm PRIVATE "-sFILESYSTEM")
538541
target_link_libraries(binaryen_wasm PRIVATE "-sEXPORT_NAME=Binaryen")
539542
target_link_libraries(binaryen_wasm PRIVATE "-sNODERAWFS=0")
@@ -563,8 +566,8 @@ if(EMSCRIPTEN)
563566
install(TARGETS binaryen_wasm DESTINATION ${CMAKE_INSTALL_BINDIR})
564567

565568
# binaryen.js JavaScript variant
566-
add_executable(binaryen_js ${binaryen_SOURCES})
567-
target_link_libraries(binaryen_js PRIVATE binaryen)
569+
add_executable(binaryen_js ${binaryen_HEADERS})
570+
target_link_libraries(binaryen_js PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,binaryen>")
568571
target_link_libraries(binaryen_js PRIVATE "-sWASM=0")
569572
target_link_libraries(binaryen_js PRIVATE "-sWASM_ASYNC_COMPILATION=0")
570573

0 commit comments

Comments
 (0)