Fix malformed CUDA version macro check
[alexxy/gromacs.git] / cmake / gmxGetCompilerInfo.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013, 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 # This macro attempts to parse the version string of the C compiler in use.
36 # With CMake 2.8.9 CMake provides a CMAKE_[C|CXX]_COMPILER_VERSION variable
37 # so we will use that if available.
38 #
39 # Currently supported are:
40 # - with cmake >2.8.8 all compilers supported by CMake
41 # - with cmake <=2.8.8: compilers that accept "-dumpversion" argument:
42 #   gcc, Intel Compiler (on Linux and Mac OS), Open64, EkoPath, clang
43 #   (and probably other gcc-compatible compilers).
44 # - with cmake <=2.8.8: xlC is not supported (it does not take -dumpversion,
45 #   but fortunately so far GROMACS never needs to know the version number)
46 #
47 # C_COMPILER_VERSION    - version string of the current C compiler (CMAKE_C_COMPILER)
48 # CXX_COMPILER_VERSION  - version string of the current C++ compiler (CMAKE_CXX_COMPILER)
49 #
50 macro(get_compiler_version)
51     if(NOT C_COMPILER_VERSION)
52         set(_cc_dumpversion_res 0)
53         if (DEFINED CMAKE_C_COMPILER_VERSION AND CMAKE_VERSION VERSION_GREATER 2.8.8)
54             set(_cc_version ${CMAKE_C_COMPILER_VERSION})
55         elseif (CMAKE_C_COMPILER_ID MATCHES "XL")
56             set(_cc_dumpversion_res 1)
57         else()
58             execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
59                 RESULT_VARIABLE _cc_dumpversion_res
60                 OUTPUT_VARIABLE _cc_version
61                 OUTPUT_STRIP_TRAILING_WHITESPACE)
62         endif()
63
64         if (${_cc_dumpversion_res} EQUAL 0)
65             SET(C_COMPILER_VERSION ${_cc_version}
66                 CACHE STRING "C compiler version" FORCE)
67         else ()
68             SET(C_COMPILER_VERSION ""
69                 CACHE STRING "C compiler version not available" FORCE)
70         endif ()
71     endif()
72
73     if(NOT CXX_COMPILER_VERSION AND CMAKE_CXX_COMPILER_LOADED)
74         set(_cxx_dumpversion_res 0)
75         if (DEFINED CMAKE_CXX_COMPILER_VERSION AND CMAKE_VERSION VERSION_GREATER 2.8.8)
76             set(_cxx_version ${CMAKE_CXX_COMPILER_VERSION})
77         elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
78             set(_cxx_dumpversion_res 1)
79         else()
80             execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
81                 RESULT_VARIABLE _cxx_dumpversion_res
82                 OUTPUT_VARIABLE _cxx_version
83                 OUTPUT_STRIP_TRAILING_WHITESPACE)
84         endif()
85
86         if (${_cxx_dumpversion_res} EQUAL 0)
87             SET(CXX_COMPILER_VERSION ${_cxx_version}
88                 CACHE STRING "C++ compiler version string" FORCE)
89         else ()
90             SET(CXX_COMPILER_VERSION ""
91                 CACHE STRING "C++ compiler version string not available" FORCE)
92         endif ()
93     endif ()
94
95     if (NOT "${C_COMPILER_VERSION}" STREQUAL "${CXX_COMPILER_VERSION}" AND CMAKE_CXX_COMPILER_LOADED)
96         message(WARNING "The version of the C and C++ compilers does not match. Note that mixing different C/C++ compilers can cause problems!")
97     endif ()
98
99     mark_as_advanced(C_COMPILER_VERSION CXX_COMPILER_VERSION)
100 endmacro()
101
102 # This macro attempts to get a reasonable version string for a compiler,
103 # and also extracts compiler flags.
104 #
105 # Parameters:
106 #   LANGUAGE       - C or CXX, the compiler to check for
107 #   BUILD_COMPILER - [output variable] string with compiler path, ID and
108 #                    some compiler-provided information
109 #   BUILD_FLAGS    - [output variable] flags for the compiler
110 #
111 macro(get_compiler_info LANGUAGE BUILD_COMPILER BUILD_FLAGS)
112     if (CMAKE_C_COMPILER_ID MATCHES "XL")
113         set(_flag_to_query_version "-qversion")
114     else()
115         set(_flag_to_query_version "--version")
116     endif()
117     execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER} ${_flag_to_query_version}
118         RESULT_VARIABLE _exec_result
119         OUTPUT_VARIABLE _compiler_version
120         ERROR_VARIABLE  _compiler_version)
121
122     if(_exec_result)
123         # Try executing just the compiler command, since --version failed
124         execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER}
125             RESULT_VARIABLE _exec_result
126             OUTPUT_VARIABLE _compiler_version
127             ERROR_VARIABLE  _compiler_version)
128     endif()
129     if(NOT "${_compiler_version}" STREQUAL "")
130         string(REGEX MATCH "[^\n]+" _compiler_version "${_compiler_version}")
131     endif()
132
133     set(${BUILD_COMPILER}
134         "${CMAKE_${LANGUAGE}_COMPILER} ${CMAKE_${LANGUAGE}_COMPILER_ID} ${_compiler_version}")
135     set(_build_flags "${CMAKE_${LANGUAGE}_FLAGS}")
136     string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
137     set(_build_flags "${_build_flags} ${CMAKE_${LANGUAGE}_FLAGS_${_build_type}}")
138     set(${BUILD_FLAGS} ${_build_flags})
139 endmacro()