Skip to content

Commit c6bd873

Browse files
committed
[CMake] Switch the CMP0091 policy (MSVC_RUNTIME_LIBRARY) to the new behaviour
With the new behaviour, the /MD or similar options aren't added to e.g. CMAKE_CXX_FLAGS_RELEASE, but are added separately by CMake. They can be changed by the cmake variable CMAKE_MSVC_RUNTIME_LIBRARY or with the target property MSVC_RUNTIME_LIBRARY. LLVM has had its own custom CMake flags, e.g. LLVM_USE_CRT_RELEASE, which affects which CRT is used for release mode builds. Deprecate these and direct users to use CMAKE_MSVC_RUNTIME_LIBRARY directly instead (and do a best effort attempt at setting CMAKE_MSVC_RUNTIME_LIBRARY based on the existing LLVM_USE_CRT_ flags). This only handles the simple cases, it doesn't handle multi-config generators with different LLVM_USE_CRT_* variables for different configs though, but that's probably fine - we should move over to the new upstream CMake mechanism anyway, and push users towards that. Change code in compiler-rt, that previously tried to override the CRT choice to /MT, to set CMAKE_MSVC_RUNTIME_LIBRARY instead of meddling in the old variables. This resolves the policy issue in llvm/llvm-project#63286, and should handle the issues that were observed originally when the minimum CMake version was bumped, in llvm/llvm-project#62719 and llvm/llvm-project#62739. Differential Revision: https://reviews.llvm.org/D155233
1 parent f69a9f3 commit c6bd873

File tree

7 files changed

+39
-45
lines changed

7 files changed

+39
-45
lines changed

Diff for: cmake/Modules/CMakePolicy.cmake

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
# CMake policy settings shared between LLVM projects
22

3-
# CMP0091: MSVC runtime library flags are selected by an abstraction.
4-
# New in CMake 3.15. https://cmake.org/cmake/help/latest/policy/CMP0091.html
5-
if(POLICY CMP0091)
6-
cmake_policy(SET CMP0091 OLD)
7-
endif()
83
# CMP0114: ExternalProject step targets fully adopt their steps.
94
# New in CMake 3.19: https://cmake.org/cmake/help/latest/policy/CMP0114.html
105
if(POLICY CMP0114)

Diff for: compiler-rt/CMakeLists.txt

+12-13
Original file line numberDiff line numberDiff line change
@@ -387,20 +387,19 @@ if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
387387
endif()
388388

389389
if(MSVC)
390-
# Replace the /M[DT][d] flags with /MT, and strip any definitions of _DEBUG,
391-
# which cause definition mismatches at link time.
392390
# FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214.
393-
if(COMPILER_RT_HAS_MT_FLAG)
394-
foreach(flag_var
395-
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
396-
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
397-
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
398-
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
399-
string(REGEX REPLACE "/M[DT]d" "/MT" ${flag_var} "${${flag_var}}")
400-
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
401-
string(REGEX REPLACE "/D_DEBUG" "" ${flag_var} "${${flag_var}}")
402-
endforeach()
403-
endif()
391+
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
392+
# Remove any /M[DT][d] flags, and strip any definitions of _DEBUG.
393+
# TODO: We probably could remove this altogether.
394+
foreach(flag_var
395+
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
396+
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
397+
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
398+
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
399+
string(REGEX REPLACE "[/-]M[DT]d" "" ${flag_var} "${${flag_var}}")
400+
string(REGEX REPLACE "[/-]MD" "" ${flag_var} "${${flag_var}}")
401+
string(REGEX REPLACE "[/-]D_DEBUG" "" ${flag_var} "${${flag_var}}")
402+
endforeach()
404403
append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS)
405404
append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS)
406405

Diff for: compiler-rt/lib/orc/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ else() # not Apple
131131
)
132132

133133
if (MSVC)
134-
set(ORC_CFLAGS "${ORC_CFLAGS} /MD")
134+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
135135
endif()
136136
else()
137137
set(ORC_BUILD_TYPE STATIC)

Diff for: llvm/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ if( WIN32 AND NOT CYGWIN )
684684
endif()
685685
set(LLVM_NATIVE_TOOL_DIR "" CACHE PATH "Path to a directory containing prebuilt matching native tools (such as llvm-tblgen)")
686686

687-
set(LLVM_INTEGRATED_CRT_ALLOC "" CACHE PATH "Replace the Windows CRT allocator with any of {rpmalloc|mimalloc|snmalloc}. Only works with /MT enabled.")
687+
set(LLVM_INTEGRATED_CRT_ALLOC "" CACHE PATH "Replace the Windows CRT allocator with any of {rpmalloc|mimalloc|snmalloc}. Only works with CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded.")
688688
if(LLVM_INTEGRATED_CRT_ALLOC)
689689
if(NOT WIN32)
690690
message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC is only supported on Windows.")

Diff for: llvm/cmake/modules/ChooseMSVCCRT.cmake

