Skip to content

Commit b80b7e5

Browse files
authored
Support linking onnxruntime statically for macOS (#403)
1 parent fabbc70 commit b80b7e5

9 files changed

+239
-10
lines changed

.github/workflows/macos.yaml

+13-4
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ concurrency:
3939
jobs:
4040
macos:
4141
runs-on: ${{ matrix.os }}
42+
name: ${{ matrix.build_type }} ${{ matrix.lib_type }}
4243
strategy:
4344
fail-fast: false
4445
matrix:
4546
os: [macos-latest]
4647
build_type: [Release, Debug]
48+
lib_type: [static, shared]
4749

4850
steps:
4951
- uses: actions/checkout@v4
@@ -53,7 +55,7 @@ jobs:
5355
- name: ccache
5456
uses: hendrikmuhs/[email protected]
5557
with:
56-
key: ${{ matrix.os }}-${{ matrix.build_type }}
58+
key: ${{ matrix.os }}-${{ matrix.build_type }}-${{ matrix.lib_type }}
5759

5860
- name: Configure CMake
5961
shell: bash
@@ -64,7 +66,14 @@ jobs:
6466
6567
mkdir build
6668
cd build
67-
cmake -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64' -DCMAKE_INSTALL_PREFIX=./install ..
69+
lib_type=${{ matrix.lib_type }}
70+
if [[ $lib_type == "static" ]]; then
71+
BUILD_SHARED_LIBS=OFF
72+
else
73+
BUILD_SHARED_LIBS=ON
74+
fi
75+
76+
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 ..
6877
6978
- name: Build sherpa-onnx for macos
7079
shell: bash
@@ -149,7 +158,7 @@ jobs:
149158
run: |
150159
SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
151160
152-
dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-osx-universal2
161+
dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-osx-universal2-${{ matrix.lib_type }}
153162
mkdir $dst
154163
155164
cp -a build/install/bin $dst/
@@ -167,4 +176,4 @@ jobs:
167176
with:
168177
file_glob: true
169178
overwrite: true
170-
file: sherpa-onnx-*osx-universal2.tar.bz2
179+
file: sherpa-onnx-*osx-universal2*.tar.bz2
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}")
5+
message(STATUS "CMAKE_APPLE_SILICON_PROCESSOR : ${CMAKE_APPLE_SILICON_PROCESSOR}")
6+
7+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
8+
message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}")
9+
endif()
10+
11+
if(BUILD_SHARED_LIBS)
12+
message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
13+
endif()
14+
15+
set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-arm64-static_lib-1.16.0.zip")
16+
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-osx-arm64-static_lib-1.16.0.zip")
17+
set(onnxruntime_HASH "SHA256=5f99c9a51d91e751ac20fcbb73dfa31a379438381c5357fd3f37bda816934e3e")
18+
19+
# If you don't have access to the Internet,
20+
# please download onnxruntime to one of the following locations.
21+
# You can add more if you want.
22+
set(possible_file_locations
23+
$ENV{HOME}/Downloads/onnxruntime-osx-arm64-static_lib-1.16.0.zip
24+
${PROJECT_SOURCE_DIR}/onnxruntime-osx-arm64-static_lib-1.16.0.zip
25+
${PROJECT_BINARY_DIR}/onnxruntime-osx-arm64-static_lib-1.16.0.zip
26+
/tmp/onnxruntime-osx-arm64-static_lib-1.16.0.zip
27+
)
28+
29+
foreach(f IN LISTS possible_file_locations)
30+
if(EXISTS ${f})
31+
set(onnxruntime_URL "${f}")
32+
file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
33+
message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
34+
set(onnxruntime_URL2)
35+
break()
36+
endif()
37+
endforeach()
38+
39+
FetchContent_Declare(onnxruntime
40+
URL
41+
${onnxruntime_URL}
42+
${onnxruntime_URL2}
43+
URL_HASH ${onnxruntime_HASH}
44+
)
45+
46+
FetchContent_GetProperties(onnxruntime)
47+
if(NOT onnxruntime_POPULATED)
48+
message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
49+
FetchContent_Populate(onnxruntime)
50+
endif()
51+
message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
52+
53+
# for static libraries, we use onnxruntime_lib_files directly below
54+
include_directories(${onnxruntime_SOURCE_DIR}/include)
55+
56+
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a")
57+
58+
set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE)
59+
60+
message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
61+
install(FILES ${onnxruntime_lib_files} DESTINATION lib)

cmake/onnxruntime-osx-arm64.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
88
message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}")
99
endif()
1010

