Merge release-5-0 into master
[alexxy/gromacs.git] / cmake / gmxCFlags.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source 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 # Test C flags FLAGS, and set VARIABLE to true if the work. Also add the
36 # flags to CFLAGSVAR.
37 MACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
38     IF(NOT DEFINED ${VARIABLE})
39         CHECK_C_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
40     ENDIF()
41     IF (${VARIABLE})
42         SET (${CFLAGSVAR} "${FLAGS} ${${CFLAGSVAR}}")
43     ENDIF ()
44 ENDMACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
45
46 # Test C++ flags FLAGS, and set VARIABLE to true if the work. Also add the
47 # flags to CXXFLAGSVAR.
48 MACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
49     IF(NOT DEFINED ${VARIABLE})
50         CHECK_CXX_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
51     ENDIF()
52     IF (${VARIABLE})
53         SET (${CXXFLAGSVAR} "${FLAGS} ${${CXXFLAGSVAR}}")
54     ENDIF ()
55 ENDMACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
56
57 # Set the real CMake variables for compiler flags. This should be a function
58 # so we can have proper local variables while avoiding duplicating code.
59 function(gmx_set_cmake_compiler_flags)
60     foreach(language C CXX)
61         # Copy the flags for the release build type to the build types
62         # that are modified forms of it. Ideally, the list of build
63         # types that are modifications of the Release build type would
64         # be set up elsewhere and passed to this function, but it is
65         # inconvenient in CMake to pass more than one list, and such a
66         # list is only used here.
67         foreach(build_type RELWITHDEBINFO RELWITHASSERT MINSIZEREL PROFILE)
68             set(GMXC_${language}FLAGS_${build_type} "${GMXC_${language}FLAGS_RELEASE}")
69         endforeach()
70         # Copy the flags that are only used by the real Release build
71         # type. Currently unused, but we plan to use -Wno-array-bounds
72         # in Release to work around gcc-4.8 being a little too vocal
73         # about some perfectly good code, while using RelWithAssert
74         # (ie. without that suppression) in Jenkins.
75         set(GMXC_${language}FLAGS_RELEASE "${GMXC_${language}FLAGS_RELEASE} ${GMXC_${language}FLAGS_RELEASE_ONLY}")
76
77         # Modify the real CMake variables for compiler flags for all
78         # builds and language types, and also those common to all
79         # build types.
80         foreach(build_type "" ${build_types_with_explicit_flags})
81             if("${build_type}" STREQUAL "")
82                 set(punctuation "") # for general compiler flags (e.g.) CMAKE_CXX_FLAGS
83             else()
84                 set(punctuation "_") # for build-type-specific compiler flags (e.g.) CMAKE_CXX_FLAGS_RELEASE
85             endif()
86
87             # Append to the variables for the given build type for
88             # each language, in the parent scope. We add our new variables at the end, so
89             # compiler-specific choices are more likely to override default CMake choices.
90             # This is for instance useful for RelWithDebInfo builds, where we want to use the full
91             # set of our optimization flags detected in this file, rather than having -O2 override them.
92             set(CMAKE_${language}_FLAGS${punctuation}${build_type}
93                 "${CMAKE_${language}_FLAGS${punctuation}${build_type}} ${GMXC_${language}FLAGS${punctuation}${build_type}}"
94                 PARENT_SCOPE)
95         endforeach()
96     endforeach()
97 endfunction()
98
99 # This is the actual exported function to be called 
100 MACRO(gmx_c_flags)
101
102     include(CheckCCompilerFlag)
103     include(CheckCXXCompilerFlag)
104
105     # gcc
106     if(CMAKE_COMPILER_IS_GNUCC)
107         #flags are added in reverse order and -Wno* need to appear after -Wall
108         if(NOT GMX_OPENMP)
109             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
110         endif()
111         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value -Wunused-parameter" GMXC_CFLAGS)
112         GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wno-sign-compare -Wpointer-arith" GMXC_CFLAGS)
113         # Since 4.8 on by default. For previous version disabling is a no-op. Only disabling for Release because with assert
114         # the warnings are OK.
115         GMX_TEST_CFLAG(CFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CFLAGS_RELEASE_ONLY)
116         if(CYGWIN)
117             GMX_TEST_CFLAG(CFLAGS_WARN_SUBSCRIPT "-Wno-char-subscripts" GMXC_CFLAGS)
118         endif()
119         # new in gcc 4.5
120         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
121         GMX_TEST_CFLAG(CFLAGS_COPT "-funroll-all-loops"
122                        GMXC_CFLAGS_RELEASE)
123         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
124     endif()
125     # g++
126     if(CMAKE_COMPILER_IS_GNUCXX)
127         if(NOT GMX_OPENMP)
128             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
129         endif()
130         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused-function" GMXC_CXXFLAGS)
131         # Problematic with CUDA
132         # GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EFFCXX "-Wnon-virtual-dtor" GMXC_CXXFLAGS)
133         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wpointer-arith" GMXC_CXXFLAGS)
134         GMX_TEST_CFLAG(CXXFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CXXFLAGS_RELEASE_ONLY)
135         # new in gcc 4.5
136         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
137         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-funroll-all-loops"
138                          GMXC_CXXFLAGS_RELEASE)
139         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
140     endif()
141
142     # icc
143     if (CMAKE_C_COMPILER_ID MATCHES "Intel")
144         if (NOT WIN32) 
145             if(NOT GMX_OPENMP)
146                 if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 13.99.99)
147 # 3180: unrecognized OpenMP #pragma
148                     GMX_TEST_CFLAG(CFLAGS_PRAGMA "-wd3180" GMXC_CFLAGS)
149                 else()
150 # 161: unrecognized #pragma
151                     GMX_TEST_CFLAG(CFLAGS_PRAGMA "-wd161" GMXC_CFLAGS)
152                 endif()
153             endif()
154 # 177: function/variable ".." was declared but never referenced
155 # 193: zero used for undefined preprocessing identifier ".."
156 # 271: trailing comma is nonstandard
157 # 304: access control not specified ("public" by default)
158 # 383: value copied to temporary, reference to temporary used
159 # 424: extra ";" ignored
160 # 444: destructor for base class ".." is not virtual
161 # 522: function ".." redeclared "inline" after being called
162 # 593: variable ".." was set but never used
163 # 869: parameter ".." was never referenced
164 # 981: operands are evaluated in unspecified order
165 #1418: external function definition with no prior declaration
166 #1419: external declaration in primary source file
167 #1572: floating-point equality and inequality comparisons are unreliable
168 #1599: declaration hides variable ".."
169 #2259: non-pointer conversion from ".." to ".." may lose significant bits
170 #2415: variable ".." of static storage duration was declared but never referenced
171 #2547: ".." was specified as both a system and non-system include directory
172 #2557: comparison between signed and unsigned operands
173 #3280: declaration hides member ".."
174 #3346: dynamic exception specifications are deprecated
175 #11074: Inlining inhibited by limit max-size(/max-total-size)
176 #11076: To get full report use -opt-report=3 -opt-report-phase ipo (shown for previous remark)
177             GMX_TEST_CFLAG(CFLAGS_WARN "-w3 -wd177 -wd193 -wd271 -wd304 -wd383 -wd424 -wd444 -wd522 -wd593 -wd869 -wd981 -wd1418 -wd1419 -wd1572 -wd1599 -wd2259 -wd2415 -wd2547 -wd2557 -wd3280 -wd3346 -wd11074 -wd11076" GMXC_CFLAGS)
178             GMX_TEST_CFLAG(CFLAGS_STDGNU "-std=gnu99" GMXC_CFLAGS)
179             GMX_TEST_CFLAG(CFLAGS_OPT "-ip -funroll-all-loops -alias-const -ansi-alias" GMXC_CFLAGS_RELEASE)
180         else()
181             if(NOT GMX_OPENMP)
182                 if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 13.99.99)
183                     GMX_TEST_CFLAG(CFLAGS_PRAGMA "/wd3180" GMXC_CFLAGS)
184                 else()
185                     GMX_TEST_CFLAG(CFLAGS_PRAGMA "/wd161" GMXC_CFLAGS)
186                 endif()
187             endif()
188             GMX_TEST_CFLAG(CFLAGS_WARN "/W3 /wd177 /wd193 /wd271 /wd304 /wd383 /wd424 /wd444 /wd522 /wd593 /wd869 /wd981 /wd1418 /wd1419 /wd1572 /wd1599 /wd2259 /wd2415 /wd2547 /wd2557 /wd3280 /wd3346" GMXC_CFLAGS)
189             GMX_TEST_CFLAG(CFLAGS_OPT "/Qip" GMXC_CFLAGS_RELEASE)
190         endif()
191     endif()
192
193     if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
194         if (NOT WIN32) 
195             if(NOT GMX_OPENMP)
196                 if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.99.99)
197                     GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-wd3180" GMXC_CXXFLAGS)
198                 else()
199                     GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-wd161" GMXC_CXXFLAGS)
200                 endif()
201             endif()
202 #All but the following warnings are identical for the C-compiler (see above)
203 #1782: #pragma once is obsolete
204 #2282: unrecognized GCC pragma
205             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-w3 -wd177 -wd193 -wd271 -wd304 -wd383 -wd424 -wd444 -wd522 -wd593 -wd869 -wd981 -wd1418 -wd1419 -wd1572 -wd1599 -wd2259 -wd2415 -wd2547 -wd2557 -wd3280 -wd3346 -wd11074 -wd11076 -wd1782 -wd2282" GMXC_CXXFLAGS)
206             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-ip -funroll-all-loops -alias-const -ansi-alias" GMXC_CXXFLAGS_RELEASE)
207         else()
208             if(NOT GMX_OPENMP)
209                 if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.99.99)
210                     GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "/wd3180" GMXC_CFLAGS)
211                 else()
212                     GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "/wd161" GMXC_CXXFLAGS)
213                 endif()
214             endif()
215             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/W3 /wd177 /wd193 /wd271 /wd304 /wd383 /wd424 /wd444 /wd522 /wd593 /wd869 /wd981 /wd1418 /wd1419 /wd1572 /wd1599 /wd2259 /wd2415 /wd2547 /wd2557 /wd3280 /wd3346 /wd1782 /wd2282" GMXC_CXXFLAGS)
216             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "/Qip" GMXC_CXXFLAGS_RELEASE)
217         endif()
218     endif()
219
220     # pgi
221     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
222         GMX_TEST_CFLAG(CFLAGS_OPT "-fastsse" GMXC_CFLAGS_RELEASE)
223     endif()
224     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
225         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-fastsse" GMXC_CXXFLAGS_RELEASE)
226     endif()
227
228     # Pathscale
229     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
230         if(NOT GMX_OPENMP)
231             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
232         endif()
233         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
234         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
235                          GMXC_CFLAGS_RELEASE)
236         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
237     endif()
238     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
239         if(NOT GMX_OPENMP)
240             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
241         endif()
242         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
243         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
244                          GMXC_CXXFLAGS_RELEASE)
245     endif()
246
247     # xlc
248     # The suppressions below stop information messages about -O3
249     # causing non-strict IEEE compliance that changes the semantics of
250     # the program (duh; 1500-036), warnings about correct PBC-related use of
251     # maximum array indices of DIM-sized C arrays (1500-010).
252     if (CMAKE_C_COMPILER_ID MATCHES "XL")
253         GMX_TEST_CFLAG(CFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CFLAGS)
254         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
255         GMX_TEST_CFLAG(CFLAGS_LANG "-qsuppress=1500-036 -qsuppress=1500-010" GMXC_CFLAGS)
256     endif()
257     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
258         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CXXFLAGS)
259         GMX_TEST_CXXFLAG(CFLAGS_LANG "-qsuppress=1500-036 -qsuppress=1500-010" GMXC_CXXFLAGS)
260     endif()
261
262     # msvc
263     if (MSVC)
264         # disable warnings for: 
265         #      forcing value to bool
266         #      "this" in initializer list
267         #      deprecated (posix, secure) functions
268         #      truncation (double -> float)
269         #      conversion from 'double' to 'real', possible loss of data
270         #      unreferenced local variable (only C)
271         #      conversion from 'size_t' to 'int', possible loss of data
272         #      conversion from 'const char*' to 'void*', different 'const' qualifiers (only C)
273         if(NOT CMAKE_CONFIGURATION_TYPES)
274             GMX_TEST_CFLAG(CFLAGS_WARN "/wd4800 /wd4355 /wd4996 /wd4305 /wd4244 /wd4101 /wd4267 /wd4090" GMXC_CFLAGS)
275             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800 /wd4355 /wd4996 /wd4305 /wd4244 /wd4267" GMXC_CXXFLAGS)
276         else() #Projects only use the C++ flags
277             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800 /wd4355 /wd4996 /wd4305 /wd4244 /wd4101 /wd4267 /wd4090" GMXC_CXXFLAGS)
278         endif()
279     endif()
280
281     if (CMAKE_C_COMPILER_ID MATCHES "Clang")
282         if(NOT GMX_OPENMP)
283             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
284         endif()
285         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value -Wunused-parameter" GMXC_CFLAGS)
286         GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wpointer-arith" GMXC_CFLAGS_EXTRA)
287     endif()
288
289     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
290         if(NOT GMX_OPENMP)
291             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
292         endif()
293         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused-function" GMXC_CXXFLAGS)
294         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wpointer-arith" GMXC_CXXFLAGS)
295     endif()
296
297     # Fujitsu compilers on PrimeHPC/Sparc64
298     if(${CMAKE_C_COMPILER_ID} MATCHES Fujitsu OR
299        (${CMAKE_C_COMPILER_ID} MATCHES unknown AND ${CMAKE_C_COMPILER} MATCHES ^fcc))
300         GMX_TEST_CFLAG(CFLAG_GNUCOMPAT "-Xg -w" GMXC_CFLAGS)
301         GMX_TEST_CFLAG(CFLAG_OPT "-Kfast,reduction,swp,simd=2,uxsimd,fsimple -x100" GMXC_CFLAGS)
302     endif()
303
304     if(${CMAKE_CXX_COMPILER_ID} MATCHES Fujitsu OR
305        (${CMAKE_CXX_COMPILER_ID} MATCHES unknown AND ${CMAKE_CXX_COMPILER} MATCHES ^FCC))
306         GMX_TEST_CXXFLAG(CXXFLAG_GNUCOMPAT "-Xg -w" GMXC_CXXFLAGS)
307         GMX_TEST_CXXFLAG(CXXFLAG_OPT "-Kfast,reduction,swp,simd=2,uxsimd,fsimple -x100" GMXC_CXXFLAGS)
308     endif()
309
310     # now actually set the flags:
311     if (NOT GMX_SKIP_DEFAULT_CFLAGS)
312         gmx_set_cmake_compiler_flags()
313     endif()
314 ENDMACRO(gmx_c_flags)
315