8c3b27f6967ad5720a2d4f14c53d97cabe6dc91a
[alexxy/gromacs.git] / cmake / gmxGetCompilerInfo.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012, 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 #
45 # C_COMPILER_VERSION    - version string of the current C compiler (CMAKE_C_COMPILER)
46 # CXX_COMPILER_VERSION  - version string of the current C++ compiler (CMAKE_CXX_COMPILER)
47 #
48 macro(get_compiler_version)
49     if(NOT C_COMPILER_VERSION)
50         set(_cc_dumpversion_res 0)
51         if (DEFINED xCMAKE_C_COMPILER_VERSION AND CMAKE_VERSION VERSION_GREATER 2.8.8)
52             set(_cc_version ${CMAKE_C_COMPILER_VERSION})
53         else()
54             execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
55                 RESULT_VARIABLE _cc_dumpversion_res
56                 OUTPUT_VARIABLE _cc_version
57                 OUTPUT_STRIP_TRAILING_WHITESPACE)
58         endif()
59
60         if (${_cc_dumpversion_res} EQUAL 0)
61             SET(C_COMPILER_VERSION ${_cc_version}
62                 CACHE STRING "C compiler version" FORCE)
63         else ()
64             SET(C_COMPILER_VERSION ""
65                 CACHE STRING "C compiler version not available" FORCE)
66         endif ()
67     endif()
68     message("CMAKE_C_COMPILER_VERSION: ${CMAKE_C_COMPILER_VERSION}")
69     message("C_COMPILER_VERSION: ${C_COMPILER_VERSION}")
70
71
72     if(NOT CXX_COMPILER_VERSION AND CMAKE_CXX_COMPILER_LOADED)
73         set(_cxx_dumpversion_res 0)
74         if (DEFINED CMAKE_CXX_COMPILER_VERSION AND CMAKE_VERSION VERSION_GREATER 2.8.8)
75             set(_cxx_version ${CMAKE_CXX_COMPILER_VERSION})
76         else()
77             execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
78                 RESULT_VARIABLE _cxx_dumpversion_res
79                 OUTPUT_VARIABLE _cxx_version
80                 OUTPUT_STRIP_TRAILING_WHITESPACE)
81         endif()
82
83         if (${_cxx_dumpversion_res} EQUAL 0)
84             SET(CXX_COMPILER_VERSION ${_cxx_version}
85                 CACHE STRING "C++ compiler version string" FORCE)
86         else ()
87             SET(CXX_COMPILER_VERSION ""
88                 CACHE STRING "C++ compiler version string not available" FORCE)
89         endif ()
90     endif ()
91
92     if (NOT "${C_COMPILER_VERSION}" STREQUAL "${CXX_COMPILER_VERSION}" AND CMAKE_CXX_COMPILER_LOADED)
93         message(WARNING "The version of the C and C++ compilers does not match. Note that mixing different C/C++ compilers can cause problems!")
94     endif ()
95
96     mark_as_advanced(C_COMPILER_VERSION CXX_COMPILER_VERSION)
97 endmacro()
98
99 # This macro attempts to get a reasonable version string for a compiler,
100 # and also extracts compiler flags.
101 #
102 # Parameters:
103 #   LANGUAGE       - C or CXX, the compiler to check for
104 #   BUILD_COMPILER - [output variable] string with compiler path, ID and
105 #                    some compiler-provided information
106 #   BUILD_FLAGS    - [output variable] flags for the compiler
107 #
108 macro(get_compiler_info LANGUAGE BUILD_COMPILER BUILD_FLAGS)
109     execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER} --version
110         RESULT_VARIABLE _exec_result
111         OUTPUT_VARIABLE _compiler_version
112         ERROR_VARIABLE  _compiler_version)
113     # Try executing just the compiler command --version failed
114     if(_exec_result)
115         execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER}
116             RESULT_VARIABLE _exec_result
117             OUTPUT_VARIABLE _compiler_version
118             ERROR_VARIABLE  _compiler_version)
119     endif()
120     if(NOT "${_compiler_version}" STREQUAL "")
121         string(REGEX MATCH "[^\n]+" _compiler_version "${_compiler_version}")
122     endif()
123
124     set(${BUILD_COMPILER}
125         "${CMAKE_${LANGUAGE}_COMPILER} ${CMAKE_${LANGUAGE}_COMPILER_ID} ${_compiler_version}")
126     set(_build_flags "${CMAKE_${LANGUAGE}_FLAGS}")
127     string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
128     set(_build_flags "${_build_flags} ${CMAKE_${LANGUAGE}_FLAGS_${_build_type}}")
129     set(${BUILD_FLAGS} ${_build_flags})
130 endmacro()