ChooseMSVCCRT.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright (C) 2020 The Khronos Group Inc.
  2. #
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. #
  12. # Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following
  14. # disclaimer in the documentation and/or other materials provided
  15. # with the distribution.
  16. #
  17. # Neither the name of The Khronos Group Inc. nor the names of its
  18. # contributors may be used to endorse or promote products derived
  19. # from this software without specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. # POSSIBILITY OF SUCH DAMAGE.
  33. # The macro choose_msvc_crt() takes a list of possible
  34. # C runtimes to choose from, in the form of compiler flags,
  35. # to present to the user. (MTd for /MTd, etc)
  36. #
  37. # The macro is invoked at the end of the file.
  38. #
  39. # CMake already sets CRT flags in the CMAKE_CXX_FLAGS_* and
  40. # CMAKE_C_FLAGS_* variables by default. To let the user
  41. # override that for each build type:
  42. # 1. Detect which CRT is already selected, and reflect this in
  43. # LLVM_USE_CRT_* so the user can have a better idea of what
  44. # changes they're making.
  45. # 2. Replace the flags in both variables with the new flag via a regex.
  46. # 3. set() the variables back into the cache so the changes
  47. # are user-visible.
  48. ### Helper macros: ###
  49. macro(make_crt_regex regex crts)
  50. set(${regex} "")
  51. foreach(crt ${${crts}})
  52. # Trying to match the beginning or end of the string with stuff
  53. # like [ ^]+ didn't work, so use a bunch of parentheses instead.
  54. set(${regex} "${${regex}}|(^| +)/${crt}($| +)")
  55. endforeach(crt)
  56. string(REGEX REPLACE "^\\|" "" ${regex} "${${regex}}")
  57. endmacro(make_crt_regex)
  58. macro(get_current_crt crt_current regex flagsvar)
  59. # Find the selected-by-CMake CRT for each build type, if any.
  60. # Strip off the leading slash and any whitespace.
  61. string(REGEX MATCH "${${regex}}" ${crt_current} "${${flagsvar}}")
  62. string(REPLACE "/" " " ${crt_current} "${${crt_current}}")
  63. string(STRIP "${${crt_current}}" ${crt_current})
  64. endmacro(get_current_crt)
  65. # Replaces or adds a flag to a variable.
  66. # Expects 'flag' to be padded with spaces.
  67. macro(set_flag_in_var flagsvar regex flag)
  68. string(REGEX MATCH "${${regex}}" current_flag "${${flagsvar}}")
  69. if("${current_flag}" STREQUAL "")
  70. set(${flagsvar} "${${flagsvar}}${${flag}}")
  71. else()
  72. string(REGEX REPLACE "${${regex}}" "${${flag}}" ${flagsvar} "${${flagsvar}}")
  73. endif()
  74. string(STRIP "${${flagsvar}}" ${flagsvar})
  75. # Make sure this change gets reflected in the cache/gui.
  76. # CMake requires the docstring parameter whenever set() touches the cache,
  77. # so get the existing docstring and re-use that.
  78. get_property(flagsvar_docs CACHE ${flagsvar} PROPERTY HELPSTRING)
  79. set(${flagsvar} "${${flagsvar}}" CACHE STRING "${flagsvar_docs}" FORCE)
  80. endmacro(set_flag_in_var)
  81. macro(choose_msvc_crt MSVC_CRT)
  82. if(LLVM_USE_CRT)
  83. message(FATAL_ERROR
  84. "LLVM_USE_CRT is deprecated. Use the CMAKE_BUILD_TYPE-specific
  85. variables (LLVM_USE_CRT_DEBUG, etc) instead.")
  86. endif()
  87. make_crt_regex(MSVC_CRT_REGEX ${MSVC_CRT})
  88. foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
  89. string(TOUPPER "${build_type}" build)
  90. if (NOT LLVM_USE_CRT_${build})
  91. get_current_crt(LLVM_USE_CRT_${build}
  92. MSVC_CRT_REGEX
  93. CMAKE_CXX_FLAGS_${build})
  94. set(LLVM_USE_CRT_${build}
  95. "${LLVM_USE_CRT_${build}}"
  96. CACHE STRING "Specify VC++ CRT to use for ${build_type} configurations."
  97. FORCE)
  98. set_property(CACHE LLVM_USE_CRT_${build}
  99. PROPERTY STRINGS ;${${MSVC_CRT}})
  100. endif(NOT LLVM_USE_CRT_${build})
  101. endforeach(build_type)
  102. foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
  103. string(TOUPPER "${build_type}" build)
  104. if ("${LLVM_USE_CRT_${build}}" STREQUAL "")
  105. set(flag_string " ")
  106. else()
  107. set(flag_string " /${LLVM_USE_CRT_${build}} ")
  108. list(FIND ${MSVC_CRT} ${LLVM_USE_CRT_${build}} idx)
  109. if (idx LESS 0)
  110. message(FATAL_ERROR
  111. "Invalid value for LLVM_USE_CRT_${build}: ${LLVM_USE_CRT_${build}}. Valid options are one of: ${${MSVC_CRT}}")
  112. endif (idx LESS 0)
  113. message(STATUS "Using ${build_type} VC++ CRT: ${LLVM_USE_CRT_${build}}")
  114. endif()
  115. foreach(lang C CXX)
  116. set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX flag_string)
  117. endforeach(lang)
  118. endforeach(build_type)
  119. endmacro(choose_msvc_crt MSVC_CRT)
  120. # List of valid CRTs for MSVC
  121. set(MSVC_CRT
  122. MD
  123. MDd
  124. MT
  125. MTd)
  126. choose_msvc_crt(MSVC_CRT)