Added cmake profiling support
[alexxy/gromacs.git] / cmake / gmxFindFlagsForSource.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 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 # 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
42 FUNCTION(GMX_FIND_CFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CFLAGSVAR)
43     IF(NOT DEFINED ${VARIABLE})
44         # Insert a blank element last in the list (try without any flags too)
45         # This must come last, since some compilers (Intel) might try to emulate
46         # emulate AVX instructions with SSE4.1 otherwise.
47         foreach(_testflag ${ARGN} "")
48             message(STATUS "Try ${DESCRIPTION} = [${_testflag}]")
49             set(CMAKE_REQUIRED_FLAGS "${${CFLAGSVAR}} ${_testflag}")
50             # make a valid variable name from the flag string: replace all non-alphanumerical chars
51             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_VARIABLE "C_FLAG_${_testflag}")
52             check_c_source_compiles("${SOURCE}" ${COMPILE_VARIABLE})
53             if(${${COMPILE_VARIABLE}})
54                 set(${VARIABLE}_FLAG "${_testflag}" CACHE INTERNAL "${DESCRIPTION}")
55                 set(${VARIABLE} 1 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
56                 break()
57             else()
58                 set(${VARIABLE} 0 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
59             endif()
60         endforeach()
61     ENDIF()
62
63     IF (${VARIABLE})
64         SET (${CFLAGSVAR} "${${CFLAGSVAR}} ${${VARIABLE}_FLAG}" PARENT_SCOPE)
65     ENDIF ()
66 ENDFUNCTION(GMX_FIND_CFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CFLAGSVAR)
67
68
69 # Helper routine to find flag (from list) to compile a specific C++ source.
70 # VARIABLE            This will be set when we have found a flag that works
71 # DESCRIPTION         Text string describing what flag we are trying to find
72 # SOURCE              Source code to test
73 #                     The compiler is chosen based on the extension of this file
74 # FLAGSVAR            Variable (string) to which we should add the correct flag
75 # Args 5 through N    Multiple strings with optimization flags to test
76 FUNCTION(GMX_FIND_CXXFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CXXFLAGSVAR)
77
78     IF(NOT DEFINED ${VARIABLE})
79         # Insert a blank element last in the list (try without any flags too)
80         # This must come last, since some compilers (Intel) might try to
81         # emulate AVX instructions with SSE4.1 otherwise.
82         foreach(_testflag ${ARGN} "")
83             message(STATUS "Try ${DESCRIPTION} = [${_testflag}]")
84             set(CMAKE_REQUIRED_FLAGS "${${CXXFLAGSVAR}} ${_testflag}")
85             # make a valid variable name from the flag string: replace all non-alphanumerical chars
86             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_VARIABLE "CXX_FLAG_${_testflag}")
87             check_cxx_source_compiles("${SOURCE}" ${COMPILE_VARIABLE})
88             if(${${COMPILE_VARIABLE}})
89                 set(${VARIABLE}_FLAG "${_testflag}" CACHE INTERNAL "${DESCRIPTION}")
90                 set(${VARIABLE} 1 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
91                 break()
92             else()
93                 set(${VARIABLE} 0 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
94             endif()
95         endforeach()
96     ENDIF()
97
98     IF (${VARIABLE})
99         SET (${CXXFLAGSVAR} "${${CXXFLAGSVAR}} ${${VARIABLE}_FLAG}" PARENT_SCOPE)
100     ENDIF ()
101
102 ENDFUNCTION(GMX_FIND_CXXFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CXXFLAGSVAR)
103