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.
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
11 # Supported compilers can be found at http://openmp.org/wp/openmp-compilers/
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>
18 # Distributed under the OSI-approved BSD License (the "License");
19 # see accompanying file Copyright.txt for details.
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.)
28 set(_OPENMP_REQUIRED_VARS)
30 function(_OPENMP_FLAG_CANDIDATES LANG)
31 set(OpenMP_FLAG_CANDIDATES
34 #Microsoft Visual Studio
40 #Empty, if compiler automatically accepts openmp
48 #Portland Group, MIPSpro
52 set(OMP_FLAG_GNU "-fopenmp")
53 set(OMP_FLAG_HP "+Oopenmp")
55 set(OMP_FLAG_Intel "-Qopenmp")
57 set(OMP_FLAG_Intel "-openmp")
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")
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}}")
74 set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_FLAG_CANDIDATES}" PARENT_SCOPE)
77 # sample openmp source code to test
78 set(OpenMP_C_TEST_SOURCE
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
95 unset(OpenMP_C_FLAG_CANDIDATES)
97 _OPENMP_FLAG_CANDIDATES("C")
98 include(CheckCSourceCompiles)
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}")
114 set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
115 CACHE STRING "C compiler flags for OpenMP parallization")
117 list(APPEND _OPENMP_REQUIRED_VARS OpenMP_C_FLAGS)
118 unset(OpenMP_C_FLAG_CANDIDATES)
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
126 unset(OpenMP_CXX_FLAG_CANDIDATES)
128 _OPENMP_FLAG_CANDIDATES("CXX")
129 include(CheckCXXSourceCompiles)
131 # use the same source for CXX as C for now
132 set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
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}")
148 set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
149 CACHE STRING "C++ compiler flags for OpenMP parallization")
151 list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
152 unset(OpenMP_CXX_FLAG_CANDIDATES)
153 unset(OpenMP_CXX_TEST_SOURCE)
156 if(_OPENMP_REQUIRED_VARS)
157 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
159 find_package_handle_standard_args(OpenMP
160 REQUIRED_VARS ${_OPENMP_REQUIRED_VARS})
162 mark_as_advanced(${_OPENMP_REQUIRED_VARS})
164 unset(_OPENMP_REQUIRED_VARS)
166 message(SEND_ERROR "FindOpenMP requires C or CXX language to be enabled")