Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / cmake / download_and_extract.cmake
1 # Copyright (C) 2018-2019 Intel Corporation
2 #
3 # SPDX-License-Identifier: Apache-2.0
4 #
5
6 include ("extract")
7 include ("download_and_check")
8
9 function (GetNameAndUrlToDownload name url archive_name_unified archive_name_win archive_name_lin archive_name_mac)
10   if (archive_name_unified)
11     set (${url} "${archive_name_unified}" PARENT_SCOPE)
12     set (${name} ${archive_name_unified} PARENT_SCOPE)
13   else()
14     if (LINUX OR (APPLE AND NOT archive_name_mac))
15       if (NOT archive_name_lin)
16         return()
17       endif()  
18       set (PLATFORM_FOLDER linux)
19       set (archive_name ${archive_name_lin})
20     elseif(APPLE)
21       if (NOT archive_name_mac)
22         return()
23       endif()  
24       set (PLATFORM_FOLDER mac)
25       set (archive_name ${archive_name_mac})
26     else()
27       #if no dependency for target platfrom skip it
28       if (NOT archive_name_win)
29         return()
30       endif()
31       set (PLATFORM_FOLDER windows)
32       set (archive_name ${archive_name_win})
33     endif()
34
35     set (${name} ${archive_name} PARENT_SCOPE)
36     set (${url}  "${archive_name}" PARENT_SCOPE)
37   endif()
38 endfunction(GetNameAndUrlToDownload)
39
40 #download from paltform specific folder from share server
41 function (DownloadAndExtractPlatformSpecific 
42   component 
43   archive_name_unified 
44   archive_name_win 
45   archive_name_lin 
46   archive_name_mac 
47   unpacked_path 
48   result_path
49   folder)
50
51   GetNameAndUrlToDownload(archive_name RELATIVE_URL ${archive_name_unified} ${archive_name_win} ${archive_name_lin} ${archive_name_mac} )
52   if (NOT archive_name OR NOT RELATIVE_URL)
53     return()
54   endif()
55   CheckOrDownloadAndExtract(${component} ${RELATIVE_URL} ${archive_name} ${unpacked_path} result_path2 ${folder} TRUE FALSE TRUE)
56   set (${result_path} ${result_path2} PARENT_SCOPE)
57
58 endfunction(DownloadAndExtractPlatformSpecific)
59
60 #download from common folder
61 function (DownloadAndExtract component archive_name unpacked_path result_path folder)
62   set (RELATIVE_URL  "${archive_name}")
63   set(fattal TRUE)
64   CheckOrDownloadAndExtract(${component} ${RELATIVE_URL} ${archive_name} ${unpacked_path} result_path2 ${folder} ${fattal} result TRUE)
65   
66   if (NOT ${result})
67     DownloadAndExtractPlatformSpecific(${component} ${archive_name} ${archive_name} ${archive_name} ${unpacked_path} ${result_path2} ${folder})
68   endif()  
69
70   set (${result_path} ${result_path2} PARENT_SCOPE)
71
72 endfunction(DownloadAndExtract)
73
74
75 function (DownloadAndExtractInternal URL archive_path  unpacked_path folder fattal result123)
76   set (status "ON")
77   DownloadAndCheck(${URL} ${archive_path} ${fattal} result1)
78   if ("${result1}" STREQUAL "ARCHIVE_DOWNLOAD_FAIL")
79     #check alternative url as well
80     set (status "OFF")
81     file(REMOVE_RECURSE "${archive_path}")    
82   endif()
83
84   if ("${result1}" STREQUAL "CHECKSUM_DOWNLOAD_FAIL" OR "${result1}" STREQUAL "HASH_MISMATCH")
85     set(status FALSE)
86     file(REMOVE_RECURSE "${archive_path}")    
87   endif()
88
89   if("${status}" STREQUAL "ON")
90     ExtractWithVersion(${URL} ${archive_path} ${unpacked_path} ${folder} result)
91   endif()
92   
93   set (result123 ${status} PARENT_SCOPE)
94
95 endfunction(DownloadAndExtractInternal)
96
97 function (ExtractWithVersion URL archive_path unpacked_path folder result)
98
99   debug_message("ExtractWithVersion : ${archive_path} : ${unpacked_path}")
100   extract(${archive_path} ${unpacked_path} ${folder} status)
101   #dont need archive actually after unpacking
102   file(REMOVE_RECURSE "${archive_path}")  
103   if (${status})
104     set (version_file ${unpacked_path}/ie_dependency.info)
105     file(WRITE ${version_file} ${URL})
106   else()
107     file(REMOVE_RECURSE "${unpacked_path}")
108   endif()
109   set (${result} ${status} PARENT_SCOPE)  
110 endfunction (ExtractWithVersion)
111
112 function (DownloadOrExtractInternal URL archive_path unpacked_path folder fattal result123)
113   debug_message("checking wether archive downloaded : ${archive_path}")
114
115   if (NOT EXISTS ${archive_path})
116     DownloadAndExtractInternal(${URL} ${archive_path} ${unpacked_path} ${folder} ${fattal} result)
117   else()
118
119     if (ENABLE_UNSAFE_LOCATIONS)
120       ExtractWithVersion(${URL} ${archive_path} ${unpacked_path} ${folder} result)
121       if(NOT ${result})
122         DownloadAndExtractInternal(${URL} ${archive_path} ${unpacked_path} ${folder} ${fattal} result)
123       endif()
124     else()
125       debug_message("archive found on FS : ${archive_path}, however we cannot check it's checksum and think that it is invalid")
126       file(REMOVE_RECURSE "${archive_path}")
127       DownloadAndExtractInternal(${URL} ${archive_path} ${unpacked_path} ${folder} ${fattal} result)
128     endif()  
129
130   
131   endif()  
132
133   if (NOT ${result})
134     message(FATAL_ERROR "error: extract of '${archive_path}' failed")
135   endif()
136
137 endfunction(DownloadOrExtractInternal)
138
139 file(REMOVE ${CMAKE_BINARY_DIR}/dependencies_64.txt)
140
141 function (CheckOrDownloadAndExtract component RELATIVE_URL archive_name unpacked_path result_path folder fattal result123 use_alternatives)
142   set (archive_path ${TEMP}/download/${archive_name})
143   set (status "ON")
144   set (on_master FALSE)
145
146   if(DEFINED ENV{IE_PATH_TO_DEPS})
147     set(URL "$ENV{IE_PATH_TO_DEPS}/${RELATIVE_URL}")
148   else()
149     set(URL "https://download.01.org/opencv/2019/openvinotoolkit/R1/inference_engine/${RELATIVE_URL}")
150   endif()
151
152   #no message on recursive calls
153   if (${use_alternatives})
154     set(DEP_INFO "${component}=${URL}")
155     debug_message (STATUS "DEPENDENCY_URL: ${DEP_INFO}")
156     file(APPEND ${CMAKE_BINARY_DIR}/dependencies_64.txt "${DEP_INFO}\n")
157   endif()
158
159   debug_message ("checking that unpacked directory exist: ${unpacked_path}")
160
161   if (NOT EXISTS ${unpacked_path})
162     DownloadOrExtractInternal(${URL} ${archive_path} ${unpacked_path} ${folder} ${fattal} status)
163   else(NOT EXISTS ${unpacked_path})  
164     #path exists, so we would like to check what was unpacked version
165     set (version_file ${unpacked_path}/ie_dependency.info)
166
167     if (DEFINED TEAMCITY_GIT_BRANCH)
168       if(${TEAMCITY_GIT_BRANCH} STREQUAL "master")
169         set(on_master TRUE)
170         debug_message ("On master branch, update data in DL_SDK_TEMP if necessary")
171       endif()
172     endif()
173
174     if (NOT EXISTS ${version_file} AND NOT ${ENABLE_ALTERNATIVE_TEMP})
175       clean_message(FATAL_ERROR "error: Dependency doesn't contain version file. Please select actions: \n"
176         "if you are not sure about your FS dependency - remove it : \n"
177         "\trm -rf ${unpacked_path}\n"
178         "and rerun cmake.\n"
179         "If your dependency is fine, then execute:\n\techo ${URL} > ${unpacked_path}/ie_dependency.info\n")
180 #     file(REMOVE_RECURSE "${unpacked_path}") 
181 #     DownloadOrExtractInternal(${URL} ${archive_path} ${unpacked_path} ${fattal} status)
182     else()
183       if (EXISTS ${version_file})
184         file(READ "${version_file}" dependency_url)
185         string(REGEX REPLACE "\n" ";" dependency_url "${dependency_url}")
186         #we have decided to stick each dependency to unique url that will be that record in version file
187         debug_message("dependency_info on FS : \"${dependency_url}\"\n"
188                       "compare to            : \"${URL}\"" )
189       else ()
190         debug_message("no version file available at ${version_file}")
191       endif()
192
193     if (NOT EXISTS ${version_file} OR NOT ${dependency_url} STREQUAL ${URL})
194       if (${use_alternatives} AND ALTERNATIVE_PATH AND NOT ${on_master})
195         #creating alternative_path
196         string(REPLACE ${TEMP} ${ALTERNATIVE_PATH} unpacked_path ${unpacked_path})
197         string(REPLACE ${TEMP} ${ALTERNATIVE_PATH} archive_path ${archive_path})
198
199         debug_message("dependency different: use local path for fetching updated version: ${alternative_path}")
200         CheckOrDownloadAndExtract(${component} ${RELATIVE_URL} ${archive_name} ${unpacked_path} ${result_path} ${folder} ${fattal} ${result123} FALSE)
201
202       else()
203         debug_message("dependency updated: download it again")
204         file(REMOVE_RECURSE "${unpacked_path}") 
205         DownloadOrExtractInternal(${URL} ${archive_path} ${unpacked_path} ${folder} ${fattal} status)
206       endif()
207     endif ()
208    endif()
209   endif()
210
211   if (${use_alternatives} OR ${on_master})
212     set (${result123} "${status}" PARENT_SCOPE)
213     set (${result_path} ${unpacked_path} PARENT_SCOPE)
214   endif()
215
216   
217   
218 endfunction(CheckOrDownloadAndExtract)
219