Skip to content

Commit c67ad2a

Browse files
committed
Go/Core Refactore: Move FFI to a dedicated folder to be reusable for other wrappers
Signed-off-by: barshaul <[email protected]>
1 parent bd36cfa commit c67ad2a

13 files changed

+133
-56
lines changed

.github/workflows/ffi.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Glide FFI CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- release-*
8+
- v*
9+
paths:
10+
- glide-core/src/**
11+
- glide-core/redis-rs/redis/src/**
12+
- ffi/**
13+
pull_request:
14+
paths:
15+
- glide-core/src/**
16+
- glide-core/redis-rs/redis/src/**
17+
- ffi/**
18+
workflow_call:
19+
20+
concurrency:
21+
group: ffi-${{ github.head_ref || github.ref }}-${{ toJson(inputs) }}
22+
cancel-in-progress: true
23+
24+
env:
25+
CARGO_TERM_COLOR: always
26+
27+
jobs:
28+
build-and-gen-c:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v4
32+
- name: Install Rust and protoc
33+
uses: ./.github/workflows/install-rust-and-protoc
34+
with:
35+
github-token: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Build the FFI library
38+
working-directory: ffi
39+
run: |
40+
cargo build --release
41+
- name: Generate the C header file
42+
working-directory: ffi
43+
run: |
44+
cargo install cbindgen
45+
cbindgen --config cbindgen.toml --crate glide-ffi --output lib.h --lang c

.github/workflows/go-cd.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,14 @@ jobs:
119119
working-directory: go
120120
run: |
121121
make install-build-tools
122-
make build-glide-client
123-
# TODO: move generation of protobuf and header file to a non-matrix step
124-
make generate-protobuf
122+
make build
125123
- name: Upload artifacts
126124
continue-on-error: true
127125
uses: actions/upload-artifact@v4
128126
with:
129127
name: ${{ matrix.host.TARGET }}
130128
path: |
131-
go/target/release/libglide_rs.a
129+
ffi/target/release/libglide_ffi.a
132130
go/lib.h
133131
go/protobuf/
134132
@@ -149,7 +147,7 @@ jobs:
149147
for dir in */; do
150148
target_name=${dir%/}
151149
mkdir -p $GITHUB_WORKSPACE/go/rustbin/${target_name}
152-
cp ${target_name}/target/release/libglide_rs.a $GITHUB_WORKSPACE/go/rustbin/${target_name}/
150+
cp ${target_name}/target/release/libglide_ffi.a $GITHUB_WORKSPACE/go/rustbin/${target_name}/
153151
done
154152
# TODO: move generation of protobuf and header file to a non-matrix step
155153
cd x86_64-unknown-linux-gnu