11+
if(NOT BUILD_SHARED_LIBS)
12+
message(FATAL_ERROR "This file is for building shared libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
13+
endif()
14+
1115
set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-arm64-1.16.0.tgz")
1216
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-osx-arm64-1.16.0.tgz")
1317
set(onnxruntime_HASH "SHA256=fec3b70ca4f642a5c6d5c3a6f3a4eddd4c1b9281893fe2c7ae03a3086e20c316")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Possible values for CMAKE_SYSTEM_NAME: Linux, Windows, Darwin
2+
3+
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
4+
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
5+
message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}")
6+
message(STATUS "CMAKE_APPLE_SILICON_PROCESSOR : ${CMAKE_APPLE_SILICON_PROCESSOR}")
7+
8+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
9+
message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}")
10+
endif()
11+
12+
if(BUILD_SHARED_LIBS)
13+
message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
14+
endif()
15+
16+
set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-universal2-static_lib-1.16.0.zip")
17+
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-osx-universal2-static_lib-1.16.0.zip")
18+
set(onnxruntime_HASH "SHA256=6df205fb519d311ff57131d35ed43374f4fe483eb665baa38bfbc00034595b35")
19+
20+
# If you don't have access to the Internet,
21+
# please download onnxruntime to one of the following locations.
22+
# You can add more if you want.
23+
set(possible_file_locations
24+
$ENV{HOME}/Downloads/onnxruntime-osx-universal2-static_lib-1.16.0.zip
25+
${PROJECT_SOURCE_DIR}/onnxruntime-osx-universal2-static_lib-1.16.0.zip
26+
${PROJECT_BINARY_DIR}/onnxruntime-osx-universal2-static_lib-1.16.0.zip
27+
/tmp/onnxruntime-osx-universal2-static_lib-1.16.0.zip
28+
)
29+
30+
foreach(f IN LISTS possible_file_locations)
31+
if(EXISTS ${f})
32+
set(onnxruntime_URL "${f}")
33+
file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
34+
message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
35+
set(onnxruntime_URL2)
36+
break()
37+
endif()
38+
endforeach()
39+
40+
FetchContent_Declare(onnxruntime
41+
URL
42+
${onnxruntime_URL}
43+
${onnxruntime_URL2}
44+
URL_HASH ${onnxruntime_HASH}
45+
)
46+
47+
FetchContent_GetProperties(onnxruntime)
48+
if(NOT onnxruntime_POPULATED)
49+
message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
50+
FetchContent_Populate(onnxruntime)
51+
endif()
52+
message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
53+
54+
# for static libraries, we use onnxruntime_lib_files directly below
55+
include_directories(${onnxruntime_SOURCE_DIR}/include)
56+
57+
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a")
58+
59+
set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE)
60+
61+
message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
62+
install(FILES ${onnxruntime_lib_files} DESTINATION lib)

cmake/onnxruntime-osx-universal.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
99
message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}")
1010
endif()
1111

12+
if(NOT BUILD_SHARED_LIBS)
13+
message(FATAL_ERROR "This file is for building shared libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
14+
endif()
15+
1216
set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-universal2-1.16.0.tgz")
1317
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-osx-universal2-1.16.0.tgz")
1418
set(onnxruntime_HASH "SHA256=e5b69ece634cf1cd5cf4b45ab478417199a5e8ab5775f6f12560e09dc5ef7749")
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}")
5+
message(STATUS "CMAKE_APPLE_SILICON_PROCESSOR : ${CMAKE_APPLE_SILICON_PROCESSOR}")
6+
7+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
8+
message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}")
9+
endif()
10+
11+
if(BUILD_SHARED_LIBS)
12+
message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
13+
endif()
14+
15+
set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-x86_64-static_lib-1.16.0.zip")
16+
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-osx-x86_64-static_lib-1.16.0.zip")
17+
set(onnxruntime_HASH "SHA256=1686a1b6778483371d29106d8c7300a8960e3db084ab84ac4f870b7685cf5259")
18+
19+
# If you don't have access to the Internet,
20+
# please download onnxruntime to one of the following locations.
21+
# You can add more if you want.
22+
set(possible_file_locations
23+
$ENV{HOME}/Downloads/onnxruntime-osx-x86_64-static_lib-1.16.0.zip
24+
${PROJECT_SOURCE_DIR}/onnxruntime-osx-x86_64-static_lib-1.16.0.zip
25+
${PROJECT_BINARY_DIR}/onnxruntime-osx-x86_64-static_lib-1.16.0.zip
26+
/tmp/onnxruntime-osx-x86_64-static_lib-1.16.0.zip
27+
)
28+
29+
foreach(f IN LISTS possible_file_locations)
30+
if(EXISTS ${f})
31+
set(onnxruntime_URL "${f}")
32+
file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
33+
message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
34+
set(onnxruntime_URL2)
35+
break()
36+
endif()
37+
endforeach()
38+
39+
FetchContent_Declare(onnxruntime
40+
URL
41+
${onnxruntime_URL}
42+
${onnxruntime_URL2}
43+
URL_HASH ${onnxruntime_HASH}
44+
)
45+
46+
FetchContent_GetProperties(onnxruntime)
47+
if(NOT onnxruntime_POPULATED)
48+
message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
49+
FetchContent_Populate(onnxruntime)
50+
endif()
51+
message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
52+
53+
# for static libraries, we use onnxruntime_lib_files directly below
54+
include_directories(${onnxruntime_SOURCE_DIR}/include)
55+
56+
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a")
57+
58+
set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE)
59+
60+
message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
61+
install(FILES ${onnxruntime_lib_files} DESTINATION lib)

