Merge branch 'release-4.0'
[kiteware-cmake.git] / Modules / CMakeDetermineVSServicePack.cmake
blobed1d0c1ca735ea709fd47325201771715e610a4c
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 CMakeDetermineVSServicePack
6 ---------------------------
8 .. deprecated:: 3.0
10   Do not use.
12 The functionality of this module has been superseded by the
13 :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable that contains
14 the compiler version number.
16 Determine the Visual Studio service pack of the 'cl' in use.
18 Usage:
20 .. code-block:: cmake
22   if(MSVC)
23     include(CMakeDetermineVSServicePack)
24     DetermineVSServicePack(my_service_pack)
25     if(my_service_pack)
26       message(STATUS "Detected: ${my_service_pack}")
27     endif()
28   endif()
30 Function DetermineVSServicePack sets the given variable to one of the
31 following values or an empty string if unknown::
33   vc80, vc80sp1
34   vc90, vc90sp1
35   vc100, vc100sp1
36   vc110, vc110sp1, vc110sp2, vc110sp3, vc110sp4
37 #]=======================================================================]
39 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.8)
40   message(DEPRECATION
41     "This module is deprecated and should not be used.  "
42     "Use the CMAKE_<LANG>_COMPILER_VERSION variable instead."
43     )
44 endif()
46 # [INTERNAL]
47 # Please do not call this function directly
48 function(_DetermineVSServicePackFromCompiler _OUT_VAR _cl_version)
49   if    (${_cl_version} VERSION_EQUAL "14.00.50727.42")
50     set(_version "vc80")
51   elseif(${_cl_version} VERSION_EQUAL "14.00.50727.762")
52     set(_version "vc80sp1")
53   elseif(${_cl_version} VERSION_EQUAL "15.00.21022.08")
54     set(_version "vc90")
55   elseif(${_cl_version} VERSION_EQUAL "15.00.30729.01")
56     set(_version "vc90sp1")
57   elseif(${_cl_version} VERSION_EQUAL "16.00.30319.01")
58     set(_version "vc100")
59   elseif(${_cl_version} VERSION_EQUAL "16.00.40219.01")
60     set(_version "vc100sp1")
61   elseif(${_cl_version} VERSION_EQUAL "17.00.50727.1")
62     set(_version "vc110")
63   elseif(${_cl_version} VERSION_EQUAL "17.00.51106.1")
64     set(_version "vc110sp1")
65   elseif(${_cl_version} VERSION_EQUAL "17.00.60315.1")
66     set(_version "vc110sp2")
67   elseif(${_cl_version} VERSION_EQUAL "17.00.60610.1")
68     set(_version "vc110sp3")
69   elseif(${_cl_version} VERSION_EQUAL "17.00.61030")
70     set(_version "vc110sp4")
71   else()
72     set(_version "")
73   endif()
74   set(${_OUT_VAR} ${_version} PARENT_SCOPE)
75 endfunction()
78 ############################################################
79 # [INTERNAL]
80 # Please do not call this function directly
81 function(_DetermineVSServicePack_FastCheckVersionWithCompiler _SUCCESS_VAR  _VERSION_VAR)
82     if(EXISTS ${CMAKE_CXX_COMPILER})
83       execute_process(
84           COMMAND ${CMAKE_CXX_COMPILER} -?
85           ERROR_VARIABLE _output
86           OUTPUT_QUIET
87         )
89       if(_output MATCHES "Compiler Version (([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\.([0-9]+))?)")
90         set(_cl_version ${CMAKE_MATCH_1})
91         set(_major ${CMAKE_MATCH_2})
92         set(_minor ${CMAKE_MATCH_3})
93         if("${_major}${_minor}" STREQUAL "${MSVC_VERSION}")
94           set(${_SUCCESS_VAR} true PARENT_SCOPE)
95           set(${_VERSION_VAR} ${_cl_version} PARENT_SCOPE)
96         endif()
97       endif()
98     endif()
99 endfunction()
101 ############################################################
102 # [INTERNAL]
103 # Please do not call this function directly
104 function(_DetermineVSServicePack_CheckVersionWithTryCompile _SUCCESS_VAR  _VERSION_VAR)
105     file(WRITE "${CMAKE_BINARY_DIR}/return0.cc"
106       "int main() { return 0; }\n")
108     try_compile(
109       _CompileResult
110       SOURCES "${CMAKE_BINARY_DIR}/return0.cc"
111       OUTPUT_VARIABLE _output
112       COPY_FILE "${CMAKE_BINARY_DIR}/return0.cc")
114     file(REMOVE "${CMAKE_BINARY_DIR}/return0.cc")
116     if(_output MATCHES "Compiler Version (([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\.([0-9]+))?)")
117       set(${_SUCCESS_VAR} true PARENT_SCOPE)
118       set(${_VERSION_VAR} "${CMAKE_MATCH_1}" PARENT_SCOPE)
119     endif()
120 endfunction()
122 ############################################################
123 # [INTERNAL]
124 # Please do not call this function directly
125 function(_DetermineVSServicePack_CheckVersionWithTryRun _SUCCESS_VAR  _VERSION_VAR)
126     file(WRITE "${CMAKE_BINARY_DIR}/return0.cc"
127         "#include <stdio.h>\n\nconst unsigned int CompilerVersion=_MSC_FULL_VER;\n\nint main(int argc, char* argv[])\n{\n  int M( CompilerVersion/10000000);\n  int m((CompilerVersion%10000000)/100000);\n  int b(CompilerVersion%100000);\n\n  printf(\"%d.%02d.%05d.01\",M,m,b);\n return 0;\n}\n")
129     try_run(
130         _RunResult
131         _CompileResult
132         SOURCES "${CMAKE_BINARY_DIR}/return0.cc"
133         RUN_OUTPUT_VARIABLE  _runoutput
134         )
136     file(REMOVE "${CMAKE_BINARY_DIR}/return0.cc")
138     string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+.[0-9]+"
139         _cl_version "${_runoutput}")
141     if(_cl_version)
142       set(${_SUCCESS_VAR} true PARENT_SCOPE)
143       set(${_VERSION_VAR} ${_cl_version} PARENT_SCOPE)
144     endif()
145 endfunction()
149 # A function to call to determine the Visual Studio service pack
150 # in use.  See documentation above.
151 function(DetermineVSServicePack _pack)
152     if(NOT DETERMINED_VS_SERVICE_PACK OR NOT ${_pack})
154         _DetermineVSServicePack_FastCheckVersionWithCompiler(DETERMINED_VS_SERVICE_PACK _cl_version)
155         if(NOT DETERMINED_VS_SERVICE_PACK)
156             _DetermineVSServicePack_CheckVersionWithTryCompile(DETERMINED_VS_SERVICE_PACK _cl_version)
157             if(NOT DETERMINED_VS_SERVICE_PACK)
158                 _DetermineVSServicePack_CheckVersionWithTryRun(DETERMINED_VS_SERVICE_PACK _cl_version)
159             endif()
160         endif()
162         if(DETERMINED_VS_SERVICE_PACK)
164             if(_cl_version)
165                 # Call helper function to determine VS version
166                 _DetermineVSServicePackFromCompiler(_sp "${_cl_version}")
167                 if(_sp)
168                     set(${_pack} ${_sp} CACHE INTERNAL
169                         "The Visual Studio Release with Service Pack")
170                 endif()
171             endif()
172         endif()
173     endif()
174 endfunction()