/
onnxruntime.cmake
206 lines (192 loc) · 7.61 KB
/
onnxruntime.cmake
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
# Copyright (c) 2022-2023 Xiaomi Corporation
function(download_onnxruntime)
include(FetchContent)
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
if(SHERPA_ONNX_ENABLE_WASM)
include(onnxruntime-wasm-simd)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL riscv64)
if(BUILD_SHARED_LIBS)
include(onnxruntime-linux-riscv64)
else()
include(onnxruntime-linux-riscv64-static)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
if(BUILD_SHARED_LIBS)
include(onnxruntime-linux-aarch64)
else()
include(onnxruntime-linux-aarch64-static)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND (CMAKE_SYSTEM_PROCESSOR STREQUAL arm OR CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l))
if(BUILD_SHARED_LIBS)
include(onnxruntime-linux-arm)
else()
include(onnxruntime-linux-arm-static)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
if(SHERPA_ONNX_ENABLE_GPU)
include(onnxruntime-linux-x86_64-gpu)
elseif(BUILD_SHARED_LIBS)
include(onnxruntime-linux-x86_64)
else()
include(onnxruntime-linux-x86_64-static)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
if (arm64 IN_LIST CMAKE_OSX_ARCHITECTURES AND x86_64 IN_LIST CMAKE_OSX_ARCHITECTURES)
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
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
if(BUILD_SHARED_LIBS)
include(onnxruntime-osx-x86_64)
else()
include(onnxruntime-osx-x86_64-static)
endif()
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64)
if(BUILD_SHARED_LIBS)
include(onnxruntime-osx-arm64)
else()
include(onnxruntime-osx-arm64-static)
endif()
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL 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()
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)
include(onnxruntime-win-x86)
else()
if(CMAKE_BUILD_TYPE STREQUAL Release)
include(onnxruntime-win-x86-static)
elseif(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo OR CMAKE_BUILD_TYPE STREQUAL MinSizeRel)
include(onnxruntime-win-x86-static-debug)
else()
message(FATAL_ERROR "Support only CMAKE_BUILD_TYPE being Release, Debug, RelWithDebInfo, or MinSizeRel. Given: ${CMAKE_BUILD_TYPE}")
endif()
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)
message(STATUS "Use dynamic onnxruntime libraries")
if(SHERPA_ONNX_ENABLE_GPU)
include(onnxruntime-win-x64-gpu)
else()
include(onnxruntime-win-x64)
endif()
else()
# static libraries for windows x64
message(STATUS "Use static onnxruntime libraries")
if(CMAKE_BUILD_TYPE STREQUAL Release)
include(onnxruntime-win-x64-static)
elseif(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo OR CMAKE_BUILD_TYPE STREQUAL MinSizeRel)
include(onnxruntime-win-x64-static-debug)
else()
message(FATAL_ERROR "Support only CMAKE_BUILD_TYPE being Release, Debug, RelWithDebInfo, or MinSizeRel. Given: ${CMAKE_BUILD_TYPE}")
endif()
endif()
endif()
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()
set(onnxruntime_SOURCE_DIR ${onnxruntime_SOURCE_DIR} PARENT_SCOPE)
endfunction()
if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
# First, we try to locate the header and the lib if the user 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})
include_directories(${location_onnxruntime_header_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)
if(NOT EXISTS ${location_onnxruntime_lib})
message(FATAL_ERROR "${location_onnxruntime_lib} cannot be found")
endif()
set(onnxruntime_lib_files $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)
message("Use static lib: ${onnxruntime_lib_files}")
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()
endif()
if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
if(NOT DEFINED onnxruntime_lib_files)
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()
endif()
else()
if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
message(STATUS "Could not find a pre-installed onnxruntime.")
endif()
message(STATUS "Downloading pre-compiled onnxruntime")
download_onnxruntime()
endif()