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 toolsemit-ffi
: A rust compiler driver that will ouput affi.json
for a cratec-ast
: AC/C++
abstract syntax tree designed for code generationc-ffi
: A library to convert affi.json
into aC/C++
AST and output it as a headercargo-ffi
: A cargo subcommand to useemit-ffi
for a crate, then translate theffi.json
into aC/C++
header
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.
See example/
for a test crate with generated headers already made.
The 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 rustc
using rustup
:
rustup toolchain add nightly
To build this workspace using rustup
:
cargo +nightly build
Additionally, 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 cargo clean
.
In order to use cargo ffi
, cargo-ffi
must be in the current $PATH
:
export PATH='path/to/rust-ffi/target/debug/':$PATH
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:
C/C++
output forenum
's containing fields- Generics
- Statics
- Zero sized types
repr(transparent)
- Opaque type propagation
fn
ABI specifications- Documentation comments
- Configuration/extensibility
- Tests