From 4970af89992951eb2b4973bf32447445e9b38f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Mon, 18 Oct 2021 17:45:30 +0200 Subject: [PATCH] Use CMake property to drive static linking Using a newer CMake version allows us to make use of the MSVC_RUNTIME_LIBRARY added in CMake 3.15, which handles static linking of the runtime library with MSVC a lot simpler --- CMakeLists.txt | 11 ----------- cmake/Macros.cmake | 21 +++++++++++++++------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36631c4e6..f9b1a68cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -207,17 +207,6 @@ if(SFML_OS_WINDOWS) if(BUILD_SHARED_LIBS AND SFML_USE_STATIC_STD_LIBS) message(FATAL_ERROR "BUILD_SHARED_LIBS and SFML_USE_STATIC_STD_LIBS cannot be used together") endif() - - # for VC++, we can apply it globally by modifying the compiler flags - if((SFML_COMPILER_MSVC OR (SFML_COMPILER_CLANG AND NOT MINGW)) AND SFML_USE_STATIC_STD_LIBS) - foreach(flag - CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE - CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) - if(${flag} MATCHES "/MD") - string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}") - endif() - endforeach() - endif() endif() # setup Mac OS X stuff diff --git a/cmake/Macros.cmake b/cmake/Macros.cmake index d40bc4ca1..3629aee0d 100644 --- a/cmake/Macros.cmake +++ b/cmake/Macros.cmake @@ -115,20 +115,25 @@ macro(sfml_add_library module) ) target_sources(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${target}.rc") source_group("" FILES "${CMAKE_CURRENT_BINARY_DIR}/${target}.rc") + + if (SFML_COMPILER_GCC OR SFML_COMPILER_CLANG) + # on Windows + gcc/clang get rid of "lib" prefix for shared libraries, + # and transform the ".dll.a" suffix into ".a" for import libraries + set_target_properties(${target} PROPERTIES PREFIX "") + set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".a") + endif() else() set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -d) endif() - if (SFML_OS_WINDOWS AND (SFML_COMPILER_GCC OR SFML_COMPILER_CLANG)) - # on Windows + gcc/clang get rid of "lib" prefix for shared libraries, - # and transform the ".dll.a" suffix into ".a" for import libraries - set_target_properties(${target} PROPERTIES PREFIX "") - set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".a") - endif() else() set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -s-d) set_target_properties(${target} PROPERTIES RELEASE_POSTFIX -s) set_target_properties(${target} PROPERTIES MINSIZEREL_POSTFIX -s) set_target_properties(${target} PROPERTIES RELWITHDEBINFO_POSTFIX -s) + + if (SFML_USE_STATIC_STD_LIBS) + set_property(TARGET ${target} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() endif() # set the version and soversion of the target (for compatible systems -- mostly Linuxes) @@ -276,6 +281,10 @@ macro(sfml_add_example target) add_executable(${target} ${target_input}) endif() + if (SFML_USE_STATIC_STD_LIBS) + set_property(TARGET ${target} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() + set_target_warnings(${target}) # set the debug suffix