-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add C/C++ bindings #645
Add C/C++ bindings #645
Conversation
Hmm I'm new to Rust (this PR is because I'm implementing testing for a C++ library), is the failure of some tests due to |
Thanks for this! Adding a C API seems reasonable to me, although I don't think we'd want to do it precisely this way. Instead I think we'd probably want to do something similar to Wasmtime itself where the C API is a separate crate (e.g. under Would you be up for splitting this C API to a separate crate? It's ok to not expose functionality from other crates for now and we can fill that in later. Otherwise though when creating a C API I think there's a few conventions we also want to handle:
Eventually longer-term I think we'll want binary released for the C API as well but that can wait too. I apologize if this seems like a lot, but supporting a C API "officially" is not a trivial thing to do unfortunately and there's a surprising amount of ground to cover to ensure it works well. |
Would |
If by |
Oh I wasn't aware that's exactly how wasmtime does it, I'll make sure to model this off of wasmtime |
How does wasmtime format its headers? I want to commit something with a consistent style |
I've moved my bindings into the |
Doxygen comments are in the header, but I'm waiting for your input on that point |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! This is all looking pretty good to me. One thing though, can you update CI to build the C API and build/test the example program here as well?
I'm not sure how best to integrate the cmake bits here since I've never managed a cmake project myself for something like this. If possible though I think it would be good to avoid placing the cmake bits at the root of the project to avoid mistaking which build system is needed to work with wasm-tools.
crates/c-api/src/lib.rs
Outdated
} | ||
} | ||
|
||
wasm_tools_error::WASM_TOOLS_INVALID_POINTER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally API-wise I think it would be best to remove this error variant, assume everything is non-null, and then proceed as usual. I think it's better to document that this shouldn't be null instead of handling it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it so the function receives a reference like wasmtime
does, the calling C code crashes when it passes a null pointer, I guess that's what we want?
CMakeLists.txt
Outdated
# TODO linux | ||
endif() | ||
|
||
target_include_directories(wasm-tools INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/crates/c-api/include) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently we don't use cmake to build the Rust code itself, so is this necessary to add? This seems somewhat nontrivial and at least personally I have no idea how to maintain cmake code myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought exposing the bindings to other code through the Cmake ecosystem would be more convenient, I personally used ExternalProject_Add when building before so anyone using the library would probably use the same solution. Since there's also a Cargo.toml I thought developers would recognize one is used for Rust and the other for C bindings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I can see the value of moving the CMakeLists.txt into crates/c-api
, it would be the same other than different relative paths
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If possible I think it's probably best to keep the new cmake files (and possibly examples
too?) in the new crates/c-api
directory. We can always lift them up later of course depending on how things go.
crates/c-api/src/lib.rs
Outdated
bytes.data = std::ptr::null_mut(); | ||
wasm_tools_error::WASM_TOOLS_INSUFFICIENT_ENTROPY | ||
} | ||
Err(_e) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the other error variants get matched here and translated to an error enum?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I'd like some examples of when those other two errors would be thrown so I can better document them in the header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I think it's ok to not exhaustively document, we may not be hitting the other error cases for now anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, thanks again! I think with the macOS build failure resolved as well as minor comments below this should be good to land.
crates/c-api/CMakeLists.txt
Outdated
# 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}) | ||
else() | ||
set(WASM_TOOLS_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different | ||
${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtools.so | ||
${CMAKE_BINARY_DIR}) | ||
endif() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this'll need a case for macOS and libwasmtools.dylib
crates/c-api/CMakeLists.txt
Outdated
if(WIN32) | ||
target_link_libraries(wasm-tools INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtools.dll.lib) | ||
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to above I think this'll need a libwasmtools.dylib
case?
crates/c-api/src/lib.rs
Outdated
bytes.data = std::ptr::null_mut(); | ||
wasm_tools_error::WASM_TOOLS_INSUFFICIENT_ENTROPY | ||
} | ||
Err(_e) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I think it's ok to not exhaustively document, we may not be hitting the other error cases for now anyway.
Looks great to me, thanks again for this! I appreciate all the work into making the initial infrastructure for this, and it may even end up making sense to lift some of these cmake bits into Wasmtime... |
* 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
This PR adds two exported functions, along with a header, through Rust FFI so that other languages can generate random modules for fuzzing as well.
An example Cmake script for linking:
While building in
./build
with wasm-tools in./third_party/wasm-tools
.Usage is as follows: