From 0facfde4a69eb007a19e801c3e2ab57086fdc7db Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Wed, 25 Mar 2015 16:19:46 +0000
Subject: [PATCH] add feature download/build NETLIB cblas in Find

---
 cmake_modules/morse/find/FindBLAS.cmake  |   2 +-
 cmake_modules/morse/find/FindCBLAS.cmake | 116 ++++++++++++++++++++++-
 2 files changed, 112 insertions(+), 6 deletions(-)

diff --git a/cmake_modules/morse/find/FindBLAS.cmake b/cmake_modules/morse/find/FindBLAS.cmake
index b7f2207b4..6ea3f5e2e 100644
--- a/cmake_modules/morse/find/FindBLAS.cmake
+++ b/cmake_modules/morse/find/FindBLAS.cmake
@@ -401,7 +401,7 @@ if (BLAS_DOWNLOAD)
             message(STATUS "Untar blas.tgz")
         endif()
         execute_process(
-            COMMAND tar xf ${CMAKE_SOURCE_DIR}/externals/blas.tgz
+            COMMAND tar xvf ${CMAKE_SOURCE_DIR}/externals/blas.tgz
             WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/externals
         )
         if (EXISTS "${CMAKE_SOURCE_DIR}/externals/BLAS/")
diff --git a/cmake_modules/morse/find/FindCBLAS.cmake b/cmake_modules/morse/find/FindCBLAS.cmake
index d04995b2b..15994828f 100644
--- a/cmake_modules/morse/find/FindCBLAS.cmake
+++ b/cmake_modules/morse/find/FindCBLAS.cmake
@@ -48,6 +48,28 @@
 # look for a stand alone cblas, please add the following in your
 # CMakeLists.txt before to call find_package(CBLAS):
 # set(CBLAS_STANDALONE TRUE)
+###
+# We handle different modes to find the dependency
+#
+# - Install from a tarball downloaded on internet (we decide the distribution to ensure compatibility)
+#   - activate it with CBLAS_DOWNLOAD=ON
+#   - will download and build NETLIB CBLAS: http://www.netlib.org/blas/blast-forum/cblas.tgz
+#   - the resulting library is cblas_LINUX.a, see their Makafile.in !
+#
+# - Install from sources somewhere on the system: 
+#   - activate it with CBLAS_SOURCE_DIR=path/to/sources, note you manage your build configuration through the make.inc 
+#   - for now we handle the NETLIB cblas only, see http://www.netlib.org/blas/blast-forum/cblas.tgz, maybe more in the future
+#
+# - Detection if already installed on the system
+#   - CBLAS libraries can be detected from different ways
+#     Here is the order of precedence:
+#     1) we look in cmake variable CBLAS_LIBDIR or CBLAS_DIR (we guess the libdirs) if defined
+#     2) we look in environnement variable CBLAS_LIBDIR or CBLAS_DIR (we guess the libdirs) if defined
+#     3) we look in common environnment variables depending on the system (INCLUDE, C_INCLUDE_PATH, CPATH - LIB, DYLD_LIBRARY_PATH, LD_LIBRARY_PATH)
+#     4) we look in common system paths depending on the system, see for example paths contained in the following cmake variables:
+#       - CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES, CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
+#       - CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES, CMAKE_C_IMPLICIT_LINK_DIRECTORIES
+#
 
 #=============================================================================
 # Copyright 2012-2013 Inria
@@ -102,10 +124,94 @@ if (NOT BLAS_FOUND)
     endif()
 endif()
 
-
 # find CBLAS
 if (BLAS_FOUND)
 
