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 # Currently supported are only compilers that accept "-dumpversion" argument:
3 # gcc, Intel Compiler (on Linux and Mac OS), Open64, EkoPath.
4 #
5 # C_COMPILER_VERSION    - version string of the current C compiler (CMAKE_C_COMPILER)
6 # CXX_COMPILER_VERSION  - version string of the current C++ compiler (CMAKE_CXX_COMPILER)
7 #
8 macro(get_compiler_version)
9     if(NOT C_COMPILER_VERSION)
10         execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
11             RESULT_VARIABLE _cc_dumpversion_res
12             OUTPUT_VARIABLE _cc_dumpversion_out
13             ERROR_VARIABLE  _cc_dumpversion_err
14             OUTPUT_STRIP_TRAILING_WHITESPACE)
15
16         if (${_cc_dumpversion_res} EQUAL 0)
17             SET(C_COMPILER_VERSION ${_cc_dumpversion_out}
18                 CACHE STRING "C compiler version string" FORCE)
19         else ()
20             SET(C_COMPILER_VERSION ""
21                 CACHE STRING "C compiler version string not available" FORCE)
22         endif ()
23     endif()
24
25     if(NOT CXX_COMPILER_VERSION AND CMAKE_CXX_COMPILER_LOADED)
26         execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
27             RESULT_VARIABLE _cxx_dumpversion_res
28             OUTPUT_VARIABLE _cxx_dumpversion_out
29             ERROR_VARIABLE  _cxx_dumpversion_err
30             OUTPUT_STRIP_TRAILING_WHITESPACE)
31
32         if (${_cxx_dumpversion_res} EQUAL 0)
33             SET(CXX_COMPILER_VERSION ${_cxx_dumpversion_out}
34                 CACHE STRING "C++ compiler version string" FORCE)
35         else ()
36             SET(CXX_COMPILER_VERSION ""
37                 CACHE STRING "C++ compiler version string not available" FORCE)
38         endif ()
39     endif ()
40
41     if (NOT "${C_COMPILER_VERSION}" STREQUAL "${CXX_COMPILER_VERSION}" AND CMAKE_CXX_COMPILER_LOADED)
42         message(WARNING "The version string of the C and C++ compilers does not match!")
43     endif ()
44
45     mark_as_advanced(C_COMPILER_VERSION CXX_COMPILER_VERSION)
46 endmacro()
47
48 # This macro attempts to get a reasonable version string for a compiler,
49 # and also extracts compiler flags.
50 #
51 # Parameters:
52 #   LANGUAGE       - C or CXX, the compiler to check for
53 #   BUILD_COMPILER - [output variable] string with compiler path, ID and
54 #                    some compiler-provided information
55 #   BUILD_FLAGS    - [output variable] flags for the compiler
56 #
57 macro(get_compiler_info LANGUAGE BUILD_COMPILER BUILD_FLAGS)
58     execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER} --version
59         RESULT_VARIABLE _exec_result
60         OUTPUT_VARIABLE _compiler_version
61         ERROR_VARIABLE  _compiler_version)
62     # Try executing just the compiler command --version failed
63     if(_exec_result)
64         execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER}
65             RESULT_VARIABLE _exec_result
66             OUTPUT_VARIABLE _compiler_version
67             ERROR_VARIABLE  _compiler_version)
68     endif()
69     if(NOT "${_compiler_version}" STREQUAL "")
70         string(REGEX MATCH "[^\n]+" _compiler_version "${_compiler_version}")
71     endif()
72
73     set(${BUILD_COMPILER}
74         "${CMAKE_${LANGUAGE}_COMPILER} ${CMAKE_${LANGUAGE}_COMPILER_ID} ${_compiler_version}")
75     set(_build_flags "${CMAKE_${LANGUAGE}_FLAGS}")
76     string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
77     set(_build_flags "${_build_flags} ${CMAKE_${LANGUAGE}_FLAGS_${_build_type}}")
78     set(${BUILD_FLAGS} ${_build_flags})
79 endmacro()