diff --git a/CMakeLists.txt b/CMakeLists.txt index c56522f459e..193c062a2e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/README.md b/README.md index 3ff76b48d7b..eec2e385a51 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 061a8e4747a..410ab910ef4 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -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) diff --git a/src/wasm-opt-c.cpp b/src/wasm-opt-c.cpp new file mode 100644 index 00000000000..0a8b84807da --- /dev/null +++ b/src/wasm-opt-c.cpp @@ -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 bytes; + if (inputSize) { + bytes.assign(input, input + inputSize); + } + wasm::TranslateToFuzzReader reader{ + wasm::FuzzParams{}, *module, std::move(bytes)}; + reader.build(); +} diff --git a/src/wasm-opt-c.h b/src/wasm-opt-c.h new file mode 100644 index 00000000000..8b101b8e2e2 --- /dev/null +++ b/src/wasm-opt-c.h @@ -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