Updated reference manual text about PME tuning
[gromacs.git] / cmake / gmxFindFlagsForSource.cmake
blob68673f69d0145d0c27c0ec2f2a48e48e342c027c
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2013,2014,2016, 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.
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.
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.
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.
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.
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.
35 # Helper routine to find flag (from a list) to compile a specific C source.
36 # VARIABLE            This will be set when we have found a flag that works
37 # DESCRIPTION         Text string describing what flag we are trying to find
38 # SOURCE              Source code to test
39 #                     The compiler is chosen based on the extension of this file
40 # FLAGSVAR            Variable (string) to which we should add the correct flag
41 # Args 5 through N    Multiple strings with optimization flags to test
43 # If a compile flag is found, but the project in check_c_source_compiles
44 # fails to build, sets SUGGEST_BINUTILS_UPDATE in parent scope to suggest
45 # that the calling code tell the user about this issue if needed.
46 FUNCTION(GMX_FIND_CFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CFLAGSVAR)
47     IF(NOT DEFINED ${VARIABLE})
48         # Insert a blank element last in the list (try without any flags too)
49         # This must come last, since some compilers (Intel) might try to emulate
50         # emulate AVX instructions with SSE4.1 otherwise.
51         foreach(_testflag ${ARGN} "")
52             message(STATUS "Try ${DESCRIPTION} = [${_testflag}]")
53             set(CMAKE_REQUIRED_FLAGS "${${CFLAGSVAR}} ${_testflag}")
54             # make valid variable names from the flag string: replace all non-alphanumerical chars
55             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_FLAG_VARIABLE "C_FLAG_${_testflag}")
56             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_SIMD_VARIABLE "C_SIMD_COMPILES_FLAG_${_testflag}")
58             # Check that the flag itself is fine, and that is does not generate warnings either
59             check_c_compiler_flag("${_testflag}" ${COMPILE_FLAG_VARIABLE})
61             if(${COMPILE_FLAG_VARIABLE})
62                 # Check that we can compile SIMD source (this does not catch compiler warnings)
63                 check_c_source_compiles("${SOURCE}" ${COMPILE_SIMD_VARIABLE})
64                 if(NOT ${COMPILE_SIMD_VARIABLE})
65                     message(STATUS "Compiler flag was valid, but executable did not build - perhaps update the binutils package")
66                     set(SUGGEST_BINUTILS_UPDATE 1 PARENT_SCOPE)
67                 endif()
68             endif()
70             if(${COMPILE_FLAG_VARIABLE} AND ${COMPILE_SIMD_VARIABLE})
71                 set(${VARIABLE}_FLAG "${_testflag}" CACHE INTERNAL "${DESCRIPTION}")
72                 set(${VARIABLE} 1 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
73                 break()
74             else()
75                 set(${VARIABLE} 0 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
76             endif()
77         endforeach()
78     ENDIF()
80     IF (${VARIABLE})
81         SET (${CFLAGSVAR} "${${CFLAGSVAR}} ${${VARIABLE}_FLAG}" PARENT_SCOPE)
82     ENDIF ()
83 ENDFUNCTION(GMX_FIND_CFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CFLAGSVAR)
86 # Helper routine to find flag (from list) to compile a specific C++ source.
87 # VARIABLE            This will be set when we have found a flag that works
88 # DESCRIPTION         Text string describing what flag we are trying to find
89 # SOURCE              Source code to test
90 #                     The compiler is chosen based on the extension of this file
91 # FLAGSVAR            Variable (string) to which we should add the correct flag
92 # Args 5 through N    Multiple strings with optimization flags to test
94 # If a compile flag is found, but the project in check_cxx_source_compiles
95 # fails to build, sets SUGGEST_BINUTILS_UPDATE in parent scope to suggest
96 # that the calling code tell the user about this issue if needed.
97 FUNCTION(GMX_FIND_CXXFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CXXFLAGSVAR)
99     IF(NOT DEFINED ${VARIABLE})
100         # Insert a blank element last in the list (try without any flags too)
101         # This must come last, since some compilers (Intel) might try to
102         # emulate AVX instructions with SSE4.1 otherwise.
103         foreach(_testflag ${ARGN} "")
104             message(STATUS "Try ${DESCRIPTION} = [${_testflag}]")
105             set(CMAKE_REQUIRED_FLAGS "${${CXXFLAGSVAR}} ${_testflag}")
106             # make valid variable names from the flag string: replace all non-alphanumerical chars
107             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_FLAG_VARIABLE "CXX_FLAG_${_testflag}")
108             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_SIMD_VARIABLE "CXX_SIMD_COMPILES_FLAG_${_testflag}")
109             
110             # Check that the flag itself is fine, and that is does not generate warnings either
111             check_cxx_compiler_flag("${_testflag}" ${COMPILE_FLAG_VARIABLE})
113             if(${COMPILE_FLAG_VARIABLE})
114                 # Check that we can compile SIMD source (this does not catch compiler warnings)
115                 check_cxx_source_compiles("${SOURCE}" ${COMPILE_SIMD_VARIABLE})
116                 if(NOT ${COMPILE_SIMD_VARIABLE})
117                     message(STATUS "Compiler flag was valid, but executable did not build - perhaps update the binutils package")
118                     set(SUGGEST_BINUTILS_UPDATE 1 PARENT_SCOPE)
119                 endif()
120             endif()
122             if(${COMPILE_FLAG_VARIABLE} AND ${COMPILE_SIMD_VARIABLE})
123                 set(${VARIABLE}_FLAG "${_testflag}" CACHE INTERNAL "${DESCRIPTION}")
124                 set(${VARIABLE} 1 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
125                 break()
126             else()
127                 set(${VARIABLE} 0 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
128             endif()
129         endforeach()
130     ENDIF()
132     IF (${VARIABLE})
133         SET (${CXXFLAGSVAR} "${${CXXFLAGSVAR}} ${${VARIABLE}_FLAG}" PARENT_SCOPE)
134     ENDIF ()
136 ENDFUNCTION(GMX_FIND_CXXFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CXXFLAGSVAR)