-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathFindONNXRuntime.cmake
More file actions
222 lines (196 loc) · 7.08 KB
/
Copy pathFindONNXRuntime.cmake
File metadata and controls
222 lines (196 loc) · 7.08 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
# ================================================
# FindONNXRuntime.cmake
# A CMake module to find or download ONNX Runtime.
#
# Priority:
# 1. Native optimized ONNXRuntime under:
# extra/onnxruntime/
# 2. System installation
# 3. Official generic ONNXRuntime release package
#
# This allows advanced users to provide architecture-optimized
# ONNXRuntime builds while keeping the default generic package
# for maximum compatibility.
# Supports: Linux, macOS, Windows
# Supports: CPU or GPU builds (based on USE_GPU env)
# ================================================
# Set default ONNX Runtime version
if (NOT ONNXRUNTIME_VERSION)
set(ONNXRUNTIME_VERSION "1.24.4")
endif()
# Automatically determine backend: cpu or gpu
if (DEFINED ENV{USE_GPU})
message(STATUS "[ONNXRuntime] USE_GPU is set in the environment. Using GPU backend.")
set(ONNXRUNTIME_BACKEND "gpu")
else()
set(ONNXRUNTIME_BACKEND "cpu")
endif()
# Detect platform and architecture
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(_os "linux")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
set(_arch "aarch64")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
set(_arch "arm")
else()
set(_arch "x64")
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(_os "osx")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
set(_arch "arm64")
else()
set(_arch "x64")
endif()
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
set(_os "win")
set(_arch "x64") # Windows ARM not currently supported by ONNX Runtime
else()
message(FATAL_ERROR "[ONNXRuntime] Unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()
# Determine archive filename based on platform, architecture and backend
if (_os STREQUAL "linux")
if (_arch STREQUAL "aarch64")
if (ONNXRUNTIME_BACKEND STREQUAL "cpu")
set(_archive_name "onnxruntime-linux-${_arch}-${ONNXRUNTIME_VERSION}.tgz")
else()
message(WARNING "[ONNXRuntime] GPU backend not available for ARM64, using CPU")
set(_archive_name "onnxruntime-linux-${_arch}-${ONNXRUNTIME_VERSION}.tgz")
endif()
elseif(_arch STREQUAL "arm")
if (ONNXRUNTIME_BACKEND STREQUAL "cpu")
set(_archive_name "onnxruntime-linux-${_arch}-${ONNXRUNTIME_VERSION}.tgz")
else()
message(WARNING "[ONNXRuntime] GPU backend not available for ARM, using CPU")
set(_archive_name "onnxruntime-linux-${_arch}-${ONNXRUNTIME_VERSION}.tgz")
endif()
else()
if (ONNXRUNTIME_BACKEND STREQUAL "cpu")
set(_archive_name "onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}.tgz")
else()
set(_archive_name "onnxruntime-linux-x64-gpu-${ONNXRUNTIME_VERSION}.tgz")
endif()
endif()
elseif(_os STREQUAL "osx")
if (_arch STREQUAL "arm64")
set(_archive_name "onnxruntime-osx-arm64-${ONNXRUNTIME_VERSION}.tgz")
else()
set(_archive_name "onnxruntime-osx-x64-${ONNXRUNTIME_VERSION}.tgz")
endif()
elseif(_os STREQUAL "win")
if (ONNXRUNTIME_BACKEND STREQUAL "cpu")
set(_archive_name "onnxruntime-win-x64-${ONNXRUNTIME_VERSION}.zip")
else()
set(_archive_name "onnxruntime-win-x64-gpu-${ONNXRUNTIME_VERSION}.zip")
endif()
endif()
# Download and install locations
set(_onnx_url "https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/${_archive_name}")
set(_download_path "${CMAKE_BINARY_DIR}/${_archive_name}")
set(_extract_dir "${CMAKE_BINARY_DIR}/_deps")
set(_install_dir "${_extract_dir}/onnxruntime")
# ============================================================
# 1. Prefer bundled native optimized ONNXRuntime if available
# ============================================================
set(_extra_onnxruntime_root
"${CMAKE_SOURCE_DIR}/extra/onnxruntime")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
set(_extra_arch "aarch64")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
set(_extra_arch "x86_64")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
set(_extra_arch "arm")
else()
set(_extra_arch "${CMAKE_SYSTEM_PROCESSOR}")
endif()
find_path(ONNXRUNTIME_NATIVE_INCLUDE_DIR
NAMES onnxruntime_c_api.h
PATHS
"${_extra_onnxruntime_root}/include"
NO_DEFAULT_PATH)
find_library(ONNXRUNTIME_NATIVE_LIBRARY
NAMES onnxruntime
PATHS
"${_extra_onnxruntime_root}/lib/${_extra_arch}"
"${_extra_onnxruntime_root}/lib"
NO_DEFAULT_PATH)
if (ONNXRUNTIME_NATIVE_INCLUDE_DIR AND
ONNXRUNTIME_NATIVE_LIBRARY)
set(ONNXRUNTIME_FOUND TRUE)
set(ONNXRUNTIME_INCLUDE_DIRS
${ONNXRUNTIME_NATIVE_INCLUDE_DIR})
set(ONNXRUNTIME_LIBRARIES
${ONNXRUNTIME_NATIVE_LIBRARY})
get_filename_component(
ONNXRUNTIME_LIB_DIR
${ONNXRUNTIME_NATIVE_LIBRARY}
DIRECTORY)
message(STATUS
"[ONNXRuntime] Using bundled native optimized build: "
"${ONNXRUNTIME_NATIVE_LIBRARY}")
return()
endif()
# ============================================================
# 2. Try system-wide ONNX Runtime installation
# ============================================================
find_path(ONNXRUNTIME_INCLUDE_DIR onnxruntime_c_api.h
PATHS /usr/include /usr/local/include /opt/include
NO_DEFAULT_PATH)
find_library(ONNXRUNTIME_LIBRARY onnxruntime
PATHS /usr/lib /usr/local/lib /opt/lib /usr/lib/aarch64-linux-gnu /usr/lib/arm-linux-gnueabihf
NO_DEFAULT_PATH)
if (ONNXRUNTIME_INCLUDE_DIR AND ONNXRUNTIME_LIBRARY)
set(ONNXRUNTIME_FOUND TRUE)
set(ONNXRUNTIME_INCLUDE_DIRS ${ONNXRUNTIME_INCLUDE_DIR})
set(ONNXRUNTIME_LIBRARIES ${ONNXRUNTIME_LIBRARY})
get_filename_component(
ONNXRUNTIME_LIB_DIR
${ONNXRUNTIME_LIBRARY}
DIRECTORY)
message(STATUS "[ONNXRuntime] Found system installation.")
return()
endif()
# Check if already downloaded and extracted
if (EXISTS "${_install_dir}")
message(STATUS "[ONNXRuntime] Found existing installation at ${_install_dir}")
else()
# If not found, download the binary archive
message(STATUS
"[ONNXRuntime] No native/system installation found. "
"Downloading generic official release: ${_onnx_url}")
file(DOWNLOAD ${_onnx_url} ${_download_path}
SHOW_PROGRESS STATUS _dl_status)
list(GET _dl_status 0 _dl_code)
if (NOT _dl_code EQUAL 0)
message(FATAL_ERROR "[ONNXRuntime] Failed to download: ${_onnx_url}")
endif()
# Extract the archive
file(MAKE_DIRECTORY ${_extract_dir})
if (_archive_name MATCHES ".zip$")
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar xzf ${_download_path} --format=zip
WORKING_DIRECTORY ${_extract_dir}
)
else()
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar xzf ${_download_path}
WORKING_DIRECTORY ${_extract_dir}
)
endif()
# Rename the extracted directory to a fixed path
file(GLOB _dirs "${_extract_dir}/onnxruntime-*")
list(GET _dirs 0 _extracted_dir)
file(RENAME ${_extracted_dir} ${_install_dir})
endif()
# Register the ONNX Runtime include and lib paths
set(ONNXRUNTIME_FOUND TRUE)
set(ONNXRUNTIME_INCLUDE_DIRS "${_install_dir}/include")
set(ONNXRUNTIME_LIB_DIR "${_install_dir}/lib/")
if (_os STREQUAL "win")
set(ONNXRUNTIME_LIBRARY "${_install_dir}/lib/onnxruntime.lib")
elseif(APPLE)
set(ONNXRUNTIME_LIBRARY "${_install_dir}/lib/libonnxruntime.dylib")
else()
set(ONNXRUNTIME_LIBRARY "${_install_dir}/lib/libonnxruntime.so")
endif()
set(ONNXRUNTIME_LIBRARIES ${ONNXRUNTIME_LIBRARY})