Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / cmake / download_and_apply.cmake
1 # Copyright (C) 2018-2019 Intel Corporation
2 #
3 # SPDX-License-Identifier: Apache-2.0
4 #
5
6 function (DownloadAndApply URL apply_to)
7
8   if (EXISTS ${apply_to})
9     file(READ ${apply_to} patchFile4Bytes LIMIT 4)
10   endif()
11   debug_message(STATUS "patchFile=" ${apply_to})
12   debug_message(STATUS "patchFile4Bytes=" ${patchFileSize} "\"")
13
14   if (NOT EXISTS ${apply_to} OR NOT patchFile4Bytes)
15     #remove empty file
16     if (EXISTS ${apply_to})
17       file(REMOVE ${apply_to})
18     endif()
19     
20     DownloadAndCheck(${URL} ${apply_to} TRUE result)
21   else ()
22     set (MIGHT_BE_APPLIED 1)
23   endif()
24
25     get_filename_component(apply_dir ${apply_to} DIRECTORY)
26     get_filename_component(patch_name ${apply_to} NAME)
27
28     # git apply silently if file wasnot downloaded :
29     #
30     if (NOT DEFINED MIGHT_BE_APPLIED)
31         debug_message(STATUS "applying patch ... ")
32     endif()
33
34 if (NOT MINGW)
35     execute_process(COMMAND git apply --verbose  ${patch_name}
36       WORKING_DIRECTORY ${apply_dir}
37       RESULT_VARIABLE rv
38       ERROR_VARIABLE err)
39 else()
40     debug_message("git patch: " ${patch_name} " not applied under MINGW, however it downloaded")
41 endif()
42
43     #TODO:bad patch indicator either it is bad by itself of failed to apply
44     #currently cannot detect what happened 
45     if (NOT DEFINED MIGHT_BE_APPLIED AND NOT (rv EQUAL 0))
46       file(REMOVE_RECURSE "${apply_to}")
47       debug_message(FATAL_ERROR "cannot apply patch ${patch_name} " ${err})
48     endif()
49   
50 endfunction(DownloadAndApply)