From 1a7e305c0fdbd43ef38167c18e44f1f75ab623fa Mon Sep 17 00:00:00 2001 From: Alex Sepkowski Date: Tue, 18 Aug 2026 17:57:56 -0700 Subject: [PATCH 1/4] [CMake] Fix MSVC runtime library selection Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0f4d0e10-e926-4bce-b36b-0abebc08f6dc --- cmake/modules/ChooseMSVCCRT.cmake | 77 ++++++++++++++++--------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/cmake/modules/ChooseMSVCCRT.cmake b/cmake/modules/ChooseMSVCCRT.cmake index 0e6e1aa552..63a025fbae 100644 --- a/cmake/modules/ChooseMSVCCRT.cmake +++ b/cmake/modules/ChooseMSVCCRT.cmake @@ -4,15 +4,10 @@ # # 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 CRT flags 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) @@ -33,24 +28,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 @@ -60,7 +37,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} @@ -75,12 +55,11 @@ 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 @@ -88,10 +67,35 @@ variables (LLVM_USE_CRT_DEBUG, etc) instead.") 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 + "$<$:${runtime_library}>") + endforeach(build_type) + set(CMAKE_MSVC_RUNTIME_LIBRARY "${cmake_msvc_runtime_library}") + endif() endmacro(choose_msvc_crt MSVC_CRT) @@ -103,4 +107,3 @@ set(MSVC_CRT MTd) choose_msvc_crt(MSVC_CRT) - From 1d4b3572ed636ccb92b25ca69c7e51e393900865 Mon Sep 17 00:00:00 2001 From: Alex Sepkowski Date: Thu, 20 Aug 2026 10:33:18 -0700 Subject: [PATCH 2/4] [CMake] Add official Release cache Compose the standard DXC cache with static Release CRT selection and document the reproducible Visual Studio configure and build commands. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- cmake/caches/OfficialRelease.cmake | 6 ++++++ docs/BuildingAndTestingDXC.rst | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 cmake/caches/OfficialRelease.cmake diff --git a/cmake/caches/OfficialRelease.cmake b/cmake/caches/OfficialRelease.cmake new file mode 100644 index 0000000000..f96fe07dae --- /dev/null +++ b/cmake/caches/OfficialRelease.cmake @@ -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.") diff --git a/docs/BuildingAndTestingDXC.rst b/docs/BuildingAndTestingDXC.rst index 005cfc4595..ed3d9636ec 100644 --- a/docs/BuildingAndTestingDXC.rst +++ b/docs/BuildingAndTestingDXC.rst @@ -89,6 +89,27 @@ Open the resulting LLVM.sln placed under the ````. DXC should build successfully with either the ``Visual Studio 17 2022`` or ``Visual Studio 16 2019`` generators. +Reproducing the Official Release Configuration +---------------------------------------------- + +The ``OfficialRelease.cmake`` cache composes the basic DXC configuration with +the settings used for official Release binaries. Configure and build an x64 +Visual Studio build with: + +.. code-block:: sh + + cmake \ + -B \ + -C /cmake/caches/OfficialRelease.cmake \ + -G "Visual Studio 17 2022" \ + -A x64 + + cmake --build --config Release --parallel + +The cache selects the static multithreaded MSVC runtime for Release targets. +It does not configure product versioning, signing, symbol packaging, or +artifact publication. + Using Visual Studio's CMake Integration --------------------------------------- From fa02b2172669df62e15905135450d8c7d5d26ee9 Mon Sep 17 00:00:00 2001 From: Alex Sepkowski Date: Thu, 20 Aug 2026 10:37:23 -0700 Subject: [PATCH 3/4] [Docs] Defer official Release build guidance Keep the initial cache focused on CRT policy until the remaining DXCBuild release settings move upstream and build equivalence is demonstrated. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/BuildingAndTestingDXC.rst | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/docs/BuildingAndTestingDXC.rst b/docs/BuildingAndTestingDXC.rst index ed3d9636ec..005cfc4595 100644 --- a/docs/BuildingAndTestingDXC.rst +++ b/docs/BuildingAndTestingDXC.rst @@ -89,27 +89,6 @@ Open the resulting LLVM.sln placed under the ````. DXC should build successfully with either the ``Visual Studio 17 2022`` or ``Visual Studio 16 2019`` generators. -Reproducing the Official Release Configuration ----------------------------------------------- - -The ``OfficialRelease.cmake`` cache composes the basic DXC configuration with -the settings used for official Release binaries. Configure and build an x64 -Visual Studio build with: - -.. code-block:: sh - - cmake \ - -B \ - -C /cmake/caches/OfficialRelease.cmake \ - -G "Visual Studio 17 2022" \ - -A x64 - - cmake --build --config Release --parallel - -The cache selects the static multithreaded MSVC runtime for Release targets. -It does not configure product versioning, signing, symbol packaging, or -artifact publication. - Using Visual Studio's CMake Integration --------------------------------------- From 4c7b1172620f7f772d44488ee419f51a7c2004f9 Mon Sep 17 00:00:00 2001 From: Alex Sepkowski Date: Thu, 20 Aug 2026 10:39:32 -0700 Subject: [PATCH 4/4] [CMake] Clarify legacy CRT flag detection Name the MSVC runtime switches and the legacy CMake flag variables inspected for compatibility. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- cmake/modules/ChooseMSVCCRT.cmake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmake/modules/ChooseMSVCCRT.cmake b/cmake/modules/ChooseMSVCCRT.cmake index 63a025fbae..95e193a64d 100644 --- a/cmake/modules/ChooseMSVCCRT.cmake +++ b/cmake/modules/ChooseMSVCCRT.cmake @@ -5,7 +5,8 @@ # The macro is invoked at the end of the file. # # To let the user override the MSVC runtime library for each build type: -# 1. Detect legacy CRT flags and reflect them in LLVM_USE_CRT_*. +# 1. Detect legacy /MD, /MDd, /MT, and /MTd switches in the +# CMAKE__FLAGS_ 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.