From 449b9a266c14e9cef60b8315db326f74a53d7aa5 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 31 Oct 2023 18:31:02 +0800 Subject: [PATCH] Support linking onnxruntime statically for macOS --- .github/workflows/macos.yaml | 17 ++++-- cmake/onnxruntime-osx-arm64-static.cmake | 61 +++++++++++++++++++ cmake/onnxruntime-osx-arm64.cmake | 4 ++ cmake/onnxruntime-osx-universal-static.cmake | 62 ++++++++++++++++++++ cmake/onnxruntime-osx-universal.cmake | 4 ++ cmake/onnxruntime-osx-x86_64-static.cmake | 61 +++++++++++++++++++ cmake/onnxruntime-osx-x86_64.cmake | 4 ++ cmake/onnxruntime.cmake | 30 ++++++++-- sherpa-onnx/csrc/CMakeLists.txt | 6 +- 9 files changed, 239 insertions(+), 10 deletions(-) create mode 100644 cmake/onnxruntime-osx-arm64-static.cmake create mode 100644 cmake/onnxruntime-osx-universal-static.cmake create mode 100644 cmake/onnxruntime-osx-x86_64-static.cmake diff --git a/.github/workflows/macos.yaml b/.github/workflows/macos.yaml index ef9e889b7..e952b38a8 100644 --- a/.github/workflows/macos.yaml +++ b/.github/workflows/macos.yaml @@ -39,11 +39,13 @@ concurrency: jobs: macos: runs-on: ${{ matrix.os }} + name: ${{ matrix.build_type }} ${{ matrix.lib_type }} strategy: fail-fast: false matrix: os: [macos-latest] build_type: [Release, Debug] + lib_type: [static, shared] steps: - uses: actions/checkout@v4 @@ -53,7 +55,7 @@ jobs: - name: ccache uses: hendrikmuhs/ccache-action@v1.2 with: - key: ${{ matrix.os }}-${{ matrix.build_type }} + key: ${{ matrix.os }}-${{ matrix.build_type }}-${{ matrix.lib_type }} - name: Configure CMake shell: bash @@ -64,7 +66,14 @@ jobs: mkdir build cd build - cmake -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64' -DCMAKE_INSTALL_PREFIX=./install .. + lib_type=${{ matrix.lib_type }} + if [[ $lib_type == "static" ]]; then + BUILD_SHARED_LIBS=OFF + else + BUILD_SHARED_LIBS=ON + fi + + cmake -D BUILD_SHARED_LIBS=$BUILD_SHARED_LIBS -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64' -DCMAKE_INSTALL_PREFIX=./install .. - name: Build sherpa-onnx for macos shell: bash @@ -149,7 +158,7 @@ jobs: run: | SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) - dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-osx-universal2 + dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-osx-universal2-${{ matrix.lib_type }} mkdir $dst cp -a build/install/bin $dst/ @@ -167,4 +176,4 @@ jobs: with: file_glob: true overwrite: true - file: sherpa-onnx-*osx-universal2.tar.bz2 + file: sherpa-onnx-*osx-universal2*.tar.bz2 diff --git a/cmake/onnxruntime-osx-arm64-static.cmake b/cmake/onnxruntime-osx-arm64-static.cmake new file mode 100644 index 000000000..f9e20581a --- /dev/null +++ b/cmake/onnxruntime-osx-arm64-static.cmake @@ -0,0 +1,61 @@ +# Copyright (c) 2022-2023 Xiaomi Corporation +message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}") +message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") +message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}") +message(STATUS "CMAKE_APPLE_SILICON_PROCESSOR : ${CMAKE_APPLE_SILICON_PROCESSOR}") + +if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) + message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}") +endif() + +if(BUILD_SHARED_LIBS) + message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}") +endif() + +set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-arm64-static_lib-1.16.0.zip") +set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-osx-arm64-static_lib-1.16.0.zip") +set(onnxruntime_HASH "SHA256=5f99c9a51d91e751ac20fcbb73dfa31a379438381c5357fd3f37bda816934e3e") + +# If you don't have access to the Internet, +# please download onnxruntime to one of the following locations. +# You can add more if you want. +set(possible_file_locations + $ENV{HOME}/Downloads/onnxruntime-osx-arm64-static_lib-1.16.0.zip + ${PROJECT_SOURCE_DIR}/onnxruntime-osx-arm64-static_lib-1.16.0.zip + ${PROJECT_BINARY_DIR}/onnxruntime-osx-arm64-static_lib-1.16.0.zip + /tmp/onnxruntime-osx-arm64-static_lib-1.16.0.zip +) + +foreach(f IN LISTS possible_file_locations) + if(EXISTS ${f}) + set(onnxruntime_URL "${f}") + file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL) + message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}") + set(onnxruntime_URL2) + break() + endif() +endforeach() + +FetchContent_Declare(onnxruntime + URL + ${onnxruntime_URL} + ${onnxruntime_URL2} + URL_HASH ${onnxruntime_HASH} +) + +FetchContent_GetProperties(onnxruntime) +if(NOT onnxruntime_POPULATED) + message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}") + FetchContent_Populate(onnxruntime) +endif() +message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}") + +# for static libraries, we use onnxruntime_lib_files directly below +include_directories(${onnxruntime_SOURCE_DIR}/include) + +file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a") + +set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE) + +message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}") +install(FILES ${onnxruntime_lib_files} DESTINATION lib) diff --git a/cmake/onnxruntime-osx-arm64.cmake b/cmake/onnxruntime-osx-arm64.cmake index d4d679017..eb731095f 100644 --- a/cmake/onnxruntime-osx-arm64.cmake +++ b/cmake/onnxruntime-osx-arm64.cmake @@ -8,6 +8,10 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}") endif() +if(NOT BUILD_SHARED_LIBS) + message(FATAL_ERROR "This file is for building shared libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}") +endif() + set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-arm64-1.16.0.tgz") set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-osx-arm64-1.16.0.tgz") set(onnxruntime_HASH "SHA256=fec3b70ca4f642a5c6d5c3a6f3a4eddd4c1b9281893fe2c7ae03a3086e20c316") diff --git a/cmake/onnxruntime-osx-universal-static.cmake b/cmake/onnxruntime-osx-universal-static.cmake new file mode 100644 index 000000000..c91f259ea --- /dev/null +++ b/cmake/onnxruntime-osx-universal-static.cmake @@ -0,0 +1,62 @@ +# Possible values for CMAKE_SYSTEM_NAME: Linux, Windows, Darwin + +message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}") +message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") +message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}") +message(STATUS "CMAKE_APPLE_SILICON_PROCESSOR : ${CMAKE_APPLE_SILICON_PROCESSOR}") + +if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) + message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}") +endif() + +if(BUILD_SHARED_LIBS) + message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}") +endif() + +set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-universal2-static_lib-1.16.0.zip") +set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-osx-universal2-static_lib-1.16.0.zip") +set(onnxruntime_HASH "SHA256=6df205fb519d311ff57131d35ed43374f4fe483eb665baa38bfbc00034595b35") + +# If you don't have access to the Internet, +# please download onnxruntime to one of the following locations. +# You can add more if you want. +set(possible_file_locations + $ENV{HOME}/Downloads/onnxruntime-osx-universal2-static_lib-1.16.0.zip + ${PROJECT_SOURCE_DIR}/onnxruntime-osx-universal2-static_lib-1.16.0.zip + ${PROJECT_BINARY_DIR}/onnxruntime-osx-universal2-static_lib-1.16.0.zip + /tmp/onnxruntime-osx-universal2-static_lib-1.16.0.zip +) + +foreach(f IN LISTS possible_file_locations) + if(EXISTS ${f}) + set(onnxruntime_URL "${f}") + file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL) + message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}") + set(onnxruntime_URL2) + break() + endif() +endforeach() + +FetchContent_Declare(onnxruntime + URL + ${onnxruntime_URL} + ${onnxruntime_URL2} + URL_HASH ${onnxruntime_HASH} +) + +FetchContent_GetProperties(onnxruntime) +if(NOT onnxruntime_POPULATED) + message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}") + FetchContent_Populate(onnxruntime) +endif() +message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}") + +# for static libraries, we use onnxruntime_lib_files directly below +include_directories(${onnxruntime_SOURCE_DIR}/include) + +file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a") + +set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE) + +message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}") +install(FILES ${onnxruntime_lib_files} DESTINATION lib) diff --git a/cmake/onnxruntime-osx-universal.cmake b/cmake/onnxruntime-osx-universal.cmake index 54e05e985..ccbc62c3d 100644 --- a/cmake/onnxruntime-osx-universal.cmake +++ b/cmake/onnxruntime-osx-universal.cmake @@ -9,6 +9,10 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}") endif() +if(NOT BUILD_SHARED_LIBS) + message(FATAL_ERROR "This file is for building shared libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}") +endif() + set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-universal2-1.16.0.tgz") set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-osx-universal2-1.16.0.tgz") set(onnxruntime_HASH "SHA256=e5b69ece634cf1cd5cf4b45ab478417199a5e8ab5775f6f12560e09dc5ef7749") diff --git a/cmake/onnxruntime-osx-x86_64-static.cmake b/cmake/onnxruntime-osx-x86_64-static.cmake new file mode 100644 index 000000000..7678552f3 --- /dev/null +++ b/cmake/onnxruntime-osx-x86_64-static.cmake @@ -0,0 +1,61 @@ +# Copyright (c) 2022-2023 Xiaomi Corporation +message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}") +message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") +message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}") +message(STATUS "CMAKE_APPLE_SILICON_PROCESSOR : ${CMAKE_APPLE_SILICON_PROCESSOR}") + +if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) + message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}") +endif() + +if(BUILD_SHARED_LIBS) + message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}") +endif() + +set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-x86_64-static_lib-1.16.0.zip") +set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-osx-x86_64-static_lib-1.16.0.zip") +set(onnxruntime_HASH "SHA256=1686a1b6778483371d29106d8c7300a8960e3db084ab84ac4f870b7685cf5259") + +# If you don't have access to the Internet, +# please download onnxruntime to one of the following locations. +# You can add more if you want. +set(possible_file_locations + $ENV{HOME}/Downloads/onnxruntime-osx-x86_64-static_lib-1.16.0.zip + ${PROJECT_SOURCE_DIR}/onnxruntime-osx-x86_64-static_lib-1.16.0.zip + ${PROJECT_BINARY_DIR}/onnxruntime-osx-x86_64-static_lib-1.16.0.zip + /tmp/onnxruntime-osx-x86_64-static_lib-1.16.0.zip +) + +foreach(f IN LISTS possible_file_locations) + if(EXISTS ${f}) + set(onnxruntime_URL "${f}") + file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL) + message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}") + set(onnxruntime_URL2) + break() + endif() +endforeach() + +FetchContent_Declare(onnxruntime + URL + ${onnxruntime_URL} + ${onnxruntime_URL2} + URL_HASH ${onnxruntime_HASH} +) + +FetchContent_GetProperties(onnxruntime) +if(NOT onnxruntime_POPULATED) + message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}") + FetchContent_Populate(onnxruntime) +endif() +message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}") + +# for static libraries, we use onnxruntime_lib_files directly below +include_directories(${onnxruntime_SOURCE_DIR}/include) + +file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a") + +set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE) + +message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}") +install(FILES ${onnxruntime_lib_files} DESTINATION lib) diff --git a/cmake/onnxruntime-osx-x86_64.cmake b/cmake/onnxruntime-osx-x86_64.cmake index 15aea2ec1..a88ec35a4 100644 --- a/cmake/onnxruntime-osx-x86_64.cmake +++ b/cmake/onnxruntime-osx-x86_64.cmake @@ -8,6 +8,10 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}") endif() +if(NOT BUILD_SHARED_LIBS) + message(FATAL_ERROR "This file is for building shared libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}") +endif() + set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-x86_64-1.16.0.tgz") set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-osx-x86_64-1.16.0.tgz") set(onnxruntime_HASH "SHA256=3d639a269af4e97a455f23cff363a709ef3a5f3e086162e65e3395c339122285") diff --git a/cmake/onnxruntime.cmake b/cmake/onnxruntime.cmake index 418a030f1..cfb66823a 100644 --- a/cmake/onnxruntime.cmake +++ b/cmake/onnxruntime.cmake @@ -27,17 +27,37 @@ function(download_onnxruntime) endif() elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin) if (arm64 IN_LIST CMAKE_OSX_ARCHITECTURES AND x86_64 IN_LIST CMAKE_OSX_ARCHITECTURES) - include(onnxruntime-osx-universal) + if(BUILD_SHARED_LIBS) + include(onnxruntime-osx-universal) + else() + include(onnxruntime-osx-universal-static) + endif() elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 AND CMAKE_OSX_ARCHITECTURES STREQUAL "arm64") # cross compiling - include(onnxruntime-osx-arm64) + if(BUILD_SHARED_LIBS) + include(onnxruntime-osx-arm64) + else() + include(onnxruntime-osx-arm64-static) + endif() elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64 AND CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64") # cross compiling - include(onnxruntime-osx-x86_64) + if(BUILD_SHARED_LIBS) + include(onnxruntime-osx-x86_64) + else() + include(onnxruntime-osx-x86_64-static) + endif() elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64) - include(onnxruntime-osx-arm64) + if(BUILD_SHARED_LIBS) + include(onnxruntime-osx-arm64) + else() + include(onnxruntime-osx-arm64-static) + endif() elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) - include(onnxruntime-osx-x86_64) + if(BUILD_SHARED_LIBS) + include(onnxruntime-osx-x86_64) + else() + include(onnxruntime-osx-x86_64-static) + endif() else() message(FATAL_ERROR "Unsupport processor {CMAKE_SYSTEM_PROCESSOR} for Darwin") endif() diff --git a/sherpa-onnx/csrc/CMakeLists.txt b/sherpa-onnx/csrc/CMakeLists.txt index 1c7031b5a..485056364 100644 --- a/sherpa-onnx/csrc/CMakeLists.txt +++ b/sherpa-onnx/csrc/CMakeLists.txt @@ -113,12 +113,16 @@ target_link_libraries(sherpa-onnx-core kaldi-native-fbank-core) target_link_libraries(sherpa-onnx-core kaldi-decoder-core) -if(BUILD_SHARED_LIBS OR APPLE) +if(BUILD_SHARED_LIBS) target_link_libraries(sherpa-onnx-core onnxruntime) else() target_link_libraries(sherpa-onnx-core ${onnxruntime_lib_files}) endif() +if(NOT BUILD_SHARED_LIBS AND APPLE) + target_link_libraries(sherpa-onnx-core "-framework Foundation") +endif() + if(SHERPA_ONNX_ENABLE_GPU) target_link_libraries(sherpa-onnx-core onnxruntime_providers_cuda