From 1cd6ce8fef9711652ae35aa11a0c19cb375ed096 Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Thu, 12 Mar 2015 15:15:33 +0000
Subject: [PATCH] update Finds for MKL case: avoid using gomp, add some flags
 and libs (dl)

---
 CMakeLists.txt                               |  14 +-
 cmake_modules/morse/find/FindBLAS.cmake      | 167 +++++++++++++++----
 cmake_modules/morse/find/FindFFTW.cmake      |  11 +-
 cmake_modules/morse/find/FindLAPACK.cmake    |   1 +
 cmake_modules/morse/find/FindSCALAPACK.cmake |   1 +
 5 files changed, 153 insertions(+), 41 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 705387cbc..4fdf9e3eb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -294,9 +294,14 @@ if (MORSE_DISTRIB_DIR OR EXISTS "${CMAKE_SOURCE_DIR}/cmake_modules/")
         # CHAMELEON depends on CBLAS
         #---------------------------
         find_package(CBLAS COMPONENTS BLASEXT)
-        if(BLAS_FOUND AND BLAS_LIBRARY_DIRS)
-            # the RPATH to be used when installing
-            list(APPEND CMAKE_INSTALL_RPATH "${BLAS_LIBRARY_DIRS}")
+        if(BLAS_FOUND)
+            if (BLAS_LIBRARY_DIRS)
+                # the RPATH to be used when installing
+                list(APPEND CMAKE_INSTALL_RPATH "${BLAS_LIBRARY_DIRS}")
+            endif()
+            if(BLAS_LINKER_FLAGS)
+                list(APPEND CMAKE_EXE_LINKER_FLAGS "${BLAS_LINKER_FLAGS}")
+            endif()
         else()
             message(FATAL_ERROR "A BLAS library is required but has not been found")
         endif()
@@ -306,9 +311,6 @@ if (MORSE_DISTRIB_DIR OR EXISTS "${CMAKE_SOURCE_DIR}/cmake_modules/")
                 # the RPATH to be used when installing
                 list(APPEND CMAKE_INSTALL_RPATH "${CBLAS_LIBRARY_DIRS}")
             endif()
-            if(CBLAS_LINKER_FLAGS)
-                list(APPEND CMAKE_EXE_LINKER_FLAGS "${CBLAS_LINKER_FLAGS}")
-            endif()
         else()
             if(MORSE_VERBOSE_FIND_PACKAGE)
                 if(CBLAS_STANDALONE OR NOT CBLAS_WORKS)
diff --git a/cmake_modules/morse/find/FindBLAS.cmake b/cmake_modules/morse/find/FindBLAS.cmake
index c1ff6586c..cf315b7fa 100644
--- a/cmake_modules/morse/find/FindBLAS.cmake
+++ b/cmake_modules/morse/find/FindBLAS.cmake
@@ -20,6 +20,7 @@
 #    is found
 #  BLAS_LINKER_FLAGS - uncached list of required linker flags (excluding -l
 #    and -L).
+#  BLAS_COMPILER_FLAGS - uncached list of required compiler flags (including -I for mkl headers).
 #  BLAS_LIBRARIES - uncached list of libraries (using full path name) to
 #    link against to use BLAS
 #  BLAS95_LIBRARIES - uncached list of libraries (using full path name)
@@ -214,6 +215,7 @@ macro(Check_Fortran_Libraries LIBRARIES _prefix _name _flags _list _thread)
             endif()
         else()
             if (ENV_MKLROOT)
+                list(APPEND _libdir "${ENV_MKLROOT}/lib")
                 if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
                     list(APPEND _libdir "${ENV_MKLROOT}/lib64")
                     list(APPEND _libdir "${ENV_MKLROOT}/lib/intel64")
@@ -275,10 +277,12 @@ macro(Check_Fortran_Libraries LIBRARIES _prefix _name _flags _list _thread)
 
     if(_libraries_work)
         # Test this combination of libraries.
-        set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_thread})
+        set(CMAKE_REQUIRED_LIBRARIES "${_flags};${${LIBRARIES}};${_thread}")
+        set(CMAKE_REQUIRED_FLAGS "${BLAS_COMPILER_FLAGS}")
         if (BLAS_VERBOSE)
