CMakeDetermineVSServicePack.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #.rst:
  2. # CMakeDetermineVSServicePack
  3. # ---------------------------
  4. #
  5. # Deprecated. Do not use.
  6. #
  7. # The functionality of this module has been superseded by the
  8. # :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable that contains
  9. # the compiler version number.
  10. #
  11. # Determine the Visual Studio service pack of the 'cl' in use.
  12. #
  13. # Usage::
  14. #
  15. # if(MSVC)
  16. # include(CMakeDetermineVSServicePack)
  17. # DetermineVSServicePack( my_service_pack )
  18. # if( my_service_pack )
  19. # message(STATUS "Detected: ${my_service_pack}")
  20. # endif()
  21. # endif()
  22. #
  23. # Function DetermineVSServicePack sets the given variable to one of the
  24. # following values or an empty string if unknown::
  25. #
  26. # vc80, vc80sp1
  27. # vc90, vc90sp1
  28. # vc100, vc100sp1
  29. # vc110, vc110sp1, vc110sp2, vc110sp3, vc110sp4
  30. #=============================================================================
  31. # Copyright 2009-2013 Kitware, Inc.
  32. # Copyright 2009-2010 Philip Lowman <philip@yhbt.com>
  33. # Copyright 2010-2011 Aaron C. meadows <cmake@shadowguarddev.com>
  34. #
  35. # Distributed under the OSI-approved BSD License (the "License");
  36. # see accompanying file Copyright.txt for details.
  37. #
  38. # This software is distributed WITHOUT ANY WARRANTY; without even the
  39. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  40. # See the License for more information.
  41. #=============================================================================
  42. # (To distribute this file outside of CMake, substitute the full
  43. # License text for the above reference.)
  44. if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.8)
  45. message(DEPRECATION
  46. "This module is deprecated and should not be used. "
  47. "Use the CMAKE_<LANG>_COMPILER_VERSION variable instead."
  48. )
  49. endif()
  50. # [INTERNAL]
  51. # Please do not call this function directly
  52. function(_DetermineVSServicePackFromCompiler _OUT_VAR _cl_version)
  53. if (${_cl_version} VERSION_EQUAL "14.00.50727.42")
  54. set(_version "vc80")
  55. elseif(${_cl_version} VERSION_EQUAL "14.00.50727.762")
  56. set(_version "vc80sp1")
  57. elseif(${_cl_version} VERSION_EQUAL "15.00.21022.08")
  58. set(_version "vc90")
  59. elseif(${_cl_version} VERSION_EQUAL "15.00.30729.01")
  60. set(_version "vc90sp1")
  61. elseif(${_cl_version} VERSION_EQUAL "16.00.30319.01")
  62. set(_version "vc100")
  63. elseif(${_cl_version} VERSION_EQUAL "16.00.40219.01")
  64. set(_version "vc100sp1")
  65. elseif(${_cl_version} VERSION_EQUAL "17.00.50727.1")
  66. set(_version "vc110")
  67. elseif(${_cl_version} VERSION_EQUAL "17.00.51106.1")
  68. set(_version "vc110sp1")
  69. elseif(${_cl_version} VERSION_EQUAL "17.00.60315.1")
  70. set(_version "vc110sp2")
  71. elseif(${_cl_version} VERSION_EQUAL "17.00.60610.1")
  72. set(_version "vc110sp3")
  73. elseif(${_cl_version} VERSION_EQUAL "17.00.61030")
  74. set(_version "vc110sp4")
  75. else()
  76. set(_version "")
  77. endif()
  78. set(${_OUT_VAR} ${_version} PARENT_SCOPE)
  79. endfunction()
  80. ############################################################
  81. # [INTERNAL]
  82. # Please do not call this function directly
  83. function(_DetermineVSServicePack_FastCheckVersionWithCompiler _SUCCESS_VAR _VERSION_VAR)
  84. if(EXISTS ${CMAKE_CXX_COMPILER})
  85. execute_process(
  86. COMMAND ${CMAKE_CXX_COMPILER} /?
  87. ERROR_VARIABLE _output
  88. OUTPUT_QUIET
  89. )
  90. if(_output MATCHES "Compiler Version (([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\.([0-9]+))?)")
  91. set(_cl_version ${CMAKE_MATCH_1})
  92. set(_major ${CMAKE_MATCH_2})
  93. set(_minor ${CMAKE_MATCH_3})
  94. if("${_major}${_minor}" STREQUAL "${MSVC_VERSION}")
  95. set(${_SUCCESS_VAR} true PARENT_SCOPE)
  96. set(${_VERSION_VAR} ${_cl_version} PARENT_SCOPE)
  97. endif()
  98. endif()
  99. endif()
  100. 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")
  107. try_compile(
  108. _CompileResult
  109. "${CMAKE_BINARY_DIR}"
  110. "${CMAKE_BINARY_DIR}/return0.cc"
  111. OUTPUT_VARIABLE _output
  112. COPY_FILE "${CMAKE_BINARY_DIR}/return0.cc")
  113. file(REMOVE "${CMAKE_BINARY_DIR}/return0.cc")
  114. if(_output MATCHES "Compiler Version (([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\.([0-9]+))?)")
  115. set(${_SUCCESS_VAR} true PARENT_SCOPE)
  116. set(${_VERSION_VAR} "${CMAKE_MATCH_1}" PARENT_SCOPE)
  117. endif()
  118. endfunction()
  119. ############################################################
  120. # [INTERNAL]
  121. # Please do not call this function directly
  122. function(_DetermineVSServicePack_CheckVersionWithTryRun _SUCCESS_VAR _VERSION_VAR)
  123. file(WRITE "${CMAKE_BINARY_DIR}/return0.cc"
  124. "#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")
  125. try_run(
  126. _RunResult
  127. _CompileResult
  128. "${CMAKE_BINARY_DIR}"
  129. "${CMAKE_BINARY_DIR}/return0.cc"
  130. RUN_OUTPUT_VARIABLE _runoutput
  131. )
  132. file(REMOVE "${CMAKE_BINARY_DIR}/return0.cc")
  133. string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+.[0-9]+"
  134. _cl_version "${_runoutput}")
  135. if(_cl_version)
  136. set(${_SUCCESS_VAR} true PARENT_SCOPE)
  137. set(${_VERSION_VAR} ${_cl_version} PARENT_SCOPE)
  138. endif()
  139. endfunction()
  140. #
  141. # A function to call to determine the Visual Studio service pack
  142. # in use. See documentation above.
  143. function(DetermineVSServicePack _pack)
  144. if(NOT DETERMINED_VS_SERVICE_PACK OR NOT ${_pack})
  145. _DetermineVSServicePack_FastCheckVersionWithCompiler(DETERMINED_VS_SERVICE_PACK _cl_version)
  146. if(NOT DETERMINED_VS_SERVICE_PACK)
  147. _DetermineVSServicePack_CheckVersionWithTryCompile(DETERMINED_VS_SERVICE_PACK _cl_version)
  148. if(NOT DETERMINED_VS_SERVICE_PACK)
  149. _DetermineVSServicePack_CheckVersionWithTryRun(DETERMINED_VS_SERVICE_PACK _cl_version)
  150. endif()
  151. endif()
  152. if(DETERMINED_VS_SERVICE_PACK)
  153. if(_cl_version)
  154. # Call helper function to determine VS version
  155. _DetermineVSServicePackFromCompiler(_sp "${_cl_version}")
  156. if(_sp)
  157. set(${_pack} ${_sp} CACHE INTERNAL
  158. "The Visual Studio Release with Service Pack")
  159. endif()
  160. endif()
  161. endif()
  162. endif()
  163. endfunction()