Ignore bool cast warning for MSVC and C++
[alexxy/gromacs.git] / cmake / gmxCFlags.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012, by the GROMACS development team, led by
5 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 # others, as listed in the AUTHORS file in the top-level source
7 # 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
36 # Test C flags FLAGS, and set VARIABLE to true if the work. Also add the
37 # flags to CFLAGSVAR.
38 MACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
39     IF(NOT DEFINED ${VARIABLE})
40         CHECK_C_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
41     ENDIF(NOT DEFINED ${VARIABLE})
42     IF (${VARIABLE})
43         SET (${CFLAGSVAR} "${FLAGS} ${${CFLAGSVAR}}")
44     ENDIF (${VARIABLE})
45 ENDMACRO(GMX_TEST_CFLAG VARIABLE FLAGS CFLAGSVAR)
46
47 # Test C++ flags FLAGS, and set VARIABLE to true if the work. Also add the
48 # flags to CXXFLAGSVAR.
49 MACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
50     IF(NOT DEFINED ${VARIABLE} AND CMAKE_CXX_COMPILER_LOADED)
51         CHECK_CXX_COMPILER_FLAG("${FLAGS}" ${VARIABLE})
52     ENDIF(NOT DEFINED ${VARIABLE} AND CMAKE_CXX_COMPILER_LOADED)
53     IF (${VARIABLE})
54         SET (${CXXFLAGSVAR} "${FLAGS} ${${CXXFLAGSVAR}}")
55     ENDIF (${VARIABLE})
56 ENDMACRO(GMX_TEST_CXXFLAG VARIABLE FLAGS CXXFLAGSVAR)
57
58
59 # This is the actual exported function to be called 
60 MACRO(gmx_c_flags)
61
62     include(CheckCCompilerFlag)
63     include(CheckCXXCompilerFlag)
64
65     # gcc
66     if(CMAKE_COMPILER_IS_GNUCC)
67
68         #Fix for LLVM OpenMP bug (redmine 900). Needs to run before OpenMP flags are set below.
69         if(GMX_OPENMP)
70             exec_program(${CMAKE_C_COMPILER} ARGS --version OUTPUT_VARIABLE _compiler_output)
71             if(_compiler_output MATCHES "llvm.*4\\.2")
72                 message(STATUS "OpenMP multithreading not supported with llvm-gcc 4.2, disabled")
73                 set(GMX_OPENMP OFF CACHE BOOL
74                     "OpenMP multithreading not not supported with llvm-gcc 4.2, disabled!" FORCE)
75             endif()
76         endif()
77
78         #flags are added in reverse order and -Wno* need to appear after -Wall
79         if(NOT GMX_OPENMP)
80             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
81         endif()
82         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
83         GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wno-sign-compare" GMXC_CFLAGS)
84         # new in gcc 4.5
85         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
86         GMX_TEST_CFLAG(CFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
87                        GMXC_CFLAGS_RELEASE)
88         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
89     endif()
90     # g++
91     if(CMAKE_COMPILER_IS_GNUCXX)
92         if(NOT GMX_OPENMP)
93             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
94         endif()
95         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
96         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wno-sign-compare" GMXC_CXXFLAGS)
97       # new in gcc 4.5
98         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
99         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
100                          GMXC_CXXFLAGS_RELEASE)
101         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
102     endif()
103
104     # icc
105     if (CMAKE_C_COMPILER_ID MATCHES "Intel")
106         if (NOT WIN32) 
107             if(NOT GMX_OPENMP)
108                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
109             endif()
110             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall" GMXC_CFLAGS)
111             GMX_TEST_CFLAG(CFLAGS_STDGNU "-std=gnu99" GMXC_CFLAGS)
112             GMX_TEST_CFLAG(CFLAGS_OPT "-ip -funroll-all-loops" GMXC_CFLAGS_RELEASE)
113             GMX_TEST_CFLAG(CFLAGS_SSE2 "-msse2" GMXC_CFLAGS_RELEASE)
114             GMX_TEST_CFLAG(CFLAGS_X86 "-mtune=core2" GMXC_CFLAGS_RELEASE)
115             GMX_TEST_CFLAG(CFLAGS_IA64 "-mtune=itanium2" GMXC_CFLAGS_RELEASE)
116         else()
117             GMX_TEST_CFLAG(CFLAGS_WARN "/W2" GMXC_CFLAGS)
118             GMX_TEST_CFLAG(CFLAGS_SSE2 "/arch:SSE2" GMXC_CFLAGS_RELEASE)
119             GMX_TEST_CFLAG(CFLAGS_X86 "/Qip" GMXC_CFLAGS_RELEASE)
120         endif()
121     endif()
122
123     if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
124         if (NOT WIN32) 
125             if(NOT GMX_OPENMP)
126                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
127             endif()
128             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
129             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-ip -funroll-all-loops" GMXC_CXXFLAGS_RELEASE)
130             GMX_TEST_CXXFLAG(CXXFLAGS_SSE2 "-msse2" GMXC_CXXFLAGS_RELEASE)
131             GMX_TEST_CXXFLAG(CXXFLAGS_X86 "-mtune=core2" GMXC_CXXFLAGS_RELEASE)
132             GMX_TEST_CXXFLAG(CXXFLAGS_IA64 "-mtune=itanium2" 
133                               GMXC_CXXFLAGS_RELEASE)
134         else()
135             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/W2" GMXC_CXXFLAGS)
136             GMX_TEST_CXXFLAG(CXXFLAGS_SSE2 "/arch:SSE2" GMXC_CXXFLAGS_RELEASE)
137             GMX_TEST_CXXFLAG(CXXFLAGS_X86 "/Qip" GMXC_CXXFLAGS_RELEASE)
138         endif()
139     endif()
140
141     # pgi
142     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
143         GMX_TEST_CFLAG(CFLAGS_OPT "-fastsse" GMXC_CFLAGS_RELEASE)
144     endif()
145     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
146         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-fastsse" GMXC_CXXFLAGS_RELEASE)
147     endif()
148
149     # Pathscale
150     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
151         if(NOT GMX_OPENMP)
152             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
153         endif()
154         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
155         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
156                          GMXC_CFLAGS_RELEASE)
157         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
158     endif()
159     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
160         if(NOT GMX_OPENMP)
161             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
162         endif()
163         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
164         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
165                          GMXC_CXXFLAGS_RELEASE)
166     endif()
167
168     # xlc
169     if (CMAKE_C_COMPILER_ID MATCHES "XL")
170         GMX_TEST_CFLAG(CFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CFLAGS)
171         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
172     endif()
173     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
174         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CXXFLAGS)
175     endif()
176
177     #msvc
178     if (MSVC)
179         # disable warnings for: 
180         #      inconsistent dll linkage
181         #      forcing value to bool (for C++)
182         GMX_TEST_CFLAG(CFLAGS_WARN "/wd4273" GMXC_CFLAGS)
183         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4273 /wd4800" GMXC_CXXFLAGS)
184     endif()
185
186     if (CMAKE_C_COMPILER_ID MATCHES "Clang")
187         if(NOT GMX_OPENMP)
188             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
189         endif()
190         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
191     endif()
192
193     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
194         if(NOT GMX_OPENMP)
195             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
196         endif()
197         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
198     endif()
199
200     # now actually set the flags:
201     # C
202     if ( NOT DEFINED GMXCFLAGS_SET AND NOT DEFINED ENV{CFLAGS} )
203         set(GMXCFLAGS_SET true CACHE INTERNAL "Whether to reset the C flags" 
204             FORCE)
205         
206         set(CMAKE_C_FLAGS "${GMXC_CFLAGS} ${CMAKE_C_FLAGS}" 
207             CACHE STRING "Flags used by the compiler during all build types." 
208             FORCE)
209         set(CMAKE_C_FLAGS_RELEASE "${GMXC_CFLAGS_RELEASE} ${CMAKE_C_FLAGS_RELEASE}" 
210             CACHE STRING "Flags used by the compiler during release builds." 
211             FORCE)
212         set(CMAKE_C_FLAGS_DEBUG "${GMXC_CFLAGS_DEBUG} ${CMAKE_C_FLAGS_DEBUG}" 
213             CACHE STRING "Flags used by the compiler during debug builds." 
214             FORCE)
215     endif()
216
217     # C++
218     if ( NOT DEFINED GMXCXXFLAGS_SET AND NOT DEFINED ENV{CXXFLAGS} AND CMAKE_CXX_COMPILER_LOADED)
219         set(GMXCXXFLAGS_SET true CACHE INTERNAL "Whether to reset the C++ flags" 
220             FORCE)
221         set(CMAKE_CXX_FLAGS "${GMXC_CXXFLAGS} ${CMAKE_CXX_FLAGS}" 
222             CACHE STRING "Flags used by the compiler during all build types." 
223             FORCE)
224         set(CMAKE_CXX_FLAGS_RELEASE 
225             "${GMXC_CXXFLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELEASE}" 
226             CACHE STRING "Flags used by the compiler during release builds." 
227             FORCE)
228         set(CMAKE_CXX_FLAGS_DEBUG 
229             "${GMXC_CXXFLAGS_DEBUG} ${CMAKE_CXX_FLAGS_DEBUG}" 
230             CACHE STRING "Flags used by the compiler during debug builds." 
231             FORCE)
232     endif()
233 ENDMACRO(gmx_c_flags)
234