-            message("${Cyan}BLAS libs found. Try to compile symbol ${_name} with"
-                    "following libraries: ${CMAKE_REQUIRED_LIBRARIES}")
+            message("${Cyan}BLAS libs found for BLA_VENDOR ${BLA_VENDOR}."
+                    "Try to compile symbol ${_name} with following libraries:"
+                    "${CMAKE_REQUIRED_LIBRARIES}")
         endif ()
         if(NOT BLAS_FOUND)
             unset(${_prefix}${_combined_name}_WORKS CACHE)
@@ -323,9 +327,126 @@ endif ()
 #BLAS in intel mkl 10 library? (em64t 64bit)
 if (BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
 
+    # Looking for include
+    # -------------------
+
+    # Add system include paths to search include
+    # ------------------------------------------
+    unset(_inc_env)
+    set(ENV_MKLROOT "$ENV{MKLROOT}")
+    set(ENV_BLAS_DIR "$ENV{BLAS_DIR}")
+    set(ENV_BLAS_INCDIR "$ENV{BLAS_INCDIR}")
+    if(ENV_BLAS_INCDIR)
+        list(APPEND _inc_env "${ENV_BLAS_INCDIR}")
+    elseif(ENV_BLAS_DIR)
+        list(APPEND _inc_env "${ENV_BLAS_DIR}")
+        list(APPEND _inc_env "${ENV_BLAS_DIR}/include")
+    else()
+        if (ENV_MKLROOT)
+            list(APPEND _inc_env "${ENV_MKLROOT}/include")
+        endif()
+        # system variables
+        if(WIN32)
+            string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
+            list(APPEND _inc_env "${_path_env}")
+        else()
+            string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
+            list(APPEND _inc_env "${_path_env}")
+            string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
+            list(APPEND _inc_env "${_path_env}")
+            string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
+            list(APPEND _inc_env "${_path_env}")
+            string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
+            list(APPEND _inc_env "${_path_env}")
+        endif()
+    endif()
+    list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
+    list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
+    list(REMOVE_DUPLICATES _inc_env)
+
+    # set paths where to look for
+    set(PATH_TO_LOOK_FOR "${_inc_env}")
+
+    # Try to find the fftw header in the given paths
+    # -------------------------------------------------
+    # call cmake macro to find the header path
+    if(BLAS_INCDIR)
+        set(BLAS_mkl.h_DIRS "BLAS_mkl.h_DIRS-NOTFOUND")
+        find_path(BLAS_mkl.h_DIRS
+          NAMES mkl.h
+          HINTS ${BLAS_INCDIR})
+    else()
+        if(BLAS_DIR)
+            set(BLAS_mkl.h_DIRS "BLAS_mkl.h_DIRS-NOTFOUND")
+            find_path(BLAS_mkl.h_DIRS
+              NAMES mkl.h
+              HINTS ${BLAS_DIR}
+              PATH_SUFFIXES "include")
+        else()
+            set(BLAS_mkl.h_DIRS "BLAS_mkl.h_DIRS-NOTFOUND")
+            find_path(BLAS_mkl.h_DIRS
+                      NAMES mkl.h
+                      HINTS ${PATH_TO_LOOK_FOR})
+        endif()
+    endif()
+    mark_as_advanced(BLAS_mkl.h_DIRS)
+
+    # If found, add path to cmake variable
+    # ------------------------------------
+    if (BLAS_mkl.h_DIRS)
+        set(BLAS_INCLUDE_DIRS "${BLAS_mkl.h_DIRS}")
+    else ()
+        set(BLAS_INCLUDE_DIRS "BLAS_INCLUDE_DIRS-NOTFOUND")
+        if(NOT BLAS_FIND_QUIETLY)
+            message(STATUS "Looking for BLAS -- mkl.h not found")
+        endif()
+    endif()
+
+    # libiomp5
+    # --------
+    if (WIN32)
+        string(REPLACE ":" ";" _libdir "$ENV{LIB}")
+    elseif (APPLE)
+        string(REPLACE ":" ";" _libdir "$ENV{DYLD_LIBRARY_PATH}")
+    else ()
+        string(REPLACE ":" ";" _libdir "$ENV{LD_LIBRARY_PATH}")
+    endif ()
+    list(APPEND _libdir "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
+    list(APPEND _libdir "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
+    set(OMP_iomp5_LIBRARY "OMP_iomp5_LIBRARY-NOTFOUND")
+    find_library(OMP_iomp5_LIBRARY
+        NAMES iomp5
+        HINTS ${_libdir}
+      )
+    mark_as_advanced(OMP_iomp5_LIBRARY)
+    set(OMP_LIB "")
+    if (OMP_iomp5_LIBRARY)
+        set(OMP_LIB "${OMP_iomp5_LIBRARY}")
+    endif()
+
     if (NOT WIN32)
         set(LM "-lm")
+        set(BLAS_COMPILER_FLAGS "")
+        if (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND NOT BLA_VENDOR STREQUAL "Intel10_64lp_seq")
+            list(APPEND BLAS_COMPILER_FLAGS "-openmp")
+        endif()
+        if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
+            if (BLA_VENDOR STREQUAL "Intel10_32")
+                #list(APPEND BLAS_COMPILER_FLAGS "-m32")
+            else()
+                #list(APPEND BLAS_COMPILER_FLAGS "-m64")
+            endif()
+            if (NOT BLA_VENDOR STREQUAL "Intel10_64lp_seq")
+                list(APPEND OMP_LIB "-ldl")
+            endif()
+        endif()
+
+        set(additional_flags "")
+        if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
+            set(additional_flags "-Wl,--no-as-needed")
+        endif()
     endif ()
+
     if (_LANGUAGES_ MATCHES C OR _LANGUAGES_ MATCHES CXX)
         if(BLAS_FIND_QUIETLY OR NOT BLAS_FIND_REQUIRED)
             find_package(Threads)
@@ -335,11 +456,6 @@ if (BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
 
         set(BLAS_SEARCH_LIBS "")
 
-        set(additional_flags "")
-        if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
-            set(additional_flags "-Wl,--no-as-needed")
-        endif()
-
         if(BLA_F95)
 
             set(BLAS_mkl_SEARCH_SYMBOL SGEMM)
@@ -394,21 +510,15 @@ if (BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
                     list(APPEND BLAS_SEARCH_LIBS
                     "mkl_blas95 mkl_intel_lp64 mkl_intel_thread mkl_core guide")
                     # mkl >= 10.3
-                    get_filename_component(CMAKE_C_COMPILER_NAME ${CMAKE_C_COMPILER} NAME)
-                    if (CMAKE_C_COMPILER_NAME STREQUAL "gcc" OR
-                        CMAKE_C_COMPILER_NAME STREQUAL "cc")
-                        list(APPEND BLAS_SEARCH_LIBS
-                        "mkl_blas95_lp64 mkl_intel_lp64 mkl_gnu_thread mkl_core")
-                        set(OMP_LIB "-lgomp")
-                    else ()
-                        list(APPEND BLAS_SEARCH_LIBS
-                        "mkl_blas95_lp64 mkl_intel_lp64 mkl_intel_thread mkl_core")
-                        set(OMP_LIB "-liomp5")
-                    endif ()
+                    list(APPEND BLAS_SEARCH_LIBS
+                    "mkl_blas95_lp64 mkl_intel_lp64 mkl_intel_thread mkl_core")
                 endif ()
                 if (BLA_VENDOR STREQUAL "Intel10_64lp_seq" OR BLA_VENDOR STREQUAL "All")
                     list(APPEND BLAS_SEARCH_LIBS
                     "mkl_intel_lp64 mkl_sequential mkl_core")
+                    if (BLA_VENDOR STREQUAL "Intel10_64lp_seq")
+                        set(OMP_LIB "")
+                    endif()
                 endif ()
             endif (WIN32)
 
@@ -462,29 +572,20 @@ if (BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
                     "mkl_intel mkl_intel_thread mkl_core guide")
                 endif ()
                 if (BLA_VENDOR STREQUAL "Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
-
                     # old version
                     list(APPEND BLAS_SEARCH_LIBS
                     "mkl_intel_lp64 mkl_intel_thread mkl_core guide")
-
                     # mkl >= 10.3
-                    get_filename_component(CMAKE_C_COMPILER_NAME ${CMAKE_C_COMPILER} NAME)
-                    if (CMAKE_C_COMPILER_NAME STREQUAL "gcc" OR
-                        CMAKE_C_COMPILER_NAME STREQUAL "cc")
-                        list(APPEND BLAS_SEARCH_LIBS
-                        "mkl_intel_lp64 mkl_gnu_thread mkl_core")
-                        set(OMP_LIB "-lgomp")
-                    else ()
-                        list(APPEND BLAS_SEARCH_LIBS
-                        "mkl_intel_lp64 mkl_intel_thread mkl_core")
-                        set(OMP_LIB "-liomp5")
-                    endif ()
+                    list(APPEND BLAS_SEARCH_LIBS
+                    "mkl_intel_lp64 mkl_intel_thread mkl_core")
                 endif ()
                 if (BLA_VENDOR STREQUAL "Intel10_64lp_seq" OR BLA_VENDOR STREQUAL "All")
                     list(APPEND BLAS_SEARCH_LIBS
                     "mkl_intel_lp64 mkl_sequential mkl_core")
+                    if (BLA_VENDOR STREQUAL "Intel10_64lp_seq")
+                        set(OMP_LIB "")
+                    endif()
                 endif ()
-
                 #older vesions of intel mkl libs
                 if (BLA_VENDOR STREQUAL "Intel" OR BLA_VENDOR STREQUAL "All")
                     list(APPEND BLAS_SEARCH_LIBS
diff --git a/cmake_modules/morse/find/FindFFTW.cmake b/cmake_modules/morse/find/FindFFTW.cmake
index bf096c03d..799f5c754 100644
--- a/cmake_modules/morse/find/FindFFTW.cmake
+++ b/cmake_modules/morse/find/FindFFTW.cmake
@@ -254,6 +254,7 @@ elseif(ENV_FFTW_DIR)
     endif()
 else()
     if (ENV_MKLROOT)
+        list(APPEND _lib_env "${ENV_MKLROOT}/lib")
         if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
             list(APPEND _lib_env "${ENV_MKLROOT}/lib64")
             list(APPEND _lib_env "${ENV_MKLROOT}/lib/intel64")
@@ -417,8 +418,14 @@ if(FFTW_LIBRARIES)
     endif()
     # OMP
     if(FFTW_LOOK_FOR_OMP)
-        if (CMAKE_C_COMPILER MATCHES ".+gcc.*")
-            set(REQUIRED_FLAGS "-fopenmp")
+        if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
+            # either gomp ...
+            #set(REQUIRED_FLAGS "-fopenmp")
+            #list(APPEND REQUIRED_LIBS "-lgomp")
+            # or iomp5
+            list(APPEND REQUIRED_LIBS "-liomp5")
+        elseif (CMAKE_C_COMPILER_ID STREQUAL "Intel")
+            list(APPEND REQUIRED_LIBS "-liomp5")
         endif()
     endif()
     # MKL
diff --git a/cmake_modules/morse/find/FindLAPACK.cmake b/cmake_modules/morse/find/FindLAPACK.cmake
index fce00b506..d8830a8d0 100644
--- a/cmake_modules/morse/find/FindLAPACK.cmake
+++ b/cmake_modules/morse/find/FindLAPACK.cmake
@@ -215,6 +215,7 @@ if (NOT _libdir)
       endif()
   else()
     if (ENV_MKLROOT)
+        list(APPEND _libdir "${ENV_MKLROOT}/lib")
         if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
             list(APPEND _libdir "${ENV_MKLROOT}/lib64")
             list(APPEND _libdir "${ENV_MKLROOT}/lib/intel64")
diff --git a/cmake_modules/morse/find/FindSCALAPACK.cmake b/cmake_modules/morse/find/FindSCALAPACK.cmake
index e5ab20fda..1f8425116 100644
--- a/cmake_modules/morse/find/FindSCALAPACK.cmake
+++ b/cmake_modules/morse/find/FindSCALAPACK.cmake
@@ -210,6 +210,7 @@ if (NOT _libdir)
       endif()
   else()
     if (ENV_MKLROOT)
+        list(APPEND _libdir "${ENV_MKLROOT}/lib")
         if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
             list(APPEND _libdir "${ENV_MKLROOT}/lib64")
             list(APPEND _libdir "${ENV_MKLROOT}/lib/intel64")
-- 
GitLab