From e0e72f9c283203543c19d5ebec79ee7d84b315b7 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 8 Nov 2023 15:18:34 +0100 Subject: [PATCH] [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 --- CMakeCPack.cmake | 4 +- CMakeLists.txt | 20 +--- ci/cmake-preloads/config-windows.txt | 1 - client/SDL/CMakeLists.txt | 20 +--- client/Sample/CMakeLists.txt | 2 + cmake/MSVCRuntime.cmake | 93 +++++++++---------- packaging/windows/preload.cmake | 1 - .../modules/bitmap-filter/CMakeLists.txt | 2 + server/proxy/modules/capture/CMakeLists.txt | 2 + server/proxy/modules/demo/CMakeLists.txt | 2 + .../modules/dyn-channel-dump/CMakeLists.txt | 2 + winpr/CMakeLists.txt | 2 + 12 files changed, 63 insertions(+), 88 deletions(-) diff --git a/CMakeCPack.cmake b/CMakeCPack.cmake index 75ba20af2..b53e30aa1 100644 --- a/CMakeCPack.cmake +++ b/CMakeCPack.cmake @@ -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) 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) include(InstallRequiredSystemLibraries) diff --git a/CMakeLists.txt b/CMakeLists.txt index e8efbe21e..83761d5a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,25 +230,7 @@ if(FREEBSD) endif() # Configure MSVC Runtime -if(MSVC) - 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() +include(MSVCRuntime) # Enable 64bit file support on linux and FreeBSD. if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux" OR FREEBSD) diff --git a/ci/cmake-preloads/config-windows.txt b/ci/cmake-preloads/config-windows.txt index 21c42ea76..e876c16b2 100644 --- a/ci/cmake-preloads/config-windows.txt +++ b/ci/cmake-preloads/config-windows.txt @@ -1,6 +1,5 @@ message("PRELOADING windows cache") 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 (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" CACHE STRING "MSVC runtime to use") set (OPENSSL_USE_STATIC_LIBS ON CACHE BOOL "link OpenSSL static") diff --git a/client/SDL/CMakeLists.txt b/client/SDL/CMakeLists.txt index e6a80da16..4d229d955 100644 --- a/client/SDL/CMakeLists.txt +++ b/client/SDL/CMakeLists.txt @@ -44,25 +44,7 @@ if(NOT CMAKE_BUILD_TYPE) endif() # Configure MSVC Runtime -if(MSVC) - 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() +include(MSVCRuntime) if(WITH_WIN_CONSOLE) set(WIN32_GUI_FLAG "") diff --git a/client/Sample/CMakeLists.txt b/client/Sample/CMakeLists.txt index 900fe89d1..abd63c6dd 100644 --- a/client/Sample/CMakeLists.txt +++ b/client/Sample/CMakeLists.txt @@ -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) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/) +include(MSVCRuntime) + if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "project default" FORCE) endif() diff --git a/cmake/MSVCRuntime.cmake b/cmake/MSVCRuntime.cmake index 7b396e118..f3fb97b4c 100644 --- a/cmake/MSVCRuntime.cmake +++ b/cmake/MSVCRuntime.cmake @@ -1,48 +1,47 @@ - -macro(configure_msvc_runtime) - if(MSVC) - # 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() +if (WIN32) + if (CMAKE_VERSION VERSION_LESS 3.15.0) + message(FATAL_ERROR "windows builds require CMake >= 3.15") 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() diff --git a/packaging/windows/preload.cmake b/packaging/windows/preload.cmake index e9bfafb6c..1fb89e0ae 100644 --- a/packaging/windows/preload.cmake +++ b/packaging/windows/preload.cmake @@ -1,7 +1,6 @@ message("PRELOADING windows cache") set (CMAKE_WINDOWS_VERSION "WIN7" CACHE STRING "windows build version") 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 (OPENSSL_USE_STATIC_LIBS ON CACHE BOOL "link OpenSSL static") set (CHANNEL_URBDRC OFF CACHE BOOL "we do not build libusb support") diff --git a/server/proxy/modules/bitmap-filter/CMakeLists.txt b/server/proxy/modules/bitmap-filter/CMakeLists.txt index caca677ac..e3ef987d0 100644 --- a/server/proxy/modules/bitmap-filter/CMakeLists.txt +++ b/server/proxy/modules/bitmap-filter/CMakeLists.txt @@ -32,6 +32,8 @@ endif() set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) +include(MSVCRuntime) + add_library(${PROJECT_NAME} MODULE bitmap-filter.cpp ) diff --git a/server/proxy/modules/capture/CMakeLists.txt b/server/proxy/modules/capture/CMakeLists.txt index 649f40fd5..f3ac4ca1e 100644 --- a/server/proxy/modules/capture/CMakeLists.txt +++ b/server/proxy/modules/capture/CMakeLists.txt @@ -33,6 +33,8 @@ if (supported) set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) endif() +include(MSVCRuntime) + add_library(${PROJECT_NAME} MODULE cap_main.c cap_config.c diff --git a/server/proxy/modules/demo/CMakeLists.txt b/server/proxy/modules/demo/CMakeLists.txt index 79f2544f3..1dc89eaf7 100644 --- a/server/proxy/modules/demo/CMakeLists.txt +++ b/server/proxy/modules/demo/CMakeLists.txt @@ -32,6 +32,8 @@ endif() set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) +include(MSVCRuntime) + add_library(${PROJECT_NAME} MODULE demo.cpp ) diff --git a/server/proxy/modules/dyn-channel-dump/CMakeLists.txt b/server/proxy/modules/dyn-channel-dump/CMakeLists.txt index ab5a73264..00517a90a 100644 --- a/server/proxy/modules/dyn-channel-dump/CMakeLists.txt +++ b/server/proxy/modules/dyn-channel-dump/CMakeLists.txt @@ -31,6 +31,8 @@ endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +include(MSVCRuntime) + add_library(${PROJECT_NAME} MODULE dyn-channel-dump.cpp ) diff --git a/winpr/CMakeLists.txt b/winpr/CMakeLists.txt index 4db9f99c1..d2532fc0c 100644 --- a/winpr/CMakeLists.txt +++ b/winpr/CMakeLists.txt @@ -49,6 +49,8 @@ if (NOT FREERDP_UNIFIED_BUILD) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") endif() endif() + + include(MSVCRuntime) endif() set(DEFAULT_DEBUG_OPTION OFF)