+20-21
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,32 @@ variables (LLVM_USE_CRT_DEBUG, etc) instead.")
6262

6363
foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
6464
string(TOUPPER "${build_type}" build)
65-
if (NOT LLVM_USE_CRT_${build})
66-
get_current_crt(LLVM_USE_CRT_${build}
67-
MSVC_CRT_REGEX
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."
72-
FORCE)
73-
set_property(CACHE LLVM_USE_CRT_${build}
74-
PROPERTY STRINGS ;${${MSVC_CRT}})
75-
endif(NOT LLVM_USE_CRT_${build})
76-
endforeach(build_type)
77-
78-
foreach(build_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
79-
string(TOUPPER "${build_type}" build)
80-
if ("${LLVM_USE_CRT_${build}}" STREQUAL "")
81-
set(flag_string " ")
82-
else()
83-
set(flag_string " /${LLVM_USE_CRT_${build}} ")
65+
if (NOT "${LLVM_USE_CRT_${build}}" STREQUAL "")
8466
if (NOT ${LLVM_USE_CRT_${build}} IN_LIST ${MSVC_CRT})
8567
message(FATAL_ERROR
8668
"Invalid value for LLVM_USE_CRT_${build}: ${LLVM_USE_CRT_${build}}. Valid options are one of: ${${MSVC_CRT}}")
8769
endif()
88-
message(STATUS "Using ${build_type} VC++ CRT: ${LLVM_USE_CRT_${build}}")
70+
set(library "MultiThreaded")
71+
if ("${LLVM_USE_CRT_${build}}" MATCHES "d$")
72+
set(library "${library}Debug")
73+
endif()
74+
if ("${LLVM_USE_CRT_${build}}" MATCHES "^MD")
75+
set(library "${library}DLL")
76+
endif()
77+
if(${runtime_library_set})
78+
message(WARNING "Conflicting LLVM_USE_CRT_* options")
79+
else()
80+
message(WARNING "The LLVM_USE_CRT_* options are deprecated, use the CMake provided CMAKE_MSVC_RUNTIME_LIBRARY setting instead")
81+
endif()
82+
set(CMAKE_MSVC_RUNTIME_LIBRARY "${library}" CACHE STRING "" FORCE)
83+
message(STATUS "Using VC++ CRT: ${CMAKE_MSVC_RUNTIME_LIBRARY}")
84+
set(runtime_library_set 1)
8985
endif()
9086
foreach(lang C CXX)
91-
set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX flag_string)
87+
# Clear any potentially manually set options from these variables.
88+
# Kept as temporary backwards compat (unsure if necessary).
89+
# TODO: We probably should remove it.
90+
set_flag_in_var(CMAKE_${lang}_FLAGS_${build} MSVC_CRT_REGEX "")
9291
endforeach(lang)
9392
endforeach(build_type)
9493
endmacro(choose_msvc_crt MSVC_CRT)

Diff for: llvm/docs/CMake.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ enabled sub-projects. Nearly all of these variable names begin with
680680
$ D:\llvm-project> cmake ... -DLLVM_INTEGRATED_CRT_ALLOC=D:\git\rpmalloc
681681
682682
This flag needs to be used along with the static CRT, ie. if building the
683-
Release target, add -DLLVM_USE_CRT_RELEASE=MT.
683+
Release target, add -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded.
684684

685685
**LLVM_INSTALL_DOXYGEN_HTML_DIR**:STRING
686686
The path to install Doxygen-generated HTML documentation to. This path can
@@ -768,7 +768,8 @@ enabled sub-projects. Nearly all of these variable names begin with
768768
**LLVM_USE_CRT_{target}**:STRING
769769
On Windows, tells which version of the C runtime library (CRT) should be used.
770770
For example, -DLLVM_USE_CRT_RELEASE=MT would statically link the CRT into the
771-
LLVM tools and library.
771+
LLVM tools and library. This is deprecated; use ``CMAKE_MSVC_RUNTIME_LIBRARY``
772+
instead.
772773

773774
**LLVM_USE_INTEL_JITEVENTS**:BOOL
774775
Enable building support for Intel JIT Events API. Defaults to OFF.

Diff for: llvm/lib/Support/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ endif()
8989

9090
# Override the C runtime allocator on Windows and embed it into LLVM tools & libraries
9191
if(LLVM_INTEGRATED_CRT_ALLOC)
92-
if (CMAKE_BUILD_TYPE AND NOT ${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$")
93-
message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC only works with /MT or /MTd. Use LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE} to set the appropriate option.")
92+
if (NOT CMAKE_MSVC_RUNTIME_LIBRARY OR CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL$")
93+
message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC only works with CMAKE_MSVC_RUNTIME_LIBRARY set to MultiThreaded or MultiThreadedDebug.")
9494
endif()
9595

9696
string(REGEX REPLACE "(/|\\\\)$" "" LLVM_INTEGRATED_CRT_ALLOC "${LLVM_INTEGRATED_CRT_ALLOC}")

0 commit comments

Comments
 (0)