SDL: cmake: collect cflags in sdl-build-options and sdl-global-options

From 373a6464c82c844426889e230b428a62a8136d0a Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 7 Jan 2023 16:15:28 +0100
Subject: [PATCH] cmake: collect cflags in sdl-build-options and
 sdl-global-options

---
 CMakeLists.txt        | 100 +++++++++++++++++++++++++-----------------
 cmake/sdlchecks.cmake |  24 +++++-----
 test/CMakeLists.txt   |   6 ++-
 3 files changed, 77 insertions(+), 53 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f13d957db66e..8b19cb671314 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,14 +17,12 @@ set(EXTRA_LDFLAGS)
 set(CMAKE_DEPENDS)
 set(PKGCONFIG_DEPENDS)
 
-# This is a virtual "library" that just exists to collect up compiler and
-#  linker options that used to be global to this CMake project. When you
-#  specify it as part of a real library's target_link_libraries(), that
-#  library will also gain all those build options too. This is meant to
-#  modularize old calls to the global add_definitions and include_directories,
-#  etc. See https://github.com/libsdl-org/SDL/issues/4150
+# The sdl-build-options interface library collects all PRIVATE build options for the SDL libraries
 add_library(sdl-build-options INTERFACE)
 
+# The sdl-global-options interface library collects all PRIVATE build options for the SDL libraries + test + ...
+add_library(sdl-global-options INTERFACE)
+
 if(WINDOWS_STORE)
   target_compile_definitions(sdl-build-options INTERFACE "SDL_BUILDING_WINRT=1")
   target_compile_options(sdl-build-options INTERFACE "-ZW")
@@ -78,7 +76,7 @@ include(${SDL3_SOURCE_DIR}/cmake/CheckCPUArchitecture.cmake)
 # with large inode numbers
 check_symbol_exists("__GLIBC__" "stdlib.h" LIBC_IS_GLIBC)
 if (LIBC_IS_GLIBC AND CMAKE_SIZEOF_VOID_P EQUAL 4)
-    add_definitions(-D_FILE_OFFSET_BITS=64)
+    target_compile_definitions(sdl-build-options "_FILE_OFFSET_BITS=64")
 endif()
 
 # See docs/release_checklist.md
@@ -539,17 +537,17 @@ if(USE_GCC OR USE_CLANG)
   #       /W3 as -Wall, and /W4 as -Wall -Wextra.  So: /W3 is enough.
   check_c_compiler_flag(-Wall HAVE_GCC_WALL)
   if(MSVC_CLANG)
-    list(APPEND EXTRA_CFLAGS "/W3")
+    target_compile_options(sdl-global-options INTERFACE "/W3")
   elseif(HAVE_GCC_WALL)
-    list(APPEND EXTRA_CFLAGS "-Wall")
+    target_compile_options(sdl-global-options INTERFACE "-Wall")
     if(HAIKU)
-      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-multichar")
+      target_compile_options(sdl-global-options INTERFACE "-Wno-multichar")
     endif()
   endif()
 
   check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING)
   if(HAVE_GCC_NO_STRICT_ALIASING)
-    list(APPEND EXTRA_CFLAGS "-fno-strict-aliasing")
+    target_compile_options(sdl-global-options INTERFACE "-fno-strict-aliasing")
   endif()
 
   check_c_compiler_flag(-Wdeclaration-after-statement HAVE_GCC_WDECLARATION_AFTER_STATEMENT)
@@ -557,10 +555,10 @@ if(USE_GCC OR USE_CLANG)
     if(SDL_WERROR)
       check_c_compiler_flag(-Werror=declaration-after-statement HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
       if(HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
-        list(APPEND EXTRA_CFLAGS "-Werror=declaration-after-statement")
+        target_compile_options(sdl-global-options INTERFACE "-Werror=declaration-after-statement")
       endif()
     endif()
-    list(APPEND EXTRA_CFLAGS "-Wdeclaration-after-statement")
+    target_compile_options(sdl-global-options INTERFACE "-Wdeclaration-after-statement")
   endif()
 
   check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION)
@@ -568,10 +566,10 @@ if(USE_GCC OR USE_CLANG)
     if(SDL_WERROR)
       check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION)
       if(HAVE_GCC_WERROR_DOCUMENTATION)
