Skip to content

Commit 0884675

Browse files
committed
fix PIC
1 parent b39e468 commit 0884675

10 files changed

Lines changed: 190 additions & 90 deletions

File tree

.github/workflows/aarch64-linux-gnu-static.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
push:
66
branches:
77
- master
8+
- rust-static
89
tags:
910
- 'v[0-9]+.[0-9]+.[0-9]+*'
1011
paths:
@@ -141,7 +142,7 @@ jobs:
141142
path: sherpa-onnx-*.tar.bz2
142143

143144
- name: Release pre-compiled binaries and libs for linux aarch64
144-
if: github.repository_owner == 'csukuangfj' && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release'
145+
# if: github.repository_owner == 'csukuangfj' && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release'
145146
uses: svenstaro/upload-release-action@v2
146147
with:
147148
file_glob: true

.github/workflows/linux.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- master
7+
- rust-static
78
tags:
89
- 'v[0-9]+.[0-9]+.[0-9]+*'
910
paths:
@@ -65,8 +66,11 @@ jobs:
6566
fail-fast: false
6667
matrix:
6768
os: [ubuntu-latest]
68-
build_type: [Release, Debug]
69-
shared_lib: [ON, OFF]
69+
# build_type: [Release, Debug]
70+
# shared_lib: [ON, OFF]
71+
# with_tts: [ON, OFF]
72+
build_type: [Release]
73+
shared_lib: [OFF]
7074
with_tts: [ON, OFF]
7175

7276
steps:
@@ -237,7 +241,7 @@ jobs:
237241
du -h -d1 .
238242
239243
- name: Release pre-compiled binaries and libs for linux x64
240-
if: github.repository_owner == 'csukuangfj' && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release'
244+
# if: github.repository_owner == 'csukuangfj' && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release'
241245
uses: svenstaro/upload-release-action@v2
242246
with:
243247
file_glob: true

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Test rust package static
33
on:
44
push:
55
branches:
6-
- rust-static
6+
- rust-static-2
77
workflow_dispatch:
88

99
concurrency:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Test rust package
33
on:
44
push:
55
branches:
6-
- rust-static
6+
- rust-static-2
77
workflow_dispatch:
88

99
concurrency:

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ endif()
237237
if(BUILD_SHARED_LIBS OR SHERPA_ONNX_ENABLE_JNI)
238238
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
239239
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
240+
endif()
241+
242+
# Rust on Linux links static sherpa-onnx archives into PIE executables by
243+
# default, so static objects need PIC as well.
244+
if(NOT WIN32)
240245
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
241246
endif()
242247

new-release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ sed -i.bak "$replace_str" ./jitpack.yml
3535
sed -i.bak "$replace_str" ./android/SherpaOnnxAar/README.md
3636

3737
sed -i.bak "$replace_str" ./rust-api-examples/Cargo.toml
38+
sed -i.bak "$replace_str" ./rust-api-examples/for-advanced-users.md
3839
sed -i.bak "$replace_str" ./sherpa-onnx/rust/sherpa-onnx-sys/Cargo.toml
3940
sed -i.bak "$replace_str" ./sherpa-onnx/rust/sherpa-onnx/Cargo.toml
4041
sed -i.bak "$replace_str" ./sherpa-onnx/rust/sherpa-onnx/src/lib.rs
41-
sed -i.bak "$replace_str" ./rust-api-examples/README.md
4242

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

rust-api-examples/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ edition = "2021"
66
[dependencies]
77
anyhow = "1.0"
88
clap = { version = "4.5", features = ["derive"] }
9-
sherpa-onnx = { version = "1.12.31", default-features = false }
10-
# sherpa-onnx = { path = "../sherpa-onnx/rust/sherpa-onnx" }
9+
# sherpa-onnx = { version = "1.12.31", default-features = false }
10+
sherpa-onnx = { path = "../sherpa-onnx/rust/sherpa-onnx" }
1111
serde_json = "1.0"
1212

1313
cpal = { version = "0.16", optional = true } # cross-platform audio I/O

rust-api-examples/README.md

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,31 @@ this repository.
55

66
## Setup
77

8-
`rust-api-examples` now uses the Rust crate's default configuration:
8+
For most users, you don't need to configure Rust linking details manually.
99

10-
- static linking is enabled by default
11-
- if `SHERPA_ONNX_LIB_DIR` is not set, the build script downloads a matching
12-
prebuilt `-lib` archive automatically
13-
- if `SHERPA_ONNX_LIB_DIR` is set, that directory is used instead
10+
Just enter this directory and run one of the helper scripts below. Each script
11+
downloads the required model files automatically if needed.
1412

