Browse Source

Merge topic 'FindOpenMP-add-Fortran-support'

d0678408 FindOpenMP: Add support for Fortran (#14656)
Brad King 11 years ago
parent
commit
c91a65f812
2 changed files with 51 additions and 0 deletions
  1. 4 0
      Help/release/dev/FindOpenMP-add-Fortran-support.rst
  2. 47 0
      Modules/FindOpenMP.cmake

+ 4 - 0
Help/release/dev/FindOpenMP-add-Fortran-support.rst

@@ -0,0 +1,4 @@
+FindOpenMP-add-Fortran-support
+------------------------------
+
+* The :module:`FindOpenMP` module learned to support Fortran.

+ 47 - 0
Modules/FindOpenMP.cmake

@@ -16,6 +16,7 @@
 #
 #    OpenMP_C_FLAGS - flags to add to the C compiler for OpenMP support
 #    OpenMP_CXX_FLAGS - flags to add to the CXX compiler for OpenMP support
+#    OpenMP_Fortran_FLAGS - flags to add to the Fortran compiler for OpenMP support
 #    OPENMP_FOUND - true if openmp is detected
 #
 #
@@ -27,6 +28,7 @@
 # Copyright 2009 Kitware, Inc.
 # Copyright 2008-2009 André Rigland Brodtkorb <[email protected]>
 # Copyright 2012 Rolf Eike Beer <[email protected]>
+# Copyright 2014 Nicolas Bock <[email protected]>
 #
 # Distributed under the OSI-approved BSD License (the "License");
 # see accompanying file Copyright.txt for details.
@@ -106,6 +108,17 @@ int main() {
 }
 ")
 
+# same in Fortran
+set(OpenMP_Fortran_TEST_SOURCE
+  "
+program test
+use omp_lib
+integer :: n
+n = omp_get_num_threads()
+end program test
+  "
+  )
+
 # check c compiler
 if(CMAKE_C_COMPILER_LOADED)
   # if these are set then do not try to find them again,
@@ -176,6 +189,40 @@ if(CMAKE_CXX_COMPILER_LOADED)
   unset(OpenMP_CXX_TEST_SOURCE)
 endif()
 
+# check Fortran compiler
+if(CMAKE_Fortran_COMPILER_LOADED)
+  # if these are set then do not try to find them again,
+  # by avoiding any try_compiles for the flags
+  if(OpenMP_Fortran_FLAGS)
+    unset(OpenMP_Fortran_FLAG_CANDIDATES)
+  else()
+    _OPENMP_FLAG_CANDIDATES("Fortran")
+    include(${CMAKE_CURRENT_LIST_DIR}/CheckFortranSourceCompiles.cmake)
+  endif()
+
+  foreach(FLAG IN LISTS OpenMP_Fortran_FLAG_CANDIDATES)
+    set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
+    set(CMAKE_REQUIRED_FLAGS "${FLAG}")
+    unset(OpenMP_FLAG_DETECTED CACHE)
+    if(NOT CMAKE_REQUIRED_QUIET)
+      message(STATUS "Try OpenMP Fortran flag = [${FLAG}]")
+    endif()
+    check_fortran_source_compiles("${OpenMP_Fortran_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
+    set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
+    if(OpenMP_FLAG_DETECTED)
+      set(OpenMP_Fortran_FLAGS_INTERNAL "${FLAG}")
+      break()
+    endif()
+  endforeach()
+
+  set(OpenMP_Fortran_FLAGS "${OpenMP_Fortran_FLAGS_INTERNAL}"
+    CACHE STRING "Fortran compiler flags for OpenMP parallization")
+
+  list(APPEND _OPENMP_REQUIRED_VARS OpenMP_Fortran_FLAGS)
+  unset(OpenMP_Fortran_FLAG_CANDIDATES)
+  unset(OpenMP_Fortran_TEST_SOURCE)
+endif()
+
 set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
 
 if(_OPENMP_REQUIRED_VARS)