From a25075101c6beb735fd69af0b019775ddeb9305a Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sat, 6 Jul 2024 16:41:54 +0800 Subject: [PATCH] Build sherpa-onnx as a single shared library (#1078) When `-D BUILD_SHARED_LIBS=ON` is passed to `cmake`, it builds a single shared library. Specifically, - For C APIs, it builds `libsherpa-onnx-c-api.so` - For Python APIs, it builds `_sherpa_onnx.cpython-xx-xx.so` - For Kotlin and Java APIs, it builds `libsherpa-onnx-jni.so` There is no `libsherpa-onnx-core.so` any longer. Note it affects only shared libraries. --- .github/scripts/test-dart.sh | 22 +++--- .github/workflows/flutter-linux.yaml | 3 +- .github/workflows/jni.yaml | 2 +- .github/workflows/linux-gpu.yaml | 13 +++- .github/workflows/linux-jni.yaml | 14 +++- .github/workflows/linux.yaml | 13 +++- .github/workflows/macos-jni.yaml | 4 +- .github/workflows/macos.yaml | 8 ++- .github/workflows/release-dart-package.yaml | 23 ++----- .github/workflows/riscv64-linux.yaml | 8 +++ .github/workflows/run-python-test-macos.yaml | 4 +- .github/workflows/swift.yaml | 2 +- .github/workflows/test-build-wheel.yaml | 10 +++ .github/workflows/test-dart.yaml | 2 +- .github/workflows/test-go-package.yaml | 4 +- .github/workflows/test-go.yaml | 68 +++++++++++++++++-- .../test-python-offline-websocket-server.yaml | 4 +- .../test-python-online-websocket-server.yaml | 4 +- .github/workflows/windows-arm64.yaml | 9 ++- .github/workflows/windows-x64-cuda.yaml | 8 ++- .github/workflows/windows-x64-debug.yaml | 7 +- .github/workflows/windows-x64-jni.yaml | 8 ++- .github/workflows/windows-x64.yaml | 8 ++- .github/workflows/windows-x86-debug.yaml | 6 +- .github/workflows/windows-x86.yaml | 12 +++- CHANGELOG.md | 4 ++ CMakeLists.txt | 64 ++++++++++------- build-android-arm64-v8a.sh | 12 ---- c-api-examples/run.sh | 2 +- cmake/cmake_extension.py | 22 +++--- cmake/espeak-ng-for-piper.cmake | 50 +++++++------- cmake/kaldi-decoder.cmake | 35 +++++----- cmake/kaldi-native-fbank.cmake | 24 +++++-- cmake/kaldifst.cmake | 15 ++++ cmake/openfst.cmake | 17 +++++ cmake/piper-phonemize.cmake | 27 +++++--- cmake/portaudio.cmake | 39 ++++------- cmake/sherpa-onnx-shared.pc.in | 25 +++++++ ....pc.in => sherpa-onnx-static-no-tts.pc.in} | 9 +++ ...pa-onnx.pc.in => sherpa-onnx-static.pc.in} | 9 +++ cmake/simple-sentencepiece.cmake | 24 +++++-- .../non-streaming-asr/pubspec.yaml | 2 +- dart-api-examples/streaming-asr/pubspec.yaml | 2 +- dart-api-examples/tts/pubspec.yaml | 2 +- dart-api-examples/vad/pubspec.yaml | 2 +- ffmpeg-examples/run.sh | 2 +- flutter-examples/streaming_asr/pubspec.yaml | 4 +- flutter/sherpa_onnx/pubspec.yaml | 10 +-- .../sherpa_onnx_linux/linux/CMakeLists.txt | 12 +--- .../windows/CMakeLists.txt | 10 --- kotlin-api-examples/run.sh | 1 + scripts/dart/release.sh | 10 --- scripts/dotnet/generate.py | 30 -------- scripts/dotnet/run.sh | 21 ++---- scripts/go/_internal/build_darwin_amd64.go | 2 +- scripts/go/_internal/build_darwin_arm64.go | 2 +- scripts/go/_internal/build_linux_amd64.go | 6 ++ scripts/go/_internal/build_linux_arm.go | 6 ++ scripts/go/_internal/build_linux_arm64.go | 6 ++ scripts/go/_internal/build_windows_386.go | 6 ++ scripts/go/_internal/build_windows_amd64.go | 6 ++ .../lib/x86_64-pc-windows-gnu/.gitkeep | 0 .../go/_internal/lib/x86_64-unknown-linux-gnu | 1 + scripts/go/release.sh | 27 ++++++++ scripts/node-addon-api/CMakeLists.txt | 9 --- scripts/node-addon-api/run.sh | 9 ++- sherpa-onnx/csrc/CMakeLists.txt | 36 +++++----- sherpa-onnx/jni/CMakeLists.txt | 3 + sherpa-onnx/jni/common.h | 22 +++++- 69 files changed, 570 insertions(+), 323 deletions(-) create mode 100644 cmake/sherpa-onnx-shared.pc.in rename cmake/{sherpa-onnx-no-tts.pc.in => sherpa-onnx-static-no-tts.pc.in} (71%) rename cmake/{sherpa-onnx.pc.in => sherpa-onnx-static.pc.in} (72%) create mode 100644 scripts/go/_internal/build_linux_amd64.go create mode 100644 scripts/go/_internal/build_linux_arm.go create mode 100644 scripts/go/_internal/build_linux_arm64.go create mode 100644 scripts/go/_internal/build_windows_386.go create mode 100644 scripts/go/_internal/build_windows_amd64.go create mode 100644 scripts/go/_internal/lib/x86_64-pc-windows-gnu/.gitkeep create mode 120000 scripts/go/_internal/lib/x86_64-unknown-linux-gnu diff --git a/.github/scripts/test-dart.sh b/.github/scripts/test-dart.sh index b0be657a7..711aa631f 100755 --- a/.github/scripts/test-dart.sh +++ b/.github/scripts/test-dart.sh @@ -6,17 +6,6 @@ cd dart-api-examples pushd non-streaming-asr -echo '----------paraformer itn----------' -./run-paraformer-itn.sh - -echo '----------paraformer----------' -./run-paraformer.sh -rm -rf sherpa-onnx-* - -echo '----------VAD with paraformer----------' -./run-vad-with-paraformer.sh -rm -rf sherpa-onnx-* - echo '----------NeMo transducer----------' ./run-nemo-transducer.sh rm -rf sherpa-onnx-* @@ -37,6 +26,17 @@ echo '----------zipformer transducer----------' ./run-zipformer-transducer.sh rm -rf sherpa-onnx-* +echo '----------paraformer itn----------' +./run-paraformer-itn.sh + +echo '----------paraformer----------' +./run-paraformer.sh +rm -rf sherpa-onnx-* + +echo '----------VAD with paraformer----------' +./run-vad-with-paraformer.sh +rm -rf sherpa-onnx-* + popd # non-streaming-asr pushd tts diff --git a/.github/workflows/flutter-linux.yaml b/.github/workflows/flutter-linux.yaml index ca665fd8b..240ab0a6f 100644 --- a/.github/workflows/flutter-linux.yaml +++ b/.github/workflows/flutter-linux.yaml @@ -32,6 +32,7 @@ concurrency: jobs: flutter_linux: + if: false name: linux runs-on: ${{ matrix.os }} container: ubuntu:18.04 @@ -49,7 +50,7 @@ jobs: shell: bash run: | apt-get update -y - apt-get install -y build-essential jq git cmake + apt-get install -y build-essential jq git cmake apt-get install -y curl - name: Setup Flutter SDK diff --git a/.github/workflows/jni.yaml b/.github/workflows/jni.yaml index fdee728fd..a0f769393 100644 --- a/.github/workflows/jni.yaml +++ b/.github/workflows/jni.yaml @@ -37,7 +37,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest, macos-14] + os: [ubuntu-latest, macos-latest, macos-13] steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/linux-gpu.yaml b/.github/workflows/linux-gpu.yaml index 3f6052e63..2a9d0529d 100644 --- a/.github/workflows/linux-gpu.yaml +++ b/.github/workflows/linux-gpu.yaml @@ -72,7 +72,12 @@ jobs: mkdir build cd build - cmake -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_INSTALL_PREFIX=./install -DBUILD_SHARED_LIBS=ON -DSHERPA_ONNX_ENABLE_GPU=ON .. + cmake \ + -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} \ + -D CMAKE_INSTALL_PREFIX=./install \ + -D BUILD_SHARED_LIBS=ON \ + -D SHERPA_ONNX_ENABLE_GPU=ON \ + .. - name: Build sherpa-onnx for ubuntu shell: bash @@ -86,6 +91,12 @@ jobs: ls -lh lib ls -lh bin + echo "----" + ls -lh install/lib + + echo "----" + ls -lh install/bin + - name: Display dependencies of sherpa-onnx for linux shell: bash run: | diff --git a/.github/workflows/linux-jni.yaml b/.github/workflows/linux-jni.yaml index 4bea999d8..f50974a67 100644 --- a/.github/workflows/linux-jni.yaml +++ b/.github/workflows/linux-jni.yaml @@ -95,7 +95,13 @@ jobs: mkdir build cd build - cmake -DSHERPA_ONNX_ENABLE_TTS=ON -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install -DSHERPA_ONNX_ENABLE_JNI=ON .. + cmake \ + -D SHERPA_ONNX_ENABLE_TTS=ON \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_SHARED_LIBS=ON \ + -D CMAKE_INSTALL_PREFIX=./install \ + -D SHERPA_ONNX_ENABLE_JNI=ON \ + .. make -j2 make install @@ -105,6 +111,12 @@ jobs: rm -rf ./install/pkgconfig rm -rf ./install/share + echo "----" + ls -lh install/lib + + echo "----" + ls -lh install/bin + - name: Display dependencies of sherpa-onnx for linux shell: bash run: | diff --git a/.github/workflows/linux.yaml b/.github/workflows/linux.yaml index 2be4db5b0..3d5077312 100644 --- a/.github/workflows/linux.yaml +++ b/.github/workflows/linux.yaml @@ -99,7 +99,12 @@ jobs: mkdir build cd build - cmake -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} -DCMAKE_INSTALL_PREFIX=./install .. + cmake \ + -D SHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} \ + -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} \ + -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} \ + -D CMAKE_INSTALL_PREFIX=./install \ + .. make -j2 make install @@ -107,6 +112,12 @@ jobs: ls -lh lib ls -lh bin + echo "----" + ls -lh install/lib + + echo "----" + ls -lh install/bin + - name: Display dependencies of sherpa-onnx for linux shell: bash run: | diff --git a/.github/workflows/macos-jni.yaml b/.github/workflows/macos-jni.yaml index 57ac69681..91c4d0d69 100644 --- a/.github/workflows/macos-jni.yaml +++ b/.github/workflows/macos-jni.yaml @@ -52,8 +52,8 @@ jobs: cmake \ -D BUILD_SHARED_LIBS=ON \ -D CMAKE_BUILD_TYPE=Release \ - -DCMAKE_OSX_ARCHITECTURES=$arch \ - -DSHERPA_ONNX_ENABLE_JNI=ON \ + -D CMAKE_OSX_ARCHITECTURES=$arch \ + -D SHERPA_ONNX_ENABLE_JNI=ON \ -DCMAKE_INSTALL_PREFIX=./install \ .. diff --git a/.github/workflows/macos.yaml b/.github/workflows/macos.yaml index cb88c8534..29b784ae5 100644 --- a/.github/workflows/macos.yaml +++ b/.github/workflows/macos.yaml @@ -84,7 +84,13 @@ jobs: BUILD_SHARED_LIBS=ON fi - cmake -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} -D BUILD_SHARED_LIBS=$BUILD_SHARED_LIBS -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64' -DCMAKE_INSTALL_PREFIX=./install .. + cmake \ + -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} \ + -D BUILD_SHARED_LIBS=$BUILD_SHARED_LIBS \ + -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} \ + -D CMAKE_OSX_ARCHITECTURES='arm64;x86_64' \ + -D CMAKE_INSTALL_PREFIX=./install \ + .. - name: Build sherpa-onnx for macos shell: bash diff --git a/.github/workflows/release-dart-package.yaml b/.github/workflows/release-dart-package.yaml index 8271954a6..1cc33542e 100644 --- a/.github/workflows/release-dart-package.yaml +++ b/.github/workflows/release-dart-package.yaml @@ -93,17 +93,12 @@ jobs: make install ls -lh ./install/lib + rm -v ./install/lib/libonnxruntime.so - name: Copy pre-built libs shell: bash run: | cp -v build/install/lib/lib*.so* flutter/sherpa_onnx_linux/linux/ - pushd flutter/sherpa_onnx_linux/linux/ - - rm libonnxruntime.so - ln -s libonnxruntime.so.* ./libonnxruntime.so - - popd mv -v flutter/sherpa_onnx_linux /tmp/to_be_published @@ -197,27 +192,19 @@ jobs: cd build make -j2 install - ls -lh install/lib/libsherpa-onnx-core.dylib - file install/lib/libsherpa-onnx-core.dylib + ls -lh install/lib/libsherpa-onnx-c-api.dylib + file install/lib/libsherpa-onnx-c-api.dylib + rm -v install/lib/libonnxruntime.dylib - name: Copy pre-built libs shell: bash run: | cp -v build/install/lib/lib*.dylib* flutter/sherpa_onnx_macos/macos/ - mv -v flutter/sherpa_onnx_macos /tmp/to_be_published ls -lh /tmp/to_be_published/macos - pushd /tmp/to_be_published/macos - - rm libonnxruntime.dylib - - ls -lh - - popd - - name: Release shell: bash run: | @@ -283,7 +270,7 @@ jobs: shell: bash run: | cd build - cmake --build . --target install --config Release + cmake --build . --target install --config Release -- -m:2 ls -lh install/lib/*.dll diff --git a/.github/workflows/riscv64-linux.yaml b/.github/workflows/riscv64-linux.yaml index acae80e41..e3e5e8b19 100644 --- a/.github/workflows/riscv64-linux.yaml +++ b/.github/workflows/riscv64-linux.yaml @@ -123,8 +123,16 @@ jobs: ls -lh build-riscv64-linux-gnu/bin ls -lh build-riscv64-linux-gnu/lib + echo "---install/lib---" + ls -lh build-riscv64-linux-gnu/install/lib + + echo "---install/bin---" + ls -lh build-riscv64-linux-gnu/install/bin + file build-riscv64-linux-gnu/bin/sherpa-onnx + readelf -d build-riscv64-linux-gnu/bin/sherpa-onnx + - name: Test sherpa-onnx shell: bash run: | diff --git a/.github/workflows/run-python-test-macos.yaml b/.github/workflows/run-python-test-macos.yaml index 23905acdd..ed51379d2 100644 --- a/.github/workflows/run-python-test-macos.yaml +++ b/.github/workflows/run-python-test-macos.yaml @@ -78,7 +78,7 @@ jobs: - name: Install Python dependencies shell: bash run: | - python3 -m pip install --upgrade pip numpy pypinyin sentencepiece>=0.1.96 soundfile + python3 -m pip install --upgrade pip numpy pypinyin sentencepiece>=0.1.96 soundfile setuptools wheel - name: Install sherpa-onnx shell: bash @@ -87,7 +87,7 @@ jobs: export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" cmake --version - python3 setup.py install + python3 -m pip install . - name: Test sherpa-onnx shell: bash diff --git a/.github/workflows/swift.yaml b/.github/workflows/swift.yaml index 6b4ef10df..3176c9b31 100644 --- a/.github/workflows/swift.yaml +++ b/.github/workflows/swift.yaml @@ -39,7 +39,7 @@ jobs: strategy: fail-fast: false matrix: - os: [macos-latest, macos-14] + os: [macos-latest, macos-13] steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/test-build-wheel.yaml b/.github/workflows/test-build-wheel.yaml index e53ce9313..303da0e59 100644 --- a/.github/workflows/test-build-wheel.yaml +++ b/.github/workflows/test-build-wheel.yaml @@ -110,10 +110,20 @@ jobs: python3 setup.py bdist_wheel ls -lh dist + - uses: actions/upload-artifact@v4 + with: + name: wheel-${{ matrix.os }}-${{ matrix.python-version }} + path: ./dist/*.whl + - name: Display wheel shell: bash run: | ls -lh dist + cd dist + + mkdir t + cd t + unzip ../*.whl - name: Install wheel shell: bash diff --git a/.github/workflows/test-dart.yaml b/.github/workflows/test-dart.yaml index 1803af9c4..fa4ee5ba5 100644 --- a/.github/workflows/test-dart.yaml +++ b/.github/workflows/test-dart.yaml @@ -70,7 +70,7 @@ jobs: cd build cmake \ - -D BUILD_SHARED_LIBS=ON \ + -DBUILD_SHARED_LIBS=ON \ -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ -DBUILD_ESPEAK_NG_EXE=OFF \ diff --git a/.github/workflows/test-go-package.yaml b/.github/workflows/test-go-package.yaml index 79cf17e44..8401f7801 100644 --- a/.github/workflows/test-go-package.yaml +++ b/.github/workflows/test-go-package.yaml @@ -26,8 +26,10 @@ jobs: include: - os: ubuntu-latest arch: amd64 - - os: macos-latest + - os: macos-13 arch: amd64 + - os: macos-14 + arch: arm64 - os: windows-latest arch: x64 - os: windows-latest diff --git a/.github/workflows/test-go.yaml b/.github/workflows/test-go.yaml index 6f25a0139..f7bfe510b 100644 --- a/.github/workflows/test-go.yaml +++ b/.github/workflows/test-go.yaml @@ -35,7 +35,7 @@ jobs: strategy: fail-fast: false matrix: - os: [macos-latest, macos-14] + os: [macos-latest, macos-13, ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v4 @@ -67,12 +67,63 @@ jobs: - name: Build sherpa-onnx shell: bash run: | + upload_dir=$PWD/to-upload + mkdir -p $upload_dir + echo "upload_dir" + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" mkdir build cd build - cmake -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DBUILD_SHARED_LIBS=ON -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF .. - make -j1 - cp -v _deps/onnxruntime-src/lib/libonnxruntime*dylib ./lib/ + cmake \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DBUILD_SHARED_LIBS=ON \ + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ + -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ + -DCMAKE_INSTALL_PREFIX=./install \ + .. + + if [[ ${{ matrix.os }} == windows-latest ]]; then + cmake --build . --target install --config Release -- -m:2 + else + make -j2 install + fi + + if [[ ${{ matrix.os }} == ubuntu-latest ]]; then + cp -v ./lib/*.so $upload_dir + cp -v _deps/onnxruntime-src/lib/libonnxruntime*so* $upload_dir + + cp -v _deps/onnxruntime-src/lib/libonnxruntime*so* ./lib/ + rm -v ./lib/libonnxruntime.so + + cd lib + ln -s libonnxruntime.so.1.17.1 libonnxruntime.so + cd .. + + rm -v ./lib/*.a + ls -h ./lib + elif [[ ${{ matrix.os }} == windows-latest ]]; then + cp -v ./install/lib/sherpa-onnx-c-api.dll ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/ + cp -v ./install/lib/onnxruntime.dll ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/ + ls -lh ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/ + cp -v ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/*.dll ../scripts/go/_internal/speaker-identification/ + cp -v ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/*.dll ../scripts/go/_internal/streaming-hlg-decoding/ + cp -v ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/*.dll ../scripts/go/_internal/non-streaming-tts/ + cp -v ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/*.dll ../scripts/go/_internal/non-streaming-decode-files/ + cp -v ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/*.dll ../scripts/go/_internal/streaming-decode-files/ + + cp -v ../scripts/go/_internal/lib/x86_64-pc-windows-gnu/*.dll $upload_dir + else + cp -v _deps/onnxruntime-src/lib/libonnxruntime*dylib $upload_dir/ + cp -v lib/*.dylib $upload_dir + + cp -v _deps/onnxruntime-src/lib/libonnxruntime*dylib ./lib/ + rm ./lib/*.a + rm ./lib/libonnxruntime.dylib + cd lib + ln -s libonnxruntime.1.17.1.dylib libonnxruntime.dylib + cd .. + fi cd ../scripts/go/_internal/ ls -lh lib @@ -83,6 +134,11 @@ jobs: go mod tidy go build + - uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.os }}-libs + path: to-upload/ + - name: Test speaker identification shell: bash run: | @@ -95,7 +151,7 @@ jobs: cd scripts/go/_internal/streaming-hlg-decoding/ ./run.sh - - name: Test non-streaming TTS (macOS) + - name: Test non-streaming TTS shell: bash run: | mkdir tts-waves @@ -130,7 +186,7 @@ jobs: name: tts-waves-${{ matrix.os }} path: tts-waves - - name: Test non-streaming decoding files (macOS) + - name: Test non-streaming decoding files shell: bash run: | cd scripts/go/_internal/non-streaming-decode-files/ diff --git a/.github/workflows/test-python-offline-websocket-server.yaml b/.github/workflows/test-python-offline-websocket-server.yaml index a1d3124c5..ab63b2683 100644 --- a/.github/workflows/test-python-offline-websocket-server.yaml +++ b/.github/workflows/test-python-offline-websocket-server.yaml @@ -57,7 +57,7 @@ jobs: - name: Install Python dependencies shell: bash run: | - python3 -m pip install --upgrade pip numpy pypinyin sentencepiece + python3 -m pip install --upgrade pip numpy pypinyin sentencepiece setuptools wheel - name: Install sherpa-onnx shell: bash @@ -66,7 +66,7 @@ jobs: export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" cmake --version - python3 setup.py install + python3 -m pip install . python3 -m pip install websockets - name: Start server for transducer models diff --git a/.github/workflows/test-python-online-websocket-server.yaml b/.github/workflows/test-python-online-websocket-server.yaml index 2084d5401..badf343a0 100644 --- a/.github/workflows/test-python-online-websocket-server.yaml +++ b/.github/workflows/test-python-online-websocket-server.yaml @@ -57,7 +57,7 @@ jobs: - name: Install Python dependencies shell: bash run: | - python3 -m pip install --upgrade pip numpy pypinyin sentencepiece + python3 -m pip install --upgrade pip numpy pypinyin sentencepiece setuptools wheel - name: Install sherpa-onnx shell: bash @@ -66,7 +66,7 @@ jobs: export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" cmake --version - python3 setup.py install + python3 -m pip install . python3 -m pip install websockets - name: Start server for zipformer2 CTC models diff --git a/.github/workflows/windows-arm64.yaml b/.github/workflows/windows-arm64.yaml index 9b56c7b22..a6d2a96da 100644 --- a/.github/workflows/windows-arm64.yaml +++ b/.github/workflows/windows-arm64.yaml @@ -47,7 +47,14 @@ jobs: run: | mkdir build cd build - cmake -A ARM64 -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} -DCMAKE_INSTALL_PREFIX=./install -DBUILD_ESPEAK_NG_EXE=OFF .. + cmake \ + -A ARM64 \ + -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} \ + -D CMAKE_INSTALL_PREFIX=./install \ + -D BUILD_ESPEAK_NG_EXE=OFF \ + .. - name: Build sherpa-onnx for windows shell: bash diff --git a/.github/workflows/windows-x64-cuda.yaml b/.github/workflows/windows-x64-cuda.yaml index 557d1d34e..fd4570455 100644 --- a/.github/workflows/windows-x64-cuda.yaml +++ b/.github/workflows/windows-x64-cuda.yaml @@ -57,7 +57,13 @@ jobs: run: | mkdir build cd build - cmake -A x64 -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install -DSHERPA_ONNX_ENABLE_GPU=ON .. + cmake \ + -A x64 \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_SHARED_LIBS=ON \ + -D CMAKE_INSTALL_PREFIX=./install \ + -D SHERPA_ONNX_ENABLE_GPU=ON \ + .. - name: Build sherpa-onnx for windows shell: bash diff --git a/.github/workflows/windows-x64-debug.yaml b/.github/workflows/windows-x64-debug.yaml index 501763248..09f93fd0d 100644 --- a/.github/workflows/windows-x64-debug.yaml +++ b/.github/workflows/windows-x64-debug.yaml @@ -59,7 +59,12 @@ jobs: run: | mkdir build cd build - cmake -A x64 -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} -DCMAKE_INSTALL_PREFIX=./install .. + cmake \ + -A x64 \ + -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} \ + -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} \ + -D CMAKE_INSTALL_PREFIX=./install \ + .. - name: Build sherpa-onnx for windows shell: bash diff --git a/.github/workflows/windows-x64-jni.yaml b/.github/workflows/windows-x64-jni.yaml index 64164332d..28d35367c 100644 --- a/.github/workflows/windows-x64-jni.yaml +++ b/.github/workflows/windows-x64-jni.yaml @@ -37,7 +37,13 @@ jobs: run: | mkdir build cd build - cmake -A x64 -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=ON -DSHERPA_ONNX_ENABLE_JNI=ON -DCMAKE_INSTALL_PREFIX=./install .. + cmake \ + -A x64 \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_SHARED_LIBS=ON \ + -D SHERPA_ONNX_ENABLE_JNI=ON \ + -D CMAKE_INSTALL_PREFIX=./install \ + .. - name: Build sherpa-onnx for windows shell: bash diff --git a/.github/workflows/windows-x64.yaml b/.github/workflows/windows-x64.yaml index 8cfc1af1f..a80b0f69d 100644 --- a/.github/workflows/windows-x64.yaml +++ b/.github/workflows/windows-x64.yaml @@ -63,7 +63,13 @@ jobs: run: | mkdir build cd build - cmake -A x64 -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} -DCMAKE_INSTALL_PREFIX=./install .. + cmake \ + -A x64 \ + -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} \ + -DCMAKE_INSTALL_PREFIX=./install \ + .. - name: Build sherpa-onnx for windows shell: bash diff --git a/.github/workflows/windows-x86-debug.yaml b/.github/workflows/windows-x86-debug.yaml index 03dce165a..f72bf2566 100644 --- a/.github/workflows/windows-x86-debug.yaml +++ b/.github/workflows/windows-x86-debug.yaml @@ -59,7 +59,11 @@ jobs: run: | mkdir build cd build - cmake -A Win32 -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} -D CMAKE_INSTALL_PREFIX=./install .. + cmake \ + -A Win32 \ + -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} \ + -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} \ + -D CMAKE_INSTALL_PREFIX=./install .. - name: Build sherpa-onnx for windows shell: bash diff --git a/.github/workflows/windows-x86.yaml b/.github/workflows/windows-x86.yaml index a445f7518..cfec7a864 100644 --- a/.github/workflows/windows-x86.yaml +++ b/.github/workflows/windows-x86.yaml @@ -63,14 +63,20 @@ jobs: run: | mkdir build cd build - cmake -A Win32 -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} -D CMAKE_INSTALL_PREFIX=./install .. + cmake \ + -A Win32 \ + -DSHERPA_ONNX_ENABLE_TTS=${{ matrix.with_tts }} \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_SHARED_LIBS=${{ matrix.shared_lib }} \ + -D CMAKE_INSTALL_PREFIX=./install \ + .. - name: Build sherpa-onnx for windows shell: bash run: | cd build - cmake --build . --config Release -- -m:2 - cmake --build . --config Release --target install -- -m:2 + cmake --build . --config Release + cmake --build . --config Release --target install ls -lh ./bin/Release/sherpa-onnx.exe diff --git a/CHANGELOG.md b/CHANGELOG.md index d58bdad62..edaa2b7ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.10.10 + +* Build sherpa-onnx into a single shared library. + ## 1.10.9 * Fix released packages. piper-phonemize was not included in v1.10.8. diff --git a/CMakeLists.txt b/CMakeLists.txt index 66284fd92..5c8c5b7d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,13 +2,16 @@ cmake_minimum_required(VERSION 3.13 FATAL_ERROR) set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14" CACHE STRING "Minimum OS X deployment version. Used only for macOS") +set(CMAKE_POLICY_DEFAULT_CMP0063 NEW) +set(CMAKE_POLICY_DEFAULT_CMP0069 NEW) + project(sherpa-onnx) # Remember to update # ./nodejs-addon-examples # ./dart-api-examples/ # ./sherpa-onnx/flutter/CHANGELOG.md -set(SHERPA_ONNX_VERSION "1.10.9") +set(SHERPA_ONNX_VERSION "1.10.10") # Disable warning about # @@ -18,6 +21,21 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") cmake_policy(SET CMP0135 NEW) endif() + +include(CheckIPOSupported) +check_ipo_supported(RESULT ipo) + +if(ipo) + message(STATUS "IPO is enabled") + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) +else() + message(STATUS "IPO is not available") +endif() + +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + option(SHERPA_ONNX_ENABLE_PYTHON "Whether to build Python" OFF) option(SHERPA_ONNX_ENABLE_TESTS "Whether to build tests" OFF) option(SHERPA_ONNX_ENABLE_CHECK "Whether to build with assert" OFF) @@ -53,8 +71,10 @@ else() set(SHERPA_ONNX_RPATH_ORIGIN "@loader_path") endif() -set(CMAKE_INSTALL_RPATH ${SHERPA_ONNX_RPATH_ORIGIN}) -set(CMAKE_BUILD_RPATH ${SHERPA_ONNX_RPATH_ORIGIN}) +if(NOT WIN32) + set(CMAKE_INSTALL_RPATH ${SHERPA_ONNX_RPATH_ORIGIN}) + set(CMAKE_BUILD_RPATH ${SHERPA_ONNX_RPATH_ORIGIN}) +endif() if(NOT CMAKE_BUILD_TYPE) message(STATUS "No CMAKE_BUILD_TYPE given, default to Release") @@ -89,22 +109,16 @@ to install CUDA toolkit if you have not installed it.") endif() endif() -if(BUILD_SHARED_LIBS AND MSVC) - set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -endif() - -if(NOT BUILD_SHARED_LIBS AND MSVC) - # see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html - # https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake - if(MSVC) - add_compile_options( - $<$:/MT> #---------| - $<$:/MTd> #---|-- Statically link the runtime libraries - $<$:/MT> #--| - $<$:/MT> - $<$:/MT> - ) - endif() +# see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html +# https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake +if(MSVC) + add_compile_options( + $<$:/MT> #---------| + $<$:/MTd> #---|-- Statically link the runtime libraries + $<$:/MT> #--| + $<$:/MT> + $<$:/MT> + ) endif() message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}") @@ -325,17 +339,21 @@ if(NOT BUILD_SHARED_LIBS) endif() endif() +if(NOT BUILD_SHARED_LIBS) # See https://people.freedesktop.org/~dbn/pkg-config-guide.html -if(SHERPA_ONNX_ENABLE_TTS) - configure_file(cmake/sherpa-onnx.pc.in ${PROJECT_BINARY_DIR}/sherpa-onnx.pc @ONLY) + if(SHERPA_ONNX_ENABLE_TTS) + configure_file(cmake/sherpa-onnx-static.pc.in ${PROJECT_BINARY_DIR}/sherpa-onnx.pc @ONLY) + else() + configure_file(cmake/sherpa-onnx-static-no-tts.pc.in ${PROJECT_BINARY_DIR}/sherpa-onnx.pc @ONLY) + endif() else() - configure_file(cmake/sherpa-onnx-no-tts.pc.in ${PROJECT_BINARY_DIR}/sherpa-onnx.pc @ONLY) + configure_file(cmake/sherpa-onnx-shared.pc.in ${PROJECT_BINARY_DIR}/sherpa-onnx.pc @ONLY) endif() install( FILES ${PROJECT_BINARY_DIR}/sherpa-onnx.pc DESTINATION - . + ./ ) message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") diff --git a/build-android-arm64-v8a.sh b/build-android-arm64-v8a.sh index b48b5b246..3aee46648 100755 --- a/build-android-arm64-v8a.sh +++ b/build-android-arm64-v8a.sh @@ -118,18 +118,6 @@ rm -rf install/lib/pkgconfig # cd /data/local/tmp # ./sherpa-onnx # -# which shows the following error log: -# -# CANNOT LINK EXECUTABLE "./sherpa-onnx": library "libsherpa-onnx-core.so" not found: needed by main executable -# -# Please run: -# -# export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH -# -# and then you can run: -# -# ./sherpa-onnx -# # It should show the help message of sherpa-onnx. # # Please use the above approach to copy model files to your phone. diff --git a/c-api-examples/run.sh b/c-api-examples/run.sh index 45ebaaa10..02054c2ec 100755 --- a/c-api-examples/run.sh +++ b/c-api-examples/run.sh @@ -11,7 +11,7 @@ if [ ! -d ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20 ]; then exit 1 fi -if [[ ! -f ../build/lib/libsherpa-onnx-core.a && ! -f ../build/lib/libsherpa-onnx-core.dylib && ! -f ../build/lib/libsherpa-onnx-core.so ]]; then +if [[ ! -f ../build/lib/libsherpa-onnx-c-api.a && ! -f ../build/lib/libsherpa-onnx-c-api.dylib && ! -f ../build/lib/libsherpa-onnx-c-api.so ]]; then echo "Please build sherpa-onnx first. You can use" echo "" echo " cd /path/to/sherpa-onnx" diff --git a/cmake/cmake_extension.py b/cmake/cmake_extension.py index c9303c688..b61de20f7 100644 --- a/cmake/cmake_extension.py +++ b/cmake/cmake_extension.py @@ -76,19 +76,8 @@ def get_binaries(): if is_windows(): binaries += [ - "espeak-ng.dll", - "kaldi-decoder-core.dll", - "kaldi-native-fbank-core.dll", "onnxruntime.dll", - "ssentencepiece_core.dll", - "piper_phonemize.dll", "sherpa-onnx-c-api.dll", - "sherpa-onnx-core.dll", - "sherpa-onnx-fstfar.dll", - "sherpa-onnx-fst.dll", - "sherpa-onnx-kaldifst-core.dll", - "sherpa-onnx-portaudio.dll", - "ucd.dll", ] return binaries @@ -145,6 +134,7 @@ def build_extension(self, ext: setuptools.extension.Extension): extra_cmake_args += " -DBUILD_PIPER_PHONMIZE_TESTS=OFF " extra_cmake_args += " -DBUILD_ESPEAK_NG_EXE=OFF " extra_cmake_args += " -DBUILD_ESPEAK_NG_TESTS=OFF " + extra_cmake_args += " -DSHERPA_ONNX_ENABLE_C_API=ON " extra_cmake_args += " -DSHERPA_ONNX_BUILD_C_API_EXAMPLES=OFF " extra_cmake_args += " -DSHERPA_ONNX_ENABLE_CHECK=OFF " @@ -161,7 +151,7 @@ def build_extension(self, ext: setuptools.extension.Extension): if is_windows(): build_cmd = f""" cmake {cmake_args} -B {self.build_temp} -S {sherpa_onnx_dir} - cmake --build {self.build_temp} --target install --config Release -- -m + cmake --build {self.build_temp} --target install --config Release -- -m:2 """ print(f"build command is:\n{build_cmd}") ret = os.system( @@ -171,7 +161,7 @@ def build_extension(self, ext: setuptools.extension.Extension): raise Exception("Failed to configure sherpa") ret = os.system( - f"cmake --build {self.build_temp} --target install --config Release -- -m" # noqa + f"cmake --build {self.build_temp} --target install --config Release -- -m:2" # noqa ) if ret != 0: raise Exception("Failed to build and install sherpa") @@ -226,5 +216,11 @@ def build_extension(self, ext: setuptools.extension.Extension): shutil.rmtree(f"{install_dir}/share") shutil.rmtree(f"{install_dir}/lib/pkgconfig") + if is_linux(): + os.remove(f"{install_dir}/lib/libonnxruntime.so") + + if is_macos(): + os.remove(f"{install_dir}/lib/libonnxruntime.dylib") + if is_windows(): shutil.rmtree(f"{install_dir}/lib") diff --git a/cmake/espeak-ng-for-piper.cmake b/cmake/espeak-ng-for-piper.cmake index 8601ab2d4..b54a0a6bd 100644 --- a/cmake/espeak-ng-for-piper.cmake +++ b/cmake/espeak-ng-for-piper.cmake @@ -1,9 +1,9 @@ function(download_espeak_ng_for_piper) include(FetchContent) - set(espeak_ng_URL "https://github.com/csukuangfj/espeak-ng/archive/69bf6927964fb042aeb827cfdf6082a30f5802eb.zip") - set(espeak_ng_URL2 "https://hub.nuaa.cf/csukuangfj/espeak-ng/archive/69bf6927964fb042aeb827cfdf6082a30f5802eb.zip") - set(espeak_ng_HASH "SHA256=745e35b21ece6804b4a1839722f9e625ac909380c8f85873ad71bf145877075a") + set(espeak_ng_URL "https://github.com/csukuangfj/espeak-ng/archive/f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip") + set(espeak_ng_URL2 "https://hub.nuaa.cf/csukuangfj/espeak-ng/archive/f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip") + set(espeak_ng_HASH "SHA256=70cbf4050e7a014aae19140b05e57249da4720f56128459fbe3a93beaf971ae6") set(BUILD_ESPEAK_NG_TESTS OFF CACHE BOOL "" FORCE) set(USE_ASYNC OFF CACHE BOOL "" FORCE) @@ -21,11 +21,11 @@ function(download_espeak_ng_for_piper) # If you don't have access to the Internet, # please pre-download kaldi-decoder set(possible_file_locations - $ENV{HOME}/Downloads/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip - ${CMAKE_SOURCE_DIR}/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip - ${CMAKE_BINARY_DIR}/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip - /tmp/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip - /star-fj/fangjun/download/github/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip + $ENV{HOME}/Downloads/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip + ${CMAKE_SOURCE_DIR}/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip + ${CMAKE_BINARY_DIR}/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip + /tmp/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip + /star-fj/fangjun/download/github/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip ) foreach(f IN LISTS possible_file_locations) @@ -53,7 +53,23 @@ function(download_espeak_ng_for_piper) message(STATUS "espeak-ng is downloaded to ${espeak_ng_SOURCE_DIR}") message(STATUS "espeak-ng binary dir is ${espeak_ng_BINARY_DIR}") + if(BUILD_SHARED_LIBS) + set(_build_shared_libs_bak ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) + endif() + add_subdirectory(${espeak_ng_SOURCE_DIR} ${espeak_ng_BINARY_DIR}) + + if(_build_shared_libs_bak) + set_target_properties(espeak-ng + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + ) + set(BUILD_SHARED_LIBS ON) + endif() + set(espeak_ng_SOURCE_DIR ${espeak_ng_SOURCE_DIR} PARENT_SCOPE) if(WIN32 AND MSVC) @@ -107,27 +123,11 @@ function(download_espeak_ng_for_piper) ${espeak_ng_SOURCE_DIR}/src/ucd-tools/src/include ) - if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS - espeak-ng - ucd - DESTINATION ..) - else() - install(TARGETS - espeak-ng - ucd - DESTINATION lib) - endif() - if(NOT BUILD_SHARED_LIBS) - install(TARGETS ucd DESTINATION lib) - endif() - - if(WIN32 AND BUILD_SHARED_LIBS) install(TARGETS espeak-ng ucd - DESTINATION bin) + DESTINATION lib) endif() endfunction() diff --git a/cmake/kaldi-decoder.cmake b/cmake/kaldi-decoder.cmake index 02e62e44f..d3d7ec2d5 100644 --- a/cmake/kaldi-decoder.cmake +++ b/cmake/kaldi-decoder.cmake @@ -45,8 +45,25 @@ function(download_kaldi_decoder) message(STATUS "kaldi-decoder's binary dir is ${kaldi_decoder_BINARY_DIR}") include_directories(${kaldi_decoder_SOURCE_DIR}) + + if(BUILD_SHARED_LIBS) + set(_build_shared_libs_bak ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) + endif() + add_subdirectory(${kaldi_decoder_SOURCE_DIR} ${kaldi_decoder_BINARY_DIR} EXCLUDE_FROM_ALL) + if(_build_shared_libs_bak) + set_target_properties( + kaldi-decoder-core + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + ) + set(BUILD_SHARED_LIBS ON) + endif() + if(WIN32 AND MSVC) target_compile_options(kaldi-decoder-core PUBLIC /wd4018 @@ -58,14 +75,7 @@ function(download_kaldi_decoder) INTERFACE ${kaldi-decoder_SOURCE_DIR}/ ) - if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS - kaldi-decoder-core - kaldifst_core - fst - fstfar - DESTINATION ..) - else() + if(NOT BUILD_SHARED_LIBS) install(TARGETS kaldi-decoder-core kaldifst_core @@ -73,15 +83,6 @@ function(download_kaldi_decoder) fstfar DESTINATION lib) endif() - - if(WIN32 AND BUILD_SHARED_LIBS) - install(TARGETS - kaldi-decoder-core - kaldifst_core - fst - fstfar - DESTINATION bin) - endif() endfunction() download_kaldi_decoder() diff --git a/cmake/kaldi-native-fbank.cmake b/cmake/kaldi-native-fbank.cmake index 1d10a01e6..ec2add8b1 100644 --- a/cmake/kaldi-native-fbank.cmake +++ b/cmake/kaldi-native-fbank.cmake @@ -44,20 +44,30 @@ function(download_kaldi_native_fbank) message(STATUS "kaldi-native-fbank is downloaded to ${kaldi_native_fbank_SOURCE_DIR}") message(STATUS "kaldi-native-fbank's binary dir is ${kaldi_native_fbank_BINARY_DIR}") + if(BUILD_SHARED_LIBS) + set(_build_shared_libs_bak ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) + endif() + add_subdirectory(${kaldi_native_fbank_SOURCE_DIR} ${kaldi_native_fbank_BINARY_DIR} EXCLUDE_FROM_ALL) + if(_build_shared_libs_bak) + set_target_properties(kaldi-native-fbank-core + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + ) + set(BUILD_SHARED_LIBS ON) + endif() + target_include_directories(kaldi-native-fbank-core INTERFACE ${kaldi_native_fbank_SOURCE_DIR}/ ) - if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS kaldi-native-fbank-core DESTINATION ..) - else() - install(TARGETS kaldi-native-fbank-core DESTINATION lib) - endif() - if(WIN32 AND BUILD_SHARED_LIBS) - install(TARGETS kaldi-native-fbank-core DESTINATION bin) + if(NOT BUILD_SHARED_LIBS) + install(TARGETS kaldi-native-fbank-core DESTINATION lib) endif() endfunction() diff --git a/cmake/kaldifst.cmake b/cmake/kaldifst.cmake index 5e2130dbe..765e2571a 100644 --- a/cmake/kaldifst.cmake +++ b/cmake/kaldifst.cmake @@ -43,8 +43,23 @@ function(download_kaldifst) list(APPEND CMAKE_MODULE_PATH ${kaldifst_SOURCE_DIR}/cmake) + if(BUILD_SHARED_LIBS) + set(_build_shared_libs_bak ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) + endif() + add_subdirectory(${kaldifst_SOURCE_DIR} ${kaldifst_BINARY_DIR} EXCLUDE_FROM_ALL) + if(_build_shared_libs_bak) + set_target_properties(kaldifst_core + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + ) + set(BUILD_SHARED_LIBS ON) + endif() + target_include_directories(kaldifst_core PUBLIC ${kaldifst_SOURCE_DIR}/ diff --git a/cmake/openfst.cmake b/cmake/openfst.cmake index 77e4c157d..0f5863b7c 100644 --- a/cmake/openfst.cmake +++ b/cmake/openfst.cmake @@ -67,7 +67,24 @@ function(download_openfst) FetchContent_Populate(openfst) endif() message(STATUS "openfst is downloaded to ${openfst_SOURCE_DIR}") + + if(_build_shared_libs_bak) + set(_build_shared_libs_bak ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) + endif() + add_subdirectory(${openfst_SOURCE_DIR} ${openfst_BINARY_DIR} EXCLUDE_FROM_ALL) + + if(_build_shared_libs_bak) + set_target_properties(fst fstfar + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + ) + set(BUILD_SHARED_LIBS ON) + endif() + set(openfst_SOURCE_DIR ${openfst_SOURCE_DIR} PARENT_SCOPE) set_target_properties(fst PROPERTIES OUTPUT_NAME "sherpa-onnx-fst") diff --git a/cmake/piper-phonemize.cmake b/cmake/piper-phonemize.cmake index 4facf7e17..7ecf1791b 100644 --- a/cmake/piper-phonemize.cmake +++ b/cmake/piper-phonemize.cmake @@ -40,8 +40,23 @@ function(download_piper_phonemize) message(STATUS "piper-phonemize is downloaded to ${piper_phonemize_SOURCE_DIR}") message(STATUS "piper-phonemize binary dir is ${piper_phonemize_BINARY_DIR}") + if(BUILD_SHARED_LIBS) + set(_build_shared_libs_bak ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) + endif() + add_subdirectory(${piper_phonemize_SOURCE_DIR} ${piper_phonemize_BINARY_DIR} EXCLUDE_FROM_ALL) + if(_build_shared_libs_bak) + set_target_properties(piper_phonemize + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + ) + set(BUILD_SHARED_LIBS ON) + endif() + if(WIN32 AND MSVC) target_compile_options(piper_phonemize PUBLIC /wd4309 @@ -53,21 +68,11 @@ function(download_piper_phonemize) ${piper_phonemize_SOURCE_DIR}/src/include ) - if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS - piper_phonemize - DESTINATION ..) - else() + if(NOT BUILD_SHARED_LIBS) install(TARGETS piper_phonemize DESTINATION lib) endif() - - if(WIN32 AND BUILD_SHARED_LIBS) - install(TARGETS - piper_phonemize - DESTINATION bin) - endif() endfunction() download_piper_phonemize() diff --git a/cmake/portaudio.cmake b/cmake/portaudio.cmake index 4f176594f..d8af8d49b 100644 --- a/cmake/portaudio.cmake +++ b/cmake/portaudio.cmake @@ -26,13 +26,9 @@ function(download_portaudio) endif() endforeach() - if(BUILD_SHARED_LIBS) - set(PA_BUILD_SHARED ON CACHE BOOL "" FORCE) - set(PA_BUILD_STATIC OFF CACHE BOOL "" FORCE) - else() - set(PA_BUILD_SHARED OFF CACHE BOOL "" FORCE) - set(PA_BUILD_STATIC ON CACHE BOOL "" FORCE) - endif() + # Always use static build + set(PA_BUILD_SHARED OFF CACHE BOOL "" FORCE) + set(PA_BUILD_STATIC ON CACHE BOOL "" FORCE) FetchContent_Declare(portaudio URL @@ -55,28 +51,17 @@ function(download_portaudio) add_subdirectory(${portaudio_SOURCE_DIR} ${portaudio_BINARY_DIR} EXCLUDE_FROM_ALL) - if(BUILD_SHARED_LIBS) - set_target_properties(portaudio PROPERTIES OUTPUT_NAME "sherpa-onnx-portaudio") - if(NOT WIN32) - target_compile_options(portaudio PRIVATE "-Wno-deprecated-declarations") - endif() + set_target_properties(portaudio_static PROPERTIES OUTPUT_NAME "sherpa-onnx-portaudio_static") + if(NOT WIN32) + target_compile_options(portaudio_static PRIVATE "-Wno-deprecated-declarations") + endif() - if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS portaudio DESTINATION ..) - else() - install(TARGETS portaudio DESTINATION lib) - endif() - else() - set_target_properties(portaudio_static PROPERTIES OUTPUT_NAME "sherpa-onnx-portaudio_static") - if(NOT WIN32) - target_compile_options(portaudio_static PRIVATE "-Wno-deprecated-declarations") - endif() - if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS portaudio_static DESTINATION ..) - else() - install(TARGETS portaudio_static DESTINATION lib) - endif() + if(NOT BUILD_SHARED_LIBS AND SHERPA_ONNX_ENABLE_BINARY) + install(TARGETS + portaudio_static + DESTINATION lib) endif() + endfunction() download_portaudio() diff --git a/cmake/sherpa-onnx-shared.pc.in b/cmake/sherpa-onnx-shared.pc.in new file mode 100644 index 000000000..e0b0eea10 --- /dev/null +++ b/cmake/sherpa-onnx-shared.pc.in @@ -0,0 +1,25 @@ +# Note: If you use Python, then the prefix might not be correct. +# +# You need to either manually modify this file to change the prefix to the location +# where this sherpa-onnx.pc file actually resides +# or +# you can use +# +# pkg-config --define-variable=prefix=/path/to/the/dir/containing/this/file --cflags sherpa-onnx + +prefix="@CMAKE_INSTALL_PREFIX@" +exec_prefix="${prefix}" +includedir="${prefix}/include" +libdir="${exec_prefix}/lib" + +Name: sherpa-onnx +Description: pkg-config for sherpa-onnx +URL: https://github.com/k2-fsa/sherpa-onnx + +Version: @SHERPA_ONNX_VERSION@ +Cflags: -I"${includedir}" + +# Note: -lcargs is required only for the following file +# https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c +# We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c +Libs: -L"${libdir}" -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${libdir} @SHERPA_ONNX_PKG_WITH_CARGS@ @SHERPA_ONNX_PKG_CONFIG_EXTRA_LIBS@ diff --git a/cmake/sherpa-onnx-no-tts.pc.in b/cmake/sherpa-onnx-static-no-tts.pc.in similarity index 71% rename from cmake/sherpa-onnx-no-tts.pc.in rename to cmake/sherpa-onnx-static-no-tts.pc.in index 0b020e43c..f100cd95b 100644 --- a/cmake/sherpa-onnx-no-tts.pc.in +++ b/cmake/sherpa-onnx-static-no-tts.pc.in @@ -1,3 +1,12 @@ +# Note: If you use Python, then the prefix might not be correct. +# +# You need to either manually modify this file to change the prefix to the location +# where this sherpa-onnx.pc file actually resides +# or +# you can use +# +# pkg-config --define-variable=prefix=/path/to/the/dir/containing/this/file --cflags sherpa-onnx + prefix="@CMAKE_INSTALL_PREFIX@" exec_prefix="${prefix}" includedir="${prefix}/include" diff --git a/cmake/sherpa-onnx.pc.in b/cmake/sherpa-onnx-static.pc.in similarity index 72% rename from cmake/sherpa-onnx.pc.in rename to cmake/sherpa-onnx-static.pc.in index 87b929a44..1f788b002 100644 --- a/cmake/sherpa-onnx.pc.in +++ b/cmake/sherpa-onnx-static.pc.in @@ -1,3 +1,12 @@ +# Note: If you use Python, then the prefix might not be correct. +# +# You need to either manually modify this file to change the prefix to the location +# where this sherpa-onnx.pc file actually resides +# or +# you can use +# +# pkg-config --define-variable=prefix=/path/to/the/dir/containing/this/file --cflags sherpa-onnx + prefix="@CMAKE_INSTALL_PREFIX@" exec_prefix="${prefix}" includedir="${prefix}/include" diff --git a/cmake/simple-sentencepiece.cmake b/cmake/simple-sentencepiece.cmake index fcdd44425..09a640b11 100644 --- a/cmake/simple-sentencepiece.cmake +++ b/cmake/simple-sentencepiece.cmake @@ -42,22 +42,32 @@ function(download_simple_sentencepiece) FetchContent_Populate(simple-sentencepiece) endif() message(STATUS "simple-sentencepiece is downloaded to ${simple-sentencepiece_SOURCE_DIR}") + + if(BUILD_SHARED_LIBS) + set(_build_shared_libs_bak ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) + endif() + add_subdirectory(${simple-sentencepiece_SOURCE_DIR} ${simple-sentencepiece_BINARY_DIR} EXCLUDE_FROM_ALL) + if(_build_shared_libs_bak) + set_target_properties(ssentencepiece_core + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + ) + set(BUILD_SHARED_LIBS ON) + endif() + target_include_directories(ssentencepiece_core PUBLIC ${simple-sentencepiece_SOURCE_DIR}/ ) - if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS ssentencepiece_core DESTINATION ..) - else() + if(NOT BUILD_SHARED_LIBS) install(TARGETS ssentencepiece_core DESTINATION lib) endif() - - if(WIN32 AND BUILD_SHARED_LIBS) - install(TARGETS ssentencepiece_core DESTINATION bin) - endif() endfunction() download_simple_sentencepiece() diff --git a/dart-api-examples/non-streaming-asr/pubspec.yaml b/dart-api-examples/non-streaming-asr/pubspec.yaml index 783497636..81dbae8c5 100644 --- a/dart-api-examples/non-streaming-asr/pubspec.yaml +++ b/dart-api-examples/non-streaming-asr/pubspec.yaml @@ -10,7 +10,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.9 + sherpa_onnx: ^1.10.10 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/streaming-asr/pubspec.yaml b/dart-api-examples/streaming-asr/pubspec.yaml index 55073f4f4..9dc7e9fc6 100644 --- a/dart-api-examples/streaming-asr/pubspec.yaml +++ b/dart-api-examples/streaming-asr/pubspec.yaml @@ -11,7 +11,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.9 + sherpa_onnx: ^1.10.10 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/tts/pubspec.yaml b/dart-api-examples/tts/pubspec.yaml index 174705fbe..a07aff98f 100644 --- a/dart-api-examples/tts/pubspec.yaml +++ b/dart-api-examples/tts/pubspec.yaml @@ -8,7 +8,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.9 + sherpa_onnx: ^1.10.10 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/vad/pubspec.yaml b/dart-api-examples/vad/pubspec.yaml index 9a6904bdb..5d2b89b8e 100644 --- a/dart-api-examples/vad/pubspec.yaml +++ b/dart-api-examples/vad/pubspec.yaml @@ -9,7 +9,7 @@ environment: sdk: ^3.4.0 dependencies: - sherpa_onnx: ^1.10.9 + sherpa_onnx: ^1.10.10 path: ^1.9.0 args: ^2.5.0 diff --git a/ffmpeg-examples/run.sh b/ffmpeg-examples/run.sh index a0651e00b..deede7168 100755 --- a/ffmpeg-examples/run.sh +++ b/ffmpeg-examples/run.sh @@ -11,7 +11,7 @@ if [ ! -d ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20 ]; then exit 1 fi -if [[ ! -f ../build/lib/libsherpa-onnx-core.a && ! -f ../build/lib/libsherpa-onnx-core.dylib && ! -f ../build/lib/libsherpa-onnx-core.so ]]; then +if [[ ! -f ../build/lib/libsherpa-onnx-c-api.a && ! -f ../build/lib/libsherpa-onnx-c-api.dylib && ! -f ../build/lib/libsherpa-onnx-c-api.so ]]; then echo "Please build sherpa-onnx first. You can use" echo "" echo " cd /path/to/sherpa-onnx" diff --git a/flutter-examples/streaming_asr/pubspec.yaml b/flutter-examples/streaming_asr/pubspec.yaml index c35869217..b30d01f55 100644 --- a/flutter-examples/streaming_asr/pubspec.yaml +++ b/flutter-examples/streaming_asr/pubspec.yaml @@ -5,7 +5,7 @@ description: > publish_to: 'none' -version: 1.10.1 +version: 1.10.10 topics: - speech-recognition @@ -30,7 +30,7 @@ dependencies: record: ^5.1.0 url_launcher: ^6.2.6 - sherpa_onnx: ^1.10.9 + sherpa_onnx: ^1.10.10 # sherpa_onnx: # path: ../../flutter/sherpa_onnx diff --git a/flutter/sherpa_onnx/pubspec.yaml b/flutter/sherpa_onnx/pubspec.yaml index a30ea31e1..e1f2aeb30 100644 --- a/flutter/sherpa_onnx/pubspec.yaml +++ b/flutter/sherpa_onnx/pubspec.yaml @@ -17,7 +17,7 @@ topics: - voice-activity-detection # remember to change the version in ../sherpa_onnx_macos/macos/sherpa_onnx_macos.podspec -version: 1.10.9 +version: 1.10.10 homepage: https://github.com/k2-fsa/sherpa-onnx @@ -30,16 +30,16 @@ dependencies: flutter: sdk: flutter - sherpa_onnx_android: ^1.10.9 + sherpa_onnx_android: ^1.10.10 # path: ../sherpa_onnx_android - sherpa_onnx_macos: ^1.10.9 + sherpa_onnx_macos: ^1.10.10 # path: ../sherpa_onnx_macos - sherpa_onnx_linux: ^1.10.9 + sherpa_onnx_linux: ^1.10.10 # path: ../sherpa_onnx_linux # - sherpa_onnx_windows: ^1.10.9 + sherpa_onnx_windows: ^1.10.10 # path: ../sherpa_onnx_windows flutter: diff --git a/flutter/sherpa_onnx_linux/linux/CMakeLists.txt b/flutter/sherpa_onnx_linux/linux/CMakeLists.txt index 5b3cc73b7..946ed597e 100644 --- a/flutter/sherpa_onnx_linux/linux/CMakeLists.txt +++ b/flutter/sherpa_onnx_linux/linux/CMakeLists.txt @@ -12,16 +12,6 @@ project(${PROJECT_NAME} LANGUAGES CXX) # external build triggered from this build file. set(sherpa_onnx_linux_bundled_libraries "${CMAKE_CURRENT_SOURCE_DIR}/libsherpa-onnx-c-api.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libsherpa-onnx-core.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libkaldi-decoder-core.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libsherpa-onnx-kaldifst-core.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libsherpa-onnx-fstfar.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libsherpa-onnx-fst.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libkaldi-native-fbank-core.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libpiper_phonemize.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libespeak-ng.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libucd.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libonnxruntime.so" - "${CMAKE_CURRENT_SOURCE_DIR}/libssentencepiece_core.so" + "${CMAKE_CURRENT_SOURCE_DIR}/libonnxruntime.so.1.17.1" PARENT_SCOPE ) diff --git a/flutter/sherpa_onnx_windows/windows/CMakeLists.txt b/flutter/sherpa_onnx_windows/windows/CMakeLists.txt index 811931ed1..ecb0ae705 100644 --- a/flutter/sherpa_onnx_windows/windows/CMakeLists.txt +++ b/flutter/sherpa_onnx_windows/windows/CMakeLists.txt @@ -13,16 +13,6 @@ project(${PROJECT_NAME} LANGUAGES CXX) # external build triggered from this build file. set(sherpa_onnx_windows_bundled_libraries "${CMAKE_CURRENT_SOURCE_DIR}/sherpa-onnx-c-api.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/sherpa-onnx-core.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/kaldi-decoder-core.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/sherpa-onnx-kaldifst-core.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/sherpa-onnx-fstfar.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/sherpa-onnx-fst.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/kaldi-native-fbank-core.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/piper_phonemize.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/espeak-ng.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/ucd.dll" "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime.dll" - "${CMAKE_CURRENT_SOURCE_DIR}/ssentencepiece_core.dll" PARENT_SCOPE ) diff --git a/kotlin-api-examples/run.sh b/kotlin-api-examples/run.sh index 5b58620e1..81f1198d3 100755 --- a/kotlin-api-examples/run.sh +++ b/kotlin-api-examples/run.sh @@ -14,6 +14,7 @@ if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa -DSHERPA_ONNX_ENABLE_TESTS=OFF \ -DSHERPA_ONNX_ENABLE_CHECK=OFF \ -DBUILD_SHARED_LIBS=ON \ + -DBUILD_SHARED_LIBS=ON \ -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ -DSHERPA_ONNX_ENABLE_JNI=ON \ .. diff --git a/scripts/dart/release.sh b/scripts/dart/release.sh index d1d728364..e3f8208a4 100755 --- a/scripts/dart/release.sh +++ b/scripts/dart/release.sh @@ -44,10 +44,6 @@ function process_linux() { pushd linux - rm -v libpiper_phonemize.so libpiper_phonemize.so.1.2.0 - rm -v libonnxruntime.so - rm -v libcargs.so - popd } @@ -69,12 +65,6 @@ function process_macos() { cp -v sherpa_onnx/lib/*.dylib ../macos cd .. rm -rf t - - pushd macos - rm -v libcargs.dylib - rm -v libonnxruntime.dylib - rm -v libpiper_phonemize.1.2.0.dylib libpiper_phonemize.dylib - popd } process_linux diff --git a/scripts/dotnet/generate.py b/scripts/dotnet/generate.py index ad6e5b91e..71b854f92 100755 --- a/scripts/dotnet/generate.py +++ b/scripts/dotnet/generate.py @@ -36,18 +36,8 @@ def get_dict(): def process_linux(s): libs = [ - "libespeak-ng.so", - "libkaldi-decoder-core.so", - "libkaldi-native-fbank-core.so", "libonnxruntime.so.1.17.1", - "libssentencepiece_core.so", - "libpiper_phonemize.so.1", "libsherpa-onnx-c-api.so", - "libsherpa-onnx-core.so", - "libsherpa-onnx-fstfar.so", - "libsherpa-onnx-fst.so", - "libsherpa-onnx-kaldifst-core.so", - "libucd.so", ] prefix = f"{src_dir}/linux/" libs = [prefix + lib for lib in libs] @@ -66,18 +56,8 @@ def process_linux(s): def process_macos(s, rid): libs = [ - "libespeak-ng.dylib", - "libkaldi-decoder-core.dylib", - "libkaldi-native-fbank-core.dylib", "libonnxruntime.1.17.1.dylib", - "libssentencepiece_core.dylib", - "libpiper_phonemize.1.dylib", "libsherpa-onnx-c-api.dylib", - "libsherpa-onnx-core.dylib", - "libsherpa-onnx-fstfar.dylib", - "libsherpa-onnx-fst.dylib", - "libsherpa-onnx-kaldifst-core.dylib", - "libucd.dylib", ] prefix = f"{src_dir}/macos-{rid}/" libs = [prefix + lib for lib in libs] @@ -96,18 +76,8 @@ def process_macos(s, rid): def process_windows(s, rid): libs = [ - "espeak-ng.dll", - "kaldi-decoder-core.dll", - "kaldi-native-fbank-core.dll", "onnxruntime.dll", - "ssentencepiece_core.dll", - "piper_phonemize.dll", "sherpa-onnx-c-api.dll", - "sherpa-onnx-core.dll", - "sherpa-onnx-fstfar.dll", - "sherpa-onnx-fst.dll", - "sherpa-onnx-kaldifst-core.dll", - "ucd.dll", ] version = get_version() diff --git a/scripts/dotnet/run.sh b/scripts/dotnet/run.sh index 8beb8f34d..9f2890b43 100755 --- a/scripts/dotnet/run.sh +++ b/scripts/dotnet/run.sh @@ -44,7 +44,7 @@ windows_x86_wheel=$src_dir/$windows_x86_wheel_filename windows_arm64_wheel_filename=sherpa-onnx-${SHERPA_ONNX_VERSION}-win-arm64.tar.bz2 windows_arm64_wheel=$src_dir/$windows_arm64_wheel_filename -if [ ! -f $src_dir/linux/libsherpa-onnx-core.so ]; then +if [ ! -f $src_dir/linux/libsherpa-onnx-c-api.so ]; then echo "---linux x86_64---" cd linux mkdir -p wheel @@ -57,15 +57,12 @@ if [ ! -f $src_dir/linux/libsherpa-onnx-core.so ]; then unzip $linux_wheel_filename cp -v sherpa_onnx/lib/*.so* ../ cd .. - rm -fv libpiper_phonemize.so libpiper_phonemize.so.1.2.0 - rm -fv libonnxruntime.so - rm -fv libcargs.so rm -rf wheel ls -lh cd .. fi -if [ ! -f $src_dir/macos-x64/libsherpa-onnx-core.dylib ]; then +if [ ! -f $src_dir/macos-x64/libsherpa-onnx-c-api.dylib ]; then echo "--- macOS x86_64---" cd macos-x64 mkdir -p wheel @@ -80,15 +77,12 @@ if [ ! -f $src_dir/macos-x64/libsherpa-onnx-core.dylib ]; then cd .. - rm -fv libcargs.dylib - rm -fv libonnxruntime.dylib - rm -fv libpiper_phonemize.1.2.0.dylib libpiper_phonemize.dylib rm -rf wheel ls -lh cd .. fi -if [ ! -f $src_dir/macos-arm64/libsherpa-onnx-core.dylib ]; then +if [ ! -f $src_dir/macos-arm64/libsherpa-onnx-c-api.dylib ]; then echo "--- macOS arm64---" cd macos-arm64 mkdir -p wheel @@ -103,15 +97,12 @@ if [ ! -f $src_dir/macos-arm64/libsherpa-onnx-core.dylib ]; then cd .. - rm -fv libcargs.dylib - rm -fv libonnxruntime.dylib - rm -fv libpiper_phonemize.1.2.0.dylib libpiper_phonemize.dylib rm -rf wheel ls -lh cd .. fi -if [ ! -f $src_dir/windows-x64/sherpa-onnx-core.dll ]; then +if [ ! -f $src_dir/windows-x64/sherpa-onnx-c-api.dll ]; then echo "---windows x64---" cd windows-x64 mkdir -p wheel @@ -130,7 +121,7 @@ if [ ! -f $src_dir/windows-x64/sherpa-onnx-core.dll ]; then cd .. fi -if [ ! -f $src_dir/windows-x86/sherpa-onnx-core.dll ]; then +if [ ! -f $src_dir/windows-x86/sherpa-onnx-c-api.dll ]; then echo "---windows x86---" cd windows-x86 mkdir -p wheel @@ -149,7 +140,7 @@ if [ ! -f $src_dir/windows-x86/sherpa-onnx-core.dll ]; then cd .. fi -if [ ! -f $src_dir/windows-arm64/sherpa-onnx-core.dll ]; then +if [ ! -f $src_dir/windows-arm64/sherpa-onnx-c-api.dll ]; then echo "---windows arm64---" cd windows-arm64 mkdir -p wheel diff --git a/scripts/go/_internal/build_darwin_amd64.go b/scripts/go/_internal/build_darwin_amd64.go index 4e55c0111..eda1649e3 100644 --- a/scripts/go/_internal/build_darwin_amd64.go +++ b/scripts/go/_internal/build_darwin_amd64.go @@ -2,5 +2,5 @@ package sherpa_onnx -// #cgo LDFLAGS: -L ${SRCDIR}/lib/x86_64-apple-darwin -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-native-fbank-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lpiper_phonemize -lespeak-ng -lucd -lonnxruntime -lssentencepiece_core -Wl,-rpath,${SRCDIR}/lib/x86_64-apple-darwin +// #cgo LDFLAGS: -L ${SRCDIR}/lib/x86_64-apple-darwin -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${SRCDIR}/lib/x86_64-apple-darwin import "C" diff --git a/scripts/go/_internal/build_darwin_arm64.go b/scripts/go/_internal/build_darwin_arm64.go index 46aa58326..2ca255c8d 100644 --- a/scripts/go/_internal/build_darwin_arm64.go +++ b/scripts/go/_internal/build_darwin_arm64.go @@ -2,5 +2,5 @@ package sherpa_onnx -// #cgo LDFLAGS: -L ${SRCDIR}/lib/aarch64-apple-darwin -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-native-fbank-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lpiper_phonemize -lespeak-ng -lucd -lonnxruntime -lssentencepiece_core -Wl,-rpath,${SRCDIR}/lib/aarch64-apple-darwin +// #cgo LDFLAGS: -L ${SRCDIR}/lib/aarch64-apple-darwin -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${SRCDIR}/lib/aarch64-apple-darwin import "C" diff --git a/scripts/go/_internal/build_linux_amd64.go b/scripts/go/_internal/build_linux_amd64.go new file mode 100644 index 000000000..518821c23 --- /dev/null +++ b/scripts/go/_internal/build_linux_amd64.go @@ -0,0 +1,6 @@ +//go:build !android && linux && amd64 && !musl + +package sherpa_onnx + +// #cgo LDFLAGS: -L ${SRCDIR}/lib/x86_64-unknown-linux-gnu -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${SRCDIR}/lib/x86_64-unknown-linux-gnu +import "C" diff --git a/scripts/go/_internal/build_linux_arm.go b/scripts/go/_internal/build_linux_arm.go new file mode 100644 index 000000000..acf6d1186 --- /dev/null +++ b/scripts/go/_internal/build_linux_arm.go @@ -0,0 +1,6 @@ +//go:build linux && arm && !arm7 + +package sherpa_onnx + +// #cgo LDFLAGS: -L ${SRCDIR}/lib/arm-unknown-linux-gnueabihf -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${SRCDIR}/lib/arm-unknown-linux-gnueabihf +import "C" diff --git a/scripts/go/_internal/build_linux_arm64.go b/scripts/go/_internal/build_linux_arm64.go new file mode 100644 index 000000000..0bcb60b86 --- /dev/null +++ b/scripts/go/_internal/build_linux_arm64.go @@ -0,0 +1,6 @@ +//go:build linux && arm64 + +package sherpa_onnx + +// #cgo LDFLAGS: -L ${SRCDIR}/lib/aarch64-unknown-linux-gnu -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-native-fbank-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lpiper_phonemize -lespeak-ng -lucd -lonnxruntime -lssentencepiece_core -Wl,-rpath,${SRCDIR}/lib/aarch64-unknown-linux-gnu +import "C" diff --git a/scripts/go/_internal/build_windows_386.go b/scripts/go/_internal/build_windows_386.go new file mode 100644 index 000000000..89d589c64 --- /dev/null +++ b/scripts/go/_internal/build_windows_386.go @@ -0,0 +1,6 @@ +//go:build windows && 386 + +package sherpa_onnx + +// #cgo LDFLAGS: -L ${SRCDIR}/lib/i686-pc-windows-gnu -lsherpa-onnx-c-api -lonnxruntime +import "C" diff --git a/scripts/go/_internal/build_windows_amd64.go b/scripts/go/_internal/build_windows_amd64.go new file mode 100644 index 000000000..27334c63a --- /dev/null +++ b/scripts/go/_internal/build_windows_amd64.go @@ -0,0 +1,6 @@ +//go:build windows && amd64 + +package sherpa_onnx + +// #cgo LDFLAGS: -L ${SRCDIR}/lib/x86_64-pc-windows-gnu -lsherpa-onnx-c-api -lonnxruntime +import "C" diff --git a/scripts/go/_internal/lib/x86_64-pc-windows-gnu/.gitkeep b/scripts/go/_internal/lib/x86_64-pc-windows-gnu/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/scripts/go/_internal/lib/x86_64-unknown-linux-gnu b/scripts/go/_internal/lib/x86_64-unknown-linux-gnu new file mode 120000 index 000000000..b21b8dfab --- /dev/null +++ b/scripts/go/_internal/lib/x86_64-unknown-linux-gnu @@ -0,0 +1 @@ +../../../../build/lib \ No newline at end of file diff --git a/scripts/go/release.sh b/scripts/go/release.sh index d46eb1cf7..10ae9f71a 100755 --- a/scripts/go/release.sh +++ b/scripts/go/release.sh @@ -17,8 +17,11 @@ echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" function linux() { echo "Process linux" git clone git@github.com:k2-fsa/sherpa-onnx-go-linux.git + rm -v ./sherpa-onnx-go-linux/*.go + cp -v ./sherpa_onnx.go ./sherpa-onnx-go-linux/ cp -v ./_internal/c-api.h ./sherpa-onnx-go-linux/ + cp -v ./_internal/build_linux_*.go ./sherpa-onnx-go-linux/ rm -rf sherpa-onnx-go-linux/lib/x86_64-unknown-linux-gnu/lib* dst=$(realpath sherpa-onnx-go-linux/lib/x86_64-unknown-linux-gnu) @@ -29,6 +32,10 @@ function linux() { cp -v sherpa_onnx/lib/*.so* $dst + pushd $dst + cp -v libonnxruntime.so.1.17.1 libonnxruntime.so + popd + cd .. rm -rf t @@ -41,6 +48,10 @@ function linux() { cp -v sherpa_onnx/lib/*.so* $dst + pushd $dst + cp -v libonnxruntime.so.1.17.1 libonnxruntime.so + popd + cd .. rm -rf t @@ -53,6 +64,10 @@ function linux() { cp -v sherpa_onnx/lib/*.so* $dst + pushd $dst + cp -v libonnxruntime.so.1.17.1 libonnxruntime.so + popd + cd .. rm -rf t @@ -71,8 +86,10 @@ function linux() { function osx() { echo "Process osx-x64" git clone git@github.com:k2-fsa/sherpa-onnx-go-macos.git + rm -v ./sherpa-onnx-go-macos/*.go cp -v ./sherpa_onnx.go ./sherpa-onnx-go-macos/ cp -v ./_internal/c-api.h ./sherpa-onnx-go-macos/ + cp -v ./_internal/build_darwin_*.go ./sherpa-onnx-go-macos/ rm -rf sherpa-onnx-go-macos/lib/x86_64-apple-darwin/lib* dst=$(realpath sherpa-onnx-go-macos/lib/x86_64-apple-darwin/) @@ -84,6 +101,10 @@ function osx() { cp -v sherpa_onnx/lib/*.dylib $dst/ + pushd $dst + cp -v libonnxruntime.1.17.1.dylib libonnxruntime.dylib + popd + cd .. rm -rf t @@ -98,6 +119,10 @@ function osx() { cp -v sherpa_onnx/lib/*.dylib $dst/ + pushd $dst + cp -v libonnxruntime.1.17.1.dylib libonnxruntime.dylib + popd + cd .. rm -rf t echo "------------------------------" @@ -115,8 +140,10 @@ function osx() { function windows() { echo "Process windows" git clone git@github.com:k2-fsa/sherpa-onnx-go-windows.git + rm -v ./sherpa-onnx-go-windows/*.go cp -v ./sherpa_onnx.go ./sherpa-onnx-go-windows/ cp -v ./_internal/c-api.h ./sherpa-onnx-go-windows/ + cp -v ./_internal/build_windows_*.go ./sherpa-onnx-go-windows/ rm -fv sherpa-onnx-go-windows/lib/x86_64-pc-windows-gnu/* dst=$(realpath sherpa-onnx-go-windows/lib/x86_64-pc-windows-gnu) diff --git a/scripts/node-addon-api/CMakeLists.txt b/scripts/node-addon-api/CMakeLists.txt index a49f9b98d..f21573698 100644 --- a/scripts/node-addon-api/CMakeLists.txt +++ b/scripts/node-addon-api/CMakeLists.txt @@ -67,15 +67,6 @@ target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) target_link_libraries(${PROJECT_NAME} sherpa-onnx-c-api - sherpa-onnx-core - kaldi-decoder-core - sherpa-onnx-kaldifst-core - sherpa-onnx-fstfar - sherpa-onnx-fst - kaldi-native-fbank-core - piper_phonemize - espeak-ng - ucd onnxruntime -Wl,-rpath,$ENV{SHERPA_ONNX_INSTALL_DIR}/lib ) diff --git a/scripts/node-addon-api/run.sh b/scripts/node-addon-api/run.sh index ef4e80c0d..58f3b5c53 100755 --- a/scripts/node-addon-api/run.sh +++ b/scripts/node-addon-api/run.sh @@ -2,11 +2,16 @@ set -ex -if [[ ! -f ../../build/install/lib/libsherpa-onnx-core.dylib && ! -f ../../build/install/lib/libsherpa-onnx-core.so ]]; then +if [[ ! -f ../../build/install/lib/libsherpa-onnx-c-api.dylib && ! -f ../../build/install/lib/libsherpa-c-api.so ]]; then pushd ../../ mkdir -p build cd build - cmake -DCMAKE_INSTALL_PREFIX=./install -DBUILD_SHARED_LIBS=ON .. + + cmake \ + -DCMAKE_INSTALL_PREFIX=./install \ + -DBUILD_SHARED_LIBS=ON \ + .. + make install popd fi diff --git a/sherpa-onnx/csrc/CMakeLists.txt b/sherpa-onnx/csrc/CMakeLists.txt index 48d0c2587..b6bda8ba9 100644 --- a/sherpa-onnx/csrc/CMakeLists.txt +++ b/sherpa-onnx/csrc/CMakeLists.txt @@ -155,7 +155,18 @@ endif() if(SHERPA_ONNX_ENABLE_CHECK) list(APPEND sources log.cc) endif() -add_library(sherpa-onnx-core ${sources}) + +# Always static build +add_library(sherpa-onnx-core STATIC ${sources}) + +set_target_properties( + sherpa-onnx-core + PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden +) + if(APPLE) target_compile_options(sherpa-onnx-core PRIVATE -Wno-deprecated-declarations @@ -213,6 +224,7 @@ if(SHERPA_ONNX_ENABLE_CHECK) endif() if(NOT BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL Linux) + # This is for linux arm32 and arm64 target_link_libraries(sherpa-onnx-core -ldl) endif() @@ -264,16 +276,10 @@ if(SHERPA_ONNX_ENABLE_BINARY) endif() endif() -if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32) - install(TARGETS sherpa-onnx-core DESTINATION ..) -else() +if(NOT BUILD_SHARED_LIBS) install(TARGETS sherpa-onnx-core DESTINATION lib) endif() -if(WIN32 AND BUILD_SHARED_LIBS) - install(TARGETS sherpa-onnx-core DESTINATION bin) -endif() - if(SHERPA_ONNX_ENABLE_BINARY) install( TARGETS @@ -311,6 +317,9 @@ if(SHERPA_ONNX_HAS_ALSA AND SHERPA_ONNX_ENABLE_BINARY) ) endif() + # # To fix the following error for Windows when building exe + # # mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_Dynamic Release' + foreach(exe IN LISTS exes) target_link_libraries(${exe} sherpa-onnx-core) endforeach() @@ -386,12 +395,6 @@ if(SHERPA_ONNX_ENABLE_PORTAUDIO AND SHERPA_ONNX_ENABLE_BINARY) microphone.cc ) - if(BUILD_SHARED_LIBS) - set(PA_LIB portaudio) - else() - set(PA_LIB portaudio_static) - endif() - set(exes sherpa-onnx-microphone sherpa-onnx-keyword-spotter-microphone @@ -408,7 +411,7 @@ if(SHERPA_ONNX_ENABLE_PORTAUDIO AND SHERPA_ONNX_ENABLE_BINARY) endif() foreach(exe IN LISTS exes) - target_link_libraries(${exe} ${PA_LIB} sherpa-onnx-core) + target_link_libraries(${exe} portaudio_static sherpa-onnx-core) endforeach() if(NOT WIN32) @@ -473,7 +476,7 @@ if(SHERPA_ONNX_ENABLE_WEBSOCKET AND SHERPA_ONNX_ENABLE_BINARY) target_link_libraries(sherpa-onnx-offline-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib") target_link_libraries(sherpa-onnx-offline-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../../../sherpa_onnx/lib") - if(SHERPA_ONNX_ENABLE_PYTHON) + if(SHERPA_ONNX_ENABLE_PYTHON AND NOT WIN32) target_link_libraries(sherpa-onnx-online-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib/python${PYTHON_VERSION}/site-packages/sherpa_onnx/lib") target_link_libraries(sherpa-onnx-online-websocket-client "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib/python${PYTHON_VERSION}/site-packages/sherpa_onnx/lib") target_link_libraries(sherpa-onnx-offline-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib/python${PYTHON_VERSION}/site-packages/sherpa_onnx/lib") @@ -550,4 +553,3 @@ add_custom_target( DEPENDS ${sources}) add_custom_target(check DEPENDS clang-tidy-check) - diff --git a/sherpa-onnx/jni/CMakeLists.txt b/sherpa-onnx/jni/CMakeLists.txt index 64944b4a9..998379084 100644 --- a/sherpa-onnx/jni/CMakeLists.txt +++ b/sherpa-onnx/jni/CMakeLists.txt @@ -35,5 +35,8 @@ endif() add_library(sherpa-onnx-jni ${sources}) +target_compile_definitions(sherpa-onnx-jni PRIVATE SHERPA_ONNX_BUILD_SHARED_LIBS=1) +target_compile_definitions(sherpa-onnx-jni PRIVATE SHERPA_ONNX_BUILD_MAIN_LIB=1) + target_link_libraries(sherpa-onnx-jni sherpa-onnx-core) install(TARGETS sherpa-onnx-jni DESTINATION lib) diff --git a/sherpa-onnx/jni/common.h b/sherpa-onnx/jni/common.h index fede5421c..cb7dabe74 100644 --- a/sherpa-onnx/jni/common.h +++ b/sherpa-onnx/jni/common.h @@ -12,11 +12,31 @@ #include "android/asset_manager_jni.h" #endif +#if defined(_WIN32) +#if defined(SHERPA_ONNX_BUILD_SHARED_LIBS) +#define SHERPA_ONNX_EXPORT __declspec(dllexport) +#define SHERPA_ONNX_IMPORT __declspec(dllimport) +#else +#define SHERPA_ONNX_EXPORT +#define SHERPA_ONNX_IMPORT +#endif +#else // WIN32 +#define SHERPA_ONNX_EXPORT __attribute__((visibility("default"))) + +#define SHERPA_ONNX_IMPORT SHERPA_ONNX_EXPORT +#endif // WIN32 + +#if defined(SHERPA_ONNX_BUILD_MAIN_LIB) +#define SHERPA_ONNX_API SHERPA_ONNX_EXPORT +#else +#define SHERPA_ONNX_API SHERPA_ONNX_IMPORT +#endif + // If you use ndk, you can find "jni.h" inside // android-ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include #include "jni.h" // NOLINT -#define SHERPA_ONNX_EXTERN_C extern "C" +#define SHERPA_ONNX_EXTERN_C extern "C" SHERPA_ONNX_API // defined in jni.cc jobject NewInteger(JNIEnv *env, int32_t value);