15-
For most users, this means you can simply run:
13+
For example:
1614

1715
```bash
18-
cargo run --example version
19-
```
20-
21-
The first build downloads the correct prebuilt libraries for your platform and
22-
prints the resolved library directory in the Cargo output.
23-
24-
### Use shared libraries instead
25-
26-
If you want to use the shared libraries, disable the default feature and enable
27-
`shared`:
28-
29-
```bash
30-
cargo run --no-default-features --features shared --example version
31-
```
32-
33-
For microphone examples, add `mic` as usual:
34-
35-
```bash
36-
cargo run --no-default-features --features "shared,mic" \
37-
--example streaming_zipformer_microphone -- --help
16+
./run-version.sh
3817
```
3918

40-
On Linux and macOS, the build script adds the runtime rpath automatically for
41-
downloaded or user-supplied shared libraries. On Windows, it copies the needed
42-
DLLs next to the generated binaries automatically.
43-
44-
### Advanced: use your own sherpa-onnx libraries
45-
46-
If you build sherpa-onnx yourself or download a release archive manually, set
47-
`SHERPA_ONNX_LIB_DIR` to the `lib` directory and rebuild:
19+
You can also run examples directly with Cargo:
4820

4921
```bash
50-
export SHERPA_ONNX_LIB_DIR=/path/to/sherpa-onnx/lib
22+
cargo run --example version
5123
```
5224

