Fixed compilation issue due to gcc4.8
[alexxy/gromacs.git] / cmake / gmxCFlags.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013, 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
36 # Test C flags FLAGS, and set VARIABLE to true if the work. Also add the
37 # flags to CFLAGSVAR.
38 MACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
39     IF(NOT DEFINED ${VARIABLE})
40         CHECK_C_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
41     ENDIF(NOT DEFINED ${VARIABLE})
42     IF (${VARIABLE})
43         SET (${CFLAGSVAR} "${FLAGS} ${${CFLAGSVAR}}")
44     ENDIF (${VARIABLE})
45 ENDMACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
46
47 # Test C++ flags FLAGS, and set VARIABLE to true if the work. Also add the
48 # flags to CXXFLAGSVAR.
49 MACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
50     IF(NOT DEFINED ${VARIABLE} AND CMAKE_CXX_COMPILER_LOADED)
51         CHECK_CXX_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
52     ENDIF(NOT DEFINED ${VARIABLE} AND CMAKE_CXX_COMPILER_LOADED)
53     IF (${VARIABLE})
54         SET (${CXXFLAGSVAR} "${FLAGS} ${${CXXFLAGSVAR}}")
55     ENDIF (${VARIABLE})
56 ENDMACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
57
58
59 # This is the actual exported function to be called 
60 MACRO(gmx_c_flags)
61
62     include(CheckCCompilerFlag)
63     include(CheckCXXCompilerFlag)
64
65     # gcc
66     if(CMAKE_COMPILER_IS_GNUCC)
67         #flags are added in reverse order and -Wno* need to appear after -Wall
68         if(NOT GMX_OPENMP)
69             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
70         endif()
71         if (C_COMPILER_VERSION VERSION_EQUAL 4.8 OR C_COMPILER_VERSION VERSION_GREATER 4.8)
72             # In gcc 4.8 the default settings for many warning types
73             # were changed. Fixing many of them in C is at best
74             # awkward, and some of the fixes might require ABI
75             # changes, which we prefer not to do in a bugfix
76             # branch. So we introduce these workarounds, and plan to
77             # addess problems in master branch.
78             set(extra_warn_flags_for_gcc_4_8 " -Wno-unused-parameter -Wno-array-bounds -Wno-maybe-uninitialized -Wno-strict-overflow")
79         endif()
80         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value${extra_warn_flags_for_gcc_4_8}" GMXC_CFLAGS)
81         GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wno-sign-compare" GMXC_CFLAGS)
82         # new in gcc 4.5
83         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
84         GMX_TEST_CFLAG(CFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
85                        GMXC_CFLAGS_RELEASE)
86         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
87     endif()
88     # g++
89     if(CMAKE_COMPILER_IS_GNUCXX)
90         if(NOT GMX_OPENMP)
91             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
92         endif()
93         if (CXX_COMPILER_VERSION VERSION_EQUAL 4.8 OR CXX_COMPILER_VERSION VERSION_GREATER 4.8)
94             # In gcc 4.8 the default settings for many warning types
95             # were changed. Fixing many of them in C is at best
96             # awkward, and some of the fixes might require ABI
97             # changes, which we prefer not to do in a bugfix
98             # branch. So we introduce these workarounds, and plan to
99             # addess problems in master branch.
100             set(extra_warn_flags_for_gxx_4_8 " -Wno-unused-parameter -Wno-array-bounds -Wno-maybe-uninitialized -Wno-strict-overflow")
101         endif()
102         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value${extra_warn_flags_for_gxx_4_8}" GMXC_CXXFLAGS)
103         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wno-sign-compare" GMXC_CXXFLAGS)
104       # new in gcc 4.5
105         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
106         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
107                          GMXC_CXXFLAGS_RELEASE)
108         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
109     endif()
110
111     # icc
112     if (CMAKE_C_COMPILER_ID MATCHES "Intel")
113         if (NOT WIN32) 
114             if(NOT GMX_OPENMP)
115                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
116             endif()
117             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall" GMXC_CFLAGS)
118             GMX_TEST_CFLAG(CFLAGS_STDGNU "-std=gnu99" GMXC_CFLAGS)
119             GMX_TEST_CFLAG(CFLAGS_OPT "-ip -funroll-all-loops" GMXC_CFLAGS_RELEASE)
120         else()
121             GMX_TEST_CFLAG(CFLAGS_WARN "/W2" GMXC_CFLAGS)
122             GMX_TEST_CFLAG(CFLAGS_X86 "/Qip" GMXC_CFLAGS_RELEASE)
123         endif()
124     endif()
125
126     if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
127         if (NOT WIN32) 
128             if(NOT GMX_OPENMP)
129                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
130             endif()
131             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
132             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-ip -funroll-all-loops" GMXC_CXXFLAGS_RELEASE)
133         else()
134             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/W2" GMXC_CXXFLAGS)
135             GMX_TEST_CXXFLAG(CXXFLAGS_X86 "/Qip" GMXC_CXXFLAGS_RELEASE)
136         endif()
137     endif()
138
139     # pgi
140     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
141         GMX_TEST_CFLAG(CFLAGS_OPT "-fastsse" GMXC_CFLAGS_RELEASE)
142     endif()
143     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
144         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-fastsse" GMXC_CXXFLAGS_RELEASE)
145     endif()
146
147     # Pathscale
148     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
149         if(NOT GMX_OPENMP)
150             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
151         endif()
152         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
153         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
154                          GMXC_CFLAGS_RELEASE)
155         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
156     endif()
157     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
158         if(NOT GMX_OPENMP)
159             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
160         endif()
161         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
162         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
163                          GMXC_CXXFLAGS_RELEASE)
164     endif()
165
166     # xlc
167     if (CMAKE_C_COMPILER_ID MATCHES "XL")
168         GMX_TEST_CFLAG(CFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CFLAGS)
169         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
170     endif()
171     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
172         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CXXFLAGS)
173     endif()
174
175     #msvc
176     if (MSVC)
177         # disable warnings for: 
178         #      inconsistent dll linkage
179         #      forcing value to bool (for C++)
180         GMX_TEST_CFLAG(CFLAGS_WARN "/wd4273" GMXC_CFLAGS)
181         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4273 /wd4800" GMXC_CXXFLAGS)
182     endif()
183
184     if (CMAKE_C_COMPILER_ID MATCHES "Clang")
185         if(NOT GMX_OPENMP)
186             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
187         endif()
188         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
189     endif()
190
191     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
192         if(NOT GMX_OPENMP)
193             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
194         endif()
195         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
196     endif()
197
198     # now actually set the flags:
199     # C
200     if ( NOT GMX_SKIP_DEFAULT_CFLAGS )
201         set(CMAKE_C_FLAGS "${GMXC_CFLAGS} ${CMAKE_C_FLAGS}")
202         set(CMAKE_C_FLAGS_RELEASE "${GMXC_CFLAGS_RELEASE} ${CMAKE_C_FLAGS_RELEASE}")
203         set(CMAKE_C_FLAGS_DEBUG "${GMXC_CFLAGS_DEBUG} ${CMAKE_C_FLAGS_DEBUG}")
204     endif()
205
206     # C++
207     if ( NOT GMX_SKIP_DEFAULT_CFLAGS)
208         set(CMAKE_CXX_FLAGS "${GMXC_CXXFLAGS} ${CMAKE_CXX_FLAGS}")
209         set(CMAKE_CXX_FLAGS_RELEASE 
210             "${GMXC_CXXFLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELEASE}")
211         set(CMAKE_CXX_FLAGS_DEBUG 
212             "${GMXC_CXXFLAGS_DEBUG} ${CMAKE_CXX_FLAGS_DEBUG}")
213     endif()
214 ENDMACRO(gmx_c_flags)
215