Add RelWithAssert build type
[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(NOT DEFINED ${VARIABLE})
41     IF (${VARIABLE})
42         SET (${CFLAGSVAR} "${FLAGS} ${${CFLAGSVAR}}")
43     ENDIF (${VARIABLE})
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(NOT DEFINED ${VARIABLE})
52     IF (${VARIABLE})
53         SET (${CXXFLAGSVAR} "${FLAGS} ${${CXXFLAGSVAR}}")
54     ENDIF (${VARIABLE})
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 RELWITHDEBUGINFO RELWITHASSERT MINSIZEREL)
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.
89             set(CMAKE_${language}_FLAGS${punctuation}${build_type}
90                 "${GMXC_${language}FLAGS${punctuation}${build_type}} ${CMAKE_${language}_FLAGS${punctuation}${build_type}}"
91                 PARENT_SCOPE)
92         endforeach()
93     endforeach()
94 endfunction()
95
96 # This is the actual exported function to be called 
97 MACRO(gmx_c_flags)
98
99     include(CheckCCompilerFlag)
100     include(CheckCXXCompilerFlag)
101
102     # gcc
103     if(CMAKE_COMPILER_IS_GNUCC)
104         #flags are added in reverse order and -Wno* need to appear after -Wall
105         if(NOT GMX_OPENMP)
106             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
107         endif()
108         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value -Wunused-parameter" GMXC_CFLAGS)
109         GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wno-sign-compare -Wpointer-arith" GMXC_CFLAGS)
110         # new in gcc 4.5
111         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
112         GMX_TEST_CFLAG(CFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
113                        GMXC_CFLAGS_RELEASE)
114         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
115     endif()
116     # g++
117     if(CMAKE_COMPILER_IS_GNUCXX)
118         if(NOT GMX_OPENMP)
119             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
120         endif()
121         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused-function" GMXC_CXXFLAGS)
122         # Problematic with CUDA
123         # GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EFFCXX "-Wnon-virtual-dtor" GMXC_CXXFLAGS)
124         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wpointer-arith" GMXC_CXXFLAGS)
125         # new in gcc 4.5
126         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
127         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
128                          GMXC_CXXFLAGS_RELEASE)
129         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
130     endif()
131
132     # icc
133     if (CMAKE_C_COMPILER_ID MATCHES "Intel")
134         if (NOT WIN32) 
135             if(NOT GMX_OPENMP)
136                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
137             endif()
138             GMX_TEST_CFLAG(CFLAGS_WARN "-w3 -wd111 -wd177 -wd181 -wd193 -wd271 -wd304 -wd383 -wd424 -wd444 -wd522 -wd593 -wd869 -wd981 -wd1418 -wd1419 -wd1572 -wd1599 -wd2259 -wd2415 -wd2547 -wd2557 -wd3280 -wd3346" GMXC_CFLAGS)
139             GMX_TEST_CFLAG(CFLAGS_STDGNU "-std=gnu99" GMXC_CFLAGS)
140             GMX_TEST_CFLAG(CFLAGS_OPT "-ip -funroll-all-loops -alias-const -ansi-alias" GMXC_CFLAGS_RELEASE)
141         else()
142             GMX_TEST_CFLAG(CFLAGS_WARN "/W2" GMXC_CFLAGS)
143             GMX_TEST_CFLAG(CFLAGS_X86 "/Qip" GMXC_CFLAGS_RELEASE)
144         endif()
145     endif()
146
147     if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
148         if (NOT WIN32) 
149             if(NOT GMX_OPENMP)
150                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
151             endif()
152             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-w3 -wd111 -wd177 -wd181 -wd193 -wd271 -wd304 -wd383 -wd424 -wd444 -wd522 -wd593 -wd869 -wd981 -wd1418 -wd1419 -wd1572 -wd1599 -wd2259 -wd2415 -wd2547 -wd2557 -wd3280 -wd3346 -wd1782" GMXC_CXXFLAGS)
153             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-ip -funroll-all-loops -alias-const -ansi-alias" GMXC_CXXFLAGS_RELEASE)
154         else()
155             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/W2" GMXC_CXXFLAGS)
156             GMX_TEST_CXXFLAG(CXXFLAGS_X86 "/Qip" GMXC_CXXFLAGS_RELEASE)
157         endif()
158     endif()
159
160     # pgi
161     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
162         GMX_TEST_CFLAG(CFLAGS_OPT "-fastsse" GMXC_CFLAGS_RELEASE)
163     endif()
164     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
165         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-fastsse" GMXC_CXXFLAGS_RELEASE)
166     endif()
167
168     # Pathscale
169     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
170         if(NOT GMX_OPENMP)
171             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
172         endif()
173         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
174         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
175                          GMXC_CFLAGS_RELEASE)
176         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
177     endif()
178     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
179         if(NOT GMX_OPENMP)
180             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
181         endif()
182         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
183         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
184                          GMXC_CXXFLAGS_RELEASE)
185     endif()
186
187     # xlc
188     if (CMAKE_C_COMPILER_ID MATCHES "XL")
189         GMX_TEST_CFLAG(CFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CFLAGS)
190         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
191     endif()
192     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
193         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CXXFLAGS)
194     endif()
195
196     # msvc
197     if (MSVC)
198         # disable warnings for: 
199         #      forcing value to bool
200         #      "this" in initializer list
201         #      deprecated (posix, secure) functions
202         GMX_TEST_CFLAG(CFLAGS_WARN "/wd4800 /wd4355 /wd4996" GMXC_CFLAGS)
203         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800 /wd4355 /wd4996" GMXC_CXXFLAGS)
204     endif()
205
206     if (CMAKE_C_COMPILER_ID MATCHES "Clang")
207         if(NOT GMX_OPENMP)
208             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
209         endif()
210         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value -Wunused-parameter" GMXC_CFLAGS)
211         GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wpointer-arith" GMXC_CFLAGS_EXTRA)
212     endif()
213
214     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
215         if(NOT GMX_OPENMP)
216             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
217         endif()
218         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused-function" GMXC_CXXFLAGS)
219         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wpointer-arith" GMXC_CXXFLAGS)
220     endif()
221
222     # now actually set the flags:
223     if (NOT GMX_SKIP_DEFAULT_CFLAGS)
224         gmx_set_cmake_compiler_flags()
225     endif()
226 ENDMACRO(gmx_c_flags)
227