cmake: support `MSVC_RUNTIME_LIBRARY` property

When using a new enough CMake (3.15+) prefer to use the
`MSVC_RUNTIME_LIBRARY` property on targets to select the runtime library
variant.  This property is automatically set to the value specified by
`CMAKE_MSVC_RUNTIME_LIBRARY`.  This property requires that the CMake
Policy 91 is set to new (see CMP0091).
pull/8851/head
Saleem Abdulrasool 4 years ago
parent 914c6fe101
commit c47adad0c3
  1. 40
      cmake/CMakeLists.txt

@ -15,6 +15,10 @@ endif ()
if(POLICY CMP0048) if(POLICY CMP0048)
cmake_policy(SET CMP0048 NEW) cmake_policy(SET CMP0048 NEW)
endif() endif()
# MSVC runtime library flags are selected by an abstraction.
if(POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif()
# Project # Project
project(protobuf C CXX) project(protobuf C CXX)
@ -176,20 +180,28 @@ if (protobuf_BUILD_SHARED_LIBS)
set(protobuf_SHARED_OR_STATIC "SHARED") set(protobuf_SHARED_OR_STATIC "SHARED")
else (protobuf_BUILD_SHARED_LIBS) else (protobuf_BUILD_SHARED_LIBS)
set(protobuf_SHARED_OR_STATIC "STATIC") set(protobuf_SHARED_OR_STATIC "STATIC")
# In case we are building static libraries, link also the runtime library statically # The CMAKE_<LANG>_FLAGS(_<BUILD_TYPE>)? is meant to be user controlled.
# so that MSVCR*.DLL is not required at runtime. # Prior to CMake 3.15, the MSVC runtime library was pushed into the same flags
# https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx # making programmatic control difficult. Prefer the functionality in newer
# This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd # CMake versions when available.
# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
if (MSVC AND protobuf_MSVC_STATIC_RUNTIME) set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
foreach(flag_var else()
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE # In case we are building static libraries, link also the runtime library statically
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) # so that MSVCR*.DLL is not required at runtime.
if(${flag_var} MATCHES "/MD") # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
endif(${flag_var} MATCHES "/MD") # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
endforeach(flag_var) if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME) foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach(flag_var)
endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
endif()
endif (protobuf_BUILD_SHARED_LIBS) endif (protobuf_BUILD_SHARED_LIBS)
if (MSVC) if (MSVC)

Loading…
Cancel
Save