Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support linking onnxruntime statically for macOS #403

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions .github/workflows/macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,7 +55,7 @@ jobs:
- name: ccache
uses: hendrikmuhs/[email protected]
with:
key: ${{ matrix.os }}-${{ matrix.build_type }}
key: ${{ matrix.os }}-${{ matrix.build_type }}-${{ matrix.lib_type }}

- name: Configure CMake
shell: bash
Expand All @@ -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
Expand Down Expand Up @@ -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/
Expand All @@ -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
61 changes: 61 additions & 0 deletions cmake/onnxruntime-osx-arm64-static.cmake
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions cmake/onnxruntime-osx-arm64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
62 changes: 62 additions & 0 deletions cmake/onnxruntime-osx-universal-static.cmake
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions cmake/onnxruntime-osx-universal.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
61 changes: 61 additions & 0 deletions cmake/onnxruntime-osx-x86_64-static.cmake
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions cmake/onnxruntime-osx-x86_64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
30 changes: 25 additions & 5 deletions cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion sherpa-onnx/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading