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