Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / cmake / FindOpenMP.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012, by the GROMACS development team, led by
5 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 # others, as listed in the AUTHORS file in the top-level source
7 # directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34 #
35 # - Finds OpenMP support
36 # Copied from cmake 2.8.7 prior versions were assuming C++
37 # compiler is enabled. Can be removed when 2.8.7 becomes 
38 # required or C++ becomes required.
39 #
40 # This module can be used to detect OpenMP support in a compiler.
41 # If the compiler supports OpenMP, the flags required to compile with
42 # openmp support are set.
43 #
44 # The following variables are set:
45 #   OpenMP_C_FLAGS - flags to add to the C compiler for OpenMP support
46 #   OpenMP_CXX_FLAGS - flags to add to the CXX compiler for OpenMP support
47 #   OPENMP_FOUND - true if openmp is detected
48 #
49 # Supported compilers can be found at http://openmp.org/wp/openmp-compilers/
50
51 #=============================================================================
52 # Copyright 2009 Kitware, Inc.
53 # Copyright 2008-2009 AndrĂ© Rigland Brodtkorb <Andre.Brodtkorb@ifi.uio.no>
54 # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
55 #
56 # Distributed under the OSI-approved BSD License (the "License");
57 # see accompanying file Copyright.txt for details.
58 #
59 # This software is distributed WITHOUT ANY WARRANTY; without even the
60 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
61 # See the License for more information.
62 #=============================================================================
63 # (To distribute this file outside of CMake, substitute the full
64 #  License text for the above reference.)
65
66 set(_OPENMP_REQUIRED_VARS)
67
68 function(_OPENMP_FLAG_CANDIDATES LANG)
69   set(OpenMP_FLAG_CANDIDATES
70     #GNU
71     "-fopenmp"
72     #Microsoft Visual Studio
73     "/openmp"
74     #Intel windows
75     "-Qopenmp"
76     #PathScale, Intel
77     "-openmp"
78     #Empty, if compiler automatically accepts openmp
79     " "
80     #Sun
81     "-xopenmp"
82     #HP
83     "+Oopenmp"
84     #IBM XL C/c++
85     "-qsmp"
86     #Portland Group, MIPSpro
87     "-mp"
88   )
89
90   set(OMP_FLAG_GNU "-fopenmp")
91   set(OMP_FLAG_HP "+Oopenmp")
92   if(WIN32)
93     set(OMP_FLAG_Intel "-Qopenmp")
94   else()
95     set(OMP_FLAG_Intel "-openmp")
96   endif()
97   set(OMP_FLAG_MIPSpro "-mp")
98   set(OMP_FLAG_MSVC "/openmp")
99   set(OMP_FLAG_PathScale "-openmp")
100   set(OMP_FLAG_PGI "-mp")
101   set(OMP_FLAG_SunPro "-xopenmp")
102   set(OMP_FLAG_XL "-qsmp")
103
104   # Move the flag that matches the compiler to the head of the list,
105   # this is faster and doesn't clutter the output that much. If that
106   # flag doesn't work we will still try all.
107   if(OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID})
108     list(REMOVE_ITEM OpenMP_FLAG_CANDIDATES "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
109     list(INSERT OpenMP_FLAG_CANDIDATES 0 "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
110   endif()
111
112   set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_FLAG_CANDIDATES}" PARENT_SCOPE)
113 endfunction(_OPENMP_FLAG_CANDIDATES)
114
115 # sample openmp source code to test
116 set(OpenMP_C_TEST_SOURCE 
117 "
118 #include <omp.h>
119 int main() { 
120 #ifdef _OPENMP
121   return 0; 
122 #else
123   breaks_on_purpose
124 #endif
125 }
126 ")
127
128 # check c compiler
129 if(CMAKE_C_COMPILER_LOADED)
130   # if these are set then do not try to find them again,
131   # by avoiding any try_compiles for the flags
132   if(OpenMP_C_FLAGS)
133     unset(OpenMP_C_FLAG_CANDIDATES)
134   else()
135     _OPENMP_FLAG_CANDIDATES("C")
136     include(CheckCSourceCompiles)
137   endif()
138
139   foreach(FLAG ${OpenMP_C_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 C flag = [${FLAG}]")
144     check_c_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
145     set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
146     if(OpenMP_FLAG_DETECTED)
147       set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
148       break()
149     endif(OpenMP_FLAG_DETECTED)
150   endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
151
152   set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
153     CACHE STRING "C compiler flags for OpenMP parallization")
154
155   list(APPEND _OPENMP_REQUIRED_VARS OpenMP_C_FLAGS)
156   unset(OpenMP_C_FLAG_CANDIDATES)
157 endif()
158
159 # check cxx compiler
160 if(CMAKE_CXX_COMPILER_LOADED)
161   # if these are set then do not try to find them again,
162   # by avoiding any try_compiles for the flags
163   if(OpenMP_CXX_FLAGS)
164     unset(OpenMP_CXX_FLAG_CANDIDATES)
165   else()
166     _OPENMP_FLAG_CANDIDATES("CXX")
167     include(CheckCXXSourceCompiles)
168
169     # use the same source for CXX as C for now
170     set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
171   endif()
172
173   foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
174     set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
175     set(CMAKE_REQUIRED_FLAGS "${FLAG}")
176     unset(OpenMP_FLAG_DETECTED CACHE)
177     message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
178     check_cxx_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
179     set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
180     if(OpenMP_FLAG_DETECTED)
181       set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
182       break()
183     endif(OpenMP_FLAG_DETECTED)
184   endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
185
186   set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
187     CACHE STRING "C++ compiler flags for OpenMP parallization")
188
189   list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
190   unset(OpenMP_CXX_FLAG_CANDIDATES)
191   unset(OpenMP_CXX_TEST_SOURCE)
192 endif()
193
194 if(_OPENMP_REQUIRED_VARS)
195   include(FindPackageHandleStandardArgs)
196
197   find_package_handle_standard_args(OpenMP
198                                     REQUIRED_VARS ${_OPENMP_REQUIRED_VARS})
199
200   mark_as_advanced(${_OPENMP_REQUIRED_VARS})
201
202   unset(_OPENMP_REQUIRED_VARS)
203 else()
204   message(SEND_ERROR "FindOpenMP requires C or CXX language to be enabled")
205 endif()