CMakeDetermineVSServicePack.cmake 6.41 KB
Newer Older
1 2 3 4
#.rst:
# CMakeDetermineVSServicePack
# ---------------------------
#
5
# Deprecated.  Do not use.
6
#
7 8 9
# The functionality of this module has been superseded by the
# :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable that contains
# the compiler version number.
10
#
11
# Determine the Visual Studio service pack of the 'cl' in use.
12
#
13
# Usage::
14 15 16 17 18 19 20 21 22
#
#   if(MSVC)
#     include(CMakeDetermineVSServicePack)
#     DetermineVSServicePack( my_service_pack )
#     if( my_service_pack )
#       message(STATUS "Detected: ${my_service_pack}")
#     endif()
#   endif()
#
23
# Function DetermineVSServicePack sets the given variable to one of the
24
# following values or an empty string if unknown::
25 26 27 28
#
#   vc80, vc80sp1
#   vc90, vc90sp1
#   vc100, vc100sp1
29
#   vc110, vc110sp1, vc110sp2, vc110sp3, vc110sp4
30 31

#=============================================================================
32
# Copyright 2009-2013 Kitware, Inc.
33
# Copyright 2009-2010 Philip Lowman <philip@yhbt.com>
34
# Copyright 2010-2011 Aaron C. meadows <cmake@shadowguarddev.com>
35 36 37 38 39 40 41 42
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
43
# (To distribute this file outside of CMake, substitute the full
44 45
#  License text for the above reference.)

46 47 48 49 50 51 52
if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.8)
  message(DEPRECATION
    "This module is deprecated and should not be used.  "
    "Use the CMAKE_<LANG>_COMPILER_VERSION variable instead."
    )
endif()

53 54 55 56 57 58 59 60 61 62 63
# [INTERNAL]
# Please do not call this function directly
function(_DetermineVSServicePackFromCompiler _OUT_VAR _cl_version)
   if    (${_cl_version} VERSION_EQUAL "14.00.50727.42")
       set(_version "vc80")
   elseif(${_cl_version} VERSION_EQUAL "14.00.50727.762")
       set(_version "vc80sp1")
   elseif(${_cl_version} VERSION_EQUAL "15.00.21022.08")
       set(_version "vc90")
   elseif(${_cl_version} VERSION_EQUAL "15.00.30729.01")
       set(_version "vc90sp1")
64 65
   elseif(${_cl_version} VERSION_EQUAL "16.00.30319.01")
       set(_version "vc100")
66 67
   elseif(${_cl_version} VERSION_EQUAL "16.00.40219.01")
       set(_version "vc100sp1")
68 69
   elseif(${_cl_version} VERSION_EQUAL "17.00.50727.1")
       set(_version "vc110")
70 71 72 73
   elseif(${_cl_version} VERSION_EQUAL "17.00.51106.1")
       set(_version "vc110sp1")
   elseif(${_cl_version} VERSION_EQUAL "17.00.60315.1")
       set(_version "vc110sp2")
74 75
   elseif(${_cl_version} VERSION_EQUAL "17.00.60610.1")
       set(_version "vc110sp3")
76 77
   elseif(${_cl_version} VERSION_EQUAL "17.00.61030")
       set(_version "vc110sp4")
78 79 80 81 82 83
   else()
       set(_version "")
   endif()
   set(${_OUT_VAR} ${_version} PARENT_SCOPE)
endfunction()

84 85 86 87 88 89 90 91 92 93 94 95

############################################################
# [INTERNAL]
# Please do not call this function directly
function(_DetermineVSServicePack_FastCheckVersionWithCompiler _SUCCESS_VAR  _VERSION_VAR)
    if(EXISTS ${CMAKE_CXX_COMPILER})
      execute_process(
          COMMAND ${CMAKE_CXX_COMPILER} /?
          ERROR_VARIABLE _output
          OUTPUT_QUIET
        )

96 97 98 99
      if(_output MATCHES "Compiler Version (([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\.([0-9]+))?)")
        set(_cl_version ${CMAKE_MATCH_1})
        set(_major ${CMAKE_MATCH_2})
        set(_minor ${CMAKE_MATCH_3})
100 101 102
        if("${_major}${_minor}" STREQUAL "${MSVC_VERSION}")
          set(${_SUCCESS_VAR} true PARENT_SCOPE)
          set(${_VERSION_VAR} ${_cl_version} PARENT_SCOPE)
103
        endif()
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      endif()
    endif()
endfunction()

############################################################
# [INTERNAL]
# Please do not call this function directly
function(_DetermineVSServicePack_CheckVersionWithTryCompile _SUCCESS_VAR  _VERSION_VAR)
    file(WRITE "${CMAKE_BINARY_DIR}/return0.cc"
      "int main() { return 0; }\n")

    try_compile(
      _CompileResult
      "${CMAKE_BINARY_DIR}"
      "${CMAKE_BINARY_DIR}/return0.cc"
      OUTPUT_VARIABLE _output
      COPY_FILE "${CMAKE_BINARY_DIR}/return0.cc")

    file(REMOVE "${CMAKE_BINARY_DIR}/return0.cc")

124
    if(_output MATCHES "Compiler Version (([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\.([0-9]+))?)")
125
      set(${_SUCCESS_VAR} true PARENT_SCOPE)
126
      set(${_VERSION_VAR} "${CMAKE_MATCH_1}" PARENT_SCOPE)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    endif()
endfunction()

############################################################
# [INTERNAL]
# Please do not call this function directly
function(_DetermineVSServicePack_CheckVersionWithTryRun _SUCCESS_VAR  _VERSION_VAR)
    file(WRITE "${CMAKE_BINARY_DIR}/return0.cc"
        "#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")

    try_run(
        _RunResult
        _CompileResult
        "${CMAKE_BINARY_DIR}"
        "${CMAKE_BINARY_DIR}/return0.cc"
        RUN_OUTPUT_VARIABLE  _runoutput
        )

    file(REMOVE "${CMAKE_BINARY_DIR}/return0.cc")

    string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+.[0-9]+"
        _cl_version "${_runoutput}")

    if(_cl_version)
      set(${_SUCCESS_VAR} true PARENT_SCOPE)
      set(${_VERSION_VAR} ${_cl_version} PARENT_SCOPE)
    endif()
endfunction()


157 158 159 160 161
#
# A function to call to determine the Visual Studio service pack
# in use.  See documentation above.
function(DetermineVSServicePack _pack)
    if(NOT DETERMINED_VS_SERVICE_PACK OR NOT ${_pack})
162 163 164 165 166 167 168 169 170 171 172

        _DetermineVSServicePack_FastCheckVersionWithCompiler(DETERMINED_VS_SERVICE_PACK _cl_version)
        if(NOT DETERMINED_VS_SERVICE_PACK)
            _DetermineVSServicePack_CheckVersionWithTryCompile(DETERMINED_VS_SERVICE_PACK _cl_version)
            if(NOT DETERMINED_VS_SERVICE_PACK)
                _DetermineVSServicePack_CheckVersionWithTryRun(DETERMINED_VS_SERVICE_PACK _cl_version)
            endif()
        endif()

        if(DETERMINED_VS_SERVICE_PACK)

173 174
            if(_cl_version)
                # Call helper function to determine VS version
175
                _DetermineVSServicePackFromCompiler(_sp "${_cl_version}")
176 177 178 179 180 181 182 183 184
                if(_sp)
                    set(${_pack} ${_sp} CACHE INTERNAL
                        "The Visual Studio Release with Service Pack")
                endif()
            endif()
        endif()
    endif()
endfunction()