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