Skip to content

Commit

Permalink
Add C/C++ bindings (#645)
Browse files Browse the repository at this point in the history
* Add wasm-smith C/C++ bindings

* Add initial c-api

* Add CMake support and examples

* Add main function to examples

* Remove references to wasmtime in doxygen.conf

* Handle insufficent entropy in c-api and add CI

* Fix c-api CI error

* Simplify wasm_tools_byte_vec_delete

* Move c-api CMakeLists into c-api crate

* Suppress examples build errors

* Add macos support to c-api building

* Fix windows c-api build

* Fix c-api type in readme
  • Loading branch information
TheGreatRambler authored Jun 30, 2022
1 parent 8f6a72f commit 77f1718
Show file tree
Hide file tree
Showing 15 changed files with 2,853 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
- run: cargo test --manifest-path crates/wasmparser/Cargo.toml --features deterministic
- run: cargo build --manifest-path crates/wast/Cargo.toml --no-default-features
- run: cargo build --manifest-path crates/wast/Cargo.toml --no-default-features --features wasm-module
- run: cmake -S ${{github.workspace}}/examples -B ${{github.workspace}}/examples/build -DCMAKE_BUILD_TYPE=Release
- run: cmake --build ${{github.workspace}}/examples/build --config Release

rustfmt:
name: Rustfmt
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
exclude = ['tests/wabt', 'tests/testsuite', 'publish.rs']

[workspace]
members = ['fuzz', 'crates/wasm-encoder', 'crates/fuzz-stats', 'crates/wasm-mutate-stats']
members = ['crates/c-api', 'fuzz', 'crates/wasm-encoder', 'crates/fuzz-stats', 'crates/wasm-mutate-stats']

[dependencies]
anyhow = "1.0"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ implemented in this repository as well. These libraries are:
It's recommended to use the libraries directly rather than the CLI tooling when
embedding into a separate project.

# C/C++ bindings

Using the `CMakeLists.txt` in `crates/c-api`, `wasm-tools` can be used from the [`wasm-tools.h` header](crates/c-api/include/wasm-tools.h).

# License

This project is licensed under the Apache 2.0 license with the LLVM exception.
Expand Down
40 changes: 40 additions & 0 deletions crates/c-api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.10)

# Copy shared library into build directory
if(WIN32)
set(WASM_TOOLS_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtools.dll
${CMAKE_BINARY_DIR})
elseif(APPLE)
set(WASM_TOOLS_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtools.dylib
${CMAKE_BINARY_DIR})
else()
set(WASM_TOOLS_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtools.so
${CMAKE_BINARY_DIR})
endif()

include(ExternalProject)
ExternalProject_Add(
wasm-tools-crate
DOWNLOAD_COMMAND ""
CONFIGURE_COMMAND ""
INSTALL_COMMAND "${WASM_TOOLS_INSTALL_COMMAND}"
BUILD_COMMAND cargo build --release
BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}
BUILD_ALWAYS ON)
add_library(wasm-tools INTERFACE)
add_dependencies(wasm-tools wasm-tools-crate)

if(WIN32)
target_link_libraries(wasm-tools INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtools.dll.lib)
elseif(APPLE)
target_link_libraries(wasm-tools INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtools.dylib)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")
else()
target_link_libraries(wasm-tools INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtools.so)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")
endif()

target_include_directories(wasm-tools INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
29 changes: 29 additions & 0 deletions crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "wasm-tools-c-api"
authors = ["Addison Hart <tgr@tgrcode.com>"]
categories = ["development-tools", "development-tools::testing", "wasm"]
description = "C API to expose wasm-tools"
edition = "2021"
license = "Apache-2.0 WITH LLVM-exception"
readme = "./README.md"
repository = "https://github.com/bytecodealliance/wasm-tools/tree/main/crates/c-api"
version = "0.1.1"
publish = false

[lib]
name = "wasmtools"
crate-type = ["cdylib"]
doc = false
test = false
doctest = false

[dependencies]
arbitrary = { version = "1.1.0", features = ["derive"] }
wasmparser-dump = { version = "0.1.4", path = "../dump" }
wasm-mutate = { version = "0.2.4", path = "../wasm-mutate" }
wasm-shrink = { version = "0.1.6", path = "../wasm-shrink" }
wasm-smith = { version = "0.11.1", path = "../wasm-smith" }
wasmparser = { version = "0.86.0", path = "../wasmparser" }
wasmprinter = { version = "0.2.36", path = "../wasmprinter" }
wast = { version = "42.0.0", path = "../wast" }
wat = { version = "1.0.44", path = "../wat" }
1 change: 1 addition & 0 deletions crates/c-api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
../../LICENSE
4 changes: 4 additions & 0 deletions crates/c-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Wasm-tools's C API

For more information you can find the documentation for this library (TODO).
TODO EXCLUDE_SYMBOLS INPUT INCLUDE_PATH in doxygen needs modifying
Loading

0 comments on commit 77f1718

Please sign in to comment.