From 64cf828fdcc5d647f59ee9bc1f4afeef6f3c6691 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkovsky@mmg.fjfi.cvut.cz>
Date: Wed, 15 Jan 2020 17:47:13 +0100
Subject: [PATCH] Fixed linking to a BLAS library on Centos systems

---
 src/Benchmarks/BLAS/CMakeLists.txt | 24 ++++++++++++++++++------
 src/Benchmarks/BLAS/blasWrappers.h |  6 ++++++
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/Benchmarks/BLAS/CMakeLists.txt b/src/Benchmarks/BLAS/CMakeLists.txt
index 3f040abc89..81d8375332 100644
--- a/src/Benchmarks/BLAS/CMakeLists.txt
+++ b/src/Benchmarks/BLAS/CMakeLists.txt
@@ -5,16 +5,28 @@ else()
     add_executable( tnl-benchmark-blas tnl-benchmark-blas.cpp )
 endif()
 
-find_library( CBLAS_LIBRARY NAMES cblas
-           PATHS /usr/lib
-                 /usr/lib64
-                 /usr/lib/x86_64-linux-gnu
-                 /usr/local/lib
-                 /usr/local/lib64 )
+find_library( CBLAS_LIBRARY NAMES cblas )
+
+# fallback for Centos 7.5 - libcblas.so does not exist, link to libtatlas.so or libsatlas.so
+# https://forums.centos.org/viewtopic.php?t=48543
+find_library( TATLAS_LIBRARY NAMES tatlas
+              PATH_SUFFIXES atlas )
+find_library( SATLAS_LIBRARY NAMES satlas
+              PATH_SUFFIXES atlas )
+
 if( CBLAS_LIBRARY )
    target_compile_definitions( tnl-benchmark-blas PUBLIC "-DHAVE_BLAS" )
    target_link_libraries( tnl-benchmark-blas ${CBLAS_LIBRARY} )
+elseif( TATLAS_LIBRARY )
+   target_compile_definitions( tnl-benchmark-blas PUBLIC "-DHAVE_BLAS" )
+   target_link_libraries( tnl-benchmark-blas ${TATLAS_LIBRARY} )
+elseif( SATLAS_LIBRARY )
+   target_compile_definitions( tnl-benchmark-blas PUBLIC "-DHAVE_BLAS" )
+   target_link_libraries( tnl-benchmark-blas ${SATLAS_LIBRARY} )
 else()
+   # FIXME: We require the CBLAS interface, but CMake's FindBLAS cannot detect that,
+   #        so this fails unless the BLAS implementation includes it in the same
+   #        shared library file as the Fortran implementation (e.g. OpenBLAS does that).
    find_package( BLAS )
    if( BLAS_FOUND )
       target_compile_definitions( tnl-benchmark-blas PUBLIC "-DHAVE_BLAS" )
diff --git a/src/Benchmarks/BLAS/blasWrappers.h b/src/Benchmarks/BLAS/blasWrappers.h
index d1e0edff11..ce30060bf3 100644
--- a/src/Benchmarks/BLAS/blasWrappers.h
+++ b/src/Benchmarks/BLAS/blasWrappers.h
@@ -2,7 +2,13 @@
 
 #ifdef HAVE_BLAS
 
+// HOTFIX: cblas.h from the atlas-devel package (version 3.10.1-12.el7) on CentOS 7
+// does not declare the functions as `extern "C"`, which breaks name mangling.
+// Note that nested `extern "C"` is valid and correct:
+// https://stackoverflow.com/questions/48099828/what-happens-if-you-nest-extern-c
+extern "C" {
 #include <cblas.h>
+}
 
 inline int blasIgamax( int n, const float *x, int incx )
 {
-- 
GitLab