Skip to content

Commit

Permalink
Add CMake option to explicitly skip wasm-opt and use `wasm-tools stri…
Browse files Browse the repository at this point in the history
…p` instead. (#89)

As per #8, it seems the main purpose of using wasm-opt was to reduce
module size by stripping debug sections. The comment in the CMake file
also indicates an intent to optimize the binary.

However, `wasm-opt -O3` is extremely expensive on large programs; on
my 18-core, 36-thread workstation, it took several minutes to
complete. This is very painful when part of a debug loop that requires
release builds (e.g. for performance features).

This PR adds a `USE_WASM_OPT` CMake option, on by default to preserve
existing behavior, that allows one to do `cmake
-DCMAKE_BUILD_TYPE=Release -DUSE_WASM_OPT=OFF` to get a release build
without `wasm-opt`. In its place, this PR uses `wasm-tools strip -a`,
which strips debug sections (and all other custom sections) without
rewriting code.
  • Loading branch information
cfallin authored Jul 29, 2024
1 parent 80a9d97 commit 35f9a26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
36 changes: 24 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,36 @@ add_executable(starling.wasm
runtime/script_loader.cpp
)

option(USE_WASM_OPT "use wasm-opt to optimize the StarlingMonkey binary" ON)

# For release builds, use wasm-opt to optimize the generated wasm file.
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_custom_command(
TARGET starling.wasm
POST_BUILD
COMMAND ${WASM_OPT} --strip-debug -O3 -o starling.wasm starling.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
elseif (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
add_custom_command(
if (USE_WASM_OPT)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_custom_command(
TARGET starling.wasm
POST_BUILD
COMMAND ${WASM_OPT} --strip-debug -O3 -o starling.wasm starling.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
elseif (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
add_custom_command(
TARGET starling.wasm
POST_BUILD
COMMAND ${WASM_OPT} -O3 -o starling.wasm starling.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
endif()
else()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_custom_command(
TARGET starling.wasm
POST_BUILD
COMMAND ${WASM_OPT} -O3 -o starling.wasm starling.wasm
COMMAND ${WASM_TOOLS_BIN} strip -a -o starling.wasm starling.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
)
endif()
endif()


target_link_libraries(starling.wasm PRIVATE host_api extension_api builtins spidermonkey rust-url)

set(RUNTIME_FILE "starling.wasm")
Expand Down
1 change: 1 addition & 0 deletions cmake/wasm-tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ set(WASM_TOOLS_VERSION 1.0.54)
set(WASM_TOOLS_URL https://github.com/bytecodealliance/wasm-tools/releases/download/wasm-tools-${WASM_TOOLS_VERSION}/wasm-tools-${WASM_TOOLS_VERSION}-${HOST_ARCH}-${HOST_OS}.tar.gz)
CPMAddPackage(NAME wasm-tools URL ${WASM_TOOLS_URL} DOWNLOAD_ONLY TRUE)
set(WASM_TOOLS_DIR ${CPM_PACKAGE_wasm-tools_SOURCE_DIR})
set(WASM_TOOLS_BIN ${WASM_TOOLS_DIR}/wasm-tools)

0 comments on commit 35f9a26

Please sign in to comment.