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)
5
# The macro is invoked at the end of the file.
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
17
### Helper macros: ###
18
macro(make_crt_regex regex crts)
20
foreach(crt ${${crts}})
21
# Trying to match the beginning or end of the string with stuff
22
# like [ ^]+ didn't work, so use a bunch of parentheses instead.
23
set(${regex} "${${regex}}|(^| +)/${crt}($| +)")
25
string(REGEX REPLACE "^\\|" "" ${regex} "${${regex}}")
26
endmacro(make_crt_regex)
28
macro(get_current_crt crt_current regex flagsvar)
29
# Find the selected-by-CMake CRT for each build type, if any.
30
# Strip off the leading slash and any whitespace.
31
string(REGEX MATCH "${${regex}}" ${crt_current} "${${flagsvar}}")
32
string(REPLACE "/" " " ${crt_current} "${${crt_current}}")
33
string(STRIP "${${crt_current}}" ${crt_current})
34
endmacro(get_current_crt)
36
# Replaces or adds a flag to a variable.
37
# Expects 'flag' to be padded with spaces.
38
macro(set_flag_in_var flagsvar regex flag)
39
string(REGEX MATCH "${${regex}}" current_flag "${${flagsvar}}")
40
if("${current_flag}" STREQUAL "")
41
set(${flagsvar} "${${flagsvar}}${${flag}}")
43
string(REGEX REPLACE "${${regex}}" "${${flag}}" ${flagsvar} "${${flagsvar}}")
45
string(STRIP "${${flagsvar}}" ${flagsvar})
46
# Make sure this change gets reflected in the cache/gui.
47
# CMake requires the docstring parameter whenever set() touches the cache,
48
# so get the existing docstring and re-use that.
49
get_property(flagsvar_docs CACHE ${flagsvar} PROPERTY HELPSTRING)
50
set(${flagsvar} "${${flagsvar}}" CACHE STRING "${flagsvar_docs}" FORCE)
51
endmacro(set_flag_in_var)
54
macro(choose_msvc_crt MSVC_CRT)
57
"LLVM_USE_CRT is deprecated. Use the CMAKE_BUILD_TYPE-specific
58
variables (LLVM_USE_CRT_DEBUG, etc) instead.")
61
make_crt_regex(MSVC_CRT_REGEX ${MSVC_CRT})
63
foreach(build_type ${CMAKE_CONFIGURATION_TYPES})
64
string(TOUPPER "${build_type}" build)
65
if (NOT LLVM_USE_CRT_${build})
66
get_current_crt(LLVM_USE_CRT_${build}
68
CMAKE_CXX_FLAGS_${build})
69
set(LLVM_USE_CRT_${build}
70
"${LLVM_USE_CRT_${build}}"
71
CACHE STRING "Specify VC++ CRT to use for ${build_type} configurations."
73
set_property(CACHE LLVM_USE_CRT_${build}
74
PROPERTY STRINGS "";${${MSVC_CRT}})
75
endif(NOT LLVM_USE_CRT_${build})
76
endforeach(build_type)
78
foreach(build_type ${CMAKE_CONFIGURATION_TYPES})
79
string(TOUPPER "${build_type}" build)
80
if ("${LLVM_USE_CRT_${build}}" STREQUAL "")
83
set(flag_string " /${LLVM_USE_CRT_${build}} ")
84
list(FIND ${MSVC_CRT} ${LLVM_USE_CRT_${build}} idx)
87
"Invalid value for LLVM_USE_CRT_${build}: ${LLVM_USE_CRT_${build}}. Valid options are one of: ${${MSVC_CRT}}")
89
message(STATUS "Using ${build_type} VC++ CRT: ${LLVM_USE_CRT_${build}}")
92
set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX flag_string)
94
endforeach(build_type)
95
endmacro(choose_msvc_crt MSVC_CRT)
98
# List of valid CRTs for MSVC
105
choose_msvc_crt(MSVC_CRT)