fix(cmake): do not override a parent-set CMAKE_MSVC_RUNTIME_LIBRARY

When protobuf is built as a static library via add_subdirectory or
FetchContent, it set CMAKE_MSVC_RUNTIME_LIBRARY unconditionally, overriding a
value the enclosing project had set explicitly. A parent forcing /MD in every
configuration would still get /MDd for the protobuf and Abseil targets in
Debug, producing LNK2038 runtime-library mismatches. Only set the runtime
library when the parent has not already defined it, leaving standalone builds
unchanged.

Fixes #23173.
This commit is contained in:
Yash Anil 2026-07-13 10:36:58 -07:00 committed by Tony Liao
parent e1c5f3ac47
commit 13501be37c

View file

@ -238,10 +238,17 @@ if (protobuf_BUILD_SHARED_LIBS)
else (protobuf_BUILD_SHARED_LIBS)
set(protobuf_SHARED_OR_STATIC "STATIC")
set(ABSL_MSVC_STATIC_RUNTIME ${protobuf_MSVC_STATIC_RUNTIME})
if (protobuf_MSVC_STATIC_RUNTIME)
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
else()
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
# Only choose an MSVC runtime library if the enclosing project has not already
# set one. When protobuf is consumed via add_subdirectory/FetchContent, the
# parent may set CMAKE_MSVC_RUNTIME_LIBRARY explicitly (for example to force
# /MD in every configuration); overriding it here causes runtime-library
# mismatches such as LNK2038 (MDd_DynamicDebug vs MD_DynamicRelease).
if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
if (protobuf_MSVC_STATIC_RUNTIME)
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
else()
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
endif()
endif()
endif (protobuf_BUILD_SHARED_LIBS)