SDL: cmake: build SDL with /W3 on MSVC

From 6e6c2b8e7d49d388c81734210e00969b4808d0c5 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 8 Aug 2024 00:25:27 +0200
Subject: [PATCH] cmake: build SDL with /W3 on MSVC

The Visual Studio projects also did this.
---
 cmake/PreseedMSVCCache.cmake | 1 +
 cmake/sdlcompilers.cmake     | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/cmake/PreseedMSVCCache.cmake b/cmake/PreseedMSVCCache.cmake
index a4095275e9ce8..d0b5ebfe4b21d 100644
--- a/cmake/PreseedMSVCCache.cmake
+++ b/cmake/PreseedMSVCCache.cmake
@@ -1,5 +1,6 @@
 if(MSVC)
   function(SDL_Preseed_CMakeCache)
+    set(COMPILER_SUPPORTS_W3                             "1"   CACHE INTERNAL "Test /W3")
     set(COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS      ""    CACHE INTERNAL "Test COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS")
     set(HAVE_ALLOCA_H                                    ""    CACHE INTERNAL "Have include alloca.h")
     set(HAVE_AUDIOCLIENT_H                               "1"   CACHE INTERNAL "Have include audioclient.h")
diff --git a/cmake/sdlcompilers.cmake b/cmake/sdlcompilers.cmake
index 9db746021b253..fc352e19c86e1 100644
--- a/cmake/sdlcompilers.cmake
+++ b/cmake/sdlcompilers.cmake
@@ -30,6 +30,14 @@ function(SDL_AddCommonCompilerFlags TARGET)
   option(SDL_WERROR "Enable -Werror" OFF)
 
   get_property(TARGET_TYPE TARGET "${TARGET}" PROPERTY TYPE)
+  if(MSVC)
+    cmake_push_check_state()
+    check_c_compiler_flag("/W3" COMPILER_SUPPORTS_W3)
+    if(COMPILER_SUPPORTS_W3)
+      target_compile_options(${TARGET} PRIVATE "/W3")
+    endif()
+    cmake_pop_check_state()
+  endif()
 
   if(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QCC)
     if(MINGW)