Merge branch release-2020 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 by the GROMACS development team.
5 # Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
6 # Copyright (c) 2019,2020, by the GROMACS development team, led by
7 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 # and including many others, as listed in the AUTHORS file in the
9 # top-level source directory and at http://www.gromacs.org.
10 #
11 # GROMACS is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU Lesser General Public License
13 # as published by the Free Software Foundation; either version 2.1
14 # of the License, or (at your option) any later version.
15 #
16 # GROMACS is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 # Lesser General Public License for more details.
20 #
21 # You should have received a copy of the GNU Lesser General Public
22 # License along with GROMACS; if not, see
23 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25 #
26 # If you want to redistribute modifications to GROMACS, please
27 # consider that scientific software is very special. Version
28 # control is crucial - bugs must be traceable. We will be happy to
29 # consider code for inclusion in the official distribution, but
30 # derived work must not be called official GROMACS. Details are found
31 # in the README & COPYING files - if they are missing, get the
32 # official version at http://www.gromacs.org.
33 #
34 # To help us fund GROMACS development, we humbly ask that you cite
35 # the research papers on the package. Check out http://www.gromacs.org.
36
37 # Test C flags FLAGS, and set VARIABLE to true if the work. Also add the
38 # flags to CFLAGSVAR.
39 MACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
40     IF(NOT DEFINED ${VARIABLE})
41         CHECK_C_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
42     ENDIF()
43     IF (${VARIABLE})
44         list(APPEND ${CFLAGSVAR} "${FLAGS}")
45     ENDIF ()
46 ENDMACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
47
48 # Test C++ flags FLAGS, and set VARIABLE to true if the work. Also add the
49 # flags to CXXFLAGSVAR.
50 MACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
51     IF(NOT DEFINED ${VARIABLE})
52         CHECK_CXX_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
53     ENDIF()
54     IF (${VARIABLE})
55         list(APPEND ${CXXFLAGSVAR} "${FLAGS}")
56     ENDIF ()
57 ENDMACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
58
59 # Prepare some local variables so CUDA and non-CUDA code in targets
60 # works the same way.
61 function(gmx_target_compile_options_inner)
62     set (CFLAGS "${SIMD_C_FLAGS};${MPI_COMPILE_FLAGS};${EXTRA_C_FLAGS};${GMXC_CFLAGS}" PARENT_SCOPE)
63     set (CXXFLAGS "${SIMD_CXX_FLAGS};${MPI_COMPILE_FLAGS};${EXTRA_CXX_FLAGS};${GMXC_CXXFLAGS}" PARENT_SCOPE)
64 endfunction()
65
66 # Implementation function to add compiler flags expected for all
67 # GROMACS build configurations, and those expected for the current
68 # CMake build type (e.g. Release) to TARGET. Other GROMACS CMake code
69 # is expected to use either gmx_target_compile_options(name_of_target)
70 # or gmx_cuda_target_compile_options(name_of_variable) because CUDA
71 # compilation has special requirements.
72 #
73 # Most targets (ie. libraries, executables) need compiler flags that
74 # are characteristic of the build configuration. This function
75 # provides a central point for adding such flags. This approach is
76 # more flexible than e.g. setting CMAKE_CXX_FLAGS globally, because
77 # that setting will apply globally, which means it applies also to
78 # "external" code that the build of GROMACS might also build.
79 function(gmx_target_compile_options TARGET)
80     if (GMX_SKIP_DEFAULT_CFLAGS)
81         return()
82     endif()
83
84     # Prepare the generic compiler options
85     gmx_target_compile_options_inner()
86     target_compile_options(${TARGET}
87         PRIVATE
88         $<$<COMPILE_LANGUAGE:C>:${CFLAGS}>
89         $<$<COMPILE_LANGUAGE:CXX>:${CXXFLAGS}>
90         )
91     # Add compiler options for the build types
92     foreach(build_type ${build_types_with_explicit_flags})
93         target_compile_options(${TARGET}
94             BEFORE PRIVATE
95             $<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:${build_type}>>:${GMXC_CFLAGS_${build_type}}>
96             $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:${build_type}>>:${GMXC_CXXFLAGS_${build_type}}>
97             )
98     endforeach()
99     # Add the release-configuration compiler options to build
100     # configurations that derive from it.
101     foreach(build_type RELWITHDEBINFO RELWITHASSERT MINSIZEREL PROFILE)
102         target_compile_options(${TARGET}
103             BEFORE PRIVATE
104             $<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:${build_type}>>:${GMXC_CFLAGS_RELEASE}>
105             $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:${build_type}>>:${GMXC_CXXFLAGS_RELEASE}>
106             )
107     endforeach()
108     # Add those flags that we only want in the proper release build
109     # configuration.
110     target_compile_options(${TARGET}
111         BEFORE PRIVATE
112         $<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:RELEASE>>:${GMXC_CFLAGS_RELEASE_ONLY}>
113         $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>:${GMXC_CXXFLAGS_RELEASE_ONLY}>
114         )
115 endfunction()
116
117 # The approach taken by FindCUDA.cmake is to require that the compiler
118 # flags are known and present in a variable before creating the target
119 # for the library. (Those get embedded in files that are generated at
120 # the point of calling cuda_add_library, which does not create a
121 # target that one could later call target_compile_options upon.) So,
122 # this function instead returns appropriate content in
123 # ${VARIABLE_NAME}, along with other such variables that are
124 # specialized for the various build_types. Hopefully this will improve
125 # when we use native CUDA language support in our CMake.
126 function(gmx_cuda_target_compile_options VARIABLE_NAME)
127     if (GMX_SKIP_DEFAULT_CFLAGS)
128         set (CXXFLAGS "")
129     else()
130         gmx_target_compile_options_inner()
131     endif()
132
133     # Only C++ compilation is supported with CUDA code in GROMACS
134     set(${VARIABLE_NAME} ${CXXFLAGS} PARENT_SCOPE)
135
136     # Now organize the flags for different build
137     # configurations. First, the debug configuration.
138     set(${VARIABLE_NAME}_DEBUG "${GMXC_CXXFLAGS_DEBUG}" PARENT_SCOPE)
139     # Add those flags that we only want in the proper release build
140     # configuration.
141     set(${VARIABLE_NAME}_RELEASE "${GMXC_CXXFLAGS_RELEASE};${GMXC_CXXFLAGS_RELEASE_ONLY}" PARENT_SCOPE)
142     # Add the release flags to build configurations that derive from it
143     foreach(build_type RELWITHDEBINFO RELWITHASSERT MINSIZEREL PROFILE)
144         set(${VARIABLE_NAME}_${build_type} "${GMXC_CXXFLAGS_RELEASE};${GMXC_CXXFLAGS_${build_type}}" PARENT_SCOPE)
145     endforeach()
146 endfunction()
147
148 # Add the WARNING_FLAG to the compile options for TARGET if the
149 # compiler supports that flag. VARNAME is used to cache the result of
150 # the check whether the compiler supports that flag, as multiple
151 # targets may use the same suppression.
152 #
153 # This is generally used to suppress warnings judged not worth fixing
154 # in code external to, or generated by, GROMACS, or code that is
155 # more efficient to work around and later replace, rather than fix.
156 function(gmx_target_warning_suppression TARGET WARNING_FLAG VARNAME)
157     check_cxx_compiler_flag(${WARNING_FLAG} ${VARNAME})
158     if(${VARNAME})
159         target_compile_options(${TARGET} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${WARNING_FLAG}>)
160     endif()
161 endfunction()
162
163 # Add the WARNING_FLAG to the compile flags for SOURCE_FILE if the
164 # compiler supports that flag. VARNAME is used to cache the result of
165 # the check whether the compiler supports that flag, as multiple
166 # targets may use the same suppression.
167 #
168 # This is generally used to suppress warnings judged not worth fixing
169 # in code external to, or generated by, GROMACS, or code that is
170 # more efficient to work around and later replace, rather than fix.
171 function(gmx_source_file_warning_suppression SOURCE_FILE WARNING_FLAG VARNAME)
172     check_cxx_compiler_flag(${WARNING_FLAG} ${VARNAME})
173     if(${VARNAME})
174         set_source_files_properties(${SOURCE_FILE} PROPERTIES COMPILE_FLAGS ${WARNING_FLAG})
175     endif()
176 endfunction()
177
178 # This is the actual exported function to be called
179 macro (gmx_c_flags)
180
181     include(CheckCCompilerFlag)
182     include(CheckCXXCompilerFlag)
183
184     # gcc
185     if(CMAKE_COMPILER_IS_GNUCC)
186         #flags are added in reverse order and -Wno* need to appear after -Wall
187         if(NOT GMX_OPENMP)
188             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
189         endif()
190         if (GMX_COMPILER_WARNINGS)
191             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value;-Wunused-parameter" GMXC_CFLAGS)
192             GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wextra;-Wno-missing-field-initializers;-Wno-sign-compare;-Wpointer-arith" GMXC_CFLAGS)
193             GMX_TEST_CFLAG(CFLAGS_WARN_UNDEF "-Wundef" GMXC_CFLAGS)
194             GMX_TEST_CFLAG(CFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CFLAGS_RELEASE_ONLY)
195             if(CYGWIN)
196                 GMX_TEST_CFLAG(CFLAGS_WARN_SUBSCRIPT "-Wno-char-subscripts" GMXC_CFLAGS)
197             endif()
198             GMX_TEST_CFLAG(CFLAGS_STRINGOP_TRUNCATION "-Werror=stringop-truncation" GMXC_CFLAGS)
199         endif()
200         # new in gcc 4.5
201         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
202         GMX_TEST_CFLAG(CFLAGS_COPT "-funroll-all-loops"
203                        GMXC_CFLAGS_RELEASE)
204         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
205     endif()
206     # g++
207     if(CMAKE_COMPILER_IS_GNUCXX)
208         if(NOT GMX_OPENMP)
209             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
210         endif()
211         if (GMX_COMPILER_WARNINGS)
212             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
213             # Problematic with CUDA
214             # GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EFFCXX "-Wnon-virtual-dtor" GMXC_CXXFLAGS)
215             GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra;-Wno-missing-field-initializers;-Wpointer-arith;-Wmissing-declarations" GMXC_CXXFLAGS)
216             # CUDA versions prior to 7.5 come with a header (math_functions.h) which uses the _MSC_VER macro
217             # unconditionally, so we don't use -Wundef for earlier CUDA versions.
218             if(NOT(GMX_GPU AND CUDA_VERSION VERSION_LESS "7.5"))
219                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN_UNDEF "-Wundef" GMXC_CXXFLAGS)
220             endif()
221             GMX_TEST_CFLAG(CXXFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CXXFLAGS_RELEASE_ONLY)
222             GMX_TEST_CXXFLAG(CXXFLAGS_STRINGOP_TRUNCATION "-Wstringop-truncation" GMXC_CXXFLAGS)
223             if (CXXFLAGS_STRINGOP_TRUNCATION)
224                 set(CXXFLAGS_NO_STRINGOP_TRUNCATION "-Wno-stringop-truncation")
225             endif()
226         endif()
227         # new in gcc 4.5
228         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
229         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-funroll-all-loops"
230                          GMXC_CXXFLAGS_RELEASE)
231         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
232     endif()
233
234     # icc
235     if (CMAKE_C_COMPILER_ID MATCHES "Intel")
236         if (NOT WIN32)
237             if(NOT GMX_OPENMP)
238 # 3180: unrecognized OpenMP #pragma
239                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "-wd3180" GMXC_CFLAGS)
240             endif()
241             if (GMX_COMPILER_WARNINGS)
242 # -w3 enables a lot of useful diagnostics but we don't care about all. -wd disables some selectively.
243 # 177: function/variable ".." was declared but never referenced
244 # 280: selector expression is constant
245 # 411: class defines no constructor to initialize the following (incorrect for struct, initializer list works)
246 # 593: variable ".." was set but never used
247 # 981: operands are evaluated in unspecified order
248 #1418: external function definition with no prior declaration
249 #1419: external declaration in primary source file
250 #1572: floating-point equality and inequality comparisons are unreliable
251 #1599: declaration hides variable ".."
252 #2259: non-pointer conversion from ".." to ".." may lose significant bits
253 #2415: variable ".." of static storage duration was declared but never referenced
254 #2547: ".." was specified as both a system and non-system include directory
255 #2557: comparison between signed and unsigned operands
256 #3280: declaration hides member ".."
257 #11074: Inlining inhibited by limit max-size(/max-total-size)
258 #11076: To get full report use -opt-report=3 -opt-report-phase ipo (shown for previous remark)
259                 GMX_TEST_CFLAG(CFLAGS_WARN "-w3;-wd177;-wd280;-wd411;-wd593;-wd981;-wd1418;-wd1419;-wd1572;-wd1599;-wd2259;-wd2415;-wd2547;-wd2557;-wd3280;-wd11074;-wd11076" GMXC_CFLAGS)
260             endif()
261             GMX_TEST_CFLAG(CFLAGS_STDGNU "-std=gnu99" GMXC_CFLAGS)
262             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)
263             GMX_TEST_CFLAG(CFLAGS_DEBUG "-O0" GMXC_CFLAGS_DEBUG) #icc defaults to -O2 even with -g
264             # The "except" fp-model requires something other than the
265             # default "fast" model, so we test and use it with
266             # "precise".
267             GMX_TEST_CFLAG(CFLAGS_FP_MODEL_RELASSERT "-fp-model=except;-fp-model=precise" GMXC_CFLAGS_RELWITHASSERT)
268         else()
269             if(NOT GMX_OPENMP)
270                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "/wd3180" GMXC_CFLAGS)
271             endif()
272             if (GMX_COMPILER_WARNINGS)
273 #only on Windows
274 #161: unrecognized pragma
275 #1786 function was declared deprecated (is issued for stdlib function such as strncpy which have a _s version)
276 GMX_TEST_CFLAG(CFLAGS_WARN "/W3;/wd161;/wd177;/wd411;/wd593;/wd981;/wd1418;/wd1419;/wd1572;/wd1599;/wd1786;/wd2259;/wd2415;/wd2547;/wd2557;/wd3280" GMXC_CFLAGS)
277             endif()
278             GMX_TEST_CFLAG(CFLAGS_OPT "/Qip" GMXC_CFLAGS_RELEASE)
279         endif()
280     endif()
281
282     if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
283         if (NOT WIN32) 
284             if(NOT GMX_OPENMP)
285                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-wd3180" GMXC_CXXFLAGS)
286             endif()
287             if (GMX_COMPILER_WARNINGS)
288 #All but the following warnings are identical for the C-compiler (see above)
289 # 304: access control not specified
290 # 383: value copied to temporary, reference to temporary used
291 # 444: destructor for base class ".." is not virtual
292 # 869: was never referenced (false positives)
293 #2282: unrecognized GCC pragma
294                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-w3;-wd177;-wd280;-wd304;-wd383;-wd411;-wd444;-wd869;-wd981;-wd1418;-wd1572;-wd1599;-wd2259;-wd2547;-wd3280;-wd11074;-wd11076;-wd2282" GMXC_CXXFLAGS)
295             endif()
296             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)
297             GMX_TEST_CXXFLAG(CXXFLAGS_DEBUG "-O0" GMXC_CXXFLAGS_DEBUG)
298             # The "except" fp-model requires something other than the
299             # default "fast" model, so we test and use it with
300             # "precise".
301             GMX_TEST_CXXFLAG(CXXFLAGS_FP_MODEL_RELASSERT "-fp-model=except;-fp-model=precise" GMXC_CXXFLAGS_RELWITHASSERT)
302         else()
303             if(NOT GMX_OPENMP)
304                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "/wd3180" GMXC_CFLAGS)
305             endif()
306             if (GMX_COMPILER_WARNINGS)
307 #161: unrecognized pragma
308 #809: exception specification for virtual function X is incompatible with that of overridden function
309                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/W3;/wd161;/wd177;/wd280;/wd304;/wd383;/wd411;/wd444;/wd809;/wd869;/wd981;/wd1418;/wd1572;/wd1599;/wd1786;/wd2259;/wd2547;/wd3280;/wd11074;/wd11076;/wd2282" GMXC_CXXFLAGS)
310             endif()
311             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "/Qip" GMXC_CXXFLAGS_RELEASE)
312         endif()
313     endif()
314
315     # PGI
316     # Inter-procedural analysis causes pgcc/pgc++ to crash when linking the library with PGI release 15.7.
317     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
318         GMX_TEST_CFLAG(CFLAGS_OPT "-Mnoipa" GMXC_CFLAGS_RELEASE)
319     endif()
320     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
321         # Using ipa exposes internal PGI-15.7 compiler bugs at compile time
322         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-Mnoipa" GMXC_CXXFLAGS_RELEASE)
323         # PGI identifies itself as GCC, but does not understand the GCC
324         # pragmas that occur in parser.cpp. Since that file is generated
325         # we cannot add a define there, but supress the warning instead.
326         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "--diag_suppress=1675" GMXC_CXXFLAGS)
327     endif()
328
329     # Pathscale
330     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
331         if(NOT GMX_OPENMP)
332             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
333         endif()
334         if (GMX_COMPILER_WARNINGS)
335             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value" GMXC_CFLAGS)
336         endif()
337         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast;-fno-math-errno;-ffast-math"
338                          GMXC_CFLAGS_RELEASE)
339         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
340     endif()
341     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
342         if(NOT GMX_OPENMP)
343             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
344         endif()
345         if (GMX_COMPILER_WARNINGS)
346             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value" GMXC_CXXFLAGS)
347         endif()
348         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast;-fno-math-errno;-ffast-math"
349                          GMXC_CXXFLAGS_RELEASE)
350     endif()
351
352     # xlc
353     # The suppressions below stop
354     # 1500-036: (I) about -O3 causing non-strict IEEE compliance that changes the semantics of the program (duh)
355     # 1500-010: (W) about correct PBC-related use of maximum array indices of DIM-sized C arrays
356     # 1500-030: (I) Additional optimization may be attained by recompiling and specifying MAXMEM option with a value greater than 8192.
357     if (CMAKE_C_COMPILER_ID MATCHES "XL")
358         GMX_TEST_CFLAG(CFLAGS_ARCH "-qarch=auto;-qtune=auto" GMXC_CFLAGS)
359         GMX_TEST_CFLAG(CFLAGS_OPT  "-O3" GMXC_CFLAGS_RELEASE)
360         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
361         GMX_TEST_CFLAG(CFLAGS_LANG "-qsuppress=1500-036;-qsuppress=1500-010;-qsuppress=1500-030" GMXC_CFLAGS)
362     endif()
363     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
364         GMX_TEST_CXXFLAG(CXXFLAGS_ARCH "-qarch=auto;-qtune=auto" GMXC_CXXFLAGS)
365         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-O3" GMXC_CXXFLAGS_RELEASE)
366         GMX_TEST_CXXFLAG(CXXFLAGS_LANG "-qsuppress=1500-036;-qsuppress=1500-010;-qsuppress=1500-030" GMXC_CXXFLAGS)
367     endif()
368
369     # msvc
370     if (MSVC)
371         # disable warnings for: 
372         #      forcing value to bool
373         #      "this" in initializer list
374         #      deprecated (posix, secure) functions
375         #      C4305: truncation (double -> float)
376         #      C4244: conversion from '.*' to '.*', possible loss of data
377         #      unreferenced local variable (only C)
378         #      C4267: conversion from 'size_t' to 'int', possible loss of data
379         #      conversion from 'const char*' to 'void*', different 'const' qualifiers (only C)
380         #      unknown pragma (4068)
381         #      remark #280: selector expression is constant
382         if(NOT CMAKE_CONFIGURATION_TYPES)
383             GMX_TEST_CFLAG(CFLAGS_WARN "/wd4800;/wd4355;/wd4996;/wd4305;/wd4244;/wd4101;/wd4267;/wd4090;/wd4068;" GMXC_CFLAGS)
384             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800;/wd4355;/wd4996;/wd4305;/wd4244;/wd4267;/wd4068;" GMXC_CXXFLAGS)
385         else() # MSVC projects only use the C++ flags
386             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800;/wd4355;/wd4996;/wd4305;/wd4244;/wd4101;/wd4267;/wd4090;/wd4068;" GMXC_CXXFLAGS)
387         endif()
388     endif()
389
390     if (CMAKE_C_COMPILER_ID MATCHES "Clang")
391         if(NOT GMX_OPENMP)
392             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
393         endif()
394         if (GMX_COMPILER_WARNINGS)
395             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value;-Wunused-parameter" GMXC_CFLAGS)
396             GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wpointer-arith" GMXC_CFLAGS_EXTRA)
397         endif()
398     endif()
399
400     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
401         if (GMX_COMPILER_WARNINGS)
402             # If used, -Wall should precede other options that silence warnings it enables
403             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
404             if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0") #LLVM BUG #21629
405                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN_NO_BRACES "-Wno-missing-braces" GMXC_CXXFLAGS)
406             endif()
407             GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra;-Wno-missing-field-initializers;-Wpointer-arith;-Wmissing-prototypes" GMXC_CXXFLAGS)
408             GMX_TEST_CXXFLAG(CXXFLAGS_DEPRECATED "-Wdeprecated" GMXC_CXXFLAGS)
409             # Functions placed in headers for inlining are not always
410             # used in every translation unit that includes the files,
411             # so we must disable the warning that there are such
412             # functions that are unused.
413             GMX_TEST_CXXFLAG(CXXFLAGS_NO_UNUSED_FUNCTION "-Wno-unused-function" GMXC_CXXFLAGS)
414         endif()
415         if(NOT GMX_OPENMP)
416             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
417         endif()
418     endif()
419
420     # Apple bastardized version of Clang
421     if(${CMAKE_C_COMPILER_ID} MATCHES "AppleClang")
422         if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER 11.0)
423             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
424             # that checks stack alignment by default, but their own C library
425             # does not align the stack properly. Embarrassing, Apple...
426             GMX_TEST_CFLAG(CFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CFLAGS)
427         endif()
428     endif()
429
430     if(${CMAKE_CXX_COMPILER_ID} MATCHES "AppleClang")
431         if(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 11.0)
432             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
433             # that checks stack alignment by default, but their own C library
434             # does not align the stack properly. Embarrassing, Apple...
435             GMX_TEST_CXXFLAG(CXXFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CXXFLAGS)
436         endif()
437     endif()
438
439     # Apple bastardized version of Clang
440     if(${CMAKE_C_COMPILER_ID} MATCHES "AppleClang")
441         if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER 11.0)
442             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
443             # that checks stack alignment by default, but their own C library
444             # does not align the stack properly. Embarrassing, Apple...
445             GMX_TEST_CFLAG(CFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CFLAGS)
446         endif()
447     endif()
448
449     if(${CMAKE_CXX_COMPILER_ID} MATCHES "AppleClang")
450         if(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 11.0)
451             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
452             # that checks stack alignment by default, but their own C library
453             # does not align the stack properly. Embarrassing, Apple...
454             GMX_TEST_CXXFLAG(CXXFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CXXFLAGS)
455         endif()
456     endif()
457
458     # Fujitsu compilers on PrimeHPC/Sparc64
459     if(${CMAKE_C_COMPILER_ID} MATCHES Fujitsu OR
460        (${CMAKE_C_COMPILER_ID} MATCHES unknown AND ${CMAKE_C_COMPILER} MATCHES ^fcc))
461         GMX_TEST_CFLAG(CFLAG_GNUCOMPAT "-Xg;-w" GMXC_CFLAGS)
462         GMX_TEST_CFLAG(CFLAG_OPT "-Kfast,reduction,swp,simd=2,uxsimd,fsimple;-x100" GMXC_CFLAGS)
463     endif()
464
465     if(${CMAKE_CXX_COMPILER_ID} MATCHES Fujitsu OR
466        (${CMAKE_CXX_COMPILER_ID} MATCHES unknown AND ${CMAKE_CXX_COMPILER} MATCHES ^FCC))
467         GMX_TEST_CXXFLAG(CXXFLAG_GNUCOMPAT "-Xg;-w" GMXC_CXXFLAGS)
468         GMX_TEST_CXXFLAG(CXXFLAG_OPT "-Kfast,reduction,swp,simd=2,uxsimd,fsimple;-x100" GMXC_CXXFLAGS)
469     endif()
470
471 endmacro()