.github/workflows/go.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
paths:
1010
- glide-core/src/**
1111
- glide-core/redis-rs/redis/src/**
12+
- ffi/src/**
1213
- utils/cluster_manager.py
1314
- go/**
1415
- .github/workflows/go.yml
@@ -22,6 +23,7 @@ on:
2223
paths:
2324
- glide-core/src/**
2425
- glide-core/redis-rs/redis/src/**
26+
- ffi/src/**
2527
- utils/cluster_manager.py
2628
- go/**
2729
- .github/workflows/go.yml
@@ -133,11 +135,6 @@ jobs:
133135
steps:
134136
- uses: actions/checkout@v4
135137

136-
- uses: ./.github/workflows/lint-rust
137-
with:
138-
cargo-toml-folder: go
139-
github-token: ${{ secrets.GITHUB_TOKEN }}
140-
141138
- name: Set up Go ${{ matrix.go }}
142139
uses: actions/setup-go@v5
143140
with:

.github/workflows/rust.yml

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
- logger_core/**
1111
- glide-core/**
1212
- glide-core/redis-rs/redis/src/**
13+
- ffi/**
1314
- utils/cluster_manager.py
1415
- .github/workflows/rust.yml
1516
- .github/workflows/install-shared-dependencies/action.yml
@@ -23,6 +24,7 @@ on:
2324
- logger_core/**
2425
- glide-core/**
2526
- glide-core/redis-rs/redis/src/**
27+
- ffi/**
2628
- utils/cluster_manager.py
2729
- .github/workflows/rust.yml
2830
- .github/workflows/install-shared-dependencies/action.yml
@@ -127,6 +129,12 @@ jobs:
127129
github-token: ${{ secrets.GITHUB_TOKEN }}
128130
name: lint glide-core
129131

132+
- uses: ./.github/workflows/lint-rust
133+
with:
134+
cargo-toml-folder: ./ffi
135+
github-token: ${{ secrets.GITHUB_TOKEN }}
136+
name: lint glide-ffi
137+
130138
- uses: ./.github/workflows/lint-rust
131139
with:
132140
cargo-toml-folder: ./logger_core

go/.cargo/config.toml ffi/.cargo/config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[env]
2-
GLIDE_NAME = { value = "GlideGo", force = true }
2+
GLIDE_NAME = { value = "GlideFFI", force = true }
33
GLIDE_VERSION = "0.1.0"
44
# Suppress error
55
# > ... was built for newer 'macOS' version (14.5) than being linked (14.0)

ffi/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# cbindgen generated header file
2+
lib.h

go/Cargo.toml ffi/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "glide-rs"
2+
name = "glide-ffi"
33
version = "0.1.0"
44
edition = "2021"
55
license = "Apache-2.0"
@@ -9,10 +9,10 @@ authors = ["Valkey GLIDE Maintainers"]
99
crate-type = ["staticlib"]
1010

1111
[dependencies]
12-
redis = { path = "../glide-core/redis-rs/redis", features = ["aio", "tokio-comp", "connection-manager", "tokio-rustls-comp"] }
12+
protobuf = { version = "3", features = [] }
13+
redis = { path = "../glide-core/redis-rs/redis", features = ["aio", "tokio-comp", "tokio-rustls-comp"] }
1314
glide-core = { path = "../glide-core", features = ["proto"] }
1415
tokio = { version = "^1", features = ["rt", "macros", "rt-multi-thread", "time"] }
15-
protobuf = { version = "3.3.0", features = [] }
1616

1717
[profile.release]
1818
lto = true

ffi/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# glide_ffi
2+
3+
## Overview
4+
5+
The glide_ffi crate provides a C-compatible Foreign Function Interface (FFI) for interacting with `glide-core`. It serves as a bridge for internal wrappers written in other languages, such as Go, to integrate seamlessly with `glide-core`.
6+
7+
## Structure
8+
• src/lib.rs: Defines the FFI interface.
9+
• cbindgen.toml: Configuration for generating the C header file (lib.h).
10+
• Cargo.toml: Manages dependencies, including glide-core.
11+
12+
## Building the Library
13+
14+
To build the FFI library:
15+
```
16+
cargo build --release
17+
```
18+
19+
To generate the C header file:
20+
```
21+
cargo install cbindgen
22+
cbindgen --config cbindgen.toml --crate glide-ffi --output lib.h --lang c
23+
```
24+
25+
## Running the linters
26+
27+
```bash
28+
rustup component add clippy rustfmt
29+
cargo clippy --all-features --all-targets -- -D warnings
30+
cargo fmt --manifest-path ./Cargo.toml --all
31+
```

go/cbindgen.toml ffi/cbindgen.toml

File renamed without changes.

go/src/lib.rs ffi/src/lib.rs

File renamed without changes.

go/DEVELOPER.md

+7-22
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,18 @@ Development on the Go wrapper may involve changes in either the Go or Rust code.
202202
- staticcheck
203203
- golines
204204

205-
**Rust:**
206-
207-
- clippy
208-
- fmt
209-
210205
#### Running the linters
211206

212207
Run from the main `/go` folder
213208

214-
1. Go
215-
216-
```bash
217-
# For go1.22:
218-
make install-dev-tools
219-
# For go1.22:
220-
make install-dev-tools-go1.22
221-
222-
make lint
223-
```
224-
225-
2. Rust
209+
```bash
210+
# For go1.22:
211+
make install-dev-tools
212+
# For go1.22:
213+
make install-dev-tools-go1.22
226214
227-
```bash
228-
rustup component add clippy rustfmt
229-
cargo clippy --all-features --all-targets -- -D warnings
230-
cargo fmt --manifest-path ./Cargo.toml --all
231-
```
215+
make lint
216+
```
232217

233218
#### Fixing lint formatting errors
234219

go/Makefile

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
22

33
SHELL:=/bin/bash
4+
GO_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
5+
ROOT_FOLDER := $(abspath $(GO_DIR)/..)
6+
GLIDE_CORE_FOLDER := $(ROOT_FOLDER)/glide-core
7+
GLIDE_FFI_FOLDER := $(ROOT_FOLDER)/ffi
48

59
install-build-tools:
610
go install google.golang.org/protobuf/cmd/[email protected]
@@ -42,51 +46,58 @@ clean:
4246
rm -rf target
4347
rm -rf reports
4448

45-
build-glide-client:
46-
cargo build --release
47-
cbindgen --config cbindgen.toml --crate glide-rs --output lib.h --lang c
49+
build-glide-ffi:
50+
cd $(GLIDE_FFI_FOLDER) && cargo build $(BUILD_FLAGS)
51+
$(MAKE) gen-c-bindings
52+
53+
# Release build
54+
build-glide-client: BUILD_FLAGS=--release
55+
build-glide-client: build-glide-ffi
56+
57+
# Debug build (no --release flag)
58+
build-glide-client-debug: BUILD_FLAGS=
59+
build-glide-client-debug: build-glide-ffi
60+
61+
gen-c-bindings:
62+
cd $(GLIDE_FFI_FOLDER) && \
63+
cbindgen --config cbindgen.toml --crate glide-ffi --output $(GO_DIR)/lib.h --lang c
4864
@if [ "$$(uname)" = "Darwin" ]; then \
49-
strip -x target/release/libglide_rs.a; \
65+
strip -x $(GLIDE_FFI_FOLDER)/target/release/libglide_ffi.a; \
5066
if [ "$$(uname -m)" = "arm64" ]; then \
5167
echo "Platform: aarch64-apple-darwin"; \
5268
mkdir -p rustbin/aarch64-apple-darwin; \
53-
cp target/release/libglide_rs.a rustbin/aarch64-apple-darwin/; \
69+
cp $(GLIDE_FFI_FOLDER)/target/release/libglide_ffi.a rustbin/aarch64-apple-darwin/; \
5470
elif [ "$$(uname -m)" = "x86_64" ]; then \
5571
echo "Platform: x86_64-apple-darwin"; \
5672
mkdir -p rustbin/x86_64-apple-darwin; \
57-
cp target/release/libglide_rs.a rustbin/x86_64-apple-darwin/; \
73+
cp $(GLIDE_FFI_FOLDER)/target/release/libglide_ffi.a rustbin/x86_64-apple-darwin/; \
5874
fi; \
5975
elif [ "$$(uname)" = "Linux" ]; then \
60-
strip --strip-unneeded target/release/libglide_rs.a; \
76+
strip --strip-unneeded $(GLIDE_FFI_FOLDER)/target/release/libglide_ffi.a; \
6177
if [ "$$(uname -m)" = "arm64" ] || [ "$$(uname -m)" = "aarch64" ]; then \
6278
echo "Platform: aarch64-unknown-linux-gnu"; \
6379
mkdir -p rustbin/aarch64-unknown-linux-gnu; \
64-
cp target/release/libglide_rs.a rustbin/aarch64-unknown-linux-gnu/; \
80+
cp $(GLIDE_FFI_FOLDER)/target/release/libglide_ffi.a rustbin/aarch64-unknown-linux-gnu/; \
6581
elif [ "$$(uname -m)" = "x86_64" ]; then \
6682
echo "Platform: x86_64-unknown-linux-gnu"; \
6783
mkdir -p rustbin/x86_64-unknown-linux-gnu; \
68-
cp target/release/libglide_rs.a rustbin/x86_64-unknown-linux-gnu/; \
84+
cp $(GLIDE_FFI_FOLDER)/target/release/libglide_ffi.a rustbin/x86_64-unknown-linux-gnu/; \
6985
fi; \
7086
else \
7187
echo "Unsupported platform: $(shell uname), $(shell uname -m)"; \
7288
exit 1; \
7389
fi
7490

75-
76-
build-glide-client-debug:
77-
cargo build
78-
cbindgen --config cbindgen.toml --crate glide-rs --output lib.h --lang c
79-
8091
generate-protobuf:
8192
rm -rf protobuf
8293
mkdir -p protobuf
83-
protoc --proto_path=../glide-core/src/protobuf \
94+
protoc --proto_path=$(GLIDE_CORE_FOLDER)/src/protobuf \
8495
--go_opt=Mconnection_request.proto=github.com/valkey-io/valkey-glide/go/protobuf \
8596
--go_opt=Mcommand_request.proto=github.com/valkey-io/valkey-glide/go/protobuf \
8697
--go_opt=Mresponse.proto=github.com/valkey-io/valkey-glide/go/protobuf \
8798
--go_out=./protobuf \
8899
--go_opt=paths=source_relative \
89-
../glide-core/src/protobuf/*.proto
100+
$(GLIDE_CORE_FOLDER)/src/protobuf/*.proto
90101

91102
lint:
92103
go vet ./...
@@ -116,7 +127,7 @@ format:
116127
unit-test:
117128
mkdir -p reports
118129
set -o pipefail; \
119-
LD_LIBRARY_PATH=$(shell find . -name libglide_rs.a|grep -w release|tail -1|xargs dirname|xargs readlink -f):${LD_LIBRARY_PATH} \
130+
LD_LIBRARY_PATH=$(shell find . -name libglide_ffi.a|grep -w release|tail -1|xargs dirname|xargs readlink -f):${LD_LIBRARY_PATH} \
120131
go test -v ./... -skip 'Example|TestGlideTestSuite' -run Test $(if $(test-filter), -run $(test-filter)) \
121132
| tee >(go tool test2json -t -p github.com/valkey-io/valkey-glide/go/utils | go-test-report -o reports/unit-tests.html -t unit-test > /dev/null)
122133

@@ -131,7 +142,7 @@ example-test:
131142
CLUSTER_OUTPUT=$$(python3 ../utils/cluster_manager.py start --cluster-mode); \
132143
GLIDE_CLUSTER_NODES=$$(echo "$$CLUSTER_OUTPUT" | grep 'CLUSTER_NODES=' | cut -d'=' -f2); \
133144
echo "GLIDE_CLUSTER_NODES=$$GLIDE_CLUSTER_NODES"; \
134-
LD_LIBRARY_PATH=$(shell find . -name libglide_rs.a|grep -w release|tail -1|xargs dirname|xargs readlink -f):${LD_LIBRARY_PATH} \
145+
LD_LIBRARY_PATH=$(shell find . -name libglide_ffi.a|grep -w release|tail -1|xargs dirname|xargs readlink -f):${LD_LIBRARY_PATH} \
135146
go test -v ./api -skip Test $(if $(test-filter), -run $(test-filter)) -clusternodes $$GLIDE_CLUSTER_NODES -standalonenode $$GLIDE_NODE \
136147
| tee >(go tool test2json -t -p github.com/valkey-io/valkey-glide/go/api \
137148
| go-test-report -o reports/example-tests.html -t example-test > /dev/null)
@@ -147,7 +158,7 @@ modules-test: __it
147158
__it:
148159
mkdir -p reports
149160
set -o pipefail; \
150-
LD_LIBRARY_PATH=$(shell find . -name libglide_rs.a|grep -w release|tail -1|xargs dirname|xargs readlink -f):${LD_LIBRARY_PATH} \
161+
LD_LIBRARY_PATH=$(shell find . -name libglide_ffi.a|grep -w release|tail -1|xargs dirname|xargs readlink -f):${LD_LIBRARY_PATH} \
151162
go test -v ./integTest/... \
152163
$(TEST_FILTER) \
153164
$(if $(filter true, $(tls)), --tls,) \

go/api/base_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package api
44

5-
// #cgo LDFLAGS: -lglide_rs
5+
// #cgo LDFLAGS: -lglide_ffi
66
// #cgo !windows LDFLAGS: -lm
77
// #cgo darwin LDFLAGS: -framework Security
88
// #cgo darwin,amd64 LDFLAGS: -framework CoreFoundation

0 commit comments

Comments
 (0)