From 54ac7b4b988fb1c079b45d54431346b7e6ac57e3 Mon Sep 17 00:00:00 2001
From: Markus Blatt <markus@dr-blatt.de>
Date: Thu, 13 Feb 2020 16:01:17 +0100
Subject: [PATCH] Get rid off fortran compiler usage for Lapack/Blas.

FindLAPACK and FindBLAS check for the name mangling with added
underline as a fallback, anyway. Hence there is no need for using
a Fortran compiler. With this commit we now simply add another check
for a LAPACK function to see whether it has an underline added and
adapt the existing macro in config.h accordingly.

Closes #184
---
 cmake/modules/CMakeLists.txt         |  1 -
 cmake/modules/DuneCommonMacros.cmake | 24 +++++-----
 cmake/modules/DuneMacros.cmake       | 28 ------------
 cmake/modules/LanguageSupport.cmake  | 66 ----------------------------
 config.h.cmake                       | 13 ++++--
 5 files changed, 22 insertions(+), 110 deletions(-)
 delete mode 100644 cmake/modules/LanguageSupport.cmake

diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt
index 429dadba1..35bc5377f 100644
--- a/cmake/modules/CMakeLists.txt
+++ b/cmake/modules/CMakeLists.txt
@@ -44,7 +44,6 @@ install(FILES
   FindTBB.cmake
   FindUMFPack.cmake
   Headercheck.cmake
-  LanguageSupport.cmake
   latexmkrc.cmake
   OverloadCompilerFlags.cmake
   UseInkscape.cmake
diff --git a/cmake/modules/DuneCommonMacros.cmake b/cmake/modules/DuneCommonMacros.cmake
index ce1925d6e..a29cb5210 100644
--- a/cmake/modules/DuneCommonMacros.cmake
+++ b/cmake/modules/DuneCommonMacros.cmake
@@ -9,18 +9,18 @@ dune_require_cxx_standard(MODULE "dune-common" VERSION 14)
 include(DuneStreams)
 dune_set_minimal_debug_level()
 
-if(Fortran_Works)
-  # search for lapack
-  find_package(LAPACK)
-  set(HAVE_LAPACK ${LAPACK_FOUND})
-  if(${HAVE_LAPACK})
-    dune_register_package_flags(LIBRARIES "${LAPACK_LIBRARIES}")
-  endif(${HAVE_LAPACK})
-  set(HAVE_BLAS ${BLAS_FOUND})
-else(Fortran_Works)
-  set(HAVE_LAPACK Off)
-  set(HAVE_BLAS Off)
-endif(Fortran_Works)
+# search for lapack
+find_package(LAPACK)
+set(HAVE_LAPACK ${LAPACK_FOUND})
+if(${HAVE_LAPACK})
+  dune_register_package_flags(LIBRARIES "${LAPACK_LIBRARIES}")
+  cmake_push_check_state()
+  set(CMAKE_REQUIRED_LIBRARIES ${LAPACK_LIBRARIES})
+  check_function_exists("dsyev_" LAPACK_NEEDS_UNDERLINE)
+  cmake_pop_check_state()
+endif(${HAVE_LAPACK})
+set(HAVE_BLAS ${BLAS_FOUND})
+
 set_package_properties("BLAS" PROPERTIES
   DESCRIPTION "fast linear algebra routines")
 set_package_properties("LAPACK" PROPERTIES
diff --git a/cmake/modules/DuneMacros.cmake b/cmake/modules/DuneMacros.cmake
index 1c9b0412f..d3250019e 100644
--- a/cmake/modules/DuneMacros.cmake
+++ b/cmake/modules/DuneMacros.cmake
@@ -646,16 +646,6 @@ macro(dune_project)
     message(FATAL_ERROR "Module name from dune.module does not match the name given in CMakeLists.txt.")
   endif()
 
-  # optional Fortran support
-  include(LanguageSupport)
-  workaround_9220(Fortran Fortran_Works)
-  if(Fortran_Works)
-    enable_language(Fortran OPTIONAL)
-    if(NOT CMAKE_Fortran_COMPILER)
-      set(Fortran_Works OFF)
-    endif()
-  endif()
-
   option(DUNE_USE_ONLY_STATIC_LIBS "If set to ON, we will force static linkage everywhere" OFF)
   if(DUNE_USE_ONLY_STATIC_LIBS)
     if(BUILD_SHARED_LIBS)
@@ -704,24 +694,6 @@ macro(dune_project)
   # Search for MPI and set the relevant variables.
   include(DuneMPI)
 
-  # Make calling fortran routines from C/C++ possible
-  if(Fortran_Works)
-    include(FortranCInterface)
-    FortranCInterface_VERIFY(CXX)
-    # Write FC.h header containing information about
-    # how to call fortran routined.
-    # It will be included in config.h
-    FortranCInterface_HEADER(FC.h MACRO_NAMESPACE "FC_")
-  else()
-    # Write empty FC.h header
-    # Make sure to only write this file once, otherwise every cmake run
-    # will trigger a full rebuild of the whole project.
-    unset(_FC_H CACHE)
-    find_file(_FC_H NAME FC.h PATHS "${CMAKE_BINARY_DIR}" NO_DEFAULT_PATH)
-    if(NOT _FC_H)
-      file(WRITE "${CMAKE_BINARY_DIR}/FC.h" "")
-    endif()
-  endif()
 
   # Create custom target for building the documentation
   # and provide macros for installing the docs and force
diff --git a/cmake/modules/LanguageSupport.cmake b/cmake/modules/LanguageSupport.cmake
deleted file mode 100644
index 273f47703..000000000
--- a/cmake/modules/LanguageSupport.cmake
+++ /dev/null
@@ -1,66 +0,0 @@
-# cmake/modules/language_support.cmake
-#
-# Temporary additional general language support is contained within this
-# file.
-
-# This additional function definition is needed to provide a workaround for
-# CMake bug 9220.
-
-# On debian testing (cmake 2.6.2), I get return code zero when calling
-# cmake the first time, but cmake crashes when running a second time
-# as follows:
-#
-#  -- The Fortran compiler identification is unknown
-#  CMake Error at /usr/share/cmake-2.6/Modules/CMakeFortranInformation.cmake:7 (GET_FILENAME_COMPONENT):
-#    get_filename_component called with incorrect number of arguments
-#  Call Stack (most recent call first):
-#    CMakeLists.txt:3 (enable_language)
-#
-# My workaround is to invoke cmake twice.  If both return codes are zero,
-# it is safe to invoke ENABLE_LANGUAGE(Fortran OPTIONAL)
-
-function(workaround_9220 language language_works)
-  #message("DEBUG: language = ${language}")
-  set(text
-    "project(test NONE)
-    cmake_minimum_required(VERSION 2.8.0)
-    set (CMAKE_Fortran_FLAGS \"${CMAKE_Fortran_FLAGS}\")
-    set (CMAKE_EXE_LINKER_FLAGS \"${CMAKE_EXE_LINKER_FLAGS}\")
-    enable_language(${language} OPTIONAL)
-  ")
-  file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/language_tests/${language})
-  file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/language_tests/${language})
-  file(WRITE ${CMAKE_BINARY_DIR}/language_tests/${language}/CMakeLists.txt
-    ${text})
-  execute_process(
-    COMMAND ${CMAKE_COMMAND} . -G "${CMAKE_GENERATOR}" -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
-    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/language_tests/${language}
-    RESULT_VARIABLE return_code
-    OUTPUT_QUIET
-    ERROR_QUIET
-    )
-
-  if(return_code EQUAL 0)
-    # Second run
-    execute_process (
-      COMMAND ${CMAKE_COMMAND} . -G "${CMAKE_GENERATOR}" -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
-      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/language_tests/${language}
-      RESULT_VARIABLE return_code
-      OUTPUT_QUIET
-      ERROR_QUIET
-      )
-    if(return_code EQUAL 0)
-      set(${language_works} ON PARENT_SCOPE)
-    else(return_code EQUAL 0)
-      set(${language_works} OFF PARENT_SCOPE)
-    endif(return_code EQUAL 0)
-  else(return_code EQUAL 0)
-    set(${language_works} OFF PARENT_SCOPE)
-  endif(return_code EQUAL 0)
-endfunction(workaround_9220)
-
-# Temporary tests of the above function.
-#workaround_9220(CXX CXX_language_works)
-#message("CXX_language_works = ${CXX_language_works}")
-#workaround_9220(CXXp CXXp_language_works)
-#message("CXXp_language_works = ${CXXp_language_works}")
diff --git a/config.h.cmake b/config.h.cmake
index 9054fc48c..91f86194b 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -204,9 +204,16 @@
 /* Define to 1 if PT-Scotch is available */
 #cmakedefine HAVE_PTSCOTCH 1
 
-/* Include always useful headers */
-#include "FC.h"
-#define FC_FUNC FC_GLOBAL_
+/* Used to call lapack functions */
+#cmakedefine LAPACK_NEEDS_UNDERLINE
+
+#ifdef LAPACK_NEEDS_UNDERLINE
+  #define LAPACK_MANGLE(name,NAME) name##_
+#else
+  #define LAPACK_MANGLE(name,NAME) name
+#endif
+
+#define FC_FUNC LAPACK_MANGLE
 
 /* end dune-common
    Everything below here will be overwritten
-- 
GitLab