Merge "Merge release-4-5-patches into release-4-6" into release-4-6
[gromacs.git] / cmake / FindOpenMP.cmake
blob24231bafe50e149a9aefa1e4e79cc5b1408481c5
1 # - Finds OpenMP support
2 # Copied from cmake 2.8.7 prior versions were assuming C++
3 # compiler is enabled. Can be removed when 2.8.7 becomes 
4 # required or C++ becomes required.
6 # This module can be used to detect OpenMP support in a compiler.
7 # If the compiler supports OpenMP, the flags required to compile with
8 # openmp support are set.
10 # The following variables are set:
11 #   OpenMP_C_FLAGS - flags to add to the C compiler for OpenMP support
12 #   OpenMP_CXX_FLAGS - flags to add to the CXX compiler for OpenMP support
13 #   OPENMP_FOUND - true if openmp is detected
15 # Supported compilers can be found at http://openmp.org/wp/openmp-compilers/
17 #=============================================================================
18 # Copyright 2009 Kitware, Inc.
19 # Copyright 2008-2009 AndrĂ© Rigland Brodtkorb <Andre.Brodtkorb@ifi.uio.no>
20 # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
22 # Distributed under the OSI-approved BSD License (the "License");
23 # see accompanying file Copyright.txt for details.
25 # This software is distributed WITHOUT ANY WARRANTY; without even the
26 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 # See the License for more information.
28 #=============================================================================
29 # (To distribute this file outside of CMake, substitute the full
30 #  License text for the above reference.)
32 set(_OPENMP_REQUIRED_VARS)
34 function(_OPENMP_FLAG_CANDIDATES LANG)
35   set(OpenMP_FLAG_CANDIDATES
36     #GNU
37     "-fopenmp"
38     #Microsoft Visual Studio
39     "/openmp"
40     #Intel windows
41     "-Qopenmp"
42     #PathScale, Intel
43     "-openmp"
44     #Empty, if compiler automatically accepts openmp
45     " "
46     #Sun
47     "-xopenmp"
48     #HP
49     "+Oopenmp"
50     #IBM XL C/c++
51     "-qsmp"
52     #Portland Group, MIPSpro
53     "-mp"
54   )
56   set(OMP_FLAG_GNU "-fopenmp")
57   set(OMP_FLAG_HP "+Oopenmp")
58   if(WIN32)
59     set(OMP_FLAG_Intel "-Qopenmp")
60   else()
61     set(OMP_FLAG_Intel "-openmp")
62   endif()
63   set(OMP_FLAG_MIPSpro "-mp")
64   set(OMP_FLAG_MSVC "/openmp")
65   set(OMP_FLAG_PathScale "-openmp")
66   set(OMP_FLAG_PGI "-mp")
67   set(OMP_FLAG_SunPro "-xopenmp")
68   set(OMP_FLAG_XL "-qsmp")
70   # Move the flag that matches the compiler to the head of the list,
71   # this is faster and doesn't clutter the output that much. If that
72   # flag doesn't work we will still try all.
73   if(OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID})
74     list(REMOVE_ITEM OpenMP_FLAG_CANDIDATES "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
75     list(INSERT OpenMP_FLAG_CANDIDATES 0 "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
76   endif()
78   set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_FLAG_CANDIDATES}" PARENT_SCOPE)
79 endfunction(_OPENMP_FLAG_CANDIDATES)
81 # sample openmp source code to test
82 set(OpenMP_C_TEST_SOURCE 
84 #include <omp.h>
85 int main() { 
86 #ifdef _OPENMP
87   return 0; 
88 #else
89   breaks_on_purpose
90 #endif
94 # check c compiler
95 if(CMAKE_C_COMPILER_LOADED)
96   # if these are set then do not try to find them again,
97   # by avoiding any try_compiles for the flags
98   if(OpenMP_C_FLAGS)
99     unset(OpenMP_C_FLAG_CANDIDATES)
100   else()
101     _OPENMP_FLAG_CANDIDATES("C")
102     include(CheckCSourceCompiles)
103   endif()
105   foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
106     set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
107     set(CMAKE_REQUIRED_FLAGS "${FLAG}")
108     unset(OpenMP_FLAG_DETECTED CACHE)
109     message(STATUS "Try OpenMP C flag = [${FLAG}]")
110     check_c_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
111     set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
112     if(OpenMP_FLAG_DETECTED)
113       set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
114       break()
115     endif(OpenMP_FLAG_DETECTED)
116   endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
118   set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
119     CACHE STRING "C compiler flags for OpenMP parallization")
121   list(APPEND _OPENMP_REQUIRED_VARS OpenMP_C_FLAGS)
122   unset(OpenMP_C_FLAG_CANDIDATES)
123 endif()
125 # check cxx compiler
126 if(CMAKE_CXX_COMPILER_LOADED)
127   # if these are set then do not try to find them again,
128   # by avoiding any try_compiles for the flags
129   if(OpenMP_CXX_FLAGS)
130     unset(OpenMP_CXX_FLAG_CANDIDATES)
131   else()
132     _OPENMP_FLAG_CANDIDATES("CXX")
133     include(CheckCXXSourceCompiles)
135     # use the same source for CXX as C for now
136     set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
137   endif()
139   foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
140     set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
141     set(CMAKE_REQUIRED_FLAGS "${FLAG}")
142     unset(OpenMP_FLAG_DETECTED CACHE)
143     message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
144     check_cxx_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
145     set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
146     if(OpenMP_FLAG_DETECTED)
147       set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
148       break()
149     endif(OpenMP_FLAG_DETECTED)
150   endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
152   set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
153     CACHE STRING "C++ compiler flags for OpenMP parallization")
155   list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
156   unset(OpenMP_CXX_FLAG_CANDIDATES)
157   unset(OpenMP_CXX_TEST_SOURCE)
158 endif()
160 if(_OPENMP_REQUIRED_VARS)
161   include(FindPackageHandleStandardArgs)
163   find_package_handle_standard_args(OpenMP
164                                     REQUIRED_VARS ${_OPENMP_REQUIRED_VARS})
166   mark_as_advanced(${_OPENMP_REQUIRED_VARS})
168   unset(_OPENMP_REQUIRED_VARS)
169 else()
170   message(SEND_ERROR "FindOpenMP requires C or CXX language to be enabled")
171 endif()