SDL: cmake: Enable CET compatibility for x86/x64 targets using VS 16.7+

From 2ceea46061ee5aa8fbfd909cdf622df12d397968 Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Sun, 21 Aug 2022 14:15:06 -0500
Subject: [PATCH] cmake: Enable CET compatibility for x86/x64 targets using VS
 16.7+

---
 CMakeLists.txt      | 5 +++++
 test/CMakeLists.txt | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index aa36eb02cf1..276c31e504b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -688,6 +688,11 @@ if(MSVC)
   target_compile_definitions(sdl-build-options INTERFACE "-D_CRT_SECURE_NO_DEPRECATE")
   target_compile_definitions(sdl-build-options INTERFACE "-D_CRT_NONSTDC_NO_DEPRECATE")
   target_compile_definitions(sdl-build-options INTERFACE "-D_CRT_SECURE_NO_WARNINGS")
+
+  # CET support was added in VS 16.7
+  if(MSVC_VERSION GREATER 1926 AND NOT CMAKE_GENERATOR_PLATFORM MATCHES ARM)
+    list(APPEND EXTRA_LDFLAGS_BUILD "-CETCOMPAT")
+  endif()
 endif()
 
 if(MSVC)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 9639b60489a..8b2738a7da4 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -47,6 +47,11 @@ if(WINDOWS)
         link_libraries(mingw32)
     endif()
 
+    # CET support was added in VS 16.7
+    if(MSVC_VERSION GREATER 1926 AND NOT CMAKE_GENERATOR_PLATFORM MATCHES ARM)
+        link_libraries(-CETCOMPAT)
+    endif()
+
     # FIXME: Parent directory CMakeLists.txt only sets these for mingw/cygwin,
     # but we need them for VS as well.
     link_libraries(SDL2main)