Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmake/caches/OfficialRelease.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Settings used to build official DXC Release binaries.

include("${CMAKE_CURRENT_LIST_DIR}/PredefinedParams.cmake")

set(LLVM_USE_CRT_RELEASE MT CACHE STRING
"Use the static multithreaded MSVC runtime for Release builds.")
78 changes: 41 additions & 37 deletions cmake/modules/ChooseMSVCCRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
#
# 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.
# To let the user override the MSVC runtime library for each build type:
# 1. Detect legacy /MD, /MDd, /MT, and /MTd switches in the
# CMAKE_<LANG>_FLAGS_<CONFIG> variables and reflect them in LLVM_USE_CRT_*.
# 2. Validate explicitly selected LLVM_USE_CRT_* values.
# 3. Translate them to CMake's MSVC runtime library abstraction.

### Helper macros: ###
macro(make_crt_regex regex crts)
Expand All @@ -33,24 +29,6 @@ macro(get_current_crt crt_current regex flagsvar)
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)


macro(choose_msvc_crt MSVC_CRT)
if(LLVM_USE_CRT)
message(FATAL_ERROR
Expand All @@ -60,7 +38,10 @@ variables (LLVM_USE_CRT_DEBUG, etc) instead.")

make_crt_regex(MSVC_CRT_REGEX ${MSVC_CRT})

foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
set(llvm_crt_build_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
list(REMOVE_DUPLICATES llvm_crt_build_types)

foreach(build_type ${llvm_crt_build_types})
string(TOUPPER "${build_type}" build)
if (NOT LLVM_USE_CRT_${build})
get_current_crt(LLVM_USE_CRT_${build}
Expand All @@ -75,23 +56,47 @@ variables (LLVM_USE_CRT_DEBUG, etc) instead.")
endif(NOT LLVM_USE_CRT_${build})
endforeach(build_type)

foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
set(llvm_crt_override_requested FALSE)
foreach(build_type ${llvm_crt_build_types})
string(TOUPPER "${build_type}" build)
if ("${LLVM_USE_CRT_${build}}" STREQUAL "")
set(flag_string " ")
else()
set(flag_string " /${LLVM_USE_CRT_${build}} ")
if (NOT "${LLVM_USE_CRT_${build}}" STREQUAL "")
set(llvm_crt_override_requested TRUE)
list(FIND ${MSVC_CRT} ${LLVM_USE_CRT_${build}} idx)
if (idx LESS 0)
message(FATAL_ERROR
"Invalid value for LLVM_USE_CRT_${build}: ${LLVM_USE_CRT_${build}}. Valid options are one of: ${${MSVC_CRT}}")
endif (idx LESS 0)
message(STATUS "Using ${build_type} VC++ CRT: ${LLVM_USE_CRT_${build}}")
endif()
foreach(lang C CXX)
set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX flag_string)
endforeach(lang)
endforeach(build_type)

if (llvm_crt_override_requested)
set(cmake_msvc_runtime_library "")
foreach(build_type ${llvm_crt_build_types})
string(TOUPPER "${build_type}" build)
set(crt "${LLVM_USE_CRT_${build}}")
if ("${crt}" STREQUAL "")
if ("${build}" STREQUAL "DEBUG")
set(crt "MDd")
else()
set(crt "MD")
endif()
endif()

if ("${crt}" STREQUAL "MD")
set(runtime_library "MultiThreadedDLL")
elseif ("${crt}" STREQUAL "MDd")
set(runtime_library "MultiThreadedDebugDLL")
elseif ("${crt}" STREQUAL "MT")
set(runtime_library "MultiThreaded")
elseif ("${crt}" STREQUAL "MTd")
set(runtime_library "MultiThreadedDebug")
endif()
string(APPEND cmake_msvc_runtime_library
"$<$<CONFIG:${build_type}>:${runtime_library}>")
endforeach(build_type)
set(CMAKE_MSVC_RUNTIME_LIBRARY "${cmake_msvc_runtime_library}")
endif()
endmacro(choose_msvc_crt MSVC_CRT)


Expand All @@ -103,4 +108,3 @@ set(MSVC_CRT
MTd)

choose_msvc_crt(MSVC_CRT)

Loading