From f9828cd0c9ae019a57dafc3acb89d80a52025e52 Mon Sep 17 00:00:00 2001
From: Alexander Alekhin <alexander.alekhin@intel.com>
Date: Thu, 19 Jan 2017 13:33:39 +0300
Subject: [PATCH] cmake: fix MKL detection in case of unsupported versions

CMake should not fail.
---
 cmake/OpenCVFindLAPACK.cmake |  3 ++-
 cmake/OpenCVFindMKL.cmake    | 21 +++++++++++----------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/cmake/OpenCVFindLAPACK.cmake b/cmake/OpenCVFindLAPACK.cmake
index 0bee1b4c90..09cd66cdae 100644
--- a/cmake/OpenCVFindLAPACK.cmake
+++ b/cmake/OpenCVFindLAPACK.cmake
@@ -133,6 +133,7 @@ if(WITH_LAPACK)
         set(LAPACK_IMPL         "LAPACK/Apple")
         ocv_lapack_check()
       else()
+        unset(LAPACK_LIBRARIES)
         unset(LAPACK_LIBRARIES CACHE)
       endif()
     endif()
@@ -147,7 +148,7 @@ if(WITH_LAPACK)
     ocv_lapack_check()
   endif()
 
-  if(NOT HAVE_LAPACK AND LAPACK_LIBRARIES)
+  if(NOT HAVE_LAPACK AND LAPACK_LIBRARIES AND LAPACK_CBLAS_H AND LAPACK_LAPACKE_H)
     ocv_lapack_check()
   endif()
 
diff --git a/cmake/OpenCVFindMKL.cmake b/cmake/OpenCVFindMKL.cmake
index 337456843b..0f5462acd2 100644
--- a/cmake/OpenCVFindMKL.cmake
+++ b/cmake/OpenCVFindMKL.cmake
@@ -20,10 +20,8 @@ macro (mkl_find_lib VAR NAME DIRS)
 endmacro()
 
 macro(mkl_fail)
-    set(HAVE_MKL OFF CACHE BOOL "True if MKL found")
+    set(HAVE_MKL OFF)
     set(MKL_ROOT_DIR ${MKL_ROOT_DIR} CACHE PATH "Path to MKL directory")
-    unset(MKL_INCLUDE_DIRS CACHE)
-    unset(MKL_LIBRARIES CACHE)
     return()
 endmacro()
 
@@ -64,11 +62,16 @@ if(NOT MKL_ROOT_DIR OR NOT EXISTS ${MKL_ROOT_DIR}/include/mkl.h)
     find_path(MKL_ROOT_DIR include/mkl.h PATHS ${mkl_root_paths})
 endif()
 
-if(NOT MKL_ROOT_DIR)
+set(MKL_INCLUDE_DIRS ${MKL_ROOT_DIR}/include CACHE PATH "Path to MKL include directory")
+
+if(NOT MKL_ROOT_DIR
+    OR NOT EXISTS "${MKL_ROOT_DIR}"
+    OR NOT EXISTS "${MKL_INCLUDE_DIRS}"
+    OR NOT EXISTS "${MKL_INCLUDE_DIRS}/mkl_version.h"
+)
     mkl_fail()
 endif()
 
-set(MKL_INCLUDE_DIRS ${MKL_ROOT_DIR}/include)
 get_mkl_version(${MKL_INCLUDE_DIRS}/mkl_version.h)
 
 #determine arch
@@ -124,13 +127,11 @@ foreach(lib ${mkl_lib_list})
 endforeach()
 
 message(STATUS "Found MKL ${MKL_VERSION_STR} at: ${MKL_ROOT_DIR}")
-set(HAVE_MKL ON CACHE BOOL "True if MKL found")
+set(HAVE_MKL ON)
 set(MKL_ROOT_DIR ${MKL_ROOT_DIR} CACHE PATH "Path to MKL directory")
 set(MKL_INCLUDE_DIRS ${MKL_INCLUDE_DIRS} CACHE PATH "Path to MKL include directory")
-if(NOT UNIX)
-    set(MKL_LIBRARIES ${MKL_LIBRARIES} CACHE FILEPATH "MKL libarries")
-else()
+set(MKL_LIBRARIES ${MKL_LIBRARIES} CACHE STRING "MKL libarries")
+if(UNIX AND NOT MKL_LIBRARIES_DONT_HACK)
     #it's ugly but helps to avoid cyclic lib problem
     set(MKL_LIBRARIES ${MKL_LIBRARIES} ${MKL_LIBRARIES} ${MKL_LIBRARIES} "-lpthread" "-lm" "-ldl")
-    set(MKL_LIBRARIES ${MKL_LIBRARIES} CACHE STRING "MKL libarries")
 endif()