Fangjun Kuang
Committed by GitHub

Support static linking onnxruntime lib for 32-bit arm (#401)

@@ -32,16 +32,23 @@ concurrency: @@ -32,16 +32,23 @@ concurrency:
32 jobs: 32 jobs:
33 arm_linux_gnueabihf: 33 arm_linux_gnueabihf:
34 runs-on: ${{ matrix.os }} 34 runs-on: ${{ matrix.os }}
  35 + name: ${{ matrix.os }} ${{ matrix.lib_type }}
35 strategy: 36 strategy:
36 fail-fast: false 37 fail-fast: false
37 matrix: 38 matrix:
38 os: [ubuntu-latest] 39 os: [ubuntu-latest]
  40 + lib_type: [static, shared]
39 41
40 steps: 42 steps:
41 - uses: actions/checkout@v4 43 - uses: actions/checkout@v4
42 with: 44 with:
43 fetch-depth: 0 45 fetch-depth: 0
44 46
  47 + - name: ccache
  48 + uses: hendrikmuhs/ccache-action@v1.2
  49 + with:
  50 + key: ${{ matrix.os }}-arm-${{ matrix.lib_type }}
  51 +
45 - name: cache-qemu 52 - name: cache-qemu
46 id: cache-qemu 53 id: cache-qemu
47 uses: actions/cache@v3 54 uses: actions/cache@v3
@@ -107,6 +114,19 @@ jobs: @@ -107,6 +114,19 @@ jobs:
107 run: | 114 run: |
108 export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH 115 export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH
109 116
  117 + export CMAKE_CXX_COMPILER_LAUNCHER=ccache
  118 + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
  119 +
  120 + cmake --version
  121 +
  122 + lib_type=${{ matrix.lib_type }}
  123 +
  124 + if [[ $lib_type == "shared" ]]; then
  125 + export BUILD_SHARED_LIBS=ON
  126 + else
  127 + export BUILD_SHARED_LIBS=OFF
  128 + fi
  129 +
110 ./build-arm-linux-gnueabihf.sh 130 ./build-arm-linux-gnueabihf.sh
111 131
112 ls -lh build-arm-linux-gnueabihf/bin 132 ls -lh build-arm-linux-gnueabihf/bin
@@ -125,12 +145,14 @@ jobs: @@ -125,12 +145,14 @@ jobs:
125 145
126 qemu-arm ./build-arm-linux-gnueabihf/bin/sherpa-onnx --help 146 qemu-arm ./build-arm-linux-gnueabihf/bin/sherpa-onnx --help
127 147
  148 + readelf -d ./build-arm-linux-gnueabihf/bin/sherpa-onnx
  149 +
128 - name: Copy files 150 - name: Copy files
129 shell: bash 151 shell: bash
130 run: | 152 run: |
131 SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) 153 SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
132 154
133 - dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-linux-arm-gnueabihf 155 + dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-linux-arm-gnueabihf-${{ matrix.lib_type }}
134 mkdir $dst 156 mkdir $dst
135 157
136 cp -a build-arm-linux-gnueabihf/install/bin $dst/ 158 cp -a build-arm-linux-gnueabihf/install/bin $dst/
@@ -141,13 +163,21 @@ jobs: @@ -141,13 +163,21 @@ jobs:
141 tar cjvf ${dst}.tar.bz2 $dst 163 tar cjvf ${dst}.tar.bz2 $dst
142 164
143 - uses: actions/upload-artifact@v3 165 - uses: actions/upload-artifact@v3
  166 + if: matrix.lib_type == 'shared'
  167 + with:
  168 + name: sherpa-onnx-linux-arm-gnueabihf-shared
  169 + path: sherpa-onnx-*linux-arm-gnueabihf-shared.tar.bz2
  170 +
  171 + - uses: actions/upload-artifact@v3
  172 + if: matrix.lib_type == 'static'
