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