11# Foreign Function Interface
22
3- Vortex is a file format that can be used by any execution engine. Nearly every programming language supports
4- the C ABI (Application Binary Interface), so by providing an FFI interface to work with Vortex objects we can
5- make it easy to support a variety of languages.
3+ Vortex is a file format that can be used by any execution engine. Nearly every
4+ programming language supports the C ABI (Application Binary Interface), so by
5+ providing an FFI interface to work with Vortex objects we can make it easy to
6+ support a variety of languages.
67
7- Check out the [ ` examples ` ] ( ./examples/ ) directory to see an example of how to use the API to build
8- a real native application.
8+ Check out the [ ` examples ` ] ( ./examples/ ) directory to see an example of how to
9+ use scanning API from C to read vortex files.
10+ Full FFI API is documented in ` docs/api/c ` and in ` cinclude/vortex.h ` header.
911
10- ## Design
12+ ## Usage from a CMake project
1113
12- The FFI is designed to be very simple and follows a very object-oriented approach:
14+ 1 . Build .so/.dylib/.dll
1315
14- - ** Constructors** are simple C functions that return opaque pointers
15- - ** Methods** are functions that receive an opaque pointer as the first argument, followed by subsequent arguments.
16- Methods may return a value or void.
17- - ** Destructors** free native resources (allocations, file handles, network sockets) and must be explicitly called by
18- the foreign language to avoid leaking resources.
19-
20- Constructors will generally allocate rust memory, and destructors free that memory.
16+ ``` sh
17+ cargo build --release -p vortex-ffi
18+ ```
2119
22- ## Documentation
20+ 2 . Add shared or static library
2321
24- The FFI API is documented in ` docs/api/c ` with explicit inclusion of types, enums, and functions, etc. Note that an
25- item cannot be referenced in the documentation if it does not have a documentation comment.
22+ ``` cmake
23+ # in your CMakeLists.txt
24+ include_directory(vortex/vortex-ffi)
25+ target_link_libraries(my_target, vortex_ffi_shared)
26+ # or target_link_libraries(my_target, vortex_ffi)
27+ ```
2628
2729## Updating Headers
2830
@@ -32,10 +34,10 @@ To rebuild the header file:
3234cargo +nightly build -p vortex-ffi
3335```
3436
35- The header generation uses cbindgen's macro expansion feature which requires nightly.
36- Stable builds use the checked-in header file at ` cinclude/vortex.h ` .
37+ Header generation uses cbindgen's macro expansion feature which requires nightly.
38+ Stable builds use header file at ` cinclude/vortex.h ` .
3739
38- ### Testing C part
40+ ## Testing C part
3941
4042Build the test library
4143
@@ -52,7 +54,7 @@ ctest --test-dir build -j $(nproc)
5254
5355You would need C++ compiler toolchain to run the tests since they use Catch2.
5456
55- ### Testing Rust part with sanitizers
57+ ## Testing Rust part with sanitizers
5658
5759AddressSanitizer:
5860
@@ -93,7 +95,7 @@ with sanitizers.
9395leaks.
9496- If you want stack trace symbolization, install ` llvm-symbolizer ` .
9597
96- ### Testing Rust and C with sanitizers
98+ ## Testing Rust and C with sanitizers
9799
981001 . Build FFI library with external sanitizer runtime:
99101
@@ -114,3 +116,11 @@ cmake -Bbuild -DWITH_ASAN=1 -DTARGET_TRIPLE=<target triple>
114116``` sh
115117./build/test/vortex_ffi_test 2>& 1 | rustfilt -i-
116118```
119+
120+ ## Running C example
121+
122+ ``` sh
123+ cmake -Bbuild -DBUILD_EXAMPLES=1
124+ cmake --build build
125+ ./build/examples/vortex_scan
126+ ```
0 commit comments