CMakeDetermineVSServicePack.cmake 5.9 KB

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