[cmake] use CMAKE_MSVC_RUNTIME_LIBRARY

* Increase required CMake version to 3.15 (windows only)
* Use
  https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html#prop_tgt:MSVC_RUNTIME_LIBRARY
  to select windows runtime
This commit is contained in:
Armin Novak 2023-11-08 15:18:34 +01:00 committed by Martin Fleisz
parent bd2ac8b11f
commit e0e72f9c28
12 changed files with 63 additions and 88 deletions

View File

@ -61,7 +61,9 @@ set(CPACK_NSIS_MUI_UNICON "${PROJECT_SOURCE_DIR}/resource\\\\FreeRDP_Icon_96px.i
set(CPACK_COMPONENTS_ALL client server libraries headers symbols tools) set(CPACK_COMPONENTS_ALL client server libraries headers symbols tools)
if(MSVC) if(MSVC)
if(MSVC_RUNTIME STREQUAL "dynamic") string(FIND ${CMAKE_MSVC_RUNTIME_LIBRARY} "DLL" IS_SHARED)
if(NOT IS_SHARED STREQUAL "-1")
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE) set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE)
include(InstallRequiredSystemLibraries) include(InstallRequiredSystemLibraries)

View File

@ -230,25 +230,7 @@ if(FREEBSD)
endif() endif()
# Configure MSVC Runtime # Configure MSVC Runtime
if(MSVC) include(MSVCRuntime)
include(MSVCRuntime)
if(NOT DEFINED MSVC_RUNTIME)
set(MSVC_RUNTIME "dynamic" CACHE STRING "MSVC runtime type [dynamic|static]")
endif()
if(MSVC_RUNTIME STREQUAL "static")
if(BUILD_SHARED_LIBS)
message(FATAL_ERROR "Static CRT is only supported in a fully static build")
endif()
message(STATUS "Use the MSVC static runtime option carefully!")
message(STATUS "OpenSSL uses /MD by default, and is very picky")
message(STATUS "Random freeing errors are a common sign of runtime issues")
endif()
configure_msvc_runtime()
if(NOT DEFINED CMAKE_SUPPRESS_REGENERATION)
set(CMAKE_SUPPRESS_REGENERATION ON)
endif()
endif()
# Enable 64bit file support on linux and FreeBSD. # Enable 64bit file support on linux and FreeBSD.
if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux" OR FREEBSD) if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux" OR FREEBSD)

View File

@ -1,6 +1,5 @@
message("PRELOADING windows cache") message("PRELOADING windows cache")
set (CMAKE_WINDOWS_VERSION "WIN7" CACHE STRING "windows build version") set (CMAKE_WINDOWS_VERSION "WIN7" CACHE STRING "windows build version")
set (MSVC_RUNTIME "static" CACHE STRING "link MSVC runtime static")
set (BUILD_SHARED_LIBS OFF CACHE BOOL "build static linked executable") set (BUILD_SHARED_LIBS OFF CACHE BOOL "build static linked executable")
set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" CACHE STRING "MSVC runtime to use") set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" CACHE STRING "MSVC runtime to use")
set (OPENSSL_USE_STATIC_LIBS ON CACHE BOOL "link OpenSSL static") set (OPENSSL_USE_STATIC_LIBS ON CACHE BOOL "link OpenSSL static")

View File

@ -44,25 +44,7 @@ if(NOT CMAKE_BUILD_TYPE)
endif() endif()
# Configure MSVC Runtime # Configure MSVC Runtime
if(MSVC) include(MSVCRuntime)
include(MSVCRuntime)
if(NOT DEFINED MSVC_RUNTIME)
set(MSVC_RUNTIME "dynamic" CACHE STRING "MSVC runtime type [dynamic|static]")
endif()
if(MSVC_RUNTIME STREQUAL "static")
if(BUILD_SHARED_LIBS)
message(FATAL_ERROR "Static CRT is only supported in a fully static build")
endif()
message(STATUS "Use the MSVC static runtime option carefully!")
message(STATUS "OpenSSL uses /MD by default, and is very picky")
message(STATUS "Random freeing errors are a common sign of runtime issues")
endif()
configure_msvc_runtime()
if(NOT DEFINED CMAKE_SUPPRESS_REGENERATION)
set(CMAKE_SUPPRESS_REGENERATION ON)
endif()
endif()
if(WITH_WIN_CONSOLE) if(WITH_WIN_CONSOLE)
set(WIN32_GUI_FLAG "") set(WIN32_GUI_FLAG "")

View File

@ -27,6 +27,8 @@ option(CMAKE_VERBOSE_MAKEFILE "verbose CMake makefile" ON)
option(CMAKE_POSITION_INDEPENDENT_CODE "build with position independent code (-fPIC or -fPIE)" ON) option(CMAKE_POSITION_INDEPENDENT_CODE "build with position independent code (-fPIC or -fPIE)" ON)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/)
include(MSVCRuntime)
if(NOT CMAKE_BUILD_TYPE) if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "project default" FORCE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "project default" FORCE)
endif() endif()

View File

