Skip to content

Commit 3ae1581

Browse files
committed
feat(ffi): Initial C API for hyper
1 parent 8861f9a commit 3ae1581

22 files changed

+2910
-14
lines changed

.github/workflows/CI.yml

+48-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
- style
1717
- test
1818
- features
19+
- ffi
1920
- doc
2021
steps:
2122
- run: exit 0
@@ -111,7 +112,53 @@ jobs:
111112
run: cargo install cargo-hack
112113

113114
- name: check --each-feature
114-
run: cargo hack check --each-feature -Z avoid-dev-deps
115+
run: cargo hack check --each-feature --skip ffi -Z avoid-dev-deps
116+
117+
ffi:
118+
name: Test C API (FFI)
119+
needs: [style]
120+
121+
runs-on: ubuntu-latest
122+
123+
steps:
124+
- name: Checkout
125+
uses: actions/checkout@v1
126+
127+
- name: Install Rust
128+
uses: actions-rs/toolchain@v1
129+
with:
130+
profile: minimal
131+
toolchain: stable
132+
override: true
133+
134+
- name: Install cbindgen
135+
uses: actions-rs/cargo@v1
136+
with:
137+
command: install
138+
args: cbindgen
139+
140+
- name: Build FFI
141+
uses: actions-rs/cargo@v1
142+
env:
143+
RUSTFLAGS: --cfg hyper_unstable_ffi
144+
with:
145+
command: build
146+
args: --features client,http1,http2,ffi
147+
148+
# TODO: re-enable check once figuring out how to get it working in CI
149+
# - name: Verify cbindgen
150+
# run: ./capi/gen_header.sh --verify
151+
152+
- name: Make Examples
153+
run: cd capi/examples && make client
154+
155+
- name: Run FFI unit tests
156+
uses: actions-rs/cargo@v1
157+
env:
158+
RUSTFLAGS: --cfg hyper_unstable_ffi
159+
with:
160+
command: test
161+
args: --features full,ffi --lib
115162

116163
doc:
117164
name: Build docs

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/target
2-
/Cargo.lock
1+
target
2+
Cargo.lock

Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ include = [
1919
#"build.rs",
2020
]
2121

22+
[lib]
23+
crate-type = ["lib", "staticlib", "cdylib"]
24+
2225
[dependencies]
2326
bytes = "1"
2427
futures-core = { version = "0.3", default-features = false }
@@ -38,6 +41,7 @@ want = "0.3"
3841

3942
# Optional
4043

44+
libc = { version = "0.2", optional = true }
4145
socket2 = { version = "0.3.16", optional = true }
4246

4347
[dev-dependencies]
@@ -94,7 +98,6 @@ server = []
9498
stream = []
9599

96100
# Tokio support
97-
98101
runtime = [
99102
"tcp",
100103
"tokio/rt",
@@ -106,6 +109,9 @@ tcp = [
106109
"tokio/time",
107110
]
108111

112+
# C-API support (currently unstable (no semver))
113+
ffi = ["libc"]
114+
109115
# internal features used in CI
110116
nightly = []
111117
__internal_happy_eyeballs_tests = []

capi/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# C API for hyper
2+
3+
This provides auxiliary pieces for a C API to use the hyper library.
4+
5+
## Unstable
6+
7+
The C API of hyper is currently **unstable**, which means it's not part of the semver contract as the rest of the Rust API is.
8+
9+
Because of that, it's only accessible if `--cfg hyper_unstable_ffi` is passed to `rustc` when compiling. The easiest way to do that is setting the `RUSTFLAGS` environment variable.
10+
11+
## Building
12+
13+
The C API is part of the Rust library, but isn't compiled by default. Using `cargo`, it can be compiled with the following command:
14+
15+
```
16+
RUSTFLAGS="--cfg hyper_unstable_ffi" cargo build --features client,http1,http2,ffi
17+
```

capi/cbindgen.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language = "C"
2+
include_guard = "_HYPER_H"
3+
no_includes = true
4+
sys_includes = ["stdint.h", "stddef.h"]
5+
cpp_compat = true
6+
documentation_style = "c"
7+
8+
[parse.expand]
9+
crates = ["hyper-capi"]
10+
11+
[export.rename]
12+
"Exec" = "hyper_executor"
13+
"Io" = "hyper_io"
14+
"Task" = "hyper_task"

capi/examples/Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Build the example client
3+
#
4+
5+
TARGET = client
6+
7+
OBJS = client.o
8+
9+
RPATH=$(PWD)/../../target/debug
10+
CFLAGS = -I../include
11+
LDFLAGS = -L$(RPATH) -Wl,-rpath,$(RPATH)
12+
LIBS = -lhyper
13+
14+
$(TARGET): $(OBJS)
15+
$(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LIBS)
16+
17+
upload: upload.o
18+
$(CC) -o upload upload.o $(LDFLAGS) $(LIBS)
19+
20+
clean:
21+
rm -f $(OBJS) $(TARGET)
22+
rm -f upload upload.o

0 commit comments

Comments
 (0)