-        list(APPEND EXTRA_CFLAGS "-Werror=documentation")
+        target_compile_options(sdl-global-options INTERFACE "-Werror=documentation")
       endif()
     endif()
-    list(APPEND EXTRA_CFLAGS "-Wdocumentation")
+    target_compile_options(sdl-global-options INTERFACE "-Wdocumentation")
   endif()
 
   check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
@@ -579,10 +577,10 @@ if(USE_GCC OR USE_CLANG)
     if(SDL_WERROR)
       check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
       if(HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
-        list(APPEND EXTRA_CFLAGS "-Werror=documentation-unknown-command")
+        target_compile_options(sdl-global-options INTERFACE "-Werror=documentation-unknown-command")
       endif()
     endif()
-    list(APPEND EXTRA_CFLAGS "-Wdocumentation-unknown-command")
+    target_compile_options(sdl-global-options INTERFACE "-Wdocumentation-unknown-command")
   endif()
 
   if(DEPENDENCY_TRACKING)
@@ -614,7 +612,7 @@ if(USE_GCC OR USE_CLANG)
 
   check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
   if(HAVE_GCC_WSHADOW)
-    list(APPEND EXTRA_CFLAGS "-Wshadow")
+    target_compile_options(sdl-global-options INTERFACE "-Wshadow")
   endif()
 
   if(APPLE)
@@ -654,9 +652,9 @@ if(USE_GCC OR USE_CLANG)
   # Force color diagnostics when one of these conditions are met
   if(DEFINED ENV{CI} OR DEFINED ENV{USE_CCACHE} OR CMAKE_GENERATOR MATCHES Ninja)
     if(EMSCRIPTEN OR (USE_GCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.9))
-      list(APPEND EXTRA_CFLAGS -fdiagnostics-color=always)
+      target_compile_options(sdl-global-options INTERFACE -fdiagnostics-color=always)
     elseif(USE_CLANG AND NOT CMAKE_C_COMPILER_ID MATCHES AppleClang)
-      list(APPEND EXTRA_CFLAGS -fcolor-diagnostics)
+      target_compile_options(sdl-global-options INTERFACE -fcolor-diagnostics)
     endif()
   endif()
 endif()
@@ -702,7 +700,7 @@ if(SDL_ASSEMBLY)
           int main(int argc, char **argv) { return 0; }" HAVE_MMX)
       cmake_pop_check_state()
       if(HAVE_MMX)
-        list(APPEND EXTRA_CFLAGS "-mmmx")
+        target_compile_options(sdl-build-options INTERFACE "-mmmx")
       endif()
     endif()
 
@@ -727,7 +725,7 @@ if(SDL_ASSEMBLY)
       cmake_pop_check_state()
       if(CPU_SUPPORTS_SSE)
         set(HAVE_SSE ON)
-        list(APPEND EXTRA_CFLAGS "-msse")
+        target_compile_options(sdl-build-options INTERFACE "-msse")
       endif()
     endif()
 
@@ -752,7 +750,7 @@ if(SDL_ASSEMBLY)
       cmake_pop_check_state()
       if(CPU_SUPPORTS_SSE2)
         set(HAVE_SSE2 TRUE)
-        list(APPEND EXTRA_CFLAGS "-msse2")
+        target_compile_options(sdl-build-options INTERFACE "-msse2")
       endif()
     endif()
 
@@ -777,7 +775,7 @@ if(SDL_ASSEMBLY)
       cmake_pop_check_state()
       if(CPU_SUPPORTS_SSE3)
         set(HAVE_SSE3 TRUE)
-        list(APPEND EXTRA_CFLAGS "-msse3")
+        target_compile_options(sdl-build-options INTERFACE "-msse3")
       endif()
     endif()
 
@@ -786,7 +784,7 @@ if(SDL_ASSEMBLY)
         if(USE_GCC)
           check_c_compiler_flag(-mfpmath=387 HAVE_FP_387)
           if(HAVE_FP_387)
-            list(APPEND EXTRA_CFLAGS "-mfpmath=387")
+            target_compile_options(sdl-build-options INTERFACE "-mfpmath=387")
           endif()
         endif()
       endif()
@@ -813,7 +811,7 @@ if(SDL_ASSEMBLY)
       cmake_pop_check_state()
       if(CPU_SUPPORTS_ALTIVEC OR HAVE_ALTIVEC_H_HDR)
         set(HAVE_ALTIVEC TRUE) # if only HAVE_ALTIVEC_H_HDR is set
-        list(APPEND EXTRA_CFLAGS "-maltivec")
+        target_compile_options(sdl-build-options INTERFACE "-maltivec")
         set(SDL_ALTIVEC_BLITTERS 1)
         if(HAVE_ALTIVEC_H_HDR)
           set(HAVE_ALTIVEC_H 1)
@@ -833,7 +831,7 @@ if(SDL_ASSEMBLY)
       cmake_pop_check_state()
 
       if(CPU_HAS_LSX AND HAVE_LSXINTRIN_H)
-        list(APPEND EXTRA_CFLAGS "-mlsx")
+        target_compile_options(sdl-build-options INTERFACE "-mlsx")
         set(HAS_LSX TRUE)
       endif()
     endif()
@@ -849,7 +847,7 @@ if(SDL_ASSEMBLY)
       check_include_file("lasxintrin.h" HAVE_LASXINTRIN_H)
       cmake_pop_check_state()
       if(CPU_HAS_LASX AND HAVE_LASXINTRIN_H)
-        list(APPEND EXTRA_CFLAGS "-mlasx")
+        target_compile_options(sdl-build-options INTERFACE "-mlasx")
         set(HAS_LASX TRUE)
       endif()
     endif()
@@ -1481,7 +1479,7 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
         pkg_search_module(DBUS dbus-1 dbus)
         if(DBUS_FOUND)
           set(HAVE_DBUS_DBUS_H TRUE)
-          target_include_directories(sdl-build-options INTERFACE "${DBUS_INCLUDE_DIRS}")
+          target_include_directories(sdl-build-options INTERFACE ${DBUS_INCLUDE_DIRS})
           # Fcitx need only dbus.
           set(HAVE_FCITX TRUE)
           set(HAVE_DBUS TRUE)
@@ -1493,7 +1491,7 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
         find_path(HAVE_SYS_INOTIFY_H NAMES sys/inotify.h)
         if(IBUS_FOUND AND HAVE_SYS_INOTIFY_H)
           set(HAVE_IBUS_IBUS_H TRUE)
-          target_include_directories(sdl-build-options INTERFACE "${IBUS_INCLUDE_DIRS}")
+          target_include_directories(sdl-build-options INTERFACE ${IBUS_INCLUDE_DIRS})
           set(HAVE_IBUS TRUE)
         endif()
       endif()
@@ -2586,7 +2584,8 @@ elseif(PSP)
   )
 
 elseif(PS2)
