onnxruntime.cmake
15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
function(download_onnxruntime)
include(FetchContent)
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
if(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
# For embedded systems
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-linux-aarch64-1.15.1.tgz
${PROJECT_SOURCE_DIR}/onnxruntime-linux-aarch64-1.15.1.tgz
${PROJECT_BINARY_DIR}/onnxruntime-linux-aarch64-1.15.1.tgz
/tmp/onnxruntime-linux-aarch64-1.15.1.tgz
/star-fj/fangjun/download/github/onnxruntime-linux-aarch64-1.15.1.tgz
)
set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-linux-aarch64-1.15.1.tgz")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-linux-aarch64-1.15.1.tgz")
set(onnxruntime_HASH "SHA256=85272e75d8dd841138de4b774a9672ea93c1be108d96038c6c34a62d7f976aee")
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
# For embedded systems
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-linux-arm-1.15.1.zip
${PROJECT_SOURCE_DIR}/onnxruntime-linux-arm-1.15.1.zip
${PROJECT_BINARY_DIR}/onnxruntime-linux-arm-1.15.1.zip
/tmp/onnxruntime-linux-arm-1.15.1.zip
/star-fj/fangjun/download/github/onnxruntime-linux-arm-1.15.1.zip
)
set(onnxruntime_URL "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-linux-arm-1.15.1.zip")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-linux-arm-1.15.1.zip")
set(onnxruntime_HASH "SHA256=867b96210a347e4b1bb949e7c9a3f222371ea0c00c9deaaba9fdd66c689f7fb7")
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
# If you don't have access to the Internet,
# please pre-download onnxruntime
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-linux-x64-1.15.1.tgz
${PROJECT_SOURCE_DIR}/onnxruntime-linux-x64-1.15.1.tgz
${PROJECT_BINARY_DIR}/onnxruntime-linux-x64-1.15.1.tgz
/tmp/onnxruntime-linux-x64-1.15.1.tgz
/star-fj/fangjun/download/github/onnxruntime-linux-x64-1.15.1.tgz
)
set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-linux-x64-1.15.1.tgz")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-linux-x64-1.15.1.tgz")
set(onnxruntime_HASH "SHA256=5492f9065f87538a286fb04c8542e9ff7950abb2ea6f8c24993a940006787d87")
# After downloading, it contains:
# ./lib/libonnxruntime.so.1.15.1
# ./lib/libonnxruntime.so, which is a symlink to lib/libonnxruntime.so.1.15.1
#
# ./include
# It contains all the needed header files
if(SHERPA_ONNX_ENABLE_GPU)
set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-linux-x64-gpu-1.15.1.tgz")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-linux-x64-gpu-1.15.1.tgz")
set(onnxruntime_HASH "SHA256=eab891393025edd5818d1aa26a42860e5739fcc49e3ca3f876110ec8736fe7f1")
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-linux-x64-gpu-1.15.1.tgz
${PROJECT_SOURCE_DIR}/onnxruntime-linux-x64-gpu-1.15.1.tgz
${PROJECT_BINARY_DIR}/onnxruntime-linux-x64-gpu-1.15.1.tgz
/tmp/onnxruntime-linux-x64-gpu-1.15.1.tgz
/star-fj/fangjun/download/github/onnxruntime-linux-x64-gpu-1.15.1.tgz
)
endif()
# After downloading, it contains:
# ./lib/libonnxruntime.so.1.15.1
# ./lib/libonnxruntime.so, which is a symlink to lib/libonnxruntime.so.1.15.1
# ./lib/libonnxruntime_providers_cuda.so
# ./include, which contains all the needed header files
elseif(APPLE)
# If you don't have access to the Internet,
# please pre-download onnxruntime
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-osx-universal2-1.15.1.tgz
${PROJECT_SOURCE_DIR}/onnxruntime-osx-universal2-1.15.1.tgz
${PROJECT_BINARY_DIR}/onnxruntime-osx-universal2-1.15.1.tgz
/tmp/onnxruntime-osx-universal2-1.15.1.tgz
)
set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-osx-universal2-1.15.1.tgz")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-osx-universal2-1.15.1.tgz")
set(onnxruntime_HASH "SHA256=ecb7651c216fe6ffaf4c578e135d98341bc5bc944c5dc6b725ef85b0d7747be0")
# After downloading, it contains:
# ./lib/libonnxruntime.1.15.1.dylib
# ./lib/libonnxruntime.dylib, which is a symlink to lib/libonnxruntime.1.15.1.dylib
#
# ./include
# It contains all the needed header files
elseif(WIN32)
message(STATUS "CMAKE_VS_PLATFORM_NAME: ${CMAKE_VS_PLATFORM_NAME}")
if(CMAKE_VS_PLATFORM_NAME STREQUAL Win32 OR CMAKE_VS_PLATFORM_NAME STREQUAL win32)
if(BUILD_SHARED_LIBS)
# If you don't have access to the Internet,
# please pre-download onnxruntime
#
# for 32-bit windows
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-win-x86-1.15.1.zip
${PROJECT_SOURCE_DIR}/onnxruntime-win-x86-1.15.1.zip
${PROJECT_BINARY_DIR}/onnxruntime-win-x86-1.15.1.zip
/tmp/onnxruntime-win-x86-1.15.1.zip
)
set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-win-x86-1.15.1.zip")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x86-1.15.1.zip")
set(onnxruntime_HASH "SHA256=8de18fdf274a8adcd95272fcf58beda0fe2fb37f0cd62c02bc4bb6200429e4e2")
else()
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-win-x86-static-1.15.1.tar.bz2
${PROJECT_SOURCE_DIR}/onnxruntime-win-x86-static-1.15.1.tar.bz2
${PROJECT_BINARY_DIR}/onnxruntime-win-x86-static-1.15.1.tar.bz2
/tmp/onnxruntime-win-x86-static-1.15.1.tar.bz2
)
set(onnxruntime_URL "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-win-x86-static-1.15.1.tar.bz2")
set(onnxruntime_URL2 "")
set(onnxruntime_HASH "SHA256=a2b33a3e8a1f89cddf303f0a97a5a88f4202579c653cfb29158c8cf7da3734eb")
endif()
if(SHERPA_ONNX_ENABLE_GPU)
message(FATAL_ERROR "GPU support for Win32 is not supported!")
endif()
else()
# for 64-bit windows
if(BUILD_SHARED_LIBS)
# If you don't have access to the Internet,
# please pre-download onnxruntime
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-win-x64-1.15.1.zip
${PROJECT_SOURCE_DIR}/onnxruntime-win-x64-1.15.1.zip
${PROJECT_BINARY_DIR}/onnxruntime-win-x64-1.15.1.zip
/tmp/onnxruntime-win-x64-1.15.1.zip
)
set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-win-x64-1.15.1.zip")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x64-1.15.1.zip")
set(onnxruntime_HASH "SHA256=261308ee5526dfd3f405ce8863e43d624a2e0bcd16b2d33cdea8c120ab3534d3")
if(SHERPA_ONNX_ENABLE_GPU)
set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-win-x64-gpu-1.15.1.zip")
set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x64-gpu-1.15.1.zip")
set(onnxruntime_HASH "SHA256=dcc3a385b415dd2e4a813018b71da5085d9b97774552edf17947826a255a3732")
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-win-x64-gpu-1.15.1.zip
${PROJECT_SOURCE_DIR}/onnxruntime-win-x64-gpu-1.15.1.zip
${PROJECT_BINARY_DIR}/onnxruntime-win-x64-gpu-1.15.1.zip
/tmp/onnxruntime-win-x64-gpu-1.15.1.zip
)
endif()
else()
# static libraries for windows x64
message(STATUS "Use static onnxruntime libraries")
# If you don't have access to the Internet,
# please pre-download onnxruntime
set(possible_file_locations
$ENV{HOME}/Downloads/onnxruntime-win-x64-static-1.15.1.tar.bz2
${PROJECT_SOURCE_DIR}/onnxruntime-win-x64-static-1.15.1.tar.bz2
${PROJECT_BINARY_DIR}/onnxruntime-win-x64-static-1.15.1.tar.bz2
/tmp/onnxruntime-win-x64-static-1.15.1.tar.bz2
)
set(onnxruntime_URL "https://huggingface.co/csukuangfj/onnxruntime-libs/resolve/main/onnxruntime-win-x64-static-1.15.1.tar.bz2")
set(onnxruntime_URL2 "")
set(onnxruntime_HASH "SHA256=f5c19ac1fc6a61c78a231a41df10aede2586665ab397bdc3f007eb8d2c8d4a19")
endif()
endif()
# After downloading, it contains:
# ./lib/onnxruntime.{dll,lib,pdb}
# ./lib/onnxruntime_providers_shared.{dll,lib,pdb}
#
# ./include
# It contains all the needed header files
else()
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
message(FATAL_ERROR "Only support Linux, macOS, and Windows at present. Will support other OSes later")
endif()
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}")
if(BUILD_SHARED_LIBS OR NOT WIN32)
find_library(location_onnxruntime onnxruntime
PATHS
"${onnxruntime_SOURCE_DIR}/lib"
NO_CMAKE_SYSTEM_PATH
)
message(STATUS "location_onnxruntime: ${location_onnxruntime}")
add_library(onnxruntime SHARED IMPORTED)
set_target_properties(onnxruntime PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime}
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include"
)
endif()
if(SHERPA_ONNX_ENABLE_GPU AND NOT WIN32)
find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda
PATHS
"${onnxruntime_SOURCE_DIR}/lib"
NO_CMAKE_SYSTEM_PATH
)
add_library(onnxruntime_providers_cuda SHARED IMPORTED)
set_target_properties(onnxruntime_providers_cuda PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime_cuda_lib}
)
endif()
if(WIN32)
if(BUILD_SHARED_LIBS)
set_property(TARGET onnxruntime
PROPERTY
IMPORTED_IMPLIB "${onnxruntime_SOURCE_DIR}/lib/onnxruntime.lib"
)
file(COPY ${onnxruntime_SOURCE_DIR}/lib/onnxruntime.dll
DESTINATION
${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
)
if(SHERPA_ONNX_ENABLE_GPU)
add_library(onnxruntime_providers_cuda SHARED IMPORTED)
set_target_properties(onnxruntime_providers_cuda PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime}
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include"
)
set_property(TARGET onnxruntime_providers_cuda
PROPERTY
IMPORTED_IMPLIB "${onnxruntime_SOURCE_DIR}/lib/onnxruntime_providers_cuda.lib"
)
file(COPY ${onnxruntime_SOURCE_DIR}/lib/onnxruntime_providers_cuda.dll
DESTINATION
${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
)
endif()
else()
# for static libraries, we use onnxruntime_lib_files directly below
include_directories(${onnxruntime_SOURCE_DIR}/include)
endif()
endif()
if(UNIX AND NOT APPLE)
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*")
elseif(APPLE)
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/libonnxruntime.*.*dylib")
elseif(WIN32)
if(BUILD_SHARED_LIBS)
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/*.dll")
else()
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/*.lib")
set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE)
endif()
endif()
message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(FILES ${onnxruntime_lib_files} DESTINATION ..)
else()
install(FILES ${onnxruntime_lib_files} DESTINATION lib)
endif()
endfunction()
# First, we try to locate the header and the lib if the use has already
# installed onnxruntime. Otherwise, we will download the pre-compiled lib
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
else()
find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h
PATHS
/usr/include
/usr/local/include
)
endif()
message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}")
if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
if(APPLE)
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib)
else()
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so)
endif()
if(NOT EXISTS ${location_onnxruntime_lib})
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)
endif()
if(SHERPA_ONNX_ENABLE_GPU)
set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.so)
if(NOT EXISTS ${location_onnxruntime_cuda_lib})
set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a)
endif()
endif()
else()
find_library(location_onnxruntime_lib onnxruntime
PATHS
/lib
/usr/lib
/usr/local/lib
)
if(SHERPA_ONNX_ENABLE_GPU)
find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda
PATHS
/lib
/usr/lib
/usr/local/lib
)
endif()
endif()
message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")
if(SHERPA_ONNX_ENABLE_GPU)
message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}")
endif()
if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
add_library(onnxruntime SHARED IMPORTED)
set_target_properties(onnxruntime PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime_lib}
INTERFACE_INCLUDE_DIRECTORIES "${location_onnxruntime_header_dir}"
)
if(SHERPA_ONNX_ENABLE_GPU AND location_onnxruntime_cuda_lib)
add_library(onnxruntime_providers_cuda SHARED IMPORTED)
set_target_properties(onnxruntime_providers_cuda PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime_cuda_lib}
)
endif()
else()
message(STATUS "Could not find a pre-installed onnxruntime. Downloading pre-compiled onnxruntime")
download_onnxruntime()
endif()