Skip to content

Commit 15e2648

Browse files
authored
Test Rust API on Windows (k2-fsa#3385)
1 parent b599d0d commit 15e2648

16 files changed

Lines changed: 226 additions & 24 deletions

File tree

.github/workflows/test-rust-package.yaml

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ concurrency:
1212

1313
jobs:
1414
test-rust-package:
15+
name: Test ${{ matrix.os }}
1516
runs-on: ${{ matrix.os }}
1617
strategy:
1718
fail-fast: false
1819
matrix:
19-
os: [ubuntu-latest, macos-latest, macos-15-intel, ubuntu-22.04-arm]
20+
os: [ubuntu-latest, macos-latest, macos-15-intel, ubuntu-22.04-arm, windows-latest]
2021

2122
env:
2223
# Placeholder, will be overwritten per OS
@@ -32,6 +33,22 @@ jobs:
3233
with:
3334
toolchain: stable
3435

36+
- name: Set up MSVC
37+
if: matrix.os == 'windows-latest'
38+
uses: ilammy/msvc-dev-cmd@v1
39+
40+
- name: Select MSVC linker
41+
if: matrix.os == 'windows-latest'
42+
shell: pwsh
43+
run: |
44+
$linker = Join-Path $env:VCToolsInstallDir 'bin\HostX64\x64\link.exe'
45+
if (-not (Test-Path $linker)) {
46+
throw "MSVC linker not found: $linker"
47+
}
48+
"CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER=$linker" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
49+
Write-Host "Using linker: $linker"
50+
& where.exe link
51+
3552
# Download prebuilt libraries depending on OS
3653
- name: Download prebuilt Sherpa-ONNX libraries
3754
shell: bash
@@ -46,6 +63,8 @@ jobs:
4663
d=sherpa-onnx-v$SHERPA_ONNX_VERSION-linux-x64-shared
4764
elif [[ "${{ matrix.os }}" == "ubuntu-22.04-arm" ]]; then
4865
d=sherpa-onnx-v$SHERPA_ONNX_VERSION-linux-aarch64-shared-cpu
66+
elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
67+
d=sherpa-onnx-v$SHERPA_ONNX_VERSION-win-x64-shared-MT-Release
4968
else
5069
echo "Unknown ${{ matrix.os }}"
5170
exit 1
@@ -60,7 +79,16 @@ jobs:
6079
6180
# Export environment variables for this step
6281
echo "SHERPA_ONNX_LIB_DIR=$PWD/$d/lib" >> $GITHUB_ENV
63-
echo "RUSTFLAGS=-C link-arg=-Wl,-rpath,$PWD/$d/lib" >> $GITHUB_ENV
82+
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
83+
if [[ -d "$PWD/$d/bin" ]]; then
84+
echo "$PWD/$d/bin" >> $GITHUB_PATH
85+
ls -lh $d/bin
86+
fi
87+
echo "$PWD/$d/lib" >> $GITHUB_PATH
88+
echo "RUSTFLAGS=" >> $GITHUB_ENV
89+
else
90+
echo "RUSTFLAGS=-C link-arg=-Wl,-rpath,$PWD/$d/lib" >> $GITHUB_ENV
91+
fi
6492
6593
- name: Show libs
6694
shell: bash
@@ -69,8 +97,17 @@ jobs:
6997
ls -lh $SHERPA_ONNX_LIB_DIR
7098
7199
echo "RUSTFLAGS: $RUSTFLAGS"
100+
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
101+
echo "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER: $CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER"
102+
fi
72103
73104
- name: Run test
74105
shell: bash
75106
run: |
107+
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
108+
export PATH="$SHERPA_ONNX_LIB_DIR:$PATH"
109+
mkdir -p rust-api-examples/target/debug/examples
110+
cp -v "$SHERPA_ONNX_LIB_DIR"/*.dll rust-api-examples/target/debug/examples/
111+
ls -lh rust-api-examples/target/debug/examples/
112+
fi
76113
./.github/scripts/test-rust.sh

.github/workflows/windows-arm64.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ jobs:
179179
cp -a build/install/lib $dst/
180180
cp -a build/install/include $dst/
181181
182+
if [[ $shared_lib == "ON" && -f build/install/lib/onnxruntime.lib ]]; then
183+
cp -v build/install/lib/onnxruntime.lib $dst/lib/
184+
fi
185+
186+
rm -rf $dst/lib/pkgconfig
187+
rm -f $dst/lib/cargs.dll $dst/lib/cargs.lib $dst/lib/cargs.h
188+
rm -f $dst/include/cargs.h
189+
182190
ls -lh $dst/bin/
183191
echo "---"
184192
ls -lh $dst/lib/

.github/workflows/windows-x64.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ jobs:
191191
cp -a build/install/lib $dst/
192192
cp -a build/install/include $dst/
193193
194+
if [[ $shared_lib == "ON" && -f build/install/lib/onnxruntime.lib ]]; then
195+
cp -v build/install/lib/onnxruntime.lib $dst/lib/
196+
fi
197+
198+
rm -rf $dst/lib/pkgconfig
199+
rm -f $dst/lib/cargs.dll $dst/lib/cargs.lib $dst/lib/cargs.h
200+
rm -f $dst/include/cargs.h
201+
194202
ls -lh $dst/bin/
195203
echo "---"
196204
ls -lh $dst/lib/
@@ -244,7 +252,7 @@ jobs:
244252
file: sherpa-onnx-*-win-x64*.tar.bz2
245253
repo_name: k2-fsa/sherpa-onnx
246254
repo_token: ${{ secrets.UPLOAD_GH_SHERPA_ONNX_TOKEN }}
247-
tag: v1.12.28
255+
tag: v1.12.31
248256

249257
- name: Release pre-compiled binaries and libs for Windows x64
250258
if: github.repository_owner == 'k2-fsa'&& github.event_name == 'push' && contains(github.ref, 'refs/tags/')

.github/workflows/windows-x86.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ jobs:
185185
cp -a build/install/lib $dst/
186186
cp -a build/install/include $dst/
187187
188+
if [[ $shared_lib == "ON" && -f build/install/lib/onnxruntime.lib ]]; then
189+
cp -v build/install/lib/onnxruntime.lib $dst/lib/
190+
fi
191+
192+
rm -rf $dst/lib/pkgconfig
193+
rm -f $dst/lib/cargs.dll $dst/lib/cargs.lib $dst/lib/cargs.h
194+
rm -f $dst/include/cargs.h
195+
188196
ls -lh $dst/bin/
189197
echo "---"
190198
ls -lh $dst/lib/

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
126126
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
127127
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
128128

129-
if(NOT WIN32)
129+
if(UNIX)
130130
set(CMAKE_SKIP_BUILD_RPATH FALSE)
131131
set(BUILD_RPATH_USE_ORIGIN TRUE)
132132
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
@@ -138,7 +138,7 @@ else()
138138
set(SHERPA_ONNX_RPATH_ORIGIN "@loader_path")
139139
endif()
140140

141-
if(NOT WIN32)
141+
if(UNIX)
142142
set(CMAKE_INSTALL_RPATH ${SHERPA_ONNX_RPATH_ORIGIN})
143143
set(CMAKE_BUILD_RPATH ${SHERPA_ONNX_RPATH_ORIGIN})
144144
endif()

cmake/onnxruntime-win-arm64.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,4 @@ message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
107107

108108
install(FILES ${onnxruntime_lib_files} DESTINATION lib)
109109
install(FILES ${onnxruntime_lib_files} DESTINATION bin)
110+
install(FILES "${onnxruntime_SOURCE_DIR}/lib/onnxruntime.lib" DESTINATION lib)

cmake/onnxruntime-win-x64.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,4 @@ message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
107107

108108
install(FILES ${onnxruntime_lib_files} DESTINATION lib)
109109
install(FILES ${onnxruntime_lib_files} DESTINATION bin)
110+
install(FILES "${onnxruntime_SOURCE_DIR}/lib/onnxruntime.lib" DESTINATION lib)

cmake/onnxruntime-win-x86.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,4 @@ message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
107107

108108
install(FILES ${onnxruntime_lib_files} DESTINATION lib)
109109
install(FILES ${onnxruntime_lib_files} DESTINATION bin)
110+
install(FILES "${onnxruntime_SOURCE_DIR}/lib/onnxruntime.lib" DESTINATION lib)

new-release.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ sed -i.bak "$replace_str" ./android/SherpaOnnxAar/README.md
3737
sed -i.bak "$replace_str" ./rust-api-examples/Cargo.toml
3838
sed -i.bak "$replace_str" ./sherpa-onnx/rust/sherpa-onnx-sys/Cargo.toml
3939
sed -i.bak "$replace_str" ./sherpa-onnx/rust/sherpa-onnx/Cargo.toml
40+
sed -i.bak "$replace_str" ./sherpa-onnx/rust/sherpa-onnx/src/lib.rs
4041
sed -i.bak "$replace_str" ./rust-api-examples/README.md
4142

4243
find android -name build.gradle -type f -exec sed -i.bak "s/sherpa-onnx:v$old_version/sherpa-onnx:v$new_version/g" {} \;

rust-api-examples/README.md

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,88 @@
11
# Introduction
22

3-
This folder uses Rust API maintained by us.
3+
This folder contains examples that use the `sherpa-onnx` Rust crate maintained in
4+
this repository.
45

5-
## Setup library path
6+
## Setup
67

7-
### Method 1 (Build from source, support only shared libs right now)
8+
The Rust crate needs to find the sherpa-onnx shared libraries at build time, and
9+
your operating system needs to find them again at run time.
10+
11+
There are two common ways to do that:
12+
13+
- Method 1: Build sherpa-onnx from source and use the generated shared libraries
14+
- Method 2: Download a prebuilt sherpa-onnx shared-library package from GitHub releases
15+
16+
### Method 1: Build from source
17+
18+
Build and install sherpa-onnx first. After that, point `SHERPA_ONNX_LIB_DIR` to
19+
the generated `lib` directory.
20+
21+
#### Linux
22+
23+
```bash
24+
export SHERPA_ONNX_LIB_DIR=/path/to/sherpa-onnx/build/install/lib
25+
export RUSTFLAGS="-C link-arg=-Wl,-rpath,$SHERPA_ONNX_LIB_DIR"
26+
```
27+
28+
#### macOS
829

930
```bash
10-
export SHERPA_ONNX_LIB_DIR=/Users/fangjun/open-source/sherpa-onnx/build/install/lib
31+
export SHERPA_ONNX_LIB_DIR=/path/to/sherpa-onnx/build/install/lib
1132
export RUSTFLAGS="-C link-arg=-Wl,-rpath,$SHERPA_ONNX_LIB_DIR"
1233
```
1334

14-
### Method 2 (Download pre-built libs)
35+
#### Windows
36+
37+
```bash
38+
export SHERPA_ONNX_LIB_DIR=/path/to/sherpa-onnx/build/install/lib
39+
export PATH="$SHERPA_ONNX_LIB_DIR:$PATH"
40+
```
41+
42+
If Windows still reports that it cannot find a DLL, or if it loads a wrong
43+
`onnxruntime.dll`, copy the DLL files from `%SHERPA_ONNX_LIB_DIR%` to the same
44+
directory as the generated example executable, e.g.
45+
`rust-api-examples/target/debug/examples/`.
46+
47+
### Method 2: Download prebuilt shared libraries
48+
49+
This is the easiest method for most users.
50+
51+
1. Go to [GitHub releases](https://github.com/k2-fsa/sherpa-onnx/releases)
52+
2. Download the archive that matches your operating system
53+
3. Extract it anywhere you like
54+
4. Set `SHERPA_ONNX_LIB_DIR` to the extracted `lib` directory
55+
5. On Linux/macOS, also set `RUSTFLAGS` so the executable can find the shared libraries
56+
6. On Windows, add the same `lib` directory to `PATH`
57+
58+
Use the latest release in practice. We use `v1.12.31` below only as an example.
59+
60+
#### Which archive should I download?
61+
62+
| OS | Archive example | Download link |
63+
|----|-----------------|---------------|
64+
| Linux x86_64 | `sherpa-onnx-v1.12.31-linux-x64-shared.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-linux-x64-shared.tar.bz2) |
65+
| Linux aarch64 | `sherpa-onnx-v1.12.31-linux-aarch64-shared-cpu.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-linux-aarch64-shared-cpu.tar.bz2) |
66+
| macOS | `sherpa-onnx-v1.12.31-osx-universal2-shared.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-osx-universal2-shared.tar.bz2) |
67+
| Windows x64 | `sherpa-onnx-v1.12.31-win-x64-shared-MT-Release.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-win-x64-shared-MT-Release.tar.bz2) |
68+
69+
#### Linux
1570

1671
```bash
17-
# You can choose any directory you like
1872
cd $HOME/Downloads
1973

20-
# We use version v1.12.31 below as an example.
21-
# Please always use the latest version from
22-
# https://github.com/k2-fsa/sherpa-onnx/releases
74+
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-linux-x64-shared.tar.bz2
75+
tar xvf sherpa-onnx-v1.12.31-linux-x64-shared.tar.bz2
76+
rm sherpa-onnx-v1.12.31-linux-x64-shared.tar.bz2
77+
78+
export SHERPA_ONNX_LIB_DIR=$HOME/Downloads/sherpa-onnx-v1.12.31-linux-x64-shared/lib
79+
export RUSTFLAGS="-C link-arg=-Wl,-rpath,$SHERPA_ONNX_LIB_DIR"
80+
```
81+
82+
#### macOS
83+
84+
```bash
85+
cd $HOME/Downloads
2386

2487
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-osx-universal2-shared.tar.bz2
2588
tar xvf sherpa-onnx-v1.12.31-osx-universal2-shared.tar.bz2
@@ -29,6 +92,26 @@ export SHERPA_ONNX_LIB_DIR=$HOME/Downloads/sherpa-onnx-v1.12.31-osx-universal2-s
2992
export RUSTFLAGS="-C link-arg=-Wl,-rpath,$SHERPA_ONNX_LIB_DIR"
3093
```
3194

95+
#### Windows
96+
97+
```bash
98+
cd /c/Users/$USERNAME/Downloads
99+
100+
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-win-x64-shared-MT-Release.tar.bz2
101+
tar xvf sherpa-onnx-v1.12.31-win-x64-shared-MT-Release.tar.bz2
102+
rm sherpa-onnx-v1.12.31-win-x64-shared-MT-Release.tar.bz2
103+
104+
export SHERPA_ONNX_LIB_DIR=/c/Users/$USERNAME/Downloads/sherpa-onnx-v1.12.31-win-x64-shared-MT-Release/lib
105+
export PATH="$SHERPA_ONNX_LIB_DIR:$PATH"
106+
```
107+
108+
In most cases, updating `PATH` is enough on Windows. If an example still loads
109+
the wrong `onnxruntime.dll`, copy the DLL files from
110+
`$SHERPA_ONNX_LIB_DIR` to `target/debug/examples/` and run the example again.
111+
112+
After setting the environment variables above, you can run the examples in this
113+
directory with `cargo run --example ...` or use the helper scripts below.
114+
32115
## Examples
33116

34117
| # | Example | Description |

0 commit comments

Comments
 (0)