ChooseMSVCCRT.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # The macro choose_msvc_crt() takes a list of possible
  2. # C runtimes to choose from, in the form of compiler flags,
  3. # to present to the user. (MTd for /MTd, etc)
  4. #
  5. # The macro is invoked at the end of the file.
  6. #
  7. # CMake already sets CRT flags in the CMAKE_CXX_FLAGS_* and
  8. # CMAKE_C_FLAGS_* variables by default. To let the user
  9. # override that for each build type:
  10. # 1. Detect which CRT is already selected, and reflect this in
  11. # LLVM_USE_CRT_* so the user can have a better idea of what
  12. # changes they're making.
  13. # 2. Replace the flags in both variables with the new flag via a regex.
  14. # 3. set() the variables back into the cache so the changes
  15. # are user-visible.
  16. ### Helper macros: ###
  17. macro(make_crt_regex regex crts)
  18. set(${regex} "")
  19. foreach(crt ${${crts}})
  20. # Trying to match the beginning or end of the string with stuff
  21. # like [ ^]+ didn't work, so use a bunch of parentheses instead.
  22. set(${regex} "${${regex}}|(^| +)/${crt}($| +)")
  23. endforeach(crt)
  24. string(REGEX REPLACE "^\\|" "" ${regex} "${${regex}}")
  25. endmacro(make_crt_regex)
  26. macro(get_current_crt crt_current regex flagsvar)
  27. # Find the selected-by-CMake CRT for each build type, if any.
  28. # Strip off the leading slash and any whitespace.
  29. string(REGEX MATCH "${${regex}}" ${crt_current} "${${flagsvar}}")
  30. string(REPLACE "/" " " ${crt_current} "${${crt_current}}")
  31. string(STRIP "${${crt_current}}" ${crt_current})
  32. endmacro(get_current_crt)
  33. # Replaces or adds a flag to a variable.
  34. # Expects 'flag' to be padded with spaces.
  35. macro(set_flag_in_var flagsvar regex flag)
  36. string(REGEX MATCH "${${regex}}" current_flag "${${flagsvar}}")
  37. if("${current_flag}" STREQUAL "")
  38. set(${flagsvar} "${${flagsvar}}${${flag}}")
  39. else()
  40. string(REGEX REPLACE "${${regex}}" "${${flag}}" ${flagsvar} "${${flagsvar}}")
  41. endif()
  42. string(STRIP "${${flagsvar}}" ${flagsvar})
  43. # Make sure this change gets reflected in the cache/gui.
  44. # CMake requires the docstring parameter whenever set() touches the cache,
  45. # so get the existing docstring and re-use that.
  46. get_property(flagsvar_docs CACHE ${flagsvar} PROPERTY HELPSTRING)
  47. set(${flagsvar} "${${flagsvar}}" CACHE STRING "${flagsvar_docs}" FORCE)
  48. endmacro(set_flag_in_var)
  49. macro(choose_msvc_crt MSVC_CRT)
  50. if(LLVM_USE_CRT)
  51. message(FATAL_ERROR
  52. "LLVM_USE_CRT is deprecated. Use the CMAKE_BUILD_TYPE-specific
  53. variables (LLVM_USE_CRT_DEBUG, etc) instead.")
  54. endif()
  55. make_crt_regex(MSVC_CRT_REGEX ${MSVC_CRT})
  56. foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
  57. string(TOUPPER "${build_type}" build)
  58. if (NOT LLVM_USE_CRT_${build})
  59. get_current_crt(LLVM_USE_CRT_${build}
  60. MSVC_CRT_REGEX
  61. CMAKE_CXX_FLAGS_${build})
  62. set(LLVM_USE_CRT_${build}
  63. "${LLVM_USE_CRT_${build}}"
  64. CACHE STRING "Specify VC++ CRT to use for ${build_type} configurations."
  65. FORCE)
  66. set_property(CACHE LLVM_USE_CRT_${build}
  67. PROPERTY STRINGS ;${${MSVC_CRT}})
  68. endif(NOT LLVM_USE_CRT_${build})
  69. endforeach(build_type)
  70. foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
  71. string(TOUPPER "${build_type}" build)
  72. if ("${LLVM_USE_CRT_${build}}" STREQUAL "")
  73. set(flag_string " ")
  74. else()
  75. set(flag_string " /${LLVM_USE_CRT_${build}} ")
  76. list(FIND ${MSVC_CRT} ${LLVM_USE_CRT_${build}} idx)
  77. if (idx LESS 0)
  78. message(FATAL_ERROR
  79. "Invalid value for LLVM_USE_CRT_${build}: ${LLVM_USE_CRT_${build}}. Valid options are one of: ${${MSVC_CRT}}")
  80. endif (idx LESS 0)
  81. message(STATUS "Using ${build_type} VC++ CRT: ${LLVM_USE_CRT_${build}}")
  82. endif()
  83. foreach(lang C CXX)
  84. set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX flag_string)
  85. endforeach(lang)
  86. endforeach(build_type)
  87. endmacro(choose_msvc_crt MSVC_CRT)
  88. # List of valid CRTs for MSVC
  89. set(MSVC_CRT
  90. MD
  91. MDd
  92. MT
  93. MTd)
  94. choose_msvc_crt(MSVC_CRT)