Skip to content

Commit 4e6b2c8

Browse files
authored
refactor(capi): Move CAPI generation to its own crate (#234)
* refactor(capi): Move CAPI generation to its own crate * reduce Biscuit visibility * remove capi.rs * add `print_block_source` c binding * add test for `biscuit_print_block_source` + use `inttypes.h` macros for handling cross-platform `uint64_t` type * remove `block()` usage and C-API usage
1 parent bf2f22a commit 4e6b2c8

File tree

13 files changed

+425
-357
lines changed

13 files changed

+425
-357
lines changed

.github/workflows/release.yml

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Release
33
on:
44
push:
55
tags:
6-
- '*'
6+
- "*"
77

88
permissions:
99
contents: write
@@ -12,43 +12,34 @@ env:
1212
CARGO_TERM_COLOR: always
1313

1414
jobs:
15-
1615
build:
17-
runs-on: ubuntu-latest
16+
runs-on: ${{ matrix.os }}
17+
strategy:
18+
matrix:
19+
include:
20+
- os: macos-latest-large
21+
target_arch: x86_64-apple-darwin
22+
- os: macos-latest
23+
target_arch: aarch64-apple-darwin
24+
- os: ubuntu-latest
25+
target_arch: x86_64-unknown-linux-gnu
1826

1927
steps:
20-
- name: Checkout sources
21-
uses: actions/checkout@v2
22-
- name: Cache
23-
uses: actions/cache@v2
24-
with:
25-
path: |
26-
~/.cargo
27-
target/
28-
key: ${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
29-
restore-keys: |
30-
${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
31-
${{ runner.os }}-cargoc-
32-
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
33-
${{ runner.os }}-
34-
- name: Install cargo-c
35-
uses: actions-rs/cargo@v1
36-
with:
37-
command: install
38-
args: cargo-c
39-
- name: Run cargo-c tests
40-
uses: actions-rs/cargo@v1
41-
with:
42-
command: ctest
43-
args: --release
44-
- name: Build
45-
uses: actions-rs/cargo@v1
46-
with:
47-
command: cinstall
48-
args: --release --prefix=/usr --destdir=./build
49-
- name: Compress
50-
run: tar cvzf biscuit_c-${{github.ref_name}}-x86_64.tar.gz -C build/ .
51-
- name: Release
52-
uses: softprops/action-gh-release@v1
53-
with:
54-
files: biscuit_c-${{github.ref_name}}-x86_64.tar.gz
28+
- name: Checkout sources
29+
uses: actions/checkout@v4
30+
- uses: actions-rust-lang/setup-rust-toolchain@v1
31+
with:
32+
target: ${{ matrix.target_arch }}
33+
- name: Install cargo-c
34+
run: cargo install cargo-c --version 0.10.5+cargo-0.83.0
35+
- name: Run cargo-c tests
36+
run: cargo ctest --release --features="capi"
37+
if: matrix.os == 'ubuntu-latest' && matrix.target_arch == 'x86_64-unknown-linux-gnu'
38+
- name: Build C-API
39+
run: cargo cinstall --release --prefix=/usr --destdir=./build --features="capi"
40+
- name: Compress
41+
run: tar cvzf biscuit_capi-${{github.ref_name}}-${{matrix.target_arch}}.tar.gz -C build/ .
42+
- name: Release
43+
uses: softprops/action-gh-release@v1
44+
with:
45+
files: biscuit_capi-${{github.ref_name}}-${{matrix.target_arch}}.tar.gz

.github/workflows/rust.yml

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,50 @@ name: Rust
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main, v5 ]
7+
branches: [main, v5]
88

99
env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
1313
build:
14-
1514
runs-on: ubuntu-latest
1615

1716
steps:
18-
- uses: actions/checkout@v2
19-
- name: Cache
20-
uses: actions/cache@v2
21-
with:
22-
path: |
23-
~/.cargo
24-
target/
25-
key: ${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
26-
restore-keys: |
27-
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
28-
${{ runner.os }}-
29-
- name: Build
30-
run: cargo build --verbose
31-
- name: Run tests
32-
run: cargo test --features="serde-error,bwk" --verbose
33-
- name: Check samples
34-
run: |
35-
cd biscuit-auth
36-
cargo run --release --example testcases --features serde-error -- ./samples --json > ./samples/samples.json
37-
git diff --exit-code
38-
17+
- uses: actions/checkout@v2
18+
- name: Cache
19+
uses: actions/cache@v2
20+
with:
21+
path: |
22+
~/.cargo
23+
target/
24+
key: ${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
25+
restore-keys: |
26+
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
27+
${{ runner.os }}-
28+
- name: Build
29+
run: cargo build --verbose
30+
- name: Run tests
31+
run: cargo test --features="serde-error,bwk" --verbose
32+
- name: Check samples
33+
run: |
34+
cd biscuit-auth
35+
cargo run --release --example testcases --features serde-error -- ./samples --json > ./samples/samples.json
36+
git diff --exit-code
3937
4038
capi:
4139
runs-on: ubuntu-latest
4240

4341
steps:
44-
- name: Checkout sources
45-
uses: actions/checkout@v2
46-
- name: Cache
47-
uses: actions/cache@v2
48-
with:
49-
path: |
50-
~/.cargo
51-
target/
52-
key: ${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
53-
restore-keys: |
54-
${{ runner.os }}-cargoc-${{ hashFiles('Cargo.toml') }}
55-
${{ runner.os }}-cargoc-
56-
${{ runner.os }}-${{ hashFiles('Cargo.toml') }}
57-
${{ runner.os }}-
58-
- name: Install cargo-c
59-
uses: actions-rs/cargo@v1
60-
with:
61-
command: install
62-
args: cargo-c --version 0.9.14+cargo-0.66
63-
- name: Run cargo-c tests
64-
uses: actions-rs/cargo@v1
65-
with:
66-
command: ctest
67-
args: --release
42+
- name: Checkout sources
43+
uses: actions/checkout@v4
44+
- uses: actions-rust-lang/setup-rust-toolchain@v1
45+
- name: Install cargo-c
46+
run: cargo install cargo-c --version 0.10.5+cargo-0.83.0
47+
- name: Run cargo-c tests
48+
run: cargo ctest --features="capi" --release
6849

6950
coverage:
7051
name: Coverage
@@ -94,4 +75,4 @@ jobs:
9475
uses: codecov/[email protected]
9576
with:
9677
token: ${{ secrets.CODECOV_TOKEN }}
97-
slug: biscuit-auth/biscuit-rust
78+
slug: biscuit-auth/biscuit-rust

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[workspace]
2-
members = ["biscuit-auth", "biscuit-quote", "biscuit-parser"]
2+
members = ["biscuit-auth", "biscuit-quote", "biscuit-parser", "biscuit-capi"]

README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,7 @@ biscuit implementations come with a default symbol table to avoid transmitting f
172172

173173
# C bindings
174174

175-
This project can generate C bindings with [cargo-c](https://crates.io/crates/cargo-c).
176-
177-
compile it with:
178-
179-
```
180-
cargo cinstall --prefix=/usr --destdir=./build
181-
```
182-
183-
Run C integration tests with:
184-
185-
```
186-
cargo ctest
187-
```
175+
You can find the C bindings and documentation in the [`biscuit-capi`](./biscuit-capi/README.md) crate.
188176

189177
## License
190178

biscuit-auth/Cargo.toml

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ repository = "https://github.com/biscuit-auth/biscuit-rust"
1212

1313
[features]
1414
default = ["regex-full", "datalog-macro"]
15-
regex-full = [ "regex/perf", "regex/unicode"]
16-
# used by cargo-c to signal the compilation of C bindings
17-
capi = ["inline-c"]
15+
regex-full = ["regex/perf", "regex/unicode"]
1816
wasm = ["wasm-bindgen", "getrandom/wasm-bindgen"]
1917
# used by biscuit-wasm to serialize errors to JSON
2018
serde-error = ["serde", "biscuit-parser/serde-error"]
@@ -33,12 +31,11 @@ sha2 = "^0.9"
3331
prost = "0.10"
3432
prost-types = "0.10"
3533
regex = { version = "1.5", default-features = false, features = ["std"] }
36-
nom = {version = "7", default-features = false, features = ["std"] }
34+
nom = { version = "7", default-features = false, features = ["std"] }
3735
hex = "0.4"
3836
zeroize = { version = "1", default-features = false }
3937
thiserror = "1"
4038
rand = { version = "0.8" }
41-
inline-c = { version = "0.1", optional = true }
4239
wasm-bindgen = { version = "0.2", optional = true }
4340
base64 = "0.13.0"
4441
ed25519-dalek = { version = "2.0.0", features = ["rand_core", "zeroize"] }
@@ -48,7 +45,9 @@ time = { version = "0.3.7", features = ["formatting", "parsing"] }
4845
uuid = { version = "1", optional = true }
4946
biscuit-parser = { version = "0.1.2", path = "../biscuit-parser" }
5047
biscuit-quote = { version = "0.2.2", optional = true, path = "../biscuit-quote" }
51-
chrono = { version = "0.4.26", optional = true, default-features = false, features = ["serde"] }
48+
chrono = { version = "0.4.26", optional = true, default-features = false, features = [
49+
"serde",
50+
] }
5251

5352

5453
[dev-dependencies]
@@ -64,23 +63,6 @@ codspeed-bencher-compat = "2.6.0"
6463
#[build-dependencies]
6564
#prost-build = "0.10"
6665

67-
[package.metadata.capi.library]
68-
# Used as the library name and defaults to the crate name. This might get
69-
# prefixed with `lib` depending on the target platform.
70-
name = "biscuit_auth"
71-
72-
include = [
73-
"Cargo.toml",
74-
"cbindgen.toml",
75-
"build.rs",
76-
"examples/*.rs",
77-
"LICENSE",
78-
"README.md",
79-
"src/*.rs",
80-
"src/*/*.rs",
81-
"tests/*.rs"
82-
]
83-
8466
[[example]]
8567
name = "testcases"
8668
required-features = ["serde-error"]

biscuit-auth/src/datalog/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl SymbolTable {
206206
}
207207
}
208208
}
209+
209210
pub fn print_fact(&self, f: &Fact) -> String {
210211
self.print_predicate(&f.predicate)
211212
}

biscuit-auth/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,6 @@ pub use token::Biscuit;
235235
pub use token::RootKeyProvider;
236236
pub use token::{ThirdPartyBlock, ThirdPartyRequest};
237237

238-
#[cfg(cargo_c)]
239-
mod capi;
240-
241-
#[cfg(cargo_c)]
242-
pub use capi::*;
243-
244238
#[cfg(feature = "bwk")]
245239
mod bwk;
246240
#[cfg(feature = "bwk")]

biscuit-capi/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "biscuit-capi"
3+
version = "5.0.0" # Should keep the same version as biscuit-auth
4+
description = "C API for Biscuit"
5+
authors = ["Geoffroy Couprie <[email protected]>"]
6+
edition = "2018"
7+
license = "Apache-2.0"
8+
documentation = "https://docs.rs/biscuit-auth"
9+
homepage = "https://github.com/biscuit-auth/biscuit"
10+
repository = "https://github.com/biscuit-auth/biscuit-rust"
11+
12+
[dependencies]
13+
biscuit-auth = { version = "5.0.0", path = "../biscuit-auth", features = [
14+
"datalog-macro",
15+
"serde-error",
16+
] }
17+
inline-c = { version = "0.1", optional = true }
18+
rand = "0.8"
19+
20+
[features]
21+
default = []
22+
capi = ["inline-c"]
23+
24+
[package.metadata.capi.library]
25+
# Used as the library name and defaults to the crate name. This might get
26+
# prefixed with `lib` depending on the target platform.
27+
name = "biscuit_auth"
28+
29+
[package.metadata.capi.header]
30+
name = "biscuit_auth"

0 commit comments

Comments
 (0)