A project to generate bindings to a rust foreign function interface for other languages.
This is only a proof of concept and not designed to be easy to use.
Currently, only generating a
C/C++ header for a rust foreign function interface is supported.
For more information see this introductory blog post.
This tool is split into several parts.
metadata-ffi: A schema for describing a rust foreign function interface that can be communicated between tools
emit-ffi: A rust compiler driver that will ouput a
ffi.jsonfor a crate
C/C++abstract syntax tree designed for code generation
c-ffi: A library to convert a
C/C++AST and output it as a header
cargo-ffi: A cargo subcommand to use
emit-ffifor a crate, then translate the
cd ~/path/to/project/ cargo +nightly ffi
The header will be output in the current working directory, named after the crate.
No command line options or configuration files are currently supported.
example/ for a test crate with generated headers already made.
emit-ffi driver links to the
rustc compiler internals and requires a nightly compiler.
The latest compiler confirmed to work is:
rustc 1.34.0-nightly (4b1e39b7b 2019-02-05)
You can install nightly
rustup toolchain add nightly
To build this workspace using
cargo +nightly build
emit-ffi requires libraries from the sysroot to be in the parent directory:
cp -R ~/.rustup/toolchains/nightly-x86_64-apple-darwin/lib target/ set -gx DYLD_LIBRARY_PATH '~/.rustup/toolchains/nightly-x86_64-apple-darwin/lib'
Be sure to re-run that command after deleting the target directory, such as after
In order to use
cargo-ffi must be in the current
cd ~/Projects/rust-ffi/ cargo +nightly build cp -R ~/.rustup/toolchains/nightly-x86_64-apple-darwin/lib target/ export PATH='$PWD/target/debug/':$PATH cd example/easy cargo +nightly ffi
Do not use
cargo-ffi in the
rust-ffi directory or you will need to rebuild.
cargo-ffi will perform a clean before generating a header to ensure the shim driver will be used, this will remove the
emit-ffi binary before it can be used.
This is currently just a proof of concept.
The significant work items remaining are:
enum's containing fields
- Zero sized types
- Opaque type propagation
- Documentation comments