Update copyright statements and change license to LGPL
[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 # Currently supported are only compilers that accept "-dumpversion" argument:
37 # gcc, Intel Compiler (on Linux and Mac OS), Open64, EkoPath.
38 #
39 # C_COMPILER_VERSION    - version string of the current C compiler (CMAKE_C_COMPILER)
40 # CXX_COMPILER_VERSION  - version string of the current C++ compiler (CMAKE_CXX_COMPILER)
41 #
42 macro(get_compiler_version)
43     if(NOT C_COMPILER_VERSION)
44         execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
45             RESULT_VARIABLE _cc_dumpversion_res
46             OUTPUT_VARIABLE _cc_dumpversion_out
47             ERROR_VARIABLE  _cc_dumpversion_err
48             OUTPUT_STRIP_TRAILING_WHITESPACE)
49
50         if (${_cc_dumpversion_res} EQUAL 0)
51             SET(C_COMPILER_VERSION ${_cc_dumpversion_out}
52                 CACHE STRING "C compiler version string" FORCE)
53         else ()
54             SET(C_COMPILER_VERSION ""
55                 CACHE STRING "C compiler version string not available" FORCE)
56         endif ()
57     endif()
58
59     if(NOT CXX_COMPILER_VERSION AND CMAKE_CXX_COMPILER_LOADED)
60         execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
61             RESULT_VARIABLE _cxx_dumpversion_res
62             OUTPUT_VARIABLE _cxx_dumpversion_out
63             ERROR_VARIABLE  _cxx_dumpversion_err
64             OUTPUT_STRIP_TRAILING_WHITESPACE)
65
66         if (${_cxx_dumpversion_res} EQUAL 0)
67             SET(CXX_COMPILER_VERSION ${_cxx_dumpversion_out}
68                 CACHE STRING "C++ compiler version string" FORCE)
69         else ()
70             SET(CXX_COMPILER_VERSION ""
71                 CACHE STRING "C++ compiler version string not available" FORCE)
72         endif ()
73     endif ()
74
75     if (NOT "${C_COMPILER_VERSION}" STREQUAL "${CXX_COMPILER_VERSION}" AND CMAKE_CXX_COMPILER_LOADED)
76         message(WARNING "The version string of the C and C++ compilers does not match!")
77     endif ()
78
79     mark_as_advanced(C_COMPILER_VERSION CXX_COMPILER_VERSION)
80 endmacro()
81
82 # This macro attempts to get a reasonable version string for a compiler,
83 # and also extracts compiler flags.
84 #
85 # Parameters:
86 #   LANGUAGE       - C or CXX, the compiler to check for
87 #   BUILD_COMPILER - [output variable] string with compiler path, ID and
88 #                    some compiler-provided information
89 #   BUILD_FLAGS    - [output variable] flags for the compiler
90 #
91 macro(get_compiler_info LANGUAGE BUILD_COMPILER BUILD_FLAGS)
92     execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER} --version
93         RESULT_VARIABLE _exec_result
94         OUTPUT_VARIABLE _compiler_version
95         ERROR_VARIABLE  _compiler_version)
96     # Try executing just the compiler command --version failed
97     if(_exec_result)
98         execute_process(COMMAND ${CMAKE_${LANGUAGE}_COMPILER}
99             RESULT_VARIABLE _exec_result
100             OUTPUT_VARIABLE _compiler_version
101             ERROR_VARIABLE  _compiler_version)
102     endif()
103     if(NOT "${_compiler_version}" STREQUAL "")
104         string(REGEX MATCH "[^\n]+" _compiler_version "${_compiler_version}")
105     endif()
106
107     set(${BUILD_COMPILER}
108         "${CMAKE_${LANGUAGE}_COMPILER} ${CMAKE_${LANGUAGE}_COMPILER_ID} ${_compiler_version}")
109     set(_build_flags "${CMAKE_${LANGUAGE}_FLAGS}")
110     string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
111     set(_build_flags "${_build_flags} ${CMAKE_${LANGUAGE}_FLAGS_${_build_type}}")
112     set(${BUILD_FLAGS} ${_build_flags})
113 endmacro()