cmake/onnxruntime-osx-x86_64.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
88
message(FATAL_ERROR "This file is for macOS only. Given: ${CMAKE_SYSTEM_NAME}")
99
endif()
1010

11+
if(NOT BUILD_SHARED_LIBS)
12+
message(FATAL_ERROR "This file is for building shared libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
13+
endif()
14+
1115
set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.16.0/onnxruntime-osx-x86_64-1.16.0.tgz")
1216
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-osx-x86_64-1.16.0.tgz")
1317
set(onnxruntime_HASH "SHA256=3d639a269af4e97a455f23cff363a709ef3a5f3e086162e65e3395c339122285")

cmake/onnxruntime.cmake

+25-5
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,37 @@ function(download_onnxruntime)
2727
endif()
2828
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
2929
if (arm64 IN_LIST CMAKE_OSX_ARCHITECTURES AND x86_64 IN_LIST CMAKE_OSX_ARCHITECTURES)
30-
include(onnxruntime-osx-universal)
30+
if(BUILD_SHARED_LIBS)
31+
include(onnxruntime-osx-universal)
32+
else()
33+
include(onnxruntime-osx-universal-static)
34+
endif()
3135
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 AND CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
3236
# cross compiling
33-
include(onnxruntime-osx-arm64)
37+
if(BUILD_SHARED_LIBS)
38+
include(onnxruntime-osx-arm64)
39+
else()
40+
include(onnxruntime-osx-arm64-static)
41+
endif()
3442
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64 AND CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
3543
# cross compiling
36-
include(onnxruntime-osx-x86_64)
44+
if(BUILD_SHARED_LIBS)
45+
include(onnxruntime-osx-x86_64)
46+
else()
47+
include(onnxruntime-osx-x86_64-static)
48+
endif()
3749
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64)
38-
include(onnxruntime-osx-arm64)
50+
if(BUILD_SHARED_LIBS)
51+
include(onnxruntime-osx-arm64)
52+
else()
53+
include(onnxruntime-osx-arm64-static)
54+
endif()
3955
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
40-
include(onnxruntime-osx-x86_64)
56+
if(BUILD_SHARED_LIBS)
57+
include(onnxruntime-osx-x86_64)
58+
else()
59+
include(onnxruntime-osx-x86_64-static)
60+
endif()
4161
else()
4262
message(FATAL_ERROR "Unsupport processor {CMAKE_SYSTEM_PROCESSOR} for Darwin")
4363
endif()

sherpa-onnx/csrc/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,16 @@ target_link_libraries(sherpa-onnx-core kaldi-native-fbank-core)
113113

114114
target_link_libraries(sherpa-onnx-core kaldi-decoder-core)
115115

116-
if(BUILD_SHARED_LIBS OR APPLE)
116+
if(BUILD_SHARED_LIBS)
117117
target_link_libraries(sherpa-onnx-core onnxruntime)
118118
else()
119119
target_link_libraries(sherpa-onnx-core ${onnxruntime_lib_files})
120120
endif()
121121

122+
if(NOT BUILD_SHARED_LIBS AND APPLE)
123+
target_link_libraries(sherpa-onnx-core "-framework Foundation")
124+
endif()
125+
122126
if(SHERPA_ONNX_ENABLE_GPU)
123127
target_link_libraries(sherpa-onnx-core
124128
onnxruntime_providers_cuda

0 commit comments

Comments
 (0)