llvm.org GIT mirror llvm / 02a8f76
New system for choosing CRT type on MSVC. Is setted on a per build type basis. This is useful for generators that comprise several build types, i.e. VC++ solutions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@110296 91177308-0d34-0410-b5e6-96231b3b80d8 Oscar Fuentes 9 years ago
2 changed file(s) with 108 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
216216 endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
217217
218218 if( MSVC )
219 # List of valid CRTs for MSVC
220 set(MSVC_CRT
221 MD
222 MDd
223 MT
224 MTd)
225
226 set(LLVM_USE_CRT "" CACHE STRING "Specify VC++ CRT to use for debug/release configurations.")
227 # Lets the GUI show a drop-down list of possible values, including
228 # an empty string as the default:
229 set_property(CACHE LLVM_USE_CRT PROPERTY STRINGS "";${MSVC_CRT})
219 include(ChooseMSVCCRT)
220
230221 add_llvm_definitions( -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS )
231222 add_llvm_definitions( -D_SCL_SECURE_NO_WARNINGS -DCRT_NONSTDC_NO_WARNINGS )
232223 add_llvm_definitions( -D_SCL_SECURE_NO_DEPRECATE )
235226
236227 # Suppress 'new behavior: elements of array 'array' will be default initialized'
237228 add_llvm_definitions( -wd4351 )
238
239 if (NOT ${LLVM_USE_CRT} STREQUAL "")
240 list(FIND MSVC_CRT ${LLVM_USE_CRT} idx)
241 if (idx LESS 0)
242 message(FATAL_ERROR "Invalid value for LLVM_USE_CRT: ${LLVM_USE_CRT}. Valid options are one of: ${MSVC_CRT}")
243 endif (idx LESS 0)
244 add_llvm_definitions("/${LLVM_USE_CRT}")
245 message(STATUS "Using VC++ CRT: ${LLVM_USE_CRT}")
246 endif (NOT ${LLVM_USE_CRT} STREQUAL "")
247229
248230 # Enable warnings
249231 if (LLVM_ENABLE_WARNINGS)
0 # The macro choose_msvc_crt() takes a list of possible
1 # C runtimes to choose from, in the form of compiler flags,
2 # to present to the user. (MTd for /MTd, etc)
3 #
4 # The macro is invoked at the end of the file.
5 #
6 # CMake already sets CRT flags in the CMAKE_CXX_FLAGS_* and
7 # CMAKE_C_FLAGS_* variables by default. To let the user
8 # override that for each build type:
9 # 1. Detect which CRT is already selected, and reflect this in
10 # LLVM_USE_CRT_* so the user can have a better idea of what
11 # changes they're making.
12 # 2. Replace the flags in both variables with the new flag via a regex.
13 # 3. set() the variables back into the cache so the changes
14 # are user-visible.
15
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
27 macro(get_current_crt crt_current regex flagsvar)
28 # Find the selected-by-CMake CRT for each build type, if any.
29 # Strip off the leading slash and any whitespace.
30 string(REGEX MATCH "${${regex}}" ${crt_current} "${${flagsvar}}")
31 string(REPLACE "/" " " ${crt_current} "${${crt_current}}")
32 string(STRIP "${${crt_current}}" ${crt_current})
33 endmacro(get_current_crt)
34
35 # Replaces or adds a flag to a variable.
36 # Expects 'flag' to be padded with spaces.
37 macro(set_flag_in_var flagsvar regex flag)
38 string(REGEX MATCH "${${regex}}" current_flag "${${flagsvar}}")
39 if("${current_flag}" STREQUAL "")
40 set(${flagsvar} "${${flagsvar}}${${flag}}")
41 else()
42 string(REGEX REPLACE "${${regex}}" "${${flag}}" ${flagsvar} "${${flagsvar}}")
43 endif()
44 string(STRIP "${${flagsvar}}" ${flagsvar})
45 # Make sure this change gets reflected in the cache/gui.
46 # CMake requires the docstring parameter whenever set() touches the cache,
47 # so get the existing docstring and re-use that.
48 get_property(flagsvar_docs CACHE ${flagsvar} PROPERTY HELPSTRING)
49 set(${flagsvar} "${${flagsvar}}" CACHE STRING "${flagsvar_docs}" FORCE)
50 endmacro(set_flag_in_var)
51
52
53 macro(choose_msvc_crt MSVC_CRT)
54 if(LLVM_USE_CRT)
55 message(FATAL_ERROR
56 "LLVM_USE_CRT is deprecated. Use the CMAKE_BUILD_TYPE-specific
57 variables (LLVM_USE_CRT_DEBUG, etc) instead.")
58 endif()
59
60 make_crt_regex(MSVC_CRT_REGEX ${MSVC_CRT})
61
62 foreach(build_type ${CMAKE_CONFIGURATION_TYPES})
63 string(TOUPPER "${build_type}" build)
64 if (NOT LLVM_USE_CRT_${build})
65 get_current_crt(LLVM_USE_CRT_${build}
66 MSVC_CRT_REGEX
67 CMAKE_CXX_FLAGS_${build})
68 set(LLVM_USE_CRT_${build}
69 "${LLVM_USE_CRT_${build}}"
70 CACHE STRING "Specify VC++ CRT to use for ${build_type} configurations."
71 FORCE)
72 set_property(CACHE LLVM_USE_CRT_${build}
73 PROPERTY STRINGS "";${${MSVC_CRT}})
74 endif(NOT LLVM_USE_CRT_${build})
75 endforeach(build_type)
76
77 foreach(build_type ${CMAKE_CONFIGURATION_TYPES})
78 string(TOUPPER "${build_type}" build)
79 if ("${LLVM_USE_CRT_${build}}" STREQUAL "")
80 set(flag_string " ")
81 else()
82 set(flag_string " /${LLVM_USE_CRT_${build}} ")
83 list(FIND ${MSVC_CRT} ${LLVM_USE_CRT_${build}} idx)
84 if (idx LESS 0)
85 message(FATAL_ERROR
86 "Invalid value for LLVM_USE_CRT_${build}: ${LLVM_USE_CRT_${build}}. Valid options are one of: ${${MSVC_CRT}}")
87 endif (idx LESS 0)
88 message(STATUS "Using ${build_type} VC++ CRT: ${LLVM_USE_CRT_${build}}")
89 endif()
90 foreach(lang C CXX)
91 set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX flag_string)
92 endforeach(lang)
93 endforeach(build_type)
94 endmacro(choose_msvc_crt MSVC_CRT)
95
96
97 # List of valid CRTs for MSVC
98 set(MSVC_CRT
99 MD
100 MDd
101 MT
102 MTd)
103
104 choose_msvc_crt(MSVC_CRT)
105