Require ICC 17.0.1
[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,2015,2016,2017,2018, 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} ${GMXC_${language}FLAGS_${build_type}}")
69         endforeach()
70         # Copy the flags that are only used by the real Release build
71         # type. Used for, e.g., -Wno-array-bounds in Release to work around
72         # gcc-4.8 being a little too vocal about some perfectly good code,
73         # while using RelWithAssert (ie. without that suppression) in Jenkins.
74         set(GMXC_${language}FLAGS_RELEASE "${GMXC_${language}FLAGS_RELEASE} ${GMXC_${language}FLAGS_RELEASE_ONLY}")
75
76         # Modify the real CMake variables for compiler flags for all
77         # builds and language types, and also those common to all
78         # build types.
79         foreach(build_type "" ${build_types_with_explicit_flags})
80             if("${build_type}" STREQUAL "")
81                 set(punctuation "") # for general compiler flags (e.g.) CMAKE_CXX_FLAGS
82             else()
83                 set(punctuation "_") # for build-type-specific compiler flags (e.g.) CMAKE_CXX_FLAGS_RELEASE
84             endif()
85
86             # Append to the variables for the given build type for
87             # each language, in the parent scope. We add our new variables at the end, so
88             # compiler-specific choices are more likely to override default CMake choices.
89             # This is for instance useful for RelWithDebInfo builds, where we want to use the full
90             # set of our optimization flags detected in this file, rather than having -O2 override them.
91             set(CMAKE_${language}_FLAGS${punctuation}${build_type}
92                 "${CMAKE_${language}_FLAGS${punctuation}${build_type}} ${GMXC_${language}FLAGS${punctuation}${build_type}}"
93                 PARENT_SCOPE)
94         endforeach()
95     endforeach()
96 endfunction()
97
98 # This is the actual exported function to be called
99 macro (gmx_c_flags)
100
101     include(CheckCCompilerFlag)
102     include(CheckCXXCompilerFlag)
103
104     # gcc
105     if(CMAKE_COMPILER_IS_GNUCC)
106         #flags are added in reverse order and -Wno* need to appear after -Wall
107         if(NOT GMX_OPENMP)
108             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
109         endif()
110         if (GMX_COMPILER_WARNINGS)
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             GMX_TEST_CFLAG(CFLAGS_WARN_UNDEF "-Wundef" GMXC_CFLAGS)
114             # Since 4.8 on by default. For previous version disabling is a no-op. Only disabling for Release because with assert
115             # the warnings are OK.
116             GMX_TEST_CFLAG(CFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CFLAGS_RELEASE_ONLY)
117             if(CYGWIN)
118                 GMX_TEST_CFLAG(CFLAGS_WARN_SUBSCRIPT "-Wno-char-subscripts" GMXC_CFLAGS)
119             endif()
120         endif()
121         # new in gcc 4.5
122         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
123         GMX_TEST_CFLAG(CFLAGS_COPT "-funroll-all-loops"
124                        GMXC_CFLAGS_RELEASE)
125         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
126     endif()
127     # g++
128     if(CMAKE_COMPILER_IS_GNUCXX)
129         if(NOT GMX_OPENMP)
130             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
131         endif()
132         if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.0.0")
133             GMX_TEST_CXXFLAG(CXXFLAGS_ATTRIBUTE "-Wno-attributes" GMXC_CXXFLAGS)
134         endif()
135         if (GMX_COMPILER_WARNINGS)
136             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
137             # Problematic with CUDA
138             # GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EFFCXX "-Wnon-virtual-dtor" GMXC_CXXFLAGS)
139             GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wpointer-arith -Wmissing-declarations" GMXC_CXXFLAGS)
140             # CUDA versions prior to 7.5 come with a header (math_functions.h) which uses the _MSC_VER macro
141             # unconditionally, so we don't use -Wundef for earlier CUDA versions.
142             if(NOT(GMX_GPU AND CUDA_VERSION VERSION_LESS "7.5"))
143                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN_UNDEF "-Wundef" GMXC_CXXFLAGS)
144             endif()
145             GMX_TEST_CFLAG(CXXFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CXXFLAGS_RELEASE_ONLY)
146         endif()
147         # new in gcc 4.5
148         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
149         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-funroll-all-loops"
150                          GMXC_CXXFLAGS_RELEASE)
151         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
152     endif()
153
154     # icc
155     if (CMAKE_C_COMPILER_ID MATCHES "Intel")
156         if (NOT WIN32)
157             if(NOT GMX_OPENMP)
158 # 3180: unrecognized OpenMP #pragma
159                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "-wd3180" GMXC_CFLAGS)
160             endif()
161             if (GMX_COMPILER_WARNINGS)
162 # -w3 enables a lot of useful diagnostics but we don't care about all. -wd disables some selectively.
163 # 177: function/variable ".." was declared but never referenced
164 # 411: class defines no constructor to initialize the following (incorrect for struct, initializer list works)
165 # 593: variable ".." was set but never used
166 # 981: operands are evaluated in unspecified order
167 #1418: external function definition with no prior declaration
168 #1419: external declaration in primary source file
169 #1572: floating-point equality and inequality comparisons are unreliable
170 #1599: declaration hides variable ".."
171 #2259: non-pointer conversion from ".." to ".." may lose significant bits
172 #2415: variable ".." of static storage duration was declared but never referenced
173 #2547: ".." was specified as both a system and non-system include directory
174 #2557: comparison between signed and unsigned operands
175 #3280: declaration hides member ".."
176 #11074: Inlining inhibited by limit max-size(/max-total-size)
177 #11076: To get full report use -opt-report=3 -opt-report-phase ipo (shown for previous remark)
178                 GMX_TEST_CFLAG(CFLAGS_WARN "-w3 -wd177 -wd411 -wd593 -wd981 -wd1418 -wd1419 -wd1572 -wd1599 -wd2259 -wd2415 -wd2547 -wd2557 -wd3280 -wd11074 -wd11076" GMXC_CFLAGS)
179             endif()
180             GMX_TEST_CFLAG(CFLAGS_STDGNU "-std=gnu99" GMXC_CFLAGS)
181             GMX_TEST_CFLAG(CFLAGS_OPT "-ip -funroll-all-loops -alias-const -ansi-alias -no-prec-div -fimf-domain-exclusion=14 -qoverride-limits" GMXC_CFLAGS_RELEASE)
182             GMX_TEST_CFLAG(CFLAGS_DEBUG "-O0" GMXC_CFLAGS_DEBUG) #icc defaults to -O2 even with -g
183             GMX_TEST_CFLAG(CFLAGS_FP_RELASSERT "-fp-model except -fp-model precise" GMXC_CFLAGS_RELWITHASSERT)
184         else()
185             if(NOT GMX_OPENMP)
186                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "/wd3180" GMXC_CFLAGS)
187             endif()
188             if (GMX_COMPILER_WARNINGS)
189 #only on Windows
190 #1786 function was declared deprecated (is issued for stdlib function such as strncpy which have a _s version)
191 GMX_TEST_CFLAG(CFLAGS_WARN "/W3 /wd177 /wd411 /wd593 /wd981 /wd1418 /wd1419 /wd1572 /wd1599 /wd1786 /wd2259 /wd2415 /wd2547 /wd2557 /wd3280" GMXC_CFLAGS)
192             endif()
193             GMX_TEST_CFLAG(CFLAGS_OPT "/Qip" GMXC_CFLAGS_RELEASE)
194         endif()
195     endif()
196
197     if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
198         if (NOT WIN32) 
199             if(NOT GMX_OPENMP)
200                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-wd3180" GMXC_CXXFLAGS)
201             endif()
202             GMX_TEST_CXXFLAG(CXXFLAGS_ATTRIBUTE "-Wno-attributes" GMXC_CXXFLAGS)
203             if (GMX_COMPILER_WARNINGS)
204                 if (GMX_GPU)
205 # Suppress warnings from CUDA headers
206 # 7:   unrecognized token
207 # 82:  storage class is not first
208 # The below are also required for math_functions.h / math_functions.hpp at least until CUDA 8.0-RC
209 # 193: zero used for undefined preprocessing identifer
210 # 3346:dynamic exception specifiers are deprecated
211                     GMX_TEST_CXXFLAG(CXXFLAGS_WARN_OLD_GPU "-wd7 -wd82 -wd193 -wd3346" GMXC_CXXFLAGS)
212                 endif()
213 #All but the following warnings are identical for the C-compiler (see above)
214 # 383: value copied to temporary, reference to temporary used
215 # 444: destructor for base class ".." is not virtual
216 #2282: unrecognized GCC pragma
217                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-w3 -wd177 -wd383 -wd411 -wd444 -wd981 -wd1418 -wd1572 -wd1599 -wd2259 -wd2547 -wd3280 -wd11074 -wd11076 -wd2282" GMXC_CXXFLAGS)
218             endif()
219             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-ip -funroll-all-loops -alias-const -ansi-alias -no-prec-div -fimf-domain-exclusion=14 -qoverride-limits" GMXC_CXXFLAGS_RELEASE)
220             GMX_TEST_CXXFLAG(CXXFLAGS_DEBUG "-O0" GMXC_CXXFLAGS_DEBUG)
221             GMX_TEST_CXXFLAG(CXXFLAGS_FP_RELASSERT "-fp-model except -fp-model precise" GMXC_CXXFLAGS_RELWITHASSERT)
222         else()
223             if(NOT GMX_OPENMP)
224                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "/wd3180" GMXC_CFLAGS)
225             endif()
226             if (GMX_COMPILER_WARNINGS)
227 #809: exception specification for virtual function X is incompatible with that of overridden function
228                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/W3 /wd177 /wd383 /wd411 /wd444 /wd809 /wd981 /wd1418 /wd1572 /wd1599 /wd1786 /wd2259 /wd2547 /wd3280 /wd11074 /wd11076 /wd2282" GMXC_CXXFLAGS)
229             endif()
230             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "/Qip" GMXC_CXXFLAGS_RELEASE)
231         endif()
232     endif()
233
234     # PGI
235     # Inter-procedural analysis causes pgcc/pgc++ to crash when linking the library with PGI release 15.7.
236     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
237         GMX_TEST_CFLAG(CFLAGS_OPT "-Mnoipa" GMXC_CFLAGS_RELEASE)
238     endif()
239     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
240         # Using ipa exposes internal PGI-15.7 compiler bugs at compile time
241         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-Mnoipa" GMXC_CXXFLAGS_RELEASE)
242         # PGI identifies itself as GCC, but does not understand the GCC
243         # pragmas that occur in parser.cpp. Since that file is generated
244         # we cannot add a define there, but supress the warning instead.
245         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "--diag_suppress=1675" GMXC_CXXFLAGS)
246     endif()
247
248     # Pathscale
249     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
250         if(NOT GMX_OPENMP)
251             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
252         endif()
253         if (GMX_COMPILER_WARNINGS)
254             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
255         endif()
256         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math"
257                          GMXC_CFLAGS_RELEASE)
258         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
259     endif()
260     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
261         if(NOT GMX_OPENMP)
262             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
263         endif()
264         if (GMX_COMPILER_WARNINGS)
265             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
266         endif()
267         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math"
268                          GMXC_CXXFLAGS_RELEASE)
269     endif()
270
271     # xlc
272     # The suppressions below stop
273     # 1500-036: (I) about -O3 causing non-strict IEEE compliance that changes the semantics of the program (duh)
274     # 1500-010: (W) about correct PBC-related use of maximum array indices of DIM-sized C arrays
275     # 1500-030: (I) Additional optimization may be attained by recompiling and specifying MAXMEM option with a value greater than 8192.
276     if (CMAKE_C_COMPILER_ID MATCHES "XL")
277         GMX_TEST_CFLAG(CFLAGS_ARCH "-qarch=auto -qtune=auto" GMXC_CFLAGS)
278         GMX_TEST_CFLAG(CFLAGS_OPT  "-O3" GMXC_CFLAGS_RELEASE)
279         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
280         GMX_TEST_CFLAG(CFLAGS_LANG "-qsuppress=1500-036 -qsuppress=1500-010 -qsuppress=1500-030" GMXC_CFLAGS)
281     endif()
282     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
283         GMX_TEST_CXXFLAG(CXXFLAGS_ARCH "-qarch=auto -qtune=auto" GMXC_CXXFLAGS)
284         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-O3" GMXC_CXXFLAGS_RELEASE)
285         GMX_TEST_CXXFLAG(CXXFLAGS_LANG "-qsuppress=1500-036 -qsuppress=1500-010 -qsuppress=1500-030" GMXC_CXXFLAGS)
286     endif()
287
288     # msvc
289     if (MSVC)
290         # disable warnings for: 
291         #      forcing value to bool
292         #      "this" in initializer list
293         #      deprecated (posix, secure) functions
294         #      truncation (double -> float)
295         #      conversion from 'double' to 'real', possible loss of data
296         #      unreferenced local variable (only C)
297         #      conversion from 'size_t' to 'int', possible loss of data
298         #      conversion from 'const char*' to 'void*', different 'const' qualifiers (only C)
299         #      attributes are ignored in this context
300         #      attribute '' is not recognized
301         if(NOT CMAKE_CONFIGURATION_TYPES)
302             GMX_TEST_CFLAG(CFLAGS_WARN "/wd4800 /wd4355 /wd4996 /wd4305 /wd4244 /wd4101 /wd4267 /wd4090" GMXC_CFLAGS)
303             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800 /wd4355 /wd4996 /wd4305 /wd4244 /wd4267 /wd5030 /wd4649" GMXC_CXXFLAGS)
304         else() #Projects only use the C++ flags
305             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800 /wd4355 /wd4996 /wd4305 /wd4244 /wd4101 /wd4267 /wd4090 /wd5030 /wd4649" GMXC_CXXFLAGS)
306         endif()
307     endif()
308
309     if (CMAKE_C_COMPILER_ID MATCHES "Clang")
310         if(NOT GMX_OPENMP)
311             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
312         endif()
313         if (GMX_COMPILER_WARNINGS)
314             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value -Wunused-parameter" GMXC_CFLAGS)
315             GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wpointer-arith" GMXC_CFLAGS_EXTRA)
316         endif()
317     endif()
318
319     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
320         if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.9.0")
321             GMX_TEST_CXXFLAG(CXXFLAGS_ATTRIBUTE "-Wno-attributes" GMXC_CXXFLAGS)
322         endif()
323         if(NOT GMX_OPENMP)
324             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
325         endif()
326         if (GMX_COMPILER_WARNINGS)
327             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
328             GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wpointer-arith -Wmissing-prototypes" GMXC_CXXFLAGS)
329             GMX_TEST_CXXFLAG(CXXFLAGS_DEPRECATED "-Wdeprecated" GMXC_CXXFLAGS)
330         endif()
331     endif()
332
333     # Fujitsu compilers on PrimeHPC/Sparc64
334     if(${CMAKE_C_COMPILER_ID} MATCHES Fujitsu OR
335        (${CMAKE_C_COMPILER_ID} MATCHES unknown AND ${CMAKE_C_COMPILER} MATCHES ^fcc))
336         GMX_TEST_CFLAG(CFLAG_GNUCOMPAT "-Xg -w" GMXC_CFLAGS)
337         GMX_TEST_CFLAG(CFLAG_OPT "-Kfast,reduction,swp,simd=2,uxsimd,fsimple -x100" GMXC_CFLAGS)
338     endif()
339
340     if(${CMAKE_CXX_COMPILER_ID} MATCHES Fujitsu OR
341        (${CMAKE_CXX_COMPILER_ID} MATCHES unknown AND ${CMAKE_CXX_COMPILER} MATCHES ^FCC))
342         GMX_TEST_CXXFLAG(CXXFLAG_GNUCOMPAT "-Xg -w" GMXC_CXXFLAGS)
343         GMX_TEST_CXXFLAG(CXXFLAG_OPT "-Kfast,reduction,swp,simd=2,uxsimd,fsimple -x100" GMXC_CXXFLAGS)
344     endif()
345
346     # now actually set the flags:
347     if (NOT GMX_SKIP_DEFAULT_CFLAGS)
348         gmx_set_cmake_compiler_flags()
349     endif()
350
351 endmacro()