Skip to content

Commit

Permalink
[CMake] Clean up old code for handling MSVC runtime setting the old way
Browse files Browse the repository at this point in the history
This was left in place to reduce the risk of breaking anything,
and to keep the diff smaller, in D155233.

Differential Revision: https://reviews.llvm.org/D155431
  • Loading branch information
mstorsjo committed Jul 19, 2023
1 parent 2ea5aa1 commit 9f4dfcb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 65 deletions.
11 changes: 0 additions & 11 deletions compiler-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,6 @@ endif()
if(MSVC)
# FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214.
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
# Remove any /M[DT][d] flags, and strip any definitions of _DEBUG.
# TODO: We probably could remove this altogether.
foreach(flag_var
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
string(REGEX REPLACE "[/-]M[DT]d" "" ${flag_var} "${${flag_var}}")
string(REGEX REPLACE "[/-]MD" "" ${flag_var} "${${flag_var}}")
string(REGEX REPLACE "[/-]D_DEBUG" "" ${flag_var} "${${flag_var}}")
endforeach()
append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS)
append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS)

Expand Down
62 changes: 8 additions & 54 deletions llvm/cmake/modules/ChooseMSVCCRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,14 @@
#
# The macro is invoked at the end of the file.
#
# CMake already sets CRT flags in the CMAKE_CXX_FLAGS_* and
# CMAKE_C_FLAGS_* variables by default. To let the user
# override that for each build type:
# 1. Detect which CRT is already selected, and reflect this in
# LLVM_USE_CRT_* so the user can have a better idea of what
# changes they're making.
# 2. Replace the flags in both variables with the new flag via a regex.
# 3. set() the variables back into the cache so the changes
# are user-visible.

### Helper macros: ###
macro(make_crt_regex regex crts)
set(${regex} "")
foreach(crt ${${crts}})
# Trying to match the beginning or end of the string with stuff
# like [ ^]+ didn't work, so use a bunch of parentheses instead.
set(${regex} "${${regex}}|(^| +)/${crt}($| +)")
endforeach(crt)
string(REGEX REPLACE "^\\|" "" ${regex} "${${regex}}")
endmacro(make_crt_regex)

macro(get_current_crt crt_current regex flagsvar)
# Find the selected-by-CMake CRT for each build type, if any.
# Strip off the leading slash and any whitespace.
string(REGEX MATCH "${${regex}}" ${crt_current} "${${flagsvar}}")
string(REPLACE "/" " " ${crt_current} "${${crt_current}}")
string(STRIP "${${crt_current}}" ${crt_current})
endmacro(get_current_crt)

# Replaces or adds a flag to a variable.
# Expects 'flag' to be padded with spaces.
macro(set_flag_in_var flagsvar regex flag)
string(REGEX MATCH "${${regex}}" current_flag "${${flagsvar}}")
if("${current_flag}" STREQUAL "")
set(${flagsvar} "${${flagsvar}}${${flag}}")
else()
string(REGEX REPLACE "${${regex}}" "${${flag}}" ${flagsvar} "${${flagsvar}}")
endif()
string(STRIP "${${flagsvar}}" ${flagsvar})
# Make sure this change gets reflected in the cache/gui.
# CMake requires the docstring parameter whenever set() touches the cache,
# so get the existing docstring and re-use that.
get_property(flagsvar_docs CACHE ${flagsvar} PROPERTY HELPSTRING)
set(${flagsvar} "${${flagsvar}}" CACHE STRING "${flagsvar_docs}" FORCE)
endmacro(set_flag_in_var)

# This mechanism is deprecated, but kept for transitioning users.
#
# This reads the LLVM_USE_CRT_<CONFIG> options and sets
# CMAKE_MSVC_RUNTIME_LIBRARY accordingly. The previous mechanism allowed
# setting different choices for different build configurations (for
# multi-config generators), but translating multiple differing choices to
# the corresponding CMAKE_MSVC_RUNTIME_LIBRARY generator expression isn't
# supported by this transitional helper.

macro(choose_msvc_crt MSVC_CRT)
if(LLVM_USE_CRT)
Expand All @@ -58,8 +20,6 @@ macro(choose_msvc_crt MSVC_CRT)
variables (LLVM_USE_CRT_DEBUG, etc) instead.")
endif()

make_crt_regex(MSVC_CRT_REGEX ${MSVC_CRT})

foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
string(TOUPPER "${build_type}" build)
if (NOT "${LLVM_USE_CRT_${build}}" STREQUAL "")
Expand All @@ -83,12 +43,6 @@ variables (LLVM_USE_CRT_DEBUG, etc) instead.")
message(STATUS "Using VC++ CRT: ${CMAKE_MSVC_RUNTIME_LIBRARY}")
set(runtime_library_set 1)
endif()
foreach(lang C CXX)
# Clear any potentially manually set options from these variables.
# Kept as temporary backwards compat (unsure if necessary).
# TODO: We probably should remove it.
set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX "")
endforeach(lang)
endforeach(build_type)
endmacro(choose_msvc_crt MSVC_CRT)

Expand Down

0 comments on commit 9f4dfcb

Please sign in to comment.