Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ if(INSTALL_LIBS OR BUILD_SHARED_LIBS)
endif()
if(INSTALL_LIBS)
install(FILES src/binaryen-c.h src/wasm-delegations.def DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
if(BUILD_TOOLS)
install(TARGETS wasm-opt-lib ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES src/wasm-opt-c.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()
endif()

# binaryen.js
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ generator: `cmake -G Ninja . && ninja`.

To avoid the gtest dependency, you can pass `-DBUILD_TESTS=OFF` to cmake.

With `BUILD_TOOLS` and `INSTALL_LIBS` enabled, installation also provides the
`wasm-opt` static library and its public `wasm-opt-c.h` header. Link it with
`binaryen` and call `WasmOptTranslateToFuzz` to generate modules from seed bytes.
Module configuration, optimization and serialization use the Binaryen C API.

Binaryen.js can be built using Emscripten, which can be installed via [the SDK](http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html).

- Building for Node.js:
Expand Down
11 changes: 10 additions & 1 deletion src/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ set(fuzzing_SOURCES
${fuzzing_HEADERS}
)

binaryen_add_executable(wasm-opt "${fuzzing_SOURCES};wasm-opt.cpp")
add_library(wasm-opt-lib STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/wasm-opt-c.cpp
${fuzzing_SOURCES}
)
set_target_properties(wasm-opt-lib PROPERTIES OUTPUT_NAME wasm-opt)
target_link_libraries(wasm-opt-lib PUBLIC binaryen)
target_include_directories(wasm-opt-lib SYSTEM INTERFACE ${PROJECT_SOURCE_DIR}/src)

binaryen_add_executable(wasm-opt wasm-opt.cpp)
target_link_libraries(wasm-opt PRIVATE wasm-opt-lib)
binaryen_add_executable(wasm-metadce wasm-metadce.cpp)
binaryen_add_executable(wasm2js wasm2js.cpp)
binaryen_add_executable(wasm-emscripten-finalize wasm-emscripten-finalize.cpp)
Expand Down
32 changes: 32 additions & 0 deletions src/wasm-opt-c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2026 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "wasm-opt-c.h"

#include "tools/fuzzing.h"

void WasmOptTranslateToFuzz(
BinaryenModuleRef module, const char* input, size_t inputSize) {
assert(module);
assert(input || !inputSize);
std::vector<char> bytes;
if (inputSize) {
bytes.assign(input, input + inputSize);
}
wasm::TranslateToFuzzReader reader{
wasm::FuzzParams{}, *module, std::move(bytes)};
reader.build();
}
35 changes: 35 additions & 0 deletions src/wasm-opt-c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2026 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef wasm_wasm_opt_c_h
#define wasm_wasm_opt_c_h

#include "binaryen-c.h"

#ifdef __cplusplus
extern "C" {
#endif

// Generate fuzz code using the module's enabled features and default settings.
// input may be NULL when inputSize is zero.
BINARYEN_API void WasmOptTranslateToFuzz(
BinaryenModuleRef module, const char* input, size_t inputSize);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // wasm_wasm_opt_c_h
Loading