Merge release-4-6 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, 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
58 # This is the actual exported function to be called 
59 MACRO(gmx_c_flags)
60
61     include(CheckCCompilerFlag)
62     include(CheckCXXCompilerFlag)
63
64     # gcc
65     if(CMAKE_COMPILER_IS_GNUCC)
66         #flags are added in reverse order and -Wno* need to appear after -Wall
67         if(NOT GMX_OPENMP)
68             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
69         endif()
70         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value -Wunused-parameter" GMXC_CFLAGS)
71         GMX_TEST_CFLAG(CFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers -Wno-sign-compare" GMXC_CFLAGS)
72         # new in gcc 4.5
73         GMX_TEST_CFLAG(CFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CFLAGS_RELEASE)
74         GMX_TEST_CFLAG(CFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
75                        GMXC_CFLAGS_RELEASE)
76         GMX_TEST_CFLAG(CFLAGS_NOINLINE "-fno-inline" GMXC_CFLAGS_DEBUG)
77     endif()
78     # g++
79     if(CMAKE_COMPILER_IS_GNUCXX)
80         if(NOT GMX_OPENMP)
81             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
82         endif()
83         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused-function" GMXC_CXXFLAGS)
84         # Problematic with CUDA
85         # GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EFFCXX "-Wnon-virtual-dtor" GMXC_CXXFLAGS)
86         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers" GMXC_CXXFLAGS)
87         # new in gcc 4.5
88         GMX_TEST_CXXFLAG(CXXFLAGS_EXCESS_PREC "-fexcess-precision=fast" GMXC_CXXFLAGS_RELEASE)
89         GMX_TEST_CXXFLAG(CXXFLAGS_COPT "-fomit-frame-pointer -funroll-all-loops"
90                          GMXC_CXXFLAGS_RELEASE)
91         GMX_TEST_CXXFLAG(CXXFLAGS_NOINLINE "-fno-inline" GMXC_CXXFLAGS_DEBUG)
92     endif()
93
94     # icc
95     if (CMAKE_C_COMPILER_ID MATCHES "Intel")
96         if (NOT WIN32) 
97             if(NOT GMX_OPENMP)
98                 GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
99             endif()
100             GMX_TEST_CFLAG(CFLAGS_WARN "-Wall" GMXC_CFLAGS)
101             GMX_TEST_CFLAG(CFLAGS_STDGNU "-std=gnu99" GMXC_CFLAGS)
102             GMX_TEST_CFLAG(CFLAGS_OPT "-ip -funroll-all-loops" GMXC_CFLAGS_RELEASE)
103         else()
104             GMX_TEST_CFLAG(CFLAGS_WARN "/W2" GMXC_CFLAGS)
105             GMX_TEST_CFLAG(CFLAGS_X86 "/Qip" GMXC_CFLAGS_RELEASE)
106         endif()
107     endif()
108
109     if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
110         if (NOT WIN32) 
111             if(NOT GMX_OPENMP)
112                 GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
113             endif()
114             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall" GMXC_CXXFLAGS)
115             GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-ip -funroll-all-loops" GMXC_CXXFLAGS_RELEASE)
116         else()
117             GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/W2" GMXC_CXXFLAGS)
118             GMX_TEST_CXXFLAG(CXXFLAGS_X86 "/Qip" GMXC_CXXFLAGS_RELEASE)
119         endif()
120     endif()
121
122     # pgi
123     if (CMAKE_C_COMPILER_ID MATCHES "PGI")
124         GMX_TEST_CFLAG(CFLAGS_OPT "-fastsse" GMXC_CFLAGS_RELEASE)
125     endif()
126     if (CMAKE_CXX_COMPILER_ID MATCHES "PGI")
127         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-fastsse" GMXC_CXXFLAGS_RELEASE)
128     endif()
129
130     # Pathscale
131     if (CMAKE_C_COMPILER_ID MATCHES "PathScale")
132         if(NOT GMX_OPENMP)
133             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
134         endif()
135         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CFLAGS)
136         GMX_TEST_CFLAG(CFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
137                          GMXC_CFLAGS_RELEASE)
138         GMX_TEST_CFLAG(CFLAGS_LANG "-std=gnu99" GMXC_CFLAGS)
139     endif()
140     if (CMAKE_CXX_COMPILER_ID MATCHES "PathScale")
141         if(NOT GMX_OPENMP)
142             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
143         endif()
144         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused -Wunused-value" GMXC_CXXFLAGS)
145         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-OPT:Ofast -fno-math-errno -ffast-math" 
146                          GMXC_CXXFLAGS_RELEASE)
147     endif()
148
149     # xlc
150     if (CMAKE_C_COMPILER_ID MATCHES "XL")
151         GMX_TEST_CFLAG(CFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CFLAGS)
152         GMX_TEST_CFLAG(CFLAGS_LANG "-qlanglvl=extc99" GMXC_CFLAGS)
153     endif()
154     if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
155         GMX_TEST_CXXFLAG(CXXFLAGS_OPT "-qarch=auto -qtune=auto" GMXC_CXXFLAGS)
156     endif()
157
158     # msvc
159     if (MSVC)
160         # disable warnings for: 
161         #      forcing value to bool
162         #      "this" in initializer list
163         #      deprecated (posix, secure) functions
164         GMX_TEST_CFLAG(CFLAGS_WARN "/wd4800 /wd4355 /wd4996" GMXC_CFLAGS)
165         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "/wd4800 /wd4355 /wd4996" GMXC_CXXFLAGS)
166     endif()
167
168     if (CMAKE_C_COMPILER_ID MATCHES "Clang")
169         if(NOT GMX_OPENMP)
170             GMX_TEST_CFLAG(CFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CFLAGS)
171         endif()
172         GMX_TEST_CFLAG(CFLAGS_WARN "-Wall -Wno-unused -Wunused-value -Wunused-parameter" GMXC_CFLAGS)
173     endif()
174
175     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
176         if(NOT GMX_OPENMP)
177             GMX_TEST_CXXFLAG(CXXFLAGS_PRAGMA "-Wno-unknown-pragmas" GMXC_CXXFLAGS)
178         endif()
179         GMX_TEST_CXXFLAG(CXXFLAGS_WARN "-Wall -Wno-unused-function" GMXC_CXXFLAGS)
180         GMX_TEST_CXXFLAG(CXXFLAGS_WARN_EXTRA "-Wextra -Wno-missing-field-initializers" GMXC_CXXFLAGS)
181     endif()
182
183     # now actually set the flags:
184     # C
185     if ( NOT GMX_SKIP_DEFAULT_CFLAGS )
186         set(CMAKE_C_FLAGS "${GMXC_CFLAGS} ${CMAKE_C_FLAGS}")
187         set(CMAKE_C_FLAGS_RELEASE "${GMXC_CFLAGS_RELEASE} ${CMAKE_C_FLAGS_RELEASE}")
188         set(CMAKE_C_FLAGS_DEBUG "${GMXC_CFLAGS_DEBUG} ${CMAKE_C_FLAGS_DEBUG}")
189     endif()
190
191     # C++
192     if ( NOT GMX_SKIP_DEFAULT_CFLAGS)
193         set(CMAKE_CXX_FLAGS "${GMXC_CXXFLAGS} ${CMAKE_CXX_FLAGS}")
194         set(CMAKE_CXX_FLAGS_RELEASE 
195             "${GMXC_CXXFLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELEASE}")
196         set(CMAKE_CXX_FLAGS_DEBUG 
197             "${GMXC_CXXFLAGS_DEBUG} ${CMAKE_CXX_FLAGS_DEBUG}")
198     endif()
199 ENDMACRO(gmx_c_flags)
200