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