forked from luxonis/depthai-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DepthaiDownloader.cmake
323 lines (254 loc) · 12.2 KB
/
DepthaiDownloader.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
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
# This script downloads depthai device side artifacts
function(DepthaiDownload)
### VARIABLES
# Artifactory
set(DEPTHAI_BASE_URL "https://artifacts.luxonis.com/artifactory")
# Repositories
set(DEPTHAI_REPO_SNAPSHOT "luxonis-myriad-snapshot-local")
set(DEPTHAI_REPO_RELEASE "luxonis-myriad-release-local")
# Prefix
set(DEPTHAI_ARTIFACT_PREFIX "depthai-device-side")
# Errors and retry count
set(DEPTHAI_TIMEOUT_S 300)
set(DEPTHAI_INACTIVE_TIMEOUT_S 60)
set(DEPTHAI_DOWNLOAD_RETRY_NUM 5)
### END VARIABLES
# PARSE ARGUMENTS
set(_download_patch_only OFF)
#if first argument PATCH_ONLY, second must be either ON/OFF
if("${ARGV2}" STREQUAL "PATCH_ONLY")
set(_depthai_shared_commit ${ARGV0})
set(_enforce_depthai_shared_commit ${ARGV1})
if( ${ARGV3} )
set(_download_patch_only ON)
else()
set(_download_patch_only OFF)
endif()
set(folder "${ARGV4}")
set(output_list_var "${ARGV5}")
set(maturity "${ARGV6}")
set(commit "${ARGV7}")
set(version "${ARGV8}") #optional
else()
set(folder "${ARGV0}")
set(output_list_var "${ARGV1}")
set(maturity "${ARGV2}")
set(commit "${ARGV3}")
set(version "${ARGV4}") #optional
endif("${ARGV2}" STREQUAL "PATCH_ONLY")
if(_download_patch_only)
message(STATUS "Downloading depthai and patch")
else()
message(STATUS "Downloading depthai, depthai-usb2 and patch")
endif()
# END PARSE ARGUMENTS
#DEBUG
#message(STATUS "folder ${folder}")
#message(STATUS "maturity ${maturity}")
#message(STATUS "commit ${commit}")
#message(STATUS "version ${version}") #optional
string(TOLOWER "${maturity}" maturity_lower)
# Switch between maturity
if(${maturity_lower} STREQUAL "snapshot")
set(_selected_repo "${DEPTHAI_REPO_SNAPSHOT}")
# Create download directory string
string(CONFIGURE "@DEPTHAI_BASE_URL@/@DEPTHAI_REPO_SNAPSHOT@/@DEPTHAI_ARTIFACT_PREFIX@/@commit@" _download_directory_url)
# Create _version_commit_identifier
set(_version_commit_identifier "${commit}")
elseif(${maturity_lower} STREQUAL "release")
set(_selected_repo "${DEPTHAI_REPO_RELEASE}")
# TODO
# Create download directory string
#string(CONFIGURE "@DEPTHAI_BASE_URL@/@DEPTHAI_REPO_SNAPSHOT@/@DEPTHAI_ARTIFACT_PREFIX@/@_commit_hash@" _download_directory_url)
else()
# Not a recognized maturity level
message(FATAL_ERROR "Cannot download DepthAI Device Side binaries. Maturity level not recognized (${maturity_lower})")
return()
endif()
# Prints error message
macro(PrintErrorMessage status)
if(${status} EQUAL 22)
message(STATUS "Resource not found, check if commit hash is correctly specified.\n")
elseif(${status} EQUAL 28)
message(STATUS "Timeout.\n")
elseif(${status} EQUAL 99)
message(STATUS "Couldn't retrieve files correctly, checksum mismatch.")
else()
message(STATUS "Unknown error.\n")
endif()
endmacro()
# Download files
function(DownloadAndChecksum url url_checksum output status_var)
# Check if file already downloaded (in resources)
if(EXISTS "${output}")
get_filename_component(_filename "${output}" NAME)
message(STATUS "File already downloaded (resources): ${_filename}")
set("${status_var}" "0" PARENT_SCOPE)
return()
endif()
# Retry again if failed
set(_num_retries_left ${DEPTHAI_DOWNLOAD_RETRY_NUM})
# Set error by default
set("${status_var}" "1" PARENT_SCOPE)
while(NOT ${_num_retries_left} EQUAL 0)
math(EXPR _num_retries_left "${_num_retries_left} - 1")
# Download checksum first
file(DOWNLOAD "${url_checksum}" "${output}.checksum" STATUS _status TIMEOUT ${DEPTHAI_TIMEOUT_S})
# Read checksum to file
file(READ "${output}.checksum" _file_checksum)
string(REGEX REPLACE "\n$" "" _file_checksum "${_file_checksum}")
# Remove checksum file
file(REMOVE "${output}.checksum")
#CHECKS
list(GET _status 0 _status_num)
if(${_status_num})
message(STATUS "Status error: ${_status}")
set("${status_var}" "${_status_num}" PARENT_SCOPE)
continue()
endif()
# Download file and validate checksum
file(DOWNLOAD "${url}" "${output}" INACTIVITY_TIMEOUT ${DEPTHAI_INACTIVE_TIMEOUT_S} STATUS _status TIMEOUT ${DEPTHAI_TIMEOUT_S} SHOW_PROGRESS)
#CHECKS
list(GET _status 0 _status_num)
if(${_status_num})
message(STATUS "Status error: ${_status}")
set("${status_var}" "${_status_num}" PARENT_SCOPE)
continue()
endif()
# Now check if hash matches (if both files were downloaded without timeouts)
file(SHA256 ${output} _downloaded_checksum)
# if hashes don't match
if(NOT (_downloaded_checksum STREQUAL _file_checksum))
message(STATUS "Downloaded file checksum mismatch: ${_downloaded_checksum} != {_file_checksum}")
set("${status_var}" "99" PARENT_SCOPE)
continue()
endif()
# If no errors happened, set status to 0
set("${status_var}" "0" PARENT_SCOPE)
# And break the loop
break()
endwhile()
endfunction()
# Check if depthai-shared matches
message(STATUS "commit: ${_depthai_shared_commit}")
if(_depthai_shared_commit)
DownloadAndChecksum(
"${_download_directory_url}/depthai-shared-commit-hash-${_version_commit_identifier}.txt" # File
"${_download_directory_url}/depthai-shared-commit-hash-${_version_commit_identifier}.sha256.checksum" # File checksum
"${folder}/depthai-shared-commit-hash-${_version_commit_identifier}.txt"
status
)
if(${status})
message(STATUS "Couldn't check if depthai-shared codebase matches between device and host")
if(${_enforce_depthai_shared_commit})
message(FATAL_ERROR "Aborting.\n")
endif()
else()
set(_message_mode WARNING)
if(${_enforce_depthai_shared_commit})
set(_message_mode FATAL_ERROR)
endif()
# Read commit hash file
file(READ "${folder}/depthai-shared-commit-hash-${_version_commit_identifier}.txt" _device_depthai_shared_commit_hash)
string(REGEX REPLACE "\n$" "" _device_depthai_shared_commit_hash "${_device_depthai_shared_commit_hash}")
string(REGEX REPLACE "\n$" "" _depthai_shared_commit "${_depthai_shared_commit}")
string(COMPARE EQUAL "${_device_depthai_shared_commit_hash}" "${_depthai_shared_commit}" _is_same)
# If commits dont match
if(NOT ${_is_same})
message(${_message_mode} "depthai-shared codebases differ between device and host. Enforce (CI): ${_enforce_depthai_shared_commit} (device: ${_device_depthai_shared_commit_hash}, host: ${_depthai_shared_commit}")
else()
message(STATUS "depthai-shared between device and host MATCH!. (device: ${_device_depthai_shared_commit_hash}, host: ${_depthai_shared_commit}")
endif()
endif()
endif()
# Download depthai-device firmware package
message(STATUS "Downloading and checking depthai-device-fwp.tar.xz")
DownloadAndChecksum(
"${_download_directory_url}/depthai-device-fwp-${_version_commit_identifier}.tar.xz" # File
"${_download_directory_url}/depthai-device-fwp-${_version_commit_identifier}.tar.xz.sha256" # File checksum
"${folder}/depthai-device-fwp-${_version_commit_identifier}.tar.xz"
status
)
if(${status})
message(STATUS "\nCouldn't download depthai-device-fwp.tar.xz\n")
PrintErrorMessage(${status})
message(FATAL_ERROR "Aborting.\n")
endif()
# add depthai-device-fwp.tar.xz to list
list(APPEND "${output_list_var}" "${folder}/depthai-device-fwp-${_version_commit_identifier}.tar.xz")
## # depthai.cmd
## message(STATUS "Downloading and checking depthai.cmd")
## DownloadAndChecksum(
## "${_download_directory_url}/depthai-${_version_commit_identifier}.cmd" # File
## "${_download_directory_url}/depthai-${_version_commit_identifier}.sha256.checksum" # File checksum
## "${folder}/depthai-${_version_commit_identifier}.cmd"
## status
## )
## if(${status})
## message(STATUS "\nCouldn't download depthai.cmd\n")
## PrintErrorMessage(${status})
## message(FATAL_ERROR "Aborting.\n")
## endif()
## # add depthai.cmd to list
## list(APPEND "${output_list_var}" "${folder}/depthai-${_version_commit_identifier}.cmd")
##
## if(NOT _download_patch_only)
## # depthai-usb2.cmd
## message(STATUS "Downloading and checking depthai-usb2.cmd")
## DownloadAndChecksum(
## "${_download_directory_url}/depthai-usb2-${_version_commit_identifier}.cmd" # File
## "${_download_directory_url}/depthai-usb2-${_version_commit_identifier}.sha256.checksum" # File checksum
## "${folder}/depthai-usb2-${_version_commit_identifier}.cmd"
## status
## )
## if(${status})
## message(STATUS "\nCouldn't download depthai-usb2.cmd.\n")
## PrintErrorMessage(${status})
## message(FATAL_ERROR "Aborting.\n")
## endif()
## # add depthai-usb2.cmd to list
## list(APPEND "${output_list_var}" "${folder}/depthai-usb2-${_version_commit_identifier}.cmd")
## endif(NOT _download_patch_only)
## # depthai-usb2-patch.patch
## message(STATUS "Downloading and checking depthai-usb2-patch.patch")
## DownloadAndChecksum(
## "${_download_directory_url}/depthai-usb2-patch-${_version_commit_identifier}.patch" # File
## "${_download_directory_url}/depthai-usb2-patch-${_version_commit_identifier}.sha256.checksum" # File checksum
## "${folder}/depthai-usb2-patch-${_version_commit_identifier}.patch"
## status
## )
## if(${status})
## message(STATUS "\nCouldn't download depthai-usb2-patch.patch.\n")
## PrintErrorMessage(${status})
## message(FATAL_ERROR "Aborting.\n")
## endif()
## # add depthai-usb2.cmd to list
## list(APPEND "${output_list_var}" "${folder}/depthai-usb2-patch-${_version_commit_identifier}.patch")
# Propagate the variable to parent
set("${output_list_var}" "${${output_list_var}}" PARENT_SCOPE)
endfunction()
function(DepthaiLocal patch_only patch_only_on_off output_dir output_list_var path1 path2 path3)
#debug
message(STATUS "patch only: ${patch_only}\non/off${patch_only_on_off}\noutput dir: ${output_dir}\nout_var: ${output_list_var}\npath1: ${path1}\npath2: ${path2}\npath3: ${path3}")
# If patch only
if(patch_only_on_off)
if(NOT path1 OR NOT path3)
message(FATAL_ERROR "Both depthai binary and depthai usb2 patch files must be specified")
else()
configure_file("${path1}" "${output_dir}/depthai.cmd" COPYONLY)
configure_file("${path3}" "${output_dir}/depthai-usb2-patch.patch" COPYONLY)
# Set output list
list(APPEND _resource_list "${output_dir}/depthai.cmd" "${output_dir}/depthai-usb2-patch.patch")
endif()
else()
if(NOT path1 OR NOT path2)
message(FATAL_ERROR "Both depthai and depthai usb2 binary files must be specified")
else()
configure_file("${path1}" "${output_dir}/depthai.cmd" COPYONLY)
configure_file("${path2}" "${output_dir}/depthai-usb2.cmd" COPYONLY)
# Set output list
list(APPEND _resource_list "${output_dir}/depthai.cmd" "${output_dir}/depthai-usb2.cmd")
endif()
endif()
set("${output_list_var}" "${_resource_list}" PARENT_SCOPE)
endfunction()