Skip to content

Commit b4e37a1

Browse files
authored
Begin to add Rust API (k2-fsa#3203)
This pull request introduces the initial Rust API for the sherpa-onnx project. It establishes a new sherpa-onnx Rust crate for safe wrappers and a sherpa-onnx-sys crate for raw FFI bindings to the underlying C API, along with an example project to demonstrate its usage. This initiative aims to provide maintained Rust support directly within the project, ensuring compatibility with the latest API and fostering broader adoption.
1 parent 6293cc7 commit b4e37a1

18 files changed

Lines changed: 478 additions & 2 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Publish Rust Crates
2+
3+
on:
4+
push:
5+
branches:
6+
- rust-api-2
7+
tags:
8+
- 'v[0-9]+.[0-9]+.[0-9]+*'
9+
10+
workflow_dispatch:
11+
12+
jobs:
13+
publish:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- uses: actions-rust-lang/setup-rust-toolchain@v1
19+
with:
20+
toolchain: stable
21+
22+
- name: Update
23+
shell: bash
24+
run: |
25+
cd sherpa-onnx/rust
26+
./publish.sh
27+
28+
- name: Login to crates.io
29+
run: cargo login ${{ secrets.CARGO_REGISTRY_TOKEN }}
30+
31+
- name: Publish sherpa-onnx-sys
32+
shell: bash
33+
env:
34+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
35+
run: |
36+
cargo publish --allow-dirty --manifest-path=sherpa-onnx/rust/sherpa-onnx-sys/Cargo.toml
37+
sleep 30 # Wait for crates.io to index
38+
39+
- name: Publish sherpa-onnx
40+
shell: bash
41+
env:
42+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
43+
run: |
44+
cargo publish --allow-dirty --manifest-path=sherpa-onnx/rust/sherpa-onnx/Cargo.toml
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Test rust package
2+
3+
on:
4+
push:
5+
branches:
6+
- rust-api
7+
workflow_dispatch:
8+
9+
concurrency:
10+
group: test-rust-package-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
test-rust-package:
15+
runs-on: ${{ matrix.os }}
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
os: [ubuntu-latest, macos-latest, macos-15-intel, ubuntu-22.04-arm]
20+
21+
env:
22+
# Placeholder, will be overwritten per OS
23+
SHERPA_ONNX_LIB_DIR: ""
24+
RUSTFLAGS: ""
25+
26+
steps:
27+
# Checkout the repository
28+
- uses: actions/checkout@v4
29+
30+
# Install Rust stable
31+
- uses: actions-rust-lang/setup-rust-toolchain@v1
32+
with:
33+
toolchain: stable
34+
35+
# Download prebuilt libraries depending on OS
36+
- name: Download prebuilt Sherpa-ONNX libraries
37+
shell: bash
38+
run: |
39+
SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
40+
41+
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
42+
d=sherpa-onnx-v$SHERPA_ONNX_VERSION-osx-universal2-shared
43+
elif [[ "${{ matrix.os }}" == "macos-15-intel" ]]; then
44+
d=sherpa-onnx-v$SHERPA_ONNX_VERSION-osx-universal2-shared
45+
elif [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
46+
d=sherpa-onnx-v$SHERPA_ONNX_VERSION-linux-x64-shared
47+
elif [[ "${{ matrix.os }}" == "ubuntu-22.04-arm" ]]; then
48+
d=sherpa-onnx-v$SHERPA_ONNX_VERSION-linux-aarch64-shared-cpu
49+
else
50+
echo "Unknown ${{ matrix.os }}"
51+
exit 1
52+
fi
53+
54+
LIB_URL="https://github.com/k2-fsa/sherpa-onnx/releases/download/v$SHERPA_ONNX_VERSION/$d.tar.bz2"
55+
56+
curl -SsL -O "$LIB_URL"
57+
tar -xvf $d.tar.bz2
58+
59+
ls -lh $d/lib
60+
61+
# Export environment variables for this step
62+
echo "SHERPA_ONNX_LIB_DIR=$PWD/$d/lib" >> $GITHUB_ENV
63+
echo "RUSTFLAGS=-C link-arg=-Wl,-rpath,$PWD/$d/lib" >> $GITHUB_ENV
64+
65+
- name: Show libs
66+
shell: bash
67+
run: |
68+
echo "SHERPA_ONNX_LIB_DIR: $SHERPA_ONNX_LIB_DIR"
69+
ls -lh $SHERPA_ONNX_LIB_DIR
70+
71+
echo "RUSTFLAGS: $RUSTFLAGS"
72+
73+
# Test using published crates.io dependencies
74+
- name: Test Published Crates
75+
shell: bash
76+
run: |
77+
cd rust-api-examples
78+
git checkout Cargo.toml
79+
cargo clean
80+
# cargo test --locked --all-features
81+
cargo run --example version

.github/workflows/test-rust.yaml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Test rust
2+
3+
on:
4+
push:
5+
branches:
6+
- rust-api
7+
workflow_dispatch:
8+
9+
concurrency:
10+
group: test-rust-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
test-rust:
15+
runs-on: ${{ matrix.os }}
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
os: [ubuntu-latest, macos-latest, macos-15-intel, ubuntu-22.04-arm]
20+
21+
env:
22+
# Placeholder, will be overwritten per OS
23+
SHERPA_ONNX_LIB_DIR: ""
24+
RUSTFLAGS: ""
25+
26+
steps:
27+
# Checkout the repository
28+
- uses: actions/checkout@v4
29+
30+
- name: ccache
31+
uses: hendrikmuhs/ccache-action@v1.2
32+
with:
33+
key: ${{ matrix.os }}-rust
34+
35+
- name: Update version
36+
shell: bash
37+
run: |
38+
./new-release.sh
39+
git diff .
40+
41+
- name: Configure sherpa-onnx
42+
shell: bash
43+
run: |
44+
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
45+
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
46+
cmake --version
47+
48+
mkdir build
49+
cd build
50+
cmake \
51+
-D SHERPA_ONNX_ENABLE_BINARY=OFF \
52+
-D BUILD_SHARED_LIBS=ON \
53+
-D CMAKE_INSTALL_PREFIX=./install \
54+
..
55+
56+
- name: Build sherpa-onnx
57+
shell: bash
58+
run: |
59+
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
60+
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
61+
cmake --version
62+
63+
cd build
64+
make -j2
65+
make install
66+
ls -lh install/lib
67+
68+
echo "SHERPA_ONNX_LIB_DIR=$PWD/install/lib" >> $GITHUB_ENV
69+
echo "RUSTFLAGS=-C link-arg=-Wl,-rpath,$PWD/install/lib" >> $GITHUB_ENV
70+
71+
# Install Rust stable
72+
- uses: actions-rust-lang/setup-rust-toolchain@v1
73+
with:
74+
toolchain: stable
75+
76+
- name: Show libs
77+
shell: bash
78+
run: |
79+
echo "SHERPA_ONNX_LIB_DIR: $SHERPA_ONNX_LIB_DIR"
80+
ls -lh $SHERPA_ONNX_LIB_DIR
81+
82+
echo "RUSTFLAGS: $RUSTFLAGS"
83+
84+
- name: Test locally
85+
shell: bash
86+
run: |
87+
cd rust-api-examples
88+
89+
sed -i.bak 's|^sherpa-onnx *=.*|sherpa-onnx = { path = "../sherpa-onnx/rust/sherpa-onnx" }|' Cargo.toml
90+
91+
92+
git diff .
93+
94+
cargo clean
95+
cargo run --example version

rust-api-examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

rust-api-examples/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust-api-examples/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "rust-api-examples"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
7+
[dependencies]
8+
sherpa-onnx = "0.1.0"
9+
# sherpa-onnx = { path = "../sherpa-onnx/rust/sherpa-onnx" }

rust-api-examples/README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
# Introduction
22

3-
Rust support is contributed and maintained by the community.
3+
This folder uses Rust API maintained by us.
44

5-
Please refer to https://github.com/thewh1teagle/sherpa-rs
5+
## Setup library path
6+
7+
### Method 1 (Build from source)
8+
9+
```bash
10+
export SHERPA_ONNX_LIB_DIR=/Users/fangjun/open-source/sherpa-onnx/build/install/lib
11+
export RUSTFLAGS="-C link-arg=-Wl,-rpath,$SHERPA_ONNX_LIB_DIR"
12+
```
13+
14+
### Method 2 (Download pre-built libs)
15+
16+
```bash
17+
# You can choose any directory you like
18+
cd $HOME/Downloads
19+
20+
# We use version v1.12.25 below as an example.
21+
# Please always use the latest version from
22+
# https://github.com/k2-fsa/sherpa-onnx/releases
23+
24+
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.25/sherpa-onnx-v1.12.25-osx-universal2-shared.tar.bz2
25+
tar xvf sherpa-onnx-v1.12.25-osx-universal2-shared.tar.bz2
26+
rm sherpa-onnx-v1.12.25-osx-universal2-shared.tar.bz2
27+
28+
export SHERPA_ONNX_LIB_DIR=$HOME/Downloads/sherpa-onnx-v1.12.25-osx-universal2-shared/lib
29+
export RUSTFLAGS="-C link-arg=-Wl,-rpath,$SHERPA_ONNX_LIB_DIR"
30+
```
31+
32+
## Run it
33+
34+
```bash
35+
cargo run --example version
36+
```
37+
38+
For macOS, you can run
39+
```
40+
otool -l target/debug/examples/version | grep -A2 LC_RPATH
41+
```
42+
to check the RPATH.
43+
44+
# Alternative rust bindings for sherpa-onnx
45+
46+
Please see also https://github.com/thewh1teagle/sherpa-rs
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use sherpa_onnx;
2+
3+
fn main() {
4+
println!("Version : {}", sherpa_onnx::version());
5+
println!("Git SHA1: {}", sherpa_onnx::git_sha1());
6+
println!("Git date: {}", sherpa_onnx::git_date());
7+
}

sherpa-onnx/rust/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
notes.md
2+
target

sherpa-onnx/rust/.rustfmt.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Put each method in a chain on its own line
2+
chain_width = 0
3+
4+
# Optional: make sure calls break vertically
5+
fn_call_width = 60
6+
7+
# Optional: control general line width
8+
max_width = 100

0 commit comments

Comments
 (0)