Skip to content

Commit 6284347

Browse files
committed
initial
Signed-off-by: Mikhail Kot <to@myrrc.dev>
1 parent d0a6dba commit 6284347

8 files changed

Lines changed: 370 additions & 252 deletions

File tree

vortex-ffi/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# SPDX-FileCopyrightText: Copyright the Vortex contributors
33
cmake_minimum_required(VERSION 3.10)
44

5+
include(FetchContent)
6+
57
project(VortexFFI
68
VERSION 0.0.1
79
LANGUAGES C)
@@ -10,6 +12,7 @@ set(CMAKE_C_STANDARD 17)
1012
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -Wpedantic")
1113

1214
option(BUILD_TESTS "Build tests" OFF)
15+
option(BUILD_EXAMPLES "Build examples" OFF)
1316

1417
set(SANITIZER "" CACHE STRING "Build with sanitizers")
1518
set(TARGET_TRIPLE "" CACHE STRING "Rust target triple for FFI library")
@@ -95,6 +98,15 @@ set_target_properties(vortex_ffi_shared PROPERTIES
9598
INTERFACE_LINK_OPTIONS "LINKER:-rpath,${LIBRARY_DIR}"
9699
)
97100

101+
if (BUILD_TESTS OR BUILD_EXAMPLES)
102+
FetchContent_Declare(
103+
Nanoarrow
104+
GIT_REPOSITORY https://github.com/apache/arrow-nanoarrow
105+
GIT_TAG apache-arrow-nanoarrow-0.8.0
106+
)
107+
FetchContent_MakeAvailable(Nanoarrow)
108+
endif()
109+
98110
if (BUILD_TESTS)
99111
enable_language(CXX)
100112
set(CMAKE_CXX_STANDARD 20)
@@ -103,3 +115,7 @@ if (BUILD_TESTS)
103115
enable_testing()
104116
add_subdirectory(test)
105117
endif()
118+
119+
if (BUILD_EXAMPLES)
120+
add_subdirectory(examples)
121+
endif()

vortex-ffi/README.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
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:
3234
cargo +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

4042
Build the test library
4143

@@ -52,7 +54,7 @@ ctest --test-dir build -j $(nproc)
5254

5355
You 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

5759
AddressSanitizer:
5860

@@ -93,7 +95,7 @@ with sanitizers.
9395
leaks.
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

98100
1. 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+
```

vortex-ffi/examples/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: CC-BY-4.0
2+
# SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
cmake_policy(SET CMP0079 NEW)
4+
add_executable(vortex_scan scan.c)
5+
target_link_libraries(vortex_scan PRIVATE vortex_ffi_shared)

vortex-ffi/examples/Makefile

Lines changed: 0 additions & 75 deletions
This file was deleted.

vortex-ffi/examples/README.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

vortex-ffi/examples/hello-vortex.c

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)