Skip to content

Commit 8c97dc6

Browse files
authored
Use CMake to build binaryen.js (#2464)
Fixes #2453 As a bonus this also provides a port of wasm-opt etc. with NODERAWFS and everything seems to work, that is, you can run stuff like nodejs wasm-opt.js input.wasm --metrics
1 parent 132d81e commit 8c97dc6

3 files changed

Lines changed: 49 additions & 196 deletions

File tree

CMakeLists.txt

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,27 @@ ELSE()
161161
ENDIF()
162162
ADD_DEBUG_COMPILE_FLAG("-O0")
163163
ADD_DEBUG_COMPILE_FLAG("-g3")
164-
ADD_NONDEBUG_COMPILE_FLAG("-O2")
164+
IF (EMSCRIPTEN)
165+
# really focus on minimizing output size when compiling sources
166+
ADD_NONDEBUG_COMPILE_FLAG("-Oz")
167+
ELSE()
168+
ADD_NONDEBUG_COMPILE_FLAG("-O2")
169+
ENDIF()
165170
ADD_NONDEBUG_COMPILE_FLAG("-UNDEBUG") # Keep asserts.
166171
ENDIF()
167172

173+
IF (EMSCRIPTEN)
174+
# link with -O3 for metadce and other powerful optimizations. note that we
175+
# must use add_link_options so that this appears after CMake's default -O2
176+
add_link_options("-O3")
177+
ADD_LINK_FLAG("-s SINGLE_FILE")
178+
ADD_LINK_FLAG("-s ALLOW_MEMORY_GROWTH=1")
179+
ADD_COMPILE_FLAG("-s DISABLE_EXCEPTION_CATCHING=0")
180+
ADD_LINK_FLAG("-s DISABLE_EXCEPTION_CATCHING=0")
181+
# make the tools immediately usable on Node.js
182+
ADD_LINK_FLAG("-s NODERAWFS")
183+
ENDIF()
184+
168185
# clang doesn't print colored diagnostics when invoked from Ninja
169186
IF (UNIX AND
170187
CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
@@ -196,7 +213,6 @@ SET(binaryen_objs
196213

197214
# Sources.
198215

199-
200216
SET(binaryen_SOURCES
201217
src/binaryen-c.cpp
202218
)
@@ -212,12 +228,6 @@ INSTALL(TARGETS binaryen DESTINATION ${CMAKE_INSTALL_LIBDIR})
212228

213229
INSTALL(FILES src/binaryen-c.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
214230

215-
# if binaryen.js was built (using "./build-js.sh", currently
216-
# optional), install it
217-
IF(EXISTS "${PROJECT_SOURCE_DIR}/out/binaryen.js")
218-
INSTALL(FILES out/binaryen.js DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME})
219-
ENDIF()
220-
221231
SET(wasm-shell_SOURCES
222232
src/tools/wasm-shell.cpp
223233
)
@@ -308,6 +318,33 @@ SET_PROPERTY(TARGET wasm-reduce PROPERTY CXX_STANDARD 14)
308318
SET_PROPERTY(TARGET wasm-reduce PROPERTY CXX_STANDARD_REQUIRED ON)
309319
INSTALL(TARGETS wasm-reduce DESTINATION ${CMAKE_INSTALL_BINDIR})
310320

321+
# binaryen.js
322+
#
323+
# Note that we can't emit binaryen.js directly, as there is libbinaryen already
324+
# declared earlier, so we create binaryen_js.js, which must then be copied.
325+
IF (EMSCRIPTEN)
326+
SET(binaryen_js_SOURCES
327+
src/binaryen-c.cpp
328+
)
329+
ADD_EXECUTABLE(binaryen_js
330+
${binaryen_js_SOURCES})
331+
TARGET_LINK_LIBRARIES(binaryen_js wasm asmjs emscripten-optimizer passes ir cfg support wasm)
332+
# note that SHELL: is needed as otherwise cmake will coalesce -s link flags
333+
# in an incorrect way for emscripten
334+
TARGET_LINK_LIBRARIES(binaryen_js "-s WASM=0")
335+
TARGET_LINK_LIBRARIES(binaryen_js "-s WASM_ASYNC_COMPILATION=0")
336+
TARGET_LINK_LIBRARIES(binaryen_js "-s MODULARIZE_INSTANCE=1")
337+
TARGET_LINK_LIBRARIES(binaryen_js "-s NO_FILESYSTEM=0")
338+
TARGET_LINK_LIBRARIES(binaryen_js "-s NODERAWFS=0")
339+
TARGET_LINK_LIBRARIES(binaryen_js "-s 'EXPORT_NAME=\"Binaryen\"'")
340+
TARGET_LINK_LIBRARIES(binaryen_js "--post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-post.js")
341+
TARGET_LINK_LIBRARIES(binaryen_js optimized "--closure 1")
342+
TARGET_LINK_LIBRARIES(binaryen_js debug "--profiling")
343+
SET_PROPERTY(TARGET binaryen_js PROPERTY CXX_STANDARD 14)
344+
SET_PROPERTY(TARGET binaryen_js PROPERTY CXX_STANDARD_REQUIRED ON)
345+
INSTALL(TARGETS binaryen_js DESTINATION ${CMAKE_INSTALL_BINDIR})
346+
ENDIF()
347+
311348
# Testing
312349
#
313350
# Currently just some very simple smoke tests.

build-js.sh

Lines changed: 0 additions & 187 deletions
This file was deleted.

travis-emcc-tests.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
set -e
22
echo "travis-test build"
3-
./build-js.sh -g
3+
emconfigure cmake -DCMAKE_BUILD_TYPE=Release
4+
emmake make -j4 binaryen_js
5+
mkdir out
6+
cp bin/binaryen_js.js out/binaryen.js
47
echo "travis-test test"
58
python3 -m scripts.test.binaryenjs
69
echo "travis-test yay!"

0 commit comments

Comments
 (0)