53-
That environment variable works for both static and shared builds.
54-
55-
### Prebuilt `-lib` archives used by the build script
56-
57-
Use the latest release in practice. We use `v1.12.31` below only as an example.
58-
59-
#### Default static archives
60-
61-
| OS | Archive example | Download link |
62-
|----|-----------------|---------------|
63-
| Linux x86_64 | `sherpa-onnx-v1.12.31-linux-x64-static-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-linux-x64-static-lib.tar.bz2) |
64-
| Linux aarch64 | `sherpa-onnx-v1.12.31-linux-aarch64-static-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-linux-aarch64-static-lib.tar.bz2) |
65-
| macOS x86_64 | `sherpa-onnx-v1.12.31-osx-x64-static-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-osx-x64-static-lib.tar.bz2) |
66-
| macOS arm64 | `sherpa-onnx-v1.12.31-osx-arm64-static-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-osx-arm64-static-lib.tar.bz2) |
67-
| Windows x64 | `sherpa-onnx-v1.12.31-win-x64-static-MT-Release-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-win-x64-static-MT-Release-lib.tar.bz2) |
68-
69-
#### Optional shared archives
25+
The first build may download the matching sherpa-onnx native libraries for your
26+
platform automatically. This is usually invisible apart from a short Cargo
27+
message showing the resolved library directory.
7028

71-
| OS | Archive example | Download link |
72-
|----|-----------------|---------------|
73-
| Linux x86_64 | `sherpa-onnx-v1.12.31-linux-x64-shared-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-linux-x64-shared-lib.tar.bz2) |
74-
| Linux aarch64 | `sherpa-onnx-v1.12.31-linux-aarch64-shared-cpu-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-linux-aarch64-shared-cpu-lib.tar.bz2) |
75-
| macOS x86_64 | `sherpa-onnx-v1.12.31-osx-x64-shared-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-osx-x64-shared-lib.tar.bz2) |
76-
| macOS arm64 | `sherpa-onnx-v1.12.31-osx-arm64-shared-lib.tar.bz2` | [Download](https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.12.31/sherpa-onnx-v1.12.31-osx-arm64-shared-lib.tar.bz2) |
77-
| Windows x64 | `sherpa-onnx-v1.12.31-win-x64-shared-MT-Release-lib.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-lib.tar.bz2) |
29+
If you want to customize which libraries are used, set `SHERPA_ONNX_LIB_DIR`,
30+
choose shared instead of the default behavior, or configure the crate directly
31+
in your own Cargo project, see
32+
[for-advanced-users.md](./for-advanced-users.md).
7833

7934
## Examples
8035

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Rust examples: advanced users
2+
3+
This note is for users who want to control how the `sherpa-onnx` Rust crate
4+
finds and links its native libraries.
5+
6+
Most users do **not** need anything here. The default behavior is:
7+
8+
1. build normally
9+
2. let the build script download the matching native libraries automatically
10+
3. run the examples or your own Rust program
11+
12+
## Use your own sherpa-onnx libraries
13+
14+
If you already have sherpa-onnx libraries on disk, set
15+
`SHERPA_ONNX_LIB_DIR` to the `lib` directory before building:
16+
17+
```bash
18+
export SHERPA_ONNX_LIB_DIR=/path/to/sherpa-onnx/lib
19+
```
20+
21+
Examples:
22+
23+
- source build output: `/path/to/sherpa-onnx/build/install/lib`
24+
- manually extracted release archive:
25+
`/path/to/sherpa-onnx-v1.12.31-linux-x64-static-lib/lib`
26+
27+
If `SHERPA_ONNX_LIB_DIR` is set, the build script uses that directory and does
28+
not auto-download another archive.
29+
30+
## Automatic download behavior
31+
32+
If `SHERPA_ONNX_LIB_DIR` is not set, `sherpa-onnx-sys/build.rs` downloads a
33+
matching prebuilt `-lib` archive from GitHub releases and uses its `lib`
34+
directory automatically.
35+
36+
The build script currently selects archives like this:
37+
38+
### Default mode
39+
40+
Default mode uses the default crate feature set. Most users just get this
41+
behavior automatically.
42+
43+
| OS | Architecture | Archive example |
44+
|----|--------------|-----------------|
45+
| Linux | x86_64 | `sherpa-onnx-v1.12.31-linux-x64-static-lib.tar.bz2` |
46+
| Linux | aarch64 | `sherpa-onnx-v1.12.31-linux-aarch64-static-lib.tar.bz2` |
47+
| macOS | x86_64 | `sherpa-onnx-v1.12.31-osx-x64-static-lib.tar.bz2` |
48+
| macOS | arm64 | `sherpa-onnx-v1.12.31-osx-arm64-static-lib.tar.bz2` |
49+
| Windows | x86_64 | `sherpa-onnx-v1.12.31-win-x64-static-MT-Release-lib.tar.bz2` |
50+
51+
### Shared mode
52+
53+
If you enable the `shared` feature, the build script downloads these shared
54+
archives instead:
55+
56+
| OS | Architecture | Archive example |
57+
|----|--------------|-----------------|
58+
| Linux | x86_64 | `sherpa-onnx-v1.12.31-linux-x64-shared-lib.tar.bz2` |
59+
| Linux | aarch64 | `sherpa-onnx-v1.12.31-linux-aarch64-shared-cpu-lib.tar.bz2` |
60+
| macOS | x86_64 | `sherpa-onnx-v1.12.31-osx-x64-shared-lib.tar.bz2` |
61+
| macOS | arm64 | `sherpa-onnx-v1.12.31-osx-arm64-shared-lib.tar.bz2` |
62+
| Windows | x86_64 | `sherpa-onnx-v1.12.31-win-x64-shared-MT-Release-lib.tar.bz2` |
63+
64+
In practice, use the latest release tag instead of the example version above.
65+
66+
## Configure the `sherpa-onnx` crate in Cargo.toml
67+
68+
### Default configuration
69+
70+
This is enough for most users:
71+
72+
```toml
73+
[dependencies]
74+
sherpa-onnx = "1.12.31"
75+
```
76+
77+
That uses the crate's default feature set.
78+
79+
### Use shared libraries
80+
81+
To use shared libraries instead, disable default features and enable `shared`:
82+
83+
```toml
84+
[dependencies]
85+
sherpa-onnx = { version = "1.12.31", default-features = false, features = ["shared"] }
86+
```
87+
88+
From the command line, the equivalent example command is:
89+
90+
```bash
91+
cargo run --no-default-features --features shared --example version
92+
```
93+
94+
### Enable microphone examples
95+
96+
In `rust-api-examples`, microphone support is controlled by the `mic` feature:
97+
98+
```bash
99+
cargo run --features mic --example streaming_zipformer_microphone -- --help
100+
```
101+
102+
If you want both microphone support and shared libraries:
103+
104+
```bash
105+
cargo run --no-default-features --features "shared,mic" \
106+
--example streaming_zipformer_microphone -- --help
107+
```
108+
109+
## Notes about runtime behavior
110+
111+
When shared libraries are used:
112+
113+
- Linux and macOS: the build script adds the runtime rpath automatically
114+
- Windows: the build script copies the required DLLs next to the generated
115+
binaries automatically
116+
117+
When `SHERPA_ONNX_LIB_DIR` is set, the same behavior applies, but the files come
118+
from your directory instead of an auto-downloaded archive.

0 commit comments

Comments
 (0)