From 5e096b3ae152997cf43f66947a5eef4c14da9934 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 19 Nov 2024 20:04:49 +0800 Subject: [PATCH] Support cross-compiling for HarmonyOS --- .github/workflows/harmony-os.yaml | 159 ++++++++++++++++++++++++++++++ CMakeLists.txt | 7 +- build-ohos-arm64-v8a.sh | 116 ++++++++++++++++++++++ build-ohos-armeabi-v7a.sh | 120 ++++++++++++++++++++++ build-ohos-x86-64.sh | 116 ++++++++++++++++++++++ 5 files changed, 517 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/harmony-os.yaml create mode 100755 build-ohos-arm64-v8a.sh create mode 100755 build-ohos-armeabi-v7a.sh create mode 100755 build-ohos-x86-64.sh diff --git a/.github/workflows/harmony-os.yaml b/.github/workflows/harmony-os.yaml new file mode 100644 index 000000000..e1a2ae1a2 --- /dev/null +++ b/.github/workflows/harmony-os.yaml @@ -0,0 +1,159 @@ +name: harmony-os + +on: + push: + branches: + - master + - ohos + tags: + - 'v[0-9]+.[0-9]+.[0-9]+*' + + workflow_dispatch: + +concurrency: + group: harmony-os-${{ github.ref }} + cancel-in-progress: true + +jobs: + harmony_os: + name: Harmony OS ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + arch: [arm64-v8a, armeabi-v7a, x86_64] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ohos-${{ matrix.arch }} + + - name: cache-toolchain + id: cache-toolchain-ohos + uses: actions/cache@v4 + with: + path: command-line-tools + key: commandline-tools-linux-x64-5.0.5.200.zip + + - name: Download toolchain + if: steps.cache-toolchain-ohos.outputs.cache-hit != 'true' + shell: bash + run: | + curl -SL -O https://huggingface.co/csukuangfj/harmonyos-commandline-tools/resolve/main/commandline-tools-linux-x64-5.0.5.200.zip + unzip commandline-tools-linux-x64-5.0.5.200.zip + rm commandline-tools-linux-x64-5.0.5.200.zip + + - name: Set environment variable + shell: bash + run: | + echo "$GITHUB_WORKSPACE/command-line-tools/sdk/default/openharmony/native/build-tools/cmake/bin" >> "$GITHUB_PATH" + which cmake + + cmake --version + + ls -lh $GITHUB_WORKSPACE/command-line-tools/sdk/default/openharmony/native/build/cmake/ohos.toolchain.cmake + + echo "====" + cat $GITHUB_WORKSPACE/command-line-tools/sdk/default/openharmony/native/build/cmake/ohos.toolchain.cmake + echo "====" + + # echo "$GITHUB_WORKSPACE/command-line-tools/sdk/default/openharmony/native/llvm/bin" >> "$GITHUB_PATH" + + ls -lh $GITHUB_WORKSPACE/command-line-tools/sdk/default/openharmony/native/llvm/bin/ + echo "--" + ls -lh $GITHUB_WORKSPACE/command-line-tools/sdk/default/openharmony/native/llvm/bin/*unknown* + + cat $GITHUB_PATH + + # /home/runner/work/onnxruntime-libs/onnxruntime-libs/command-line-tools/sdk/default/openharmony/native/llvm/bin/aarch64-unknown-linux-ohos-clang -v || true + export PATH=$PWD/command-line-tools/sdk/default/openharmony/native/llvm/bin:$PATH + echo "path: $PATH" + + which aarch64-unknown-linux-ohos-clang++ || true + which aarch64-unknown-linux-ohos-clang || true + + aarch64-unknown-linux-ohos-clang++ --version || true + aarch64-unknown-linux-ohos-clang --version || true + + which armv7-unknown-linux-ohos-clang++ + which armv7-unknown-linux-ohos-clang + + armv7-unknown-linux-ohos-clang++ --version + armv7-unknown-linux-ohos-clang --version + + which x86_64-unknown-linux-ohos-clang++ + which x86_64-unknown-linux-ohos-clang + + x86_64-unknown-linux-ohos-clang++ --version + x86_64-unknown-linux-ohos-clang --version + + - name: Build ${{ matrix.arch }} + shell: bash + run: | + export CMAKE_CXX_COMPILER_LAUNCHER=ccache + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" + cmake --version + + arch=${{ matrix.arch }} + + echo "arch: $arch" + + export OHOS_SDK_NATIVE_DIR="$GITHUB_WORKSPACE/command-line-tools/sdk/default/openharmony/native" + + if [[ $arch == arm64-v8a ]]; then + ./build-ohos-arm64-v8a.sh + elif [[ $arch == armeabi-v7a ]]; then + ./build-ohos-armeabi-v7a.sh + elif [[ $arch == x86_64 ]]; then + ./build-ohos-x86-64.sh + else + echo "Unknown arch $arch" + fi + + - name: Collect result for ${{ matrix.arch }} + shell: bash + run: | + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) + echo "SHERPA_ONNX_VERSION=$SHERPA_ONNX_VERSION" >> "$GITHUB_ENV" + + arch=${{ matrix.arch }} + d=sherpa-onnx-$SHERPA_ONNX_VERSION-ohos-$arch + if [[ $arch == x86_64 ]]; then + cd ./build-ohos-x86-64 + else + cd ./build-ohos-$arch + fi + + mv install $d + tar cjfv $d.tar.bz2 $d + + ls -lh $d/lib + + + file $d/lib/* + + readelf -d $d/lib/libsherpa-onnx-c-api.so + + mv $d.tar.bz2 ../ + + - uses: actions/upload-artifact@v4 + with: + name: sherpa-onnx-ohos-${{ matrix.arch }} + path: ./*.tar.bz2 + + - name: Release jar + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/') + uses: svenstaro/upload-release-action@v2 + with: + file_glob: true + overwrite: true + file: ./*.tar.bz2 + # repo_name: k2-fsa/sherpa-onnx + # repo_token: ${{ secrets.UPLOAD_GH_SHERPA_ONNX_TOKEN }} + # tag: v1.10.23 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4adc3dca5..d6f94c668 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,11 @@ if(MSVC) ) endif() +if(CMAKE_SYSTEM_NAME STREQUAL OHOS) + set(CMAKE_CXX_FLAGS "-Wno-unused-command-line-argument ${CMAKE_CXX_FLAGS}") + set(CMAKE_C_FLAGS "-Wno-unused-command-line-argument ${CMAKE_C_FLAGS}") +endif() + message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}") message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}") message(STATUS "BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}") @@ -263,7 +268,7 @@ message(STATUS "C++ Standard version: ${CMAKE_CXX_STANDARD}") include(CheckIncludeFileCXX) -if(UNIX AND NOT APPLE AND NOT SHERPA_ONNX_ENABLE_WASM AND NOT CMAKE_SYSTEM_NAME STREQUAL Android) +if(UNIX AND NOT APPLE AND NOT SHERPA_ONNX_ENABLE_WASM AND NOT CMAKE_SYSTEM_NAME STREQUAL Android AND NOT CMAKE_SYSTEM_NAME STREQUAL OHOS) check_include_file_cxx(alsa/asoundlib.h SHERPA_ONNX_HAS_ALSA) if(SHERPA_ONNX_HAS_ALSA) message(STATUS "With Alsa") diff --git a/build-ohos-arm64-v8a.sh b/build-ohos-arm64-v8a.sh new file mode 100755 index 000000000..3cf07b412 --- /dev/null +++ b/build-ohos-arm64-v8a.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +set -ex + +dir=$PWD/build-ohos-arm64-v8a + +mkdir -p $dir +cd $dir + +# Please first download the commandline tools from +# https://developer.huawei.com/consumer/cn/download/ +# +# Example filename on Linux: commandline-tools-linux-x64-5.0.5.200.zip +# You can also download it from https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main + +# mkdir /star-fj/fangjun/software/huawei +# cd /star-fj/fangjun/software/huawei +# wget https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/resolve/main/commandline-tools-linux-x64-5.0.5.200.zip +# unzip commandline-tools-linux-x64-5.0.5.200.zip +# rm commandline-tools-linux-x64-5.0.5.200.zip +if [ -z $OHOS_SDK_NATIVE_DIR ]; then + OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ + # You can find the following content inside OHOS_SDK_NATIVE_DIR + # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ + # total 524K + # -rw-r--r-- 1 kuangfangjun root 501K Jan 1 2001 NOTICE.txt + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 build + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 build-tools + # -rw-r--r-- 1 kuangfangjun root 371 Jan 1 2001 compatible_config.json + # drwxr-xr-x 4 kuangfangjun root 0 Nov 6 22:36 docs + # drwxr-xr-x 10 kuangfangjun root 0 Nov 6 22:36 llvm + # -rw-r--r-- 1 kuangfangjun root 16K Jan 1 2001 nativeapi_syscap_config.json + # -rw-r--r-- 1 kuangfangjun root 5.9K Jan 1 2001 ndk_system_capability.json + # -rw-r--r-- 1 kuangfangjun root 167 Jan 1 2001 oh-uni-package.json + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot +fi + +if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/aarch64-unknown-linux-ohos-clang ]; then + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/aarch64-unknown-linux-ohos-clang does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH +export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH + +OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake + +if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then + echo "$OHOS_TOOLCHAIN_FILE does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + + +sleep 1 +onnxruntime_version=1.16.3 +onnxruntime_dir=onnxruntime-ohos-arm64-v8a-$onnxruntime_version + +if [ ! -f $onnxruntime_dir/lib/libonnxruntime.so ]; then + wget -c -q https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${onnxruntime_version}/$onnxruntime_dir.zip + unzip $onnxruntime_dir.zip + rm $onnxruntime_dir.zip +fi + +export SHERPA_ONNXRUNTIME_LIB_DIR=$dir/$onnxruntime_dir/lib +export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$dir/$onnxruntime_dir/include + +echo "SHERPA_ONNXRUNTIME_LIB_DIR: $SHERPA_ONNXRUNTIME_LIB_DIR" +echo "SHERPA_ONNXRUNTIME_INCLUDE_DIR $SHERPA_ONNXRUNTIME_INCLUDE_DIR" + +if [ -z $SHERPA_ONNX_ENABLE_TTS ]; then + SHERPA_ONNX_ENABLE_TTS=ON +fi + +if [ -z $SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION ]; then + SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=ON +fi + +if [ -z $SHERPA_ONNX_ENABLE_BINARY ]; then + SHERPA_ONNX_ENABLE_BINARY=OFF +fi + +cmake \ + -DOHOS_ARCH=arm64-v8a \ + -DCMAKE_TOOLCHAIN_FILE=$OHOS_TOOLCHAIN_FILE \ + -DSHERPA_ONNX_ENABLE_TTS=$SHERPA_ONNX_ENABLE_TTS \ + -DSHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=$SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION \ + -DSHERPA_ONNX_ENABLE_BINARY=$SHERPA_ONNX_ENABLE_BINARY \ + -DBUILD_PIPER_PHONMIZE_EXE=OFF \ + -DBUILD_PIPER_PHONMIZE_TESTS=OFF \ + -DBUILD_ESPEAK_NG_EXE=OFF \ + -DBUILD_ESPEAK_NG_TESTS=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ + -DSHERPA_ONNX_ENABLE_TESTS=OFF \ + -DSHERPA_ONNX_ENABLE_CHECK=OFF \ + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ + -DSHERPA_ONNX_ENABLE_JNI=OFF \ + -DSHERPA_ONNX_ENABLE_C_API=ON \ + -DCMAKE_INSTALL_PREFIX=./install \ + .. + +# make VERBOSE=1 -j4 +make -j2 +make install/strip +cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib +cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/aarch64-linux-ohos/libc++_shared.so install/lib + +rm -rf install/share +rm -rf install/lib/pkgconfig diff --git a/build-ohos-armeabi-v7a.sh b/build-ohos-armeabi-v7a.sh new file mode 100755 index 000000000..b8eb91774 --- /dev/null +++ b/build-ohos-armeabi-v7a.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +set -ex + +dir=$PWD/build-ohos-armeabi-v7a + +mkdir -p $dir +cd $dir + +# Please first download the commandline tools from +# https://developer.huawei.com/consumer/cn/download/ +# +# Example filename on Linux: commandline-tools-linux-x64-5.0.5.200.zip +# You can also download it from https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main + +# mkdir /star-fj/fangjun/software/huawei +# cd /star-fj/fangjun/software/huawei +# wget https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/resolve/main/commandline-tools-linux-x64-5.0.5.200.zip +# unzip commandline-tools-linux-x64-5.0.5.200.zip +# rm commandline-tools-linux-x64-5.0.5.200.zip +if [ -z $OHOS_SDK_NATIVE_DIR ]; then + OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ + # You can find the following content inside OHOS_SDK_NATIVE_DIR + # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ + # total 524K + # -rw-r--r-- 1 kuangfangjun root 501K Jan 1 2001 NOTICE.txt + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 build + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 build-tools + # -rw-r--r-- 1 kuangfangjun root 371 Jan 1 2001 compatible_config.json + # drwxr-xr-x 4 kuangfangjun root 0 Nov 6 22:36 docs + # drwxr-xr-x 10 kuangfangjun root 0 Nov 6 22:36 llvm + # -rw-r--r-- 1 kuangfangjun root 16K Jan 1 2001 nativeapi_syscap_config.json + # -rw-r--r-- 1 kuangfangjun root 5.9K Jan 1 2001 ndk_system_capability.json + # -rw-r--r-- 1 kuangfangjun root 167 Jan 1 2001 oh-uni-package.json + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot +fi + +if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/armv7-unknown-linux-ohos-clang ]; then + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/armv7-unknown-linux-ohos-clang does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH +export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH + +OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake + +if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then + echo "$OHOS_TOOLCHAIN_FILE does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + + +sleep 1 +onnxruntime_version=1.16.3 +onnxruntime_dir=onnxruntime-ohos-armeabi-v7a-$onnxruntime_version + +if [ ! -f $onnxruntime_dir/lib/libonnxruntime.so ]; then + wget -c -q https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${onnxruntime_version}/$onnxruntime_dir.zip + unzip $onnxruntime_dir.zip + rm $onnxruntime_dir.zip +fi + +export SHERPA_ONNXRUNTIME_LIB_DIR=$dir/$onnxruntime_dir/lib +export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$dir/$onnxruntime_dir/include + +echo "SHERPA_ONNXRUNTIME_LIB_DIR: $SHERPA_ONNXRUNTIME_LIB_DIR" +echo "SHERPA_ONNXRUNTIME_INCLUDE_DIR $SHERPA_ONNXRUNTIME_INCLUDE_DIR" + +if [ -z $SHERPA_ONNX_ENABLE_TTS ]; then + SHERPA_ONNX_ENABLE_TTS=ON +fi + +if [ -z $SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION ]; then + SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=ON +fi + +if [ -z $SHERPA_ONNX_ENABLE_BINARY ]; then + SHERPA_ONNX_ENABLE_BINARY=OFF +fi + +# See https://github.com/llvm/llvm-project/issues/57732 +# we need to use -mfloat-abi=hard +cmake \ + -DOHOS_ARCH=armeabi-v7a \ + -DCMAKE_CXX_FLAGS="-mfloat-abi=hard" \ + -DCMAKE_C_FLAGS="-mfloat-abi=hard" \ + -DCMAKE_TOOLCHAIN_FILE=$OHOS_TOOLCHAIN_FILE \ + -DSHERPA_ONNX_ENABLE_TTS=$SHERPA_ONNX_ENABLE_TTS \ + -DSHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=$SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION \ + -DSHERPA_ONNX_ENABLE_BINARY=$SHERPA_ONNX_ENABLE_BINARY \ + -DBUILD_PIPER_PHONMIZE_EXE=OFF \ + -DBUILD_PIPER_PHONMIZE_TESTS=OFF \ + -DBUILD_ESPEAK_NG_EXE=OFF \ + -DBUILD_ESPEAK_NG_TESTS=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ + -DSHERPA_ONNX_ENABLE_TESTS=OFF \ + -DSHERPA_ONNX_ENABLE_CHECK=OFF \ + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ + -DSHERPA_ONNX_ENABLE_JNI=OFF \ + -DSHERPA_ONNX_ENABLE_C_API=ON \ + -DCMAKE_INSTALL_PREFIX=./install \ + .. + +# make VERBOSE=1 -j4 +make -j2 +make install/strip +cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib +cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/arm-linux-ohos/libc++_shared.so install/lib + +rm -rf install/share +rm -rf install/lib/pkgconfig diff --git a/build-ohos-x86-64.sh b/build-ohos-x86-64.sh new file mode 100755 index 000000000..c43aff694 --- /dev/null +++ b/build-ohos-x86-64.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +set -ex + +dir=$PWD/build-ohos-x86-64 + +mkdir -p $dir +cd $dir + +# Please first download the commandline tools from +# https://developer.huawei.com/consumer/cn/download/ +# +# Example filename on Linux: commandline-tools-linux-x64-5.0.5.200.zip +# You can also download it from https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/tree/main + +# mkdir /star-fj/fangjun/software/huawei +# cd /star-fj/fangjun/software/huawei +# wget https://hf-mirror.com/csukuangfj/harmonyos-commandline-tools/resolve/main/commandline-tools-linux-x64-5.0.5.200.zip +# unzip commandline-tools-linux-x64-5.0.5.200.zip +# rm commandline-tools-linux-x64-5.0.5.200.zip +if [ -z $OHOS_SDK_NATIVE_DIR ]; then + OHOS_SDK_NATIVE_DIR=/star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ + # You can find the following content inside OHOS_SDK_NATIVE_DIR + # ls -lh /star-fj/fangjun/software/huawei/command-line-tools/sdk/default/openharmony/native/ + # total 524K + # -rw-r--r-- 1 kuangfangjun root 501K Jan 1 2001 NOTICE.txt + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 build + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 build-tools + # -rw-r--r-- 1 kuangfangjun root 371 Jan 1 2001 compatible_config.json + # drwxr-xr-x 4 kuangfangjun root 0 Nov 6 22:36 docs + # drwxr-xr-x 10 kuangfangjun root 0 Nov 6 22:36 llvm + # -rw-r--r-- 1 kuangfangjun root 16K Jan 1 2001 nativeapi_syscap_config.json + # -rw-r--r-- 1 kuangfangjun root 5.9K Jan 1 2001 ndk_system_capability.json + # -rw-r--r-- 1 kuangfangjun root 167 Jan 1 2001 oh-uni-package.json + # drwxr-xr-x 3 kuangfangjun root 0 Nov 6 22:36 sysroot +fi + +if [ ! -d $OHOS_SDK_NATIVE_DIR ]; then + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + +if [ ! -f $OHOS_SDK_NATIVE_DIR/llvm/bin/x86_64-unknown-linux-ohos-clang ]; then + echo "$OHOS_SDK_NATIVE_DIR/llvm/bin/x86_64-unknown-linux-ohos-clang does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + +export PATH=$OHOS_SDK_NATIVE_DIR/build-tools/cmake/bin:$PATH +export PATH=$OHOS_SDK_NATIVE_DIR/llvm/bin:$PATH + +OHOS_TOOLCHAIN_FILE=$OHOS_SDK_NATIVE_DIR/build/cmake/ohos.toolchain.cmake + +if [ ! -f $OHOS_TOOLCHAIN_FILE ]; then + echo "$OHOS_TOOLCHAIN_FILE does not exist" + echo "Please first download Command Line Tools for HarmonyOS" + exit 1 +fi + + +sleep 1 +onnxruntime_version=1.16.3 +onnxruntime_dir=onnxruntime-ohos-x86_64-$onnxruntime_version + +if [ ! -f $onnxruntime_dir/lib/libonnxruntime.so ]; then + wget -c -q https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${onnxruntime_version}/$onnxruntime_dir.zip + unzip $onnxruntime_dir.zip + rm $onnxruntime_dir.zip +fi + +export SHERPA_ONNXRUNTIME_LIB_DIR=$dir/$onnxruntime_dir/lib +export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$dir/$onnxruntime_dir/include + +echo "SHERPA_ONNXRUNTIME_LIB_DIR: $SHERPA_ONNXRUNTIME_LIB_DIR" +echo "SHERPA_ONNXRUNTIME_INCLUDE_DIR $SHERPA_ONNXRUNTIME_INCLUDE_DIR" + +if [ -z $SHERPA_ONNX_ENABLE_TTS ]; then + SHERPA_ONNX_ENABLE_TTS=ON +fi + +if [ -z $SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION ]; then + SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=ON +fi + +if [ -z $SHERPA_ONNX_ENABLE_BINARY ]; then + SHERPA_ONNX_ENABLE_BINARY=OFF +fi + +cmake \ + -DOHOS_ARCH=x86_64 \ + -DCMAKE_TOOLCHAIN_FILE=$OHOS_TOOLCHAIN_FILE \ + -DSHERPA_ONNX_ENABLE_TTS=$SHERPA_ONNX_ENABLE_TTS \ + -DSHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=$SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION \ + -DSHERPA_ONNX_ENABLE_BINARY=$SHERPA_ONNX_ENABLE_BINARY \ + -DBUILD_PIPER_PHONMIZE_EXE=OFF \ + -DBUILD_PIPER_PHONMIZE_TESTS=OFF \ + -DBUILD_ESPEAK_NG_EXE=OFF \ + -DBUILD_ESPEAK_NG_TESTS=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ + -DSHERPA_ONNX_ENABLE_TESTS=OFF \ + -DSHERPA_ONNX_ENABLE_CHECK=OFF \ + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ + -DSHERPA_ONNX_ENABLE_JNI=OFF \ + -DSHERPA_ONNX_ENABLE_C_API=ON \ + -DCMAKE_INSTALL_PREFIX=./install \ + .. + +# make VERBOSE=1 -j4 +make -j2 +make install/strip +cp -fv $onnxruntime_dir/lib/libonnxruntime.so install/lib +cp -fv $OHOS_SDK_NATIVE_DIR/llvm/lib/x86_64-linux-ohos/libc++_shared.so install/lib + +rm -rf install/share +rm -rf install/lib/pkgconfig