Modernize MSVC runtime library code

This commit is in response to this issue:
https://github.com/glfw/glfw/issues/1783
This commit is contained in:
hdf89shfdfs 2020-10-26 06:26:46 -04:00
parent 0ef149c8f2
commit 5f2d8fb9a4
3 changed files with 63 additions and 25 deletions

View File

@ -0,0 +1,55 @@
if (DEFINED glfw_msvc_runtime_library_include_guard)
return()
endif()
set(glfw_msvc_runtime_library_include_guard ON)
include(CMakeDependentOption)
macro(glfw_msvc_runtime_library GLFW_STANDALONE)
# Prior to cmake 3.15 there wasn't a very good way to handle building against the static/dyanmic
# MSVC runtime libraries. As of cmake 3.15 this is no longer the case and clients can use the
# official cmake variable CMAKE_MSVC_RUNTIME_LIBRARY:
# https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html#variable:CMAKE_MSVC_RUNTIME_LIBRARY
#
# The new way has better support for multi-configuration generators and clang-cl. So if clients are interested in
# these features/functionality they should be using a newer version of cmake.
#
# However for backwards compatibility keep the old way.
if (${CMAKE_VERSION} VERSION_LESS "3.15")
cmake_dependent_option(USE_MSVC_RUNTIME_LIBRARY_DLL "Use MSVC runtime library DLL" ON "MSVC" OFF)
#--------------------------------------------------------------------
# Set compiler specific flags
#--------------------------------------------------------------------
if (MSVC)
if (NOT USE_MSVC_RUNTIME_LIBRARY_DLL)
foreach (flag CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO)
if (${flag} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
endif()
if (${flag} MATCHES "/MDd")
string(REGEX REPLACE "/MDd" "/MTd" ${flag} "${${flag}}")
endif()
endforeach()
endif()
endif()
else()
# If building GLFW standalone just use the static libraries.
# It simplifies working/deploying on test machines.
if (${GLFW_STANDALONE})
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
# Otherwise the client should specify this critical build variable.
# When compiling for the WIN32 platform gives clients a helpful warning about what they should do.
if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY AND WIN32)
message(AUTHOR_WARNING "GLFW: User has not defined CMAKE_MSVC_RUNTIME_LIBRARY.")
endif()
endif()
endif()
endmacro()

View File

@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.1...3.17 FATAL_ERROR)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Cmake/")
project(GLFW VERSION 3.4.0 LANGUAGES C)
set(CMAKE_LEGACY_CYGWIN_WIN32 OFF)
@ -31,6 +33,7 @@ option(GLFW_VULKAN_STATIC "Assume the Vulkan loader is linked with the applicati
include(GNUInstallDirs)
include(CMakeDependentOption)
include(glfw_msvc_runtime_library)
cmake_dependent_option(GLFW_USE_OSMESA "Use OSMesa for offscreen context creation" OFF
"UNIX" OFF)
@ -38,8 +41,8 @@ cmake_dependent_option(GLFW_USE_HYBRID_HPG "Force use of high-performance GPU on
"WIN32" OFF)
cmake_dependent_option(GLFW_USE_WAYLAND "Use Wayland for window creation" OFF
"UNIX;NOT APPLE" OFF)
cmake_dependent_option(USE_MSVC_RUNTIME_LIBRARY_DLL "Use MSVC runtime library DLL" ON
"MSVC" OFF)
glfw_msvc_runtime_library(${GLFW_STANDALONE})
if (BUILD_SHARED_LIBS AND UNIX)
# On Unix-like systems, shared libraries can use the soname system.
@ -66,28 +69,6 @@ if (GLFW_BUILD_DOCS)
find_package(Doxygen)
endif()
#--------------------------------------------------------------------
# Set compiler specific flags
#--------------------------------------------------------------------
if (MSVC)
if (NOT USE_MSVC_RUNTIME_LIBRARY_DLL)
foreach (flag CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO)
if (${flag} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
endif()
if (${flag} MATCHES "/MDd")
string(REGEX REPLACE "/MDd" "/MTd" ${flag} "${${flag}}")
endif()
endforeach()
endif()
endif()
#--------------------------------------------------------------------
# Detect and select backend APIs
#--------------------------------------------------------------------

View File

@ -233,7 +233,9 @@ directly with the application.
@anchor USE_MSVC_RUNTIME_LIBRARY_DLL
__USE_MSVC_RUNTIME_LIBRARY_DLL__ determines whether to use the DLL version or the
static library version of the Visual C++ runtime library. If set to `ON`, the
DLL version of the Visual C++ library is used.
DLL version of the Visual C++ library is used. This variable exists for cmake users
whose version of cmake is less than 3.15 where this issue has been solved in a standard
way, see the official cmake docs on 'CMAKE_MSVC_RUNTIME_LIBRARY'.
@anchor GLFW_USE_HYBRID_HPG
__GLFW_USE_HYBRID_HPG__ determines whether to export the `NvOptimusEnablement` and