+    if (CBLAS_DOWNLOAD)
+
+        # Download
+        if(NOT CBLAS_FIND_QUIETLY)
+            message(STATUS "Download: http://www.netlib.org/blas/blast-forum/cblas.tgz")
+        endif()
+        if (NOT EXISTS "${CMAKE_SOURCE_DIR}/externals/cblas.tgz")
+            file(DOWNLOAD http://www.netlib.org/blas/blast-forum/cblas.tgz
+                 ${CMAKE_SOURCE_DIR}/externals/cblas.tgz
+                 STATUS IS_GOT
+                 SHOW_PROGRESS
+                 TIMEOUT 30
+            )
+            if (EXISTS "${CMAKE_SOURCE_DIR}/externals/cblas.tgz")
+                if(NOT CBLAS_FIND_QUIETLY)
+                    message(STATUS "cblas.tgz downloaded: ${CMAKE_SOURCE_DIR}/externals/cblas.tgz")
+                endif()
+            else()
+                if(CBLAS_FIND_REQUIRED)
+                    message(FATAL_ERROR "http://www.netlib.org/blas/blast-forum/cblas.tgz download has failed")
+                elseif(NOT CBLAS_FIND_QUIETLY)
+                    message(STATUS "http://www.netlib.org/blas/blast-forum/cblas.tgz download has failed")
+                endif()
+            endif()
+        endif()
+
+        # Untar
+        if (NOT EXISTS "${CMAKE_SOURCE_DIR}/externals/CBLAS/")
+            if(NOT CBLAS_FIND_QUIETLY)
+                message(STATUS "Untar cblas.tgz")
+            endif()
+            execute_process(
+                COMMAND tar xvf ${CMAKE_SOURCE_DIR}/externals/cblas.tgz
+                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/externals
+            )
+            if (EXISTS "${CMAKE_SOURCE_DIR}/externals/CBLAS/")
+                if(NOT CBLAS_FIND_QUIETLY)
+                    message(STATUS "CBLAS untared: ${CMAKE_SOURCE_DIR}/externals/CBLAS/")
+                endif()
+            else()
+                if(CBLAS_FIND_REQUIRED)
+                    message(FATAL_ERROR "CBLAS untar has failed")
+                elseif(NOT CBLAS_FIND_QUIETLY)
+                    message(STATUS "CBLAS untar has failed")
+                endif()
+            endif()
+        endif()
+
+        # Build
+        if(NOT CBLAS_FIND_QUIETLY)
+            message(STATUS "Build NETLIB cblas")
+        endif()
+        execute_process(
+            COMMAND make alllib
+            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/externals/CBLAS
+        )
+
+        set(CBLAS_DIR "${CMAKE_SOURCE_DIR}/externals/CBLAS" CACHE PATH "Installation directory of CBLAS library" FORCE)
+
+    elseif(CBLAS_SOURCE_DIR)
+
+        # Build
+        if(NOT CBLAS_FIND_QUIETLY)
+            message(STATUS "Build NETLIB cblas from sources: ${CBLAS_SOURCE_DIR}")
+        endif()
+        execute_process(
+            COMMAND make alllib
+            WORKING_DIRECTORY ${CBLAS_SOURCE_DIR}
+        )
+        if (NOT CBLAS_SOURCE_LIBNAME)
+            set(CBLAS_SOURCE_LIBNAME cblas_LINUX.a)
+            if(NOT CBLAS_FIND_QUIETLY)
+                message(STATUS "CBLAS_SOURCE_LIBNAME not defined, the default value is ${CBLAS_SOURCE_LIBNAME}")
+            endif()
+        else()
+            if(NOT CBLAS_FIND_QUIETLY)
+                message(STATUS "CBLAS_SOURCE_LIBNAME is defined, we check that ${CBLAS_SOURCE_LIBNAME} exists")
+            endif()
+        endif()
+
+        set(CBLAS_DIR "${CMAKE_SOURCE_DIR}/externals/CBLAS" CACHE PATH "Installation directory of CBLAS library" FORCE)
+
+    endif()
+
+    # Find on the system
     if (NOT CBLAS_STANDALONE)
         # check if a cblas function exists in the BLAS lib
         # this can be the case with libs such as MKL, ACML
@@ -248,19 +354,19 @@ if (BLAS_FOUND)
         if(CBLAS_LIBDIR)
             set(CBLAS_cblas_LIBRARY "CBLAS_cblas_LIBRARY-NOTFOUND")
             find_library(CBLAS_cblas_LIBRARY
-                NAMES cblas
+                NAMES cblas cblas_LINUX.a
                 HINTS ${CBLAS_LIBDIR})
         else()
             if(CBLAS_DIR)
                 set(CBLAS_cblas_LIBRARY "CBLAS_cblas_LIBRARY-NOTFOUND")
                 find_library(CBLAS_cblas_LIBRARY
-                    NAMES cblas
+                    NAMES cblas cblas_LINUX.a
                     HINTS ${CBLAS_DIR}
                     PATH_SUFFIXES lib lib32 lib64)
             else()
                 set(CBLAS_cblas_LIBRARY "CBLAS_cblas_LIBRARY-NOTFOUND")
                 find_library(CBLAS_cblas_LIBRARY
-                    NAMES cblas
+                    NAMES cblas cblas_LINUX.a
                     HINTS ${_lib_env})
             endif()
         endif()
@@ -314,7 +420,7 @@ if (BLAS_FOUND)
             set(CMAKE_REQUIRED_LIBRARIES)
             list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LDFLAGS}")
             foreach(lib_dir ${REQUIRED_LIBDIRS})
-                list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
+                #list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
             endforeach()
             list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
             string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
-- 
GitLab