SDL: cmake: avoid modifying CMAKE_C_FLAGS

From 053c37583c486b16cde757f0719c4b9299b4ef8c Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 7 Jan 2023 17:45:58 +0100
Subject: [PATCH] cmake: avoid modifying CMAKE_C_FLAGS

---
 CMakeLists.txt        | 26 +++++++-------------------
 cmake/sdlchecks.cmake |  5 ++---
 2 files changed, 9 insertions(+), 22 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 067f9665da4e..5e297e8560fa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -201,7 +201,6 @@ elseif(CMAKE_COMPILER_IS_GNUCC)
   set(SDL_ASSEMBLY_DEFAULT TRUE)
 elseif(MSVC_VERSION GREATER 1400) # VisualStudio 8.0+
   set(SDL_ASSEMBLY_DEFAULT TRUE)
-  #set(CMAKE_C_FLAGS "/ZI /WX- /
 else()
   set(SDL_ASSEMBLY_DEFAULT FALSE)
 endif()
@@ -641,8 +640,7 @@ if(USE_GCC OR USE_CLANG)
     cmake_push_check_state()
     check_c_compiler_flag("-gdwarf-4" HAVE_GDWARF_4)
     if(HAVE_GDWARF_4)
-      set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-4")
-      set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -gdwarf-4")
+      target_compile_options(sdl-global-options INTERFACE "-gdwarf-4")
     endif()
     cmake_pop_check_state()
   endif()
@@ -2799,26 +2797,16 @@ if(SDL_WERROR)
   if(MSVC)
     check_c_compiler_flag(/WX HAVE_WX)
     if(HAVE_WX)
-      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
-      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
+      target_compile_options(sdl-global-options INTERFACE "/WX")
     endif()
   elseif(USE_GCC OR USE_CLANG)
     check_c_compiler_flag(-Werror HAVE_WERROR)
     if(HAVE_WERROR)
-      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
-      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
-      set(CMAKE_OBJC_FLAGS "${CMAKE_OBJC_FLAGS} -Werror")
+      target_compile_options(sdl-global-options INTERFACE "-Werror")
     endif()
   endif()
 endif()
 
-# Append the -MMD -MT flags
-# if(DEPENDENCY_TRACKING)
-#   if(COMPILER_IS_GNUCC)
-#     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -MMD -MT \$@")
-#   endif()
-# endif()
-
 # config variables may contain generator expression, so we need to generate SDL_build_config.h in 2 steps:
 # 1. replace all `#cmakedefine`'s and `@abc@`
 configure_file("${SDL3_SOURCE_DIR}/include/build_config/SDL_build_config.h.cmake"
@@ -2971,7 +2959,7 @@ macro(asan_check_add_debug_flag2 ASAN_FLAG)
 endmacro()
 
 # enable AddressSanitizer if supported
-if (SDL_ASAN)
+if(SDL_ASAN)
   asan_check_add_debug_flag2("address")
   asan_check_add_debug_flag("bool")
   asan_check_add_debug_flag("bounds")
@@ -3048,13 +3036,13 @@ message(STATUS "")
 message(STATUS "Options:")
 list(SORT ALLOPTIONS)
 message_dictlist(ALLOPTIONS)
+message(STATUS "")
+message(STATUS " CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
 if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
-  message(STATUS "")
   message(STATUS " CMAKE_C_FLAGS_DEBUG:   ${CMAKE_C_FLAGS_DEBUG}")
   message(STATUS " CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}")
+  message(STATUS "")
 endif()
-message(STATUS "")
-message(STATUS " CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
 message(STATUS " EXTRA DEFS:    ${INFO_DEFS}")
 message(STATUS " EXTRA CFLAGS:  ${INFO_CFLAGS} ${INFO_CHECKS}")
 message(STATUS " EXTRA INCDIRS: ${INFO_INCDIRS}")
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 8abac2d69dbd..4e2873d32bab 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -1033,7 +1033,7 @@ macro(CheckHIDAPI)
         check_include_file(libusb.h HAVE_LIBUSB_H ${PKG_LIBUSB_CFLAGS})
         if(HAVE_LIBUSB_H)
           set(HAVE_LIBUSB TRUE)
-          set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PKG_LIBUSB_CFLAGS}")
+          target_compile_options(sdl-build-options INTERFACE ${PKG_LIBUSB_CFLAGS})
           if(HIDAPI_ONLY_LIBUSB)
             list(APPEND EXTRA_LIBS ${PKG_LIBUSB_LIBRARIES})
           else()
@@ -1108,8 +1108,7 @@ macro(CheckRPI)
       file(GLOB VIDEO_RPI_SOURCES ${SDL3_SOURCE_DIR}/src/video/raspberry/*.c)
       list(APPEND SOURCE_FILES ${VIDEO_RPI_SOURCES})
       list(APPEND EXTRA_LIBS ${VIDEO_RPI_LIBRARIES})
-      # !!! FIXME: shouldn't be using CMAKE_C_FLAGS, right?
-      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VIDEO_RPI_INCLUDE_FLAGS} ${VIDEO_RPI_LIBRARY_FLAGS}")
+      target_compile_options(sdl-build-options INTERFACE ${VIDEO_RPI_INCLUDE_FLAGS} ${VIDEO_RPI_LIBRARY_FLAGS})
       list(APPEND EXTRA_LDFLAGS ${VIDEO_RPI_LDFLAGS})
     endif()
   endif()