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