@ -1,48 +1,47 @@
if (WIN32)
macro(configure_msvc_runtime) if (CMAKE_VERSION VERSION_LESS 3.15.0)
if(MSVC) message(FATAL_ERROR "windows builds require CMake >= 3.15")
# Default to statically-linked runtime.
if("${MSVC_RUNTIME}" STREQUAL "")
set(MSVC_RUNTIME "dynamic")
endif()
# Set compiler options.
set(variables
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${MSVC_RUNTIME} STREQUAL "static")
message(STATUS "MSVC: using statically-linked runtime (/MT and /MTd).")
foreach(variable ${variables})
if(${variable} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}")
endif()
endforeach()
else()
message(STATUS "MSVC: using dynamically-linked runtime (/MD and /MDd).")
foreach(variable ${variables})
if(${variable} MATCHES "/MT")
string(REGEX REPLACE "/MT" "/MD" ${variable} "${${variable}}")
endif()
endforeach()
endif()
foreach(variable ${variables})
if(${variable} MATCHES "/Ob0")
string(REGEX REPLACE "/Ob0" "/Ob2" ${variable} "${${variable}}")
endif()
endforeach()
foreach(variable ${variables})
set(${variable} "${${variable}}" CACHE STRING "MSVC_${variable}" FORCE)
endforeach()
endif() endif()
endmacro(configure_msvc_runtime) if(NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
if (MSVC_RUNTIME STREQUAL "dynamic")
set(MSVC_DEFAULT_RUNTIME "MultiThreadedDLL")
elseif(MSVC_RUNTIME STREQUAL "static")
set(MSVC_DEFAULT_RUNTIME "MultiThreaded")
else()
message(WARNING "invalid MSVC_RUNTIME (deprecated) value '${MSVC_RUNTIME}', ignoring")
endif()
if(MSVC_DEFAULT_RUNTIME)
message("Using CMAKE_MSVC_RUNTIME_LIBRARY=${MSVC_DEFAULT_RUNTIME} (derived from MSVC_RUNTIME (deprecated) value '${MSVC_RUNTIME}')" )
else()
set(MSVC_DEFAULT_RUNTIME "MultiThreaded")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
string(APPEND MSVC_DEFAULT_RUNTIME "Debug")
endif()
if (BUILD_SHARED_LIBS)
string(APPEND MSVC_DEFAULT_RUNTIME "DLL")
endif()
message("Using CMAKE_MSVC_RUNTIME_LIBRARY=${MSVC_DEFAULT_RUNTIME}" )
endif()
set(CMAKE_MSVC_RUNTIME_LIBRARY ${MSVC_DEFAULT_RUNTIME} CACHE STRING "MSVC runtime")
endif()
message("build is using MSVC runtime ${CMAKE_MSVC_RUNTIME_LIBRARY}")
string(FIND ${CMAKE_MSVC_RUNTIME_LIBRARY} "DLL" IS_SHARED)
if(IS_SHARED STREQUAL "-1")
if(BUILD_SHARED_LIBS)
message(FATAL_ERROR "Static CRT is only supported in a fully static build")
endif()
message(STATUS "Use the MSVC static runtime option carefully!")
message(STATUS "OpenSSL uses /MD by default, and is very picky")
message(STATUS "Random freeing errors are a common sign of runtime issues")
endif()
if(NOT DEFINED CMAKE_SUPPRESS_REGENERATION)
set(CMAKE_SUPPRESS_REGENERATION ON)
endif()
endif()

View File

@ -1,7 +1,6 @@
message("PRELOADING windows cache") message("PRELOADING windows cache")
set (CMAKE_WINDOWS_VERSION "WIN7" CACHE STRING "windows build version") set (CMAKE_WINDOWS_VERSION "WIN7" CACHE STRING "windows build version")
set (BUILD_SHARED_LIBS OFF CACHE BOOL "build static linked executable") set (BUILD_SHARED_LIBS OFF CACHE BOOL "build static linked executable")
set (MSVC_RUNTIME "static" CACHE STRING "link MSVC runtime static")
set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" CACHE STRING "MSVC runtime to use") set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" CACHE STRING "MSVC runtime to use")
set (OPENSSL_USE_STATIC_LIBS ON CACHE BOOL "link OpenSSL static") set (OPENSSL_USE_STATIC_LIBS ON CACHE BOOL "link OpenSSL static")
set (CHANNEL_URBDRC OFF CACHE BOOL "we do not build libusb support") set (CHANNEL_URBDRC OFF CACHE BOOL "we do not build libusb support")

View File

@ -32,6 +32,8 @@ endif()
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(MSVCRuntime)
add_library(${PROJECT_NAME} MODULE add_library(${PROJECT_NAME} MODULE
bitmap-filter.cpp bitmap-filter.cpp
) )

View File

@ -33,6 +33,8 @@ if (supported)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif() endif()
include(MSVCRuntime)
add_library(${PROJECT_NAME} MODULE add_library(${PROJECT_NAME} MODULE
cap_main.c cap_main.c
cap_config.c cap_config.c

View File

@ -32,6 +32,8 @@ endif()
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(MSVCRuntime)
add_library(${PROJECT_NAME} MODULE add_library(${PROJECT_NAME} MODULE
demo.cpp demo.cpp
) )

View File

@ -31,6 +31,8 @@ endif()
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(MSVCRuntime)
add_library(${PROJECT_NAME} MODULE add_library(${PROJECT_NAME} MODULE
dyn-channel-dump.cpp dyn-channel-dump.cpp
) )

View File

@ -49,6 +49,8 @@ if (NOT FREERDP_UNIFIED_BUILD)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
endif() endif()
endif() endif()
include(MSVCRuntime)
endif() endif()
set(DEFAULT_DEBUG_OPTION OFF) set(DEFAULT_DEBUG_OPTION OFF)