CMakeDetermineVSServicePack.cmake 5.8 KB

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