Merge release-4-6 into master
[alexxy/gromacs.git] / cmake / gmxGetCompilerInfo.cmake
1 # This macro attempts to parse the version string of the C compiler in use.
2 # With CMake 2.8.9 CMake provides a CMAKE_[C|CXX]_COMPILER_VERSION variable
3 # so we will use that if available.
4 #
5 # Currently supported are:
6 # - with cmake >2.8.8 all compilers supported by CMake
7 # - with cmake <=2.8.8: compilers that accept "-dumpversion" argument:
8 #   gcc, Intel Compiler (on Linux and Mac OS), Open64, EkoPath, clang
9 #   (and probably other gcc-compatible compilers).
10 #
11 # C_COMPILER_VERSION    - version string of the current C compiler (CMAKE_C_COMPILER)
12 # CXX_COMPILER_VERSION  - version string of the current C++ compiler (CMAKE_CXX_COMPILER)
13 #
14 macro(get_compiler_version)
15     if(NOT C_COMPILER_VERSION)
16         set(_cc_dumpversion_res 0)
17         if (DEFINED xCMAKE_C_COMPILER_VERSION AND CMAKE_VERSION VERSION_GREATER 2.8.8)
18             set(_cc_version ${CMAKE_C_COMPILER_VERSION})
19         else()
20             execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
21                 RESULT_VARIABLE _cc_dumpversion_res
22                 OUTPUT_VARIABLE _cc_version
23                 OUTPUT_STRIP_TRAILING_WHITESPACE)
24         endif()
25
26         if (${_cc_dumpversion_res} EQUAL 0)
27             SET(C_COMPILER_VERSION ${_cc_version}
28                 CACHE STRING "C compiler version" FORCE)
29         else ()
30             SET(C_COMPILER_VERSION ""
31                 CACHE STRING "C compiler version not available" FORCE)
32         endif ()
33     endif()
34
35     if(NOT CXX_COMPILER_VERSION AND CMAKE_CXX_COMPILER_LOADED)
36         set(_cxx_dumpversion_res 0)
37         if (DEFINED CMAKE_CXX_COMPILER_VERSION AND CMAKE_VERSION VERSION_GREATER 2.8.8)
38             set(_cxx_version ${CMAKE_CXX_COMPILER_VERSION})
39         else()
40             execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
41                 RESULT_VARIABLE _cxx_dumpversion_res
42                 OUTPUT_VARIABLE _cxx_version
43                 OUTPUT_STRIP_TRAILING_WHITESPACE)
44         endif()
45
46         if (${_cxx_dumpversion_res} EQUAL 0)
47             SET(CXX_COMPILER_VERSION ${_cxx_version}
48                 CACHE STRING "C++ compiler version string" FORCE)
49         else ()
50             SET(CXX_COMPILER_VERSION ""
51                 CACHE STRING "C++ compiler version string not available" FORCE)
52         endif ()
53     endif ()
54
55     if (NOT "${C_COMPILER_VERSION}" STREQUAL "${CXX_COMPILER_VERSION}" AND CMAKE_CXX_COMPILER_LOADED)
56         message(WARNING "The version of the C and C++ compilers does not match. Note that mixing different C/C++ compilers can cause problems!")
57     endif ()
58
59     mark_as_advanced(C_COMPILER_VERSION CXX_COMPILER_VERSION)
60 endmacro()
61
62 # This macro attempts to get a reasonable version string for a compiler,
63 # and also extracts compiler flags.
64 #
65 # Parameters:
66 #   LANGUAGE       - C or CXX, the compiler to check for
67 #   BUILD_COMPILER - [output variable] string with compiler path, ID and
68 #                    some compiler-provided information
69 #   BUILD_FLAGS    - [output variable] flags for the compiler
70 #
71 macro(get_compiler_info LANGUAGE BUILD_COMPILER BUILD_FLAGS)
72     execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER} --version
73         RESULT_VARIABLE _exec_result
74         OUTPUT_VARIABLE _compiler_version
75         ERROR_VARIABLE  _compiler_version)
76     # Try executing just the compiler command --version failed
77     if(_exec_result)
78         execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER}
79             RESULT_VARIABLE _exec_result
80             OUTPUT_VARIABLE _compiler_version
81             ERROR_VARIABLE  _compiler_version)
82     endif()
83     if(NOT "${_compiler_version}" STREQUAL "")
84         string(REGEX MATCH "[^\n]+" _compiler_version "${_compiler_version}")
85     endif()
86
87     set(${BUILD_COMPILER}
88         "${CMAKE_${LANGUAGE}_COMPILER} ${CMAKE_${LANGUAGE}_COMPILER_ID} ${_compiler_version}")
89     set(_build_flags "${CMAKE_${LANGUAGE}_FLAGS}")
90     string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
91     set(_build_flags "${_build_flags} ${CMAKE_${LANGUAGE}_FLAGS_${_build_type}}")
92     set(${BUILD_FLAGS} ${_build_flags})
93 endmacro()