144 with: 173 with:
145 - path: sherpa-onnx-*linux-arm-gnueabihf.tar.bz2 174 + name: sherpa-onnx-linux-arm-gnueabihf-static
  175 + path: sherpa-onnx-*linux-arm-gnueabihf-static.tar.bz2
146 176
147 - - name: Release pre-compiled binaries and libs for arm linux gnueabihf 177 + - name: Release pre-compiled binaries and libs for arm linux gnueabihf ${{ matrix.lib_type }}
148 if: github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa' && github.event_name == 'push' && contains(github.ref, 'refs/tags/') 178 if: github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa' && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
149 uses: svenstaro/upload-release-action@v2 179 uses: svenstaro/upload-release-action@v2
150 with: 180 with:
151 file_glob: true 181 file_glob: true
152 overwrite: true 182 overwrite: true
153 - file: sherpa-onnx-*linux-arm-gnueabihf.tar.bz2 183 + file: sherpa-onnx-*linux-arm-gnueabihf*.tar.bz2
@@ -34,10 +34,15 @@ fi @@ -34,10 +34,15 @@ fi
34 export CPLUS_INCLUDE_PATH=$PWD/alsa-lib/include:$CPLUS_INCLUDE_PATH 34 export CPLUS_INCLUDE_PATH=$PWD/alsa-lib/include:$CPLUS_INCLUDE_PATH
35 export SHERPA_ONNX_ALSA_LIB_DIR=$PWD/alsa-lib/src/.libs 35 export SHERPA_ONNX_ALSA_LIB_DIR=$PWD/alsa-lib/src/.libs
36 36
  37 +if [[ x"$BUILD_SHARED_LIBS" == x"" ]]; then
  38 + # By default, use static link
  39 + BUILD_SHARED_LIBS=OFF
  40 +fi
  41 +
37 cmake \ 42 cmake \
38 -DCMAKE_INSTALL_PREFIX=./install \ 43 -DCMAKE_INSTALL_PREFIX=./install \
39 -DCMAKE_BUILD_TYPE=Release \ 44 -DCMAKE_BUILD_TYPE=Release \
40 - -DBUILD_SHARED_LIBS=OFF \ 45 + -DBUILD_SHARED_LIBS=$BUILD_SHARED_LIBS \
41 -DSHERPA_ONNX_ENABLE_TESTS=OFF \ 46 -DSHERPA_ONNX_ENABLE_TESTS=OFF \
42 -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ 47 -DSHERPA_ONNX_ENABLE_PYTHON=OFF \
43 -DSHERPA_ONNX_ENABLE_CHECK=OFF \ 48 -DSHERPA_ONNX_ENABLE_CHECK=OFF \
  1 +# Copyright (c) 2022-2023 Xiaomi Corporation
  2 +message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
  3 +message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  4 +
  5 +if(NOT CMAKE_SYSTEM_NAME STREQUAL Linux)
  6 + message(FATAL_ERROR "This file is for Linux only. Given: ${CMAKE_SYSTEM_NAME}")
  7 +endif()
  8 +
  9 +if(NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL arm OR CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l))
  10 + message(FATAL_ERROR "This file is for arm only. Given: ${CMAKE_SYSTEM_PROCESSOR}")
  11 +endif()
  12 +
  13 +if(BUILD_SHARED_LIBS)
  14 + message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
  15 +endif()
  16 +
  17 +set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-linux-arm-static_lib-1.16.0.zip")
  18 +set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-linux-arm-static_lib-1.16.0.zip")
  19 +set(onnxruntime_HASH "SHA256=d2bf3d69a479ac786bf2f019e720218a74634b73cfda758ba50d0b1adef1c76a")
  20 +
  21 +# If you don't have access to the Internet,
  22 +# please download onnxruntime to one of the following locations.
  23 +# You can add more if you want.
  24 +set(possible_file_locations
  25 + $ENV{HOME}/Downloads/onnxruntime-linux-arm-static_lib-1.16.0.zip
  26 + ${PROJECT_SOURCE_DIR}/onnxruntime-linux-arm-static_lib-1.16.0.zip
  27 + ${PROJECT_BINARY_DIR}/onnxruntime-linux-arm-static_lib-1.16.0.zip
  28 + /tmp/onnxruntime-linux-arm-static_lib-1.16.0.zip
  29 + /star-fj/fangjun/download/github/onnxruntime-linux-arm-static_lib-1.16.0.zip
  30 +)
  31 +
  32 +foreach(f IN LISTS possible_file_locations)
  33 + if(EXISTS ${f})
  34 + set(onnxruntime_URL "${f}")
  35 + file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
  36 + message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
  37 + set(onnxruntime_URL2)
  38 + break()
  39 + endif()
  40 +endforeach()
  41 +
  42 +FetchContent_Declare(onnxruntime
  43 + URL
  44 + ${onnxruntime_URL}
  45 + ${onnxruntime_URL2}
  46 + URL_HASH ${onnxruntime_HASH}
  47 +)
  48 +
  49 +FetchContent_GetProperties(onnxruntime)
  50 +if(NOT onnxruntime_POPULATED)
  51 + message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
  52 + FetchContent_Populate(onnxruntime)
  53 +endif()
  54 +message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
  55 +
  56 +find_library(location_onnxruntime onnxruntime
  57 + PATHS
  58 + "${onnxruntime_SOURCE_DIR}/lib"
  59 + NO_CMAKE_SYSTEM_PATH
  60 +)
  61 +
  62 +message(STATUS "location_onnxruntime: ${location_onnxruntime}")
  63 +
  64 +# for static libraries, we use onnxruntime_lib_files directly below
  65 +include_directories(${onnxruntime_SOURCE_DIR}/include)
  66 +
  67 +file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a")
  68 +
  69 +set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE)
  70 +
  71 +message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
  72 +install(FILES ${onnxruntime_lib_files} DESTINATION lib)
