Add initial support for python bindings
[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 valid variable names from the flag string: replace all non-alphanumerical chars
51             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_FLAG_VARIABLE "C_FLAG_${_testflag}")
52             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_SIMD_VARIABLE "C_SIMD_COMPILES_FLAG_${_testflag}")
53
54             # Check that the flag itself is fine, and that is does not generate warnings either
55             check_c_compiler_flag("${_testflag}" ${COMPILE_FLAG_VARIABLE})
56
57             if(${COMPILE_FLAG_VARIABLE})
58                 # Check that we can compile SIMD source (this does not catch warnings)
59                 check_c_source_compiles("${SOURCE}" ${COMPILE_SIMD_VARIABLE})
60             endif(${COMPILE_FLAG_VARIABLE})
61
62             if(${COMPILE_FLAG_VARIABLE} AND ${COMPILE_SIMD_VARIABLE})
63                 set(${VARIABLE}_FLAG "${_testflag}" CACHE INTERNAL "${DESCRIPTION}")
64                 set(${VARIABLE} 1 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
65                 break()
66             else()
67                 set(${VARIABLE} 0 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
68             endif()
69         endforeach()
70     ENDIF()
71
72     IF (${VARIABLE})
73         SET (${CFLAGSVAR} "${${CFLAGSVAR}} ${${VARIABLE}_FLAG}" PARENT_SCOPE)
74     ENDIF ()
75 ENDFUNCTION(GMX_FIND_CFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CFLAGSVAR)
76
77
78 # Helper routine to find flag (from list) to compile a specific C++ source.
79 # VARIABLE            This will be set when we have found a flag that works
80 # DESCRIPTION         Text string describing what flag we are trying to find
81 # SOURCE              Source code to test
82 #                     The compiler is chosen based on the extension of this file
83 # FLAGSVAR            Variable (string) to which we should add the correct flag
84 # Args 5 through N    Multiple strings with optimization flags to test
85 FUNCTION(GMX_FIND_CXXFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CXXFLAGSVAR)
86
87     IF(NOT DEFINED ${VARIABLE})
88         # Insert a blank element last in the list (try without any flags too)
89         # This must come last, since some compilers (Intel) might try to
90         # emulate AVX instructions with SSE4.1 otherwise.
91         foreach(_testflag ${ARGN} "")
92             message(STATUS "Try ${DESCRIPTION} = [${_testflag}]")
93             set(CMAKE_REQUIRED_FLAGS "${${CXXFLAGSVAR}} ${_testflag}")
94             # make valid variable names from the flag string: replace all non-alphanumerical chars
95             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_FLAG_VARIABLE "CXX_FLAG_${_testflag}")
96             string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" COMPILE_SIMD_VARIABLE "CXX_SIMD_COMPILES_FLAG_${_testflag}")
97             
98             # Check that the flag itself is fine, and that is does not generate warnings either
99             check_cxx_compiler_flag("${_testflag}" ${COMPILE_FLAG_VARIABLE})
100
101             if(${COMPILE_FLAG_VARIABLE})
102                 # Check that we can compile SIMD source (this does not catch warnings)
103                 check_cxx_source_compiles("${SOURCE}" ${COMPILE_SIMD_VARIABLE})
104             endif(${COMPILE_FLAG_VARIABLE})
105
106             if(${COMPILE_FLAG_VARIABLE} AND ${COMPILE_SIMD_VARIABLE})
107                 set(${VARIABLE}_FLAG "${_testflag}" CACHE INTERNAL "${DESCRIPTION}")
108                 set(${VARIABLE} 1 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
109                 break()
110             else()
111                 set(${VARIABLE} 0 CACHE INTERNAL "Result of test for ${DESCRIPTION}" FORCE)
112             endif()
113         endforeach()
114     ENDIF()
115
116     IF (${VARIABLE})
117         SET (${CXXFLAGSVAR} "${${CXXFLAGSVAR}} ${${VARIABLE}_FLAG}" PARENT_SCOPE)
118     ENDIF ()
119
120 ENDFUNCTION(GMX_FIND_CXXFLAG_FOR_SOURCE VARIABLE DESCRIPTION SOURCE CXXFLAGSVAR)
121