597d425f04bd410f8b2b56272582733df62ac0ab
[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,2021, 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 include(CheckCCompilerFlag)
38 include(CheckCXXCompilerFlag)
39
40 # Test C flags FLAGS, and set VARIABLE to true if the work. Also add the
41 # flags to CFLAGSVAR.
42 MACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
43     IF(NOT DEFINED ${VARIABLE})
44         CHECK_C_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
45     ENDIF()
46     IF (${VARIABLE})
47         list(APPEND ${CFLAGSVAR} "${FLAGS}")
48     ENDIF ()
49 ENDMACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
50
51 # Test C++ flags FLAGS, and set VARIABLE to true if the work. Also add the
52 # flags to CXXFLAGSVAR.
53 MACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
54     IF(NOT DEFINED ${VARIABLE})
55         CHECK_CXX_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
56     ENDIF()
57     IF (${VARIABLE})
58         list(APPEND ${CXXFLAGSVAR} "${FLAGS}")
59     ENDIF ()
60 ENDMACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
61
62 # Prepare some local variables so CUDA and non-CUDA code in targets
63 # works the same way.
64 function(gmx_target_compile_options_inner)
65     set(CFLAGS
66             ${SIMD_C_FLAGS}
67             ${MPI_C_COMPILE_OPTIONS}
68             ${EXTRA_C_FLAGS}
69             ${GMXC_CFLAGS}
70          PARENT_SCOPE)
71
72     # When SYCL support has been enabled (so the flag is non-empty), we still *disable* things
73     # by default to avoid running each file three passes through the compiler. Then we'll explicitly
74     # enable SYCL for the few files using it, as well as the linker.
75     set(CXXFLAGS
76             ${SIMD_CXX_FLAGS}
77             ${MPI_CXX_COMPILE_OPTIONS}
78             ${DISABLE_SYCL_CXX_FLAGS}
79             ${EXTRA_CXX_FLAGS}
80             ${GMXC_CXXFLAGS}
81         PARENT_SCOPE)
82 endfunction()
83
84 # Implementation function to add compiler flags expected for all
85 # GROMACS build configurations, and those expected for the current
86 # CMake build type (e.g. Release) to TARGET. Other GROMACS CMake code
87 # is expected to use either gmx_target_compile_options(name_of_target)
88 # or gmx_cuda_target_compile_options(name_of_variable) because CUDA
89 # compilation has special requirements.
90 #
91 # Most targets (ie. libraries, executables) need compiler flags that
92 # are characteristic of the build configuration. This function
93 # provides a central point for adding such flags. This approach is
94 # more flexible than e.g. setting CMAKE_CXX_FLAGS globally, because
95 # that setting will apply globally, which means it applies also to
96 # "external" code that the build of GROMACS might also build.
97 function(gmx_target_compile_options TARGET)
98     if (GMX_SKIP_DEFAULT_CFLAGS)
99         return()
100     endif()
101
102     # Prepare the generic compiler options
103     gmx_target_compile_options_inner()
104     target_compile_options(${TARGET}
105         PRIVATE
106         $<$<COMPILE_LANGUAGE:C>:${CFLAGS}>
107         $<$<COMPILE_LANGUAGE:CXX>:${CXXFLAGS}>
108         )
109     # Add compiler options for the build types
110     foreach(build_type ${build_types_with_explicit_flags})
111         target_compile_options(${TARGET}
112             BEFORE PRIVATE
113             $<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:${build_type}>>:${GMXC_CFLAGS_${build_type}}>
114             $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:${build_type}>>:${GMXC_CXXFLAGS_${build_type}}>
115             )
116     endforeach()
117     # Add the release-configuration compiler options to build
118     # configurations that derive from it.
119     foreach(build_type RELWITHDEBINFO RELWITHASSERT MINSIZEREL PROFILE)
120         target_compile_options(${TARGET}
121             BEFORE PRIVATE
122             $<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:${build_type}>>:${GMXC_CFLAGS_RELEASE}>
123             $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:${build_type}>>:${GMXC_CXXFLAGS_RELEASE}>
124             )
125     endforeach()
126     # Add those flags that we only want in the proper release build
127     # configuration.
128     target_compile_options(${TARGET}
129         BEFORE PRIVATE
130         $<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:RELEASE>>:${GMXC_CFLAGS_RELEASE_ONLY}>
131         $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>:${GMXC_CXXFLAGS_RELEASE_ONLY}>
132         )
133 endfunction()
134
135 # The approach taken by FindCUDA.cmake is to require that the compiler
136 # flags are known and present in a variable before creating the target
137 # for the library. (Those get embedded in files that are generated at
138 # the point of calling cuda_add_library, which does not create a
139 # target that one could later call target_compile_options upon.) So,
140 # this function instead returns appropriate content in
141 # ${VARIABLE_NAME}, along with other such variables that are
142 # specialized for the various build_types. Hopefully this will improve
143 # when we use native CUDA language support in our CMake.
144 function(gmx_cuda_target_compile_options VARIABLE_NAME)
145     if (GMX_SKIP_DEFAULT_CFLAGS)
146         set (CXXFLAGS "")
147     else()
148         # Prepare the generic compiler options
149         gmx_target_compile_options_inner()
150         # CUDA headers issue lots of warnings when compiled with
151         # -Wundef because they use old-style #ifdef a lot. We'd prefer
152         # to have FindCUDA.cmake treat CUDA internal headers with
153         # -isystem so that these warnings are naturally suppressed,
154         # but there's no way to do that without bundling a modified
155         # form of FindCUDA.cmake. That creates its own problems,
156         # because people either don't know we do that, or don't
157         # remember that we don't do that in user tarballs.
158         #
159         # We have make check-source ensuring that we have included
160         # config.h any time we use such symbols in commits in a merge
161         # request. Local development could run that too. So, we can
162         # tolerate any remaining risk from accidentally using
163         # e.g. #ifdef GMX_MPI rather than #if GMX_MPI in CUDA source
164         # files.
165         #
166         # So we disable -Wundef by the simple hack of appending
167         # -Wno-undef after it. That's more maintainable than having
168         # logic to avoid adding -Wundef to GMXC_CXXFLAGS, given the
169         # current approach to adding them. Hopefully this will improve
170         # if/when we have more CMake object libraries, and/or native
171         # CUDA compilation.
172         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_NOUNDEF "-Wno-undef" CXXFLAGS)
173     endif()
174
175     # Only C++ compilation is supported with CUDA code in GROMACS
176     set(${VARIABLE_NAME} ${CXXFLAGS} PARENT_SCOPE)
177
178     # Now organize the flags for different build
179     # configurations. First, the debug configuration.
180     set(${VARIABLE_NAME}_DEBUG "${GMXC_CXXFLAGS_DEBUG}" PARENT_SCOPE)
181     # Add those flags that we only want in the proper release build
182     # configuration.
183     set(${VARIABLE_NAME}_RELEASE "${GMXC_CXXFLAGS_RELEASE};${GMXC_CXXFLAGS_RELEASE_ONLY}" PARENT_SCOPE)
184     # Add the release flags to build configurations that derive from it
185     foreach(build_type RELWITHDEBINFO RELWITHASSERT MINSIZEREL PROFILE)
186         set(${VARIABLE_NAME}_${build_type} "${GMXC_CXXFLAGS_RELEASE};${GMXC_CXXFLAGS_${build_type}}" PARENT_SCOPE)
187     endforeach()
188 endfunction()
189
190 # Add the WARNING_FLAG to the compile options for TARGET if the
191 # compiler supports that flag. VARNAME is used to cache the result of
192 # the check whether the compiler supports that flag, as multiple
193 # targets may use the same suppression.
194 #
195 # This is generally used to suppress warnings judged not worth fixing
196 # in code external to, or generated by, GROMACS, or code that is
197 # more efficient to work around and later replace, rather than fix.
198 function(gmx_target_warning_suppression TARGET WARNING_FLAG VARNAME)
199     check_cxx_compiler_flag(${WARNING_FLAG} ${VARNAME})
200     if(${VARNAME})
201         target_compile_options(${TARGET} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${WARNING_FLAG}>)
202     endif()
203 endfunction()
204
205 # Add the WARNING_FLAG to the compile flags for SOURCE_FILE if the
206 # compiler supports that flag. VARNAME is used to cache the result of
207 # the check whether the compiler supports that flag, as multiple
208 # targets may use the same suppression.
209 #
210 # This is generally used to suppress warnings judged not worth fixing
211 # in code external to, or generated by, GROMACS, or code that is
212 # more efficient to work around and later replace, rather than fix.
213 function(gmx_source_file_warning_suppression SOURCE_FILE WARNING_FLAG VARNAME)
214     check_cxx_compiler_flag(${WARNING_FLAG} ${VARNAME})
215     if(${VARNAME})
216         set_source_files_properties(${SOURCE_FILE} PROPERTIES COMPILE_FLAGS ${WARNING_FLAG})
217     endif()
218 endfunction()
219
220 # This is the actual exported function to be called
221 macro (gmx_c_flags)
222
223     include(CheckCCompilerFlag)
224     include(CheckCXXCompilerFlag)
225
226     # gcc
227     if(CMAKE_COMPILER_IS_GNUCC)
228         #flags are added in reverse order and -Wno* need to appear after -Wall
229         if(NOT GMX_OPENMP)
230             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
231         endif()
232         if (GMX_COMPILER_WARNINGS)
233             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value;-Wunused-parameter" GMXC_CFLAGS)
234             GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wextra;-Wno-sign-compare;-Wpointer-arith" GMXC_CFLAGS)
235             GMX_TEST_CFLAG(CFLAGS_WARN_UNDEF "-Wundef" GMXC_CFLAGS)
236             GMX_TEST_CFLAG(CFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CFLAGS_RELEASE_ONLY)
237             if(CYGWIN)
238                 GMX_TEST_CFLAG(CFLAGS_WARN_SUBSCRIPT "-Wno-char-subscripts" GMXC_CFLAGS)
239             endif()
240             GMX_TEST_CFLAG(CFLAGS_STRINGOP_TRUNCATION "-Werror=stringop-truncation" GMXC_CFLAGS)
241         endif()
242         GMX_TEST_CFLAG(CFLAGS_WARN_NO_MISSING_FIELD_INITIALIZERS "-Wno-missing-field-initializers" GMXC_CFLAGS)
243         # new in gcc 4.5
244         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
245         GMX_TEST_CFLAG(CFLAGS_COPT "-funroll-all-loops"
246                        GMXC_CFLAGS_RELEASE)
247         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
248     endif()
249     # g++
250     if(CMAKE_COMPILER_IS_GNUCXX)
251         if(NOT GMX_OPENMP)
252             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
253         endif()
254         if (GMX_COMPILER_WARNINGS)
255             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
256             # Problematic with CUDA
257             # GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EFFCXX "-Wnon-virtual-dtor" GMXC_CXXFLAGS)
258             GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra;-Wpointer-arith;-Wmissing-declarations" GMXC_CXXFLAGS)
259             GMX_TEST_CXXFLAG(CXXFLAGS_WARN_UNDEF "-Wundef" GMXC_CXXFLAGS)
260             GMX_TEST_CFLAG(CXXFLAGS_WARN_REL "-Wno-array-bounds" GMXC_CXXFLAGS_RELEASE_ONLY)
261             GMX_TEST_CXXFLAG(CXXFLAGS_STRINGOP_TRUNCATION "-Wstringop-truncation" GMXC_CXXFLAGS)
262             if (CXXFLAGS_STRINGOP_TRUNCATION)
263                 set(CXXFLAGS_NO_STRINGOP_TRUNCATION "-Wno-stringop-truncation")
264             endif()
265         endif()
266         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_NO_MISSING_FIELD_INITIALIZERS "-Wno-missing-field-initializers" GMXC_CXXFLAGS)
267         # new in gcc 4.5
268         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
269         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-funroll-all-loops"
270                          GMXC_CXXFLAGS_RELEASE)
271         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
272     endif()
273
274     # PGI
275     # Inter-procedural analysis causes pgcc/pgc++ to crash when linking the library with PGI release 15.7.
276     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
277         GMX_TEST_CFLAG(CFLAGS_OPT "-Mnoipa" GMXC_CFLAGS_RELEASE)
278     endif()
279     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
280         # Using ipa exposes internal PGI-15.7 compiler bugs at compile time
281         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-Mnoipa" GMXC_CXXFLAGS_RELEASE)
282         # PGI identifies itself as GCC, but does not understand the GCC
283         # pragmas that occur in parser.cpp. Since that file is generated
284         # we cannot add a define there, but supress the warning instead.
285         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "--diag_suppress=1675" GMXC_CXXFLAGS)
286     endif()
287
288     # Pathscale
289     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
290         if(NOT GMX_OPENMP)
291             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
292         endif()
293         if (GMX_COMPILER_WARNINGS)
294             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value" GMXC_CFLAGS)
295         endif()
296         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast;-fno-math-errno;-ffast-math"
297                          GMXC_CFLAGS_RELEASE)
298         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
299     endif()
300     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
301         if(NOT GMX_OPENMP)
302             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
303         endif()
304         if (GMX_COMPILER_WARNINGS)
305             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value" GMXC_CXXFLAGS)
306         endif()
307         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast;-fno-math-errno;-ffast-math"
308                          GMXC_CXXFLAGS_RELEASE)
309     endif()
310
311     # xlc
312     # The suppressions below stop
313     # 1500-036: (I) about -O3 causing non-strict IEEE compliance that changes the semantics of the program (duh)
314     # 1500-010: (W) about correct PBC-related use of maximum array indices of DIM-sized C arrays
315     # 1500-030: (I) Additional optimization may be attained by recompiling and specifying MAXMEM option with a value greater than 8192.
316     if (CMAKE_C_COMPILER_ID MATCHES "XL")
317         GMX_TEST_CFLAG(CFLAGS_ARCH "-qarch=auto;-qtune=auto" GMXC_CFLAGS)
318         GMX_TEST_CFLAG(CFLAGS_OPT  "-O3" GMXC_CFLAGS_RELEASE)
319         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
320         GMX_TEST_CFLAG(CFLAGS_LANG "-qsuppress=1500-036;-qsuppress=1500-010;-qsuppress=1500-030" GMXC_CFLAGS)
321     endif()
322     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
323         GMX_TEST_CXXFLAG(CXXFLAGS_ARCH "-qarch=auto;-qtune=auto" GMXC_CXXFLAGS)
324         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-O3" GMXC_CXXFLAGS_RELEASE)
325         GMX_TEST_CXXFLAG(CXXFLAGS_LANG "-qsuppress=1500-036;-qsuppress=1500-010;-qsuppress=1500-030" GMXC_CXXFLAGS)
326     endif()
327
328     # msvc
329     if (MSVC)
330         # disable warnings for: 
331         #      forcing value to bool
332         #      "this" in initializer list
333         #      deprecated (posix, secure) functions
334         #      C4305: truncation (double -> float)
335         #      C4244: conversion from '.*' to '.*', possible loss of data
336         #      unreferenced local variable (only C)
337         #      C4267: conversion from 'size_t' to 'int', possible loss of data
338         #      conversion from 'const char*' to 'void*', different 'const' qualifiers (only C)
339         #      unknown pragma (4068)
340         #      remark #280: selector expression is constant
341         if(NOT CMAKE_CONFIGURATION_TYPES)
342             GMX_TEST_CFLAG(CFLAGS_WARN "/wd4800;/wd4355;/wd4996;/wd4305;/wd4244;/wd4101;/wd4267;/wd4090;/wd4068;" GMXC_CFLAGS)
343             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800;/wd4355;/wd4996;/wd4305;/wd4244;/wd4267;/wd4068;" GMXC_CXXFLAGS)
344         else() # MSVC projects only use the C++ flags
345             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800;/wd4355;/wd4996;/wd4305;/wd4244;/wd4101;/wd4267;/wd4090;/wd4068;" GMXC_CXXFLAGS)
346         endif()
347         GMX_TEST_CXXFLAG(CXXFLAGS_LANG "/permissive-" GMXC_CXXFLAGS)
348     endif()
349
350     if (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "IntelLLVM")
351         if(NOT GMX_OPENMP)
352             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
353         endif()
354         if (GMX_COMPILER_WARNINGS)
355             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall;-Wno-unused;-Wunused-value;-Wunused-parameter" GMXC_CFLAGS)
356             GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wpointer-arith" GMXC_CFLAGS_EXTRA)
357         endif()
358         GMX_TEST_CFLAG(CFLAGS_WARN_NO_MISSING_FIELD_INITIALIZERS "-Wno-missing-field-initializers" GMXC_CFLAGS)
359     endif()
360
361     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM")
362         if (GMX_COMPILER_WARNINGS)
363             # If used, -Wall should precede other options that silence warnings it enables
364             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
365             if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0") #LLVM BUG #21629
366                 GMX_TEST_CXXFLAG(CXXFLAGS_WARN_NO_BRACES "-Wno-missing-braces" GMXC_CXXFLAGS)
367             endif()
368             GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra;-Wpointer-arith;-Wmissing-prototypes" GMXC_CXXFLAGS)
369             GMX_TEST_CXXFLAG(CXXFLAGS_DEPRECATED "-Wdeprecated" GMXC_CXXFLAGS)
370             # Functions placed in headers for inlining are not always
371             # used in every translation unit that includes the files,
372             # so we must disable the warning that there are such
373             # functions that are unused.
374             GMX_TEST_CXXFLAG(CXXFLAGS_NO_UNUSED_FUNCTION "-Wno-unused-function" GMXC_CXXFLAGS)
375         endif()
376         if(NOT GMX_OPENMP)
377             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
378         endif()
379         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_NO_RESERVED_IDENTIFIER "-Wno-reserved-identifier" GMXC_CXXFLAGS) # LLVM BUG #50644
380         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_NO_MISSING_FIELD_INITIALIZERS "-Wno-missing-field-initializers" GMXC_CXXFLAGS)
381         # Intel LLVM 2021.2 defaults to no-finite-math which isn't OK for GROMACS
382         if(GMX_INTEL_LLVM AND GMX_INTEL_LLVM_VERSION GREATER_EQUAL 2021020)
383             GMX_TEST_CXXFLAG(CXXFLAGS_FINITE_MATH "-fno-finite-math-only" GMXC_CXXFLAGS)
384         endif()
385         # Some versions of Intel ICPX compiler (at least 2021.1.1 to 2021.3.0) fail to unroll a loop
386         # in sycl::accessor::__init, and emit -Wpass-failed=transform-warning. This is a useful
387         # warning, but mostly noise right now. Probably related to using shared memory accessors.
388         # Note: not a typo: ICPX 2021.1.1 has GMX_INTEL_LLVM_VERSION 202110; 2021.2.0 has 20210200.
389         if(GMX_INTEL_LLVM AND GMX_INTEL_LLVM_VERSION GREATER_EQUAL 202110)
390             GMX_TEST_CXXFLAG(CXXFLAGS_NO_UNROLL_WARNING "-Wno-pass-failed" GMXC_CXXFLAGS)
391         endif()
392     endif()
393
394     # Apple bastardized version of Clang
395     if(${CMAKE_C_COMPILER_ID} MATCHES "AppleClang")
396         if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER 11.0)
397             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
398             # that checks stack alignment by default, but their own C library
399             # does not align the stack properly. Embarrassing, Apple...
400             GMX_TEST_CFLAG(CFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CFLAGS)
401         endif()
402     endif()
403
404     if(${CMAKE_CXX_COMPILER_ID} MATCHES "AppleClang")
405         if(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 11.0)
406             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
407             # that checks stack alignment by default, but their own C library
408             # does not align the stack properly. Embarrassing, Apple...
409             GMX_TEST_CXXFLAG(CXXFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CXXFLAGS)
410         endif()
411     endif()
412
413     # Apple bastardized version of Clang
414     if(${CMAKE_C_COMPILER_ID} MATCHES "AppleClang")
415         if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER 11.0)
416             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
417             # that checks stack alignment by default, but their own C library
418             # does not align the stack properly. Embarrassing, Apple...
419             GMX_TEST_CFLAG(CFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CFLAGS)
420         endif()
421     endif()
422
423     if(${CMAKE_CXX_COMPILER_ID} MATCHES "AppleClang")
424         if(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 11.0)
425             # Mac OS Catalina ships with a horribly broken compiler (version 11.0.0.11000033)
426             # that checks stack alignment by default, but their own C library
427             # does not align the stack properly. Embarrassing, Apple...
428             GMX_TEST_CXXFLAG(CXXFLAG_NO_STACK_CHECK "-fno-stack-check" GMXC_CXXFLAGS)
429         endif()
430     endif()
431
432 endmacro()
433
434 # Make sure we generate warnings (and hopefully fix) "everything"
435 # reported by compilers that support that (ie recent clang and its
436 # derivatives).
437 function(gmx_warn_on_everything target)
438
439     # If the compiler suports warning on "everything" then we'll turn
440     # it on. Note that all warnings become errors for developer
441     # builds, but not for user builds.
442     gmx_target_warning_suppression(${target} "-Weverything" HAS_WARNING_EVERYTHING)
443
444     if (NOT HAS_WARNING_EVERYTHING)
445         # There's no need to suppress aspects of "-Weverything" if
446         # that warning is not supported.
447         return()
448     endif()
449
450     # We don't actually fix everything, so list the exceptions that we
451     # choose to make. We may be able to eliminate some of these over
452     # time.
453     #
454     # We check whether the flag is accepted first, so that we suppress
455     # such warnings also with compilers that don't directly identify
456     # as e.g. clang despite being based on it (e.g. most vendor
457     # compilers), and also don't fail to compile GROMACS when future
458     # versions of any such compiler changes how the warnings
459     # look/work.
460
461     # We have no intention of C++98 compability
462     gmx_target_warning_suppression(${target} "-Wno-c++98-compat" HAS_WARNING_NO_CPLUSPLUS98_COMPAT)
463     gmx_target_warning_suppression(${target} "-Wno-c++98-compat-pedantic" HAS_WARNING_NO_CPLUSPLUS98_COMPAT_PEDANTIC)
464
465     # Don't warn for use of OpenMP pragmas in no-omp build
466     gmx_target_warning_suppression(${target} "-Wno-source-uses-openmp" HAS_WARNING_NO_SOURCE_USED_OPENMP)
467
468     # Allowed in attributes (compilers are required to ignore unknown attributes)
469     gmx_target_warning_suppression(${target} "-Wno-c++17-extensions" HAS_WARNING_NO_CPLUSPLUS17_EXTENSIONS)
470
471     # Custom Doxygen commands are used
472     gmx_target_warning_suppression(${target} "-Wno-documentation-unknown-command" HAS_WARNING_NO_DOCUMENTATION_UNKNOWN_COMMAND)
473
474     # We need to use default labels in switch statements, because GCC gives
475     # maybe-uninitialized without default label and checks for illegal enum values.
476     gmx_target_warning_suppression(${target} "-Wno-covered-switch-default" HAS_WARNING_NO_COVERED_SWITCH_DEFAULT)
477
478     # Default statement for enum is OK.
479     # It's OK to not have branches for Count members of enum classes
480     gmx_target_warning_suppression(${target} "-Wno-switch-enum" HAS_WARNING_NO_SWITCH_ENUM)
481
482     # We need to use macros like
483     # GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR and
484     # CLANG_DIAGNOSTIC_IGNORE. Those will look strange if they don't
485     # have a semicolon after them, and might confuse tools like IDEs
486     # also.
487     gmx_target_warning_suppression(${target} "-Wno-extra-semi-stmt" HAS_WARNING_NO_EXTRA_SEMI_STMT)
488
489     # We intend to use fully inline classes with virtual methods
490     gmx_target_warning_suppression(${target} "-Wno-weak-vtables" HAS_WARNING_NO_WEAK_VTABLES)
491
492     # We intend to use constructor arguments that shadow member variables
493     gmx_target_warning_suppression(${target} "-Wno-shadow" HAS_WARNING_NO_SHADOW)
494
495     # Padding of structs is routine, we don't need to hear about it
496     gmx_target_warning_suppression(${target} "-Wno-padded" HAS_WARNING_NO_PADDED)
497
498     # Our uses of double underscores in macro names are OK
499     gmx_target_warning_suppression(${target} "-Wno-reserved-id-macro" HAS_WARNING_NO_RESERVED_ID_MACRO)
500
501     # Implicit conversion of float to double is fine
502     gmx_target_warning_suppression(${target} "-Wno-double-promotion" HAS_WARNING_NO_DOUBLE_PROMOTION)
503
504     # No resources in static variables need exit-time destructors
505     gmx_target_warning_suppression(${target} "-Wno-exit-time-destructors" HAS_WARNING_NO_EXIT_TIME_DESTRUCTORS)
506
507     # Global constructors are not needed
508     gmx_target_warning_suppression(${target} "-Wno-global-constructors" HAS_WARNING_NO_GLOBAL_CONSTRUCTORS)
509
510     # False positives are emitted
511     gmx_target_warning_suppression(${target} "-Wno-documentation" HAS_WARNING_NO_DOCUMENTATION)
512
513     # We intend to use format strings that we construct, even though that is a security risk
514     gmx_target_warning_suppression(${target} "-Wno-format-nonliteral" HAS_WARNING_NO_FORMAT_NONLITERAL)
515
516     # We do a lot of conditional compilation that sometimes uses a symbol and sometimes does not
517     gmx_target_warning_suppression(${target} "-Wno-used-but-marked-unused" HAS_WARNING_NO_USED_BUT_MARKED_UNUSED)
518
519     # It's only risky to compare floats for equality when they are the
520     # result of computation.  Unfortunately it's hard to tell the
521     # difference and there's no good way to suppress this on a
522     # case-by-base basis.
523     gmx_target_warning_suppression(${target} "-Wno-float-equal" HAS_WARNING_NO_FLOAT_EQUAL)
524
525     #
526     # Exceptions we should consider fixing
527     #
528
529     # Much code in gmxana uses complex logic that may or may not be valid
530     gmx_target_warning_suppression(${target} "-Wno-conditional-uninitialized" HAS_WARNING_CONDITIONAL_UNINITIALIZED)
531
532     # We have many places implicit conversions still occur, most of which need fixing
533     gmx_target_warning_suppression(${target} "-Wno-conversion" HAS_WARNING_NO_CONVERSION)
534
535     # We use the Linux signal handlers in the intended way, but it triggers this warning.
536     # It would be better to localize this exception.
537     gmx_target_warning_suppression(${target} "-Wno-disabled-macro-expansion" HAS_WARNING_NO_DISABLED_MACRO_EXPANSION)
538
539     # The NBNXM simd kernels define lots of macros that are not used
540     # It would be better to localize this exception.
541     gmx_target_warning_suppression(${target} "-Wno-unused-macros" HAS_WARNING_NO_UNUSED_MACROS)
542
543 endfunction()