@@ -8,7 +8,11 @@ function(download_onnxruntime) @@ -8,7 +8,11 @@ function(download_onnxruntime)
8 if(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) 8 if(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
9 include(onnxruntime-linux-aarch64) 9 include(onnxruntime-linux-aarch64)
10 elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND (CMAKE_SYSTEM_PROCESSOR STREQUAL arm OR CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)) 10 elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND (CMAKE_SYSTEM_PROCESSOR STREQUAL arm OR CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l))
11 - include(onnxruntime-linux-arm) 11 + if(BUILD_SHARED_LIBS)
  12 + include(onnxruntime-linux-arm)
  13 + else()
  14 + include(onnxruntime-linux-arm-static)
  15 + endif()
12 elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) 16 elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
13 if(SHERPA_ONNX_ENABLE_GPU) 17 if(SHERPA_ONNX_ENABLE_GPU)
14 include(onnxruntime-linux-x86_64-gpu) 18 include(onnxruntime-linux-x86_64-gpu)
@@ -113,7 +113,7 @@ target_link_libraries(sherpa-onnx-core kaldi-native-fbank-core) @@ -113,7 +113,7 @@ target_link_libraries(sherpa-onnx-core kaldi-native-fbank-core)
113 113
114 target_link_libraries(sherpa-onnx-core kaldi-decoder-core) 114 target_link_libraries(sherpa-onnx-core kaldi-decoder-core)
115 115
116 -if(BUILD_SHARED_LIBS OR APPLE OR CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL arm) 116 +if(BUILD_SHARED_LIBS OR APPLE OR CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
117 target_link_libraries(sherpa-onnx-core onnxruntime) 117 target_link_libraries(sherpa-onnx-core onnxruntime)
118 else() 118 else()
119 target_link_libraries(sherpa-onnx-core ${onnxruntime_lib_files}) 119 target_link_libraries(sherpa-onnx-core ${onnxruntime_lib_files})