-  list(APPEND EXTRA_CFLAGS "-DPS2" "-D__PS2__" "-I$ENV{PS2SDK}/ports/include" "-I$ENV{PS2DEV}/gsKit/include")
+  target_compile_definitions(sdl-build-options INTERFACE "PS2" "__PS2__")
+  target_include_directories(sdl-build-options INTERFACE "$ENV{PS2SDK}/ports/include" "$ENV{PS2DEV}/gsKit/include")
 
   file(GLOB PS2_CORE_SOURCES ${SDL3_SOURCE_DIR}/src/core/ps2/*.c)
   set(SOURCE_FILES ${SOURCE_FILES} ${PS2_CORE_SOURCES})
@@ -3018,6 +3017,22 @@ if(SDL_TESTS)
   set(HAVE_TESTS ON)
 endif()
 
+get_property(INFO_CFLAGS TARGET sdl-build-options PROPERTY INTERFACE_COMPILE_OPTIONS)
+list(REMOVE_DUPLICATES INFO_CFLAGS)
+listtostr(INFO_CFLAGS INFO_CFLAGS)
+
+get_property(INFO_INCDIRS TARGET sdl-build-options PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
+list(REMOVE_DUPLICATES INFO_INCDIRS)
+listtostr(INFO_INCDIRS INFO_INCDIRS "-I")
+
+get_property(INFO_DEFS TARGET sdl-build-options PROPERTY INTERFACE_COMPILE_DEFINITIONS)
+list(REMOVE_DUPLICATES INFO_DEFS)
+listtostr(INFO_DEFS INFO_DEFS "-D")
+
+get_property(INFO_CHECKS TARGET sdl-global-options PROPERTY INTERFACE_COMPILE_OPTIONS)
+list(REMOVE_DUPLICATES INFO_CHECKS)
+listtostr(INFO_CHECKS INFO_CHECKS)
+
 ##### Info output #####
 message(STATUS "")
 message(STATUS "SDL3 was configured with the following options:")
@@ -3047,8 +3062,10 @@ if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
   message(STATUS " CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}")
 endif()
 message(STATUS "")
-message(STATUS " CFLAGS:        ${CMAKE_C_FLAGS}")
-message(STATUS " EXTRA_CFLAGS:  ${EXTRA_CFLAGS}")
+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}")
 message(STATUS " EXTRA_LDFLAGS: ${EXTRA_LDFLAGS} ${EXTRA_LDFLAGS_BUILD}")
 message(STATUS " EXTRA_LIBS:    ${EXTRA_LIBS}")
 message(STATUS "")
@@ -3083,9 +3100,6 @@ if(WARN_ABOUT_ARM_NEON_ASM_MIT)
   message(STATUS "    -DSDL_ARMNEON=OFF")
 endif()
 
-# Ensure that the extra cflags are used at compile time
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} ${EXTRA_CFLAGS_BUILD}")
-
 if(ANDROID)
   target_include_directories(sdl-build-options INTERFACE "${ANDROID_NDK}/sources/android/cpufeatures")
 endif()
@@ -3165,6 +3179,7 @@ if(SDL_SHARED)
   )
   # This picks up all the compiler options and such we've accumulated up to here.
   target_link_libraries(SDL3 PRIVATE $<BUILD_INTERFACE:sdl-build-options>)
+  target_link_libraries(SDL3 PRIVATE $<BUILD_INTERFACE:sdl-global-options>)
   if(MINGW OR CYGWIN)
     if(NOT CMAKE_VERSION VERSION_LESS "3.13")
       target_link_options(SDL3 PRIVATE -static-libgcc)
@@ -3184,7 +3199,8 @@ if(SDL_STATIC)
   add_library(SDL3::SDL3-static ALIAS SDL3-static)
   set_target_properties(SDL3-static PROPERTIES
     OUTPUT_NAME "${sdl_static_libname}"
-    POSITION_INDEPENDENT_CODE "${SDL_STATIC_PIC}")
+    POSITION_INDEPENDENT_CODE "${SDL_STATIC_PIC}"
+  )
   target_compile_definitions(SDL3-static PRIVATE SDL_STATIC_LIB)
   target_link_libraries(SDL3-static PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS} ${CMAKE_DEPENDS})
   target_include_directories(SDL3-static
@@ -3200,6 +3216,7 @@ if(SDL_STATIC)
   )
   # This picks up all the compiler options and such we've accumulated up to here.
   target_link_libraries(SDL3-static PRIVATE $<BUILD_INTERFACE:sdl-build-options>)
+  target_link_libraries(SDL3-static PRIVATE $<BUILD_INTERFACE:sdl-global-options>)
   # Use `Compatible Interface Properties` to allow consumers to enforce a shared/static library
   set_property(TARGET SDL3-static PROPERTY INTERFACE_SDL3_SHARED FALSE)
   set_property(TARGET SDL3-static APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL SDL3_SHARED)
@@ -3208,9 +3225,11 @@ if(SDL_STATIC)
   endif()
 endif()
 
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSDL_BUILD_MAJOR_VERSION=${SDL_MAJOR_VERSION}")
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSDL_BUILD_MINOR_VERSION=${SDL_MINOR_VERSION}")
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSDL_BUILD_MICRO_VERSION=${SDL_MICRO_VERSION}")
+target_compile_definitions(sdl-build-options INTERFACE
+  "SDL_BUILD_MAJOR_VERSION=${SDL_MAJOR_VERSION}"
+  "SDL_BUILD_MINOR_VERSION=${SDL_MINOR_VERSION}"
+  "SDL_BUILD_MICRO_VERSION=${SDL_MICRO_VERSION}"
+)
 
 ##### Tests #####
 
@@ -3218,6 +3237,7 @@ if(SDL_TEST)
   file(GLOB TEST_SOURCES ${SDL3_SOURCE_DIR}/src/test/*.c)
   add_library(SDL3_test STATIC ${TEST_SOURCES})
   add_library(SDL3::SDL3_test ALIAS SDL3_test)
+  target_link_libraries(SDL3_test PRIVATE $<BUILD_INTERFACE:sdl-global-options>)
   set_target_properties(SDL3_test PROPERTIES
       EXPORT_NAME SDL3_test)
   target_include_directories(SDL3_test
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 84a104c35e70..515ad490fa35 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -138,7 +138,7 @@ macro(CheckPipewire)
             file(GLOB PIPEWIRE_SOURCES ${SDL3_SOURCE_DIR}/src/audio/pipewire/*.c)
             list(APPEND SOURCE_FILES ${PIPEWIRE_SOURCES})
             set(SDL_AUDIO_DRIVER_PIPEWIRE 1)
-            list(APPEND EXTRA_CFLAGS ${PKG_PIPEWIRE_CFLAGS})
+            target_compile_options(sdl-build-options INTERFACE ${PKG_PIPEWIRE_CFLAGS})
             if(SDL_PIPEWIRE_SHARED AND NOT HAVE_SDL_LOADSO)
                 message_warn("You must have SDL_LoadObject() support for dynamic Pipewire loading")
             endif()
@@ -167,7 +167,7 @@ macro(CheckPulseAudio)
       file(GLOB PULSEAUDIO_SOURCES ${SDL3_SOURCE_DIR}/src/audio/pulseaudio/*.c)
       list(APPEND SOURCE_FILES ${PULSEAUDIO_SOURCES})
       set(SDL_AUDIO_DRIVER_PULSEAUDIO 1)
-      list(APPEND EXTRA_CFLAGS ${PKG_PULSEAUDIO_CFLAGS})
+      target_compile_options(sdl-build-options INTERFACE ${PKG_PULSEAUDIO_CFLAGS})
       if(SDL_PULSEAUDIO_SHARED AND NOT HAVE_SDL_LOADSO)
         message_warn("You must have SDL_LoadObject() support for dynamic PulseAudio loading")
       endif()
@@ -196,7 +196,7 @@ macro(CheckJACK)
       file(GLOB JACK_SOURCES ${SDL3_SOURCE_DIR}/src/audio/jack/*.c)
       list(APPEND SOURCE_FILES ${JACK_SOURCES})
       set(SDL_AUDIO_DRIVER_JACK 1)
-      list(APPEND EXTRA_CFLAGS ${PKG_JACK_CFLAGS})
+      target_compile_options(sdl-build-options INTERFACE ${PKG_JACK_CFLAGS})
       if(SDL_JACK_SHARED AND NOT HAVE_SDL_LOADSO)
         message_warn("You must have SDL_LoadObject() support for dynamic JACK audio loading")
       endif()
@@ -225,7 +225,7 @@ macro(CheckSNDIO)
       file(GLOB SNDIO_SOURCES ${SDL3_SOURCE_DIR}/src/audio/sndio/*.c)
       list(APPEND SOURCE_FILES ${SNDIO_SOURCES})
       set(SDL_AUDIO_DRIVER_SNDIO 1)
-      list(APPEND EXTRA_CFLAGS ${PKG_SNDIO_CFLAGS})
+      target_compile_options(sdl-build-options INTERFACE ${PKG_SNDIO_CFLAGS})
       if(SDL_SNDIO_SHARED AND NOT HAVE_SDL_LOADSO)
         message_warn("You must have SDL_LoadObject() support for dynamic sndio loading")
       endif()
@@ -318,7 +318,7 @@ macro(CheckX11)
     )
 
     if(X_INCLUDEDIR)
-      list(APPEND EXTRA_CFLAGS "-I${X_INCLUDEDIR}")
+      target_include_directories(sdl-build-options INTERFACE "${X_INCLUDEDIR}")
       list(APPEND CMAKE_REQUIRED_INCLUDES ${X_INCLUDEDIR})
     endif()
 
@@ -356,7 +356,7 @@ macro(CheckX11)
           list(APPEND EXTRA_LIBS ipc)
         endif()
         if(NOT HAVE_SHMAT_IN_LIBIPC)
-          list(APPEND EXTRA_CFLAGS "-DNO_SHARED_MEMORY")
+          target_compile_definitions(sdl-build-options INTERFACE "NO_SHARED_MEMORY")
         endif()
       endif()
 
@@ -491,7 +491,7 @@ macro(CheckX11)
   endif()
   if(NOT HAVE_X11)
     # Prevent Mesa from including X11 headers
-    list(APPEND EXTRA_CFLAGS "-DMESA_EGL_NO_X11_HEADERS -DEGL_NO_X11")
+    target_compile_definitions(sdl-build-options INTERFACE "MESA_EGL_NO_X11_HEADERS" "EGL_NO_X11")
   endif()
   cmake_pop_check_state()
 endmacro()
@@ -553,8 +553,8 @@ macro(CheckWayland)
     endif()
 
     if(WAYLAND_FOUND)
-      target_link_directories(sdl-build-options INTERFACE "${PKG_WAYLAND_LIBRARY_DIRS}")
-      target_include_directories(sdl-build-options INTERFACE "${PKG_WAYLAND_INCLUDE_DIRS}")
+      target_link_directories(sdl-build-options INTERFACE ${PKG_WAYLAND_LIBRARY_DIRS})
+      target_include_directories(sdl-build-options INTERFACE ${PKG_WAYLAND_INCLUDE_DIRS})
 
       set(HAVE_WAYLAND TRUE)
       set(HAVE_SDL_VIDEO TRUE)
@@ -806,7 +806,7 @@ macro(CheckPTHREAD)
       }" HAVE_PTHREADS)
     if(HAVE_PTHREADS)
       set(SDL_THREAD_PTHREAD 1)
-      list(APPEND EXTRA_CFLAGS ${PTHREAD_CFLAGS})
+      target_compile_options(sdl-build-options INTERFACE ${PTHREAD_CFLAGS})
       list(APPEND EXTRA_LDFLAGS ${PTHREAD_LDFLAGS})
       list(APPEND SDL_CFLAGS ${PTHREAD_CFLAGS})
 
@@ -1014,7 +1014,7 @@ macro(CheckUSBHID)
     set(SDL_JOYSTICK_USBHID 1)
     file(GLOB BSD_JOYSTICK_SOURCES ${SDL3_SOURCE_DIR}/src/joystick/bsd/*.c)
     list(APPEND SOURCE_FILES ${BSD_JOYSTICK_SOURCES})
-    list(APPEND EXTRA_CFLAGS ${USB_CFLAGS})
+    target_compile_options(sdl-build-options INTERFACE ${USB_CFLAGS})
     list(APPEND EXTRA_LIBS ${USB_LIBS})
     set(HAVE_SDL_JOYSTICK TRUE)
 
@@ -1148,7 +1148,7 @@ macro(CheckKMSDRM)
       file(GLOB KMSDRM_SOURCES ${SDL3_SOURCE_DIR}/src/video/kmsdrm/*.c)
       list(APPEND SOURCE_FILES ${KMSDRM_SOURCES})
 
-      list(APPEND EXTRA_CFLAGS ${PKG_KMSDRM_CFLAGS})
+      target_compile_options(sdl-build-options INTERFACE ${PKG_KMSDRM_CFLAGS})
 
       set(SDL_VIDEO_DRIVER_KMSDRM 1)
 
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 851b7e3f96a4..3736b32c750e 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -94,6 +94,10 @@ macro(add_sdl_test_executable TARGET)
     if(OPENGL_FOUND)
         target_compile_definitions(${TARGET} PRIVATE HAVE_OPENGL)
     endif()
+
+    if(TARGET sdl-global-options)
+        target_link_libraries(${TARGET} PRIVATE $<BUILD_INTERFACE:sdl-global-options>)
+    endif()
 endmacro()
 
 check_include_file(signal.h HAVE_SIGNAL_H)
@@ -305,7 +309,7 @@ if(N3DS)
     foreach(APP ${SDL_TEST_EXECUTABLES})
         get_target_property(TARGET_BINARY_DIR ${APP} BINARY_DIR)
         set(SMDH_FILE "${TARGET_BINARY_DIR}/${APP}.smdh")
-        ctr_generate_smdh("${SMDH_FILE}" 
+        ctr_generate_smdh("${SMDH_FILE}"
             NAME "SDL-${APP}"
             DESCRIPTION "SDL3 Test suite"
             AUTHOR "SDL3 Contributors"