From f3a3c803cad6645a2b2f3c3a9c53b0f74b4831b4 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 19 Jan 2026 20:39:50 +0100
Subject: [PATCH] cmake: avoid re-running checks by wrapping
check_c_compiler_flag
---
cmake/sdlcompilers.cmake | 44 +++++++++++++++++++++++-----------------
1 file changed, 25 insertions(+), 19 deletions(-)
diff --git a/cmake/sdlcompilers.cmake b/cmake/sdlcompilers.cmake
index ab62c5012ebfc..3ea5a9755574d 100644
--- a/cmake/sdlcompilers.cmake
+++ b/cmake/sdlcompilers.cmake
@@ -29,13 +29,19 @@ function(sdl_target_compile_option_all_languages TARGET OPTION)
endif()
endfunction()
+macro(cached_check_c_compiler_flag _arg _var)
+ if(NOT DEFINED "${_var}")
+ check_c_compiler_flag("${_arg}" "${_var}" ${ARGN})
+ endif()
+endmacro()
+
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)
+ cached_check_c_compiler_flag("/W3" COMPILER_SUPPORTS_W3)
if(COMPILER_SUPPORTS_W3)
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:/W3>")
endif()
@@ -47,7 +53,7 @@ function(SDL_AddCommonCompilerFlags TARGET)
# See if GCC's -gdwarf-4 is supported
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101377 for why this is needed on Windows
cmake_push_check_state()
- check_c_compiler_flag("-gdwarf-4" HAVE_GDWARF_4)
+ cached_check_c_compiler_flag("-gdwarf-4" HAVE_GDWARF_4)
if(HAVE_GDWARF_4)
target_compile_options(${TARGET} PRIVATE "$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:-gdwarf-4>")
endif()
@@ -57,7 +63,7 @@ function(SDL_AddCommonCompilerFlags TARGET)
# Check for -Wall first, so later things can override pieces of it.
# Note: clang-cl treats -Wall as -Weverything (which is very loud),
# /W3 as -Wall, and /W4 as -Wall -Wextra. So: /W3 is enough.
- check_c_compiler_flag(-Wall HAVE_GCC_WALL)
+ cached_check_c_compiler_flag(-Wall HAVE_GCC_WALL)
if(MSVC_CLANG)
target_compile_options(${TARGET} PRIVATE "/W3")
elseif(HAVE_GCC_WALL)
@@ -67,25 +73,25 @@ function(SDL_AddCommonCompilerFlags TARGET)
endif()
endif()
- check_c_compiler_flag(-Wundef HAVE_GCC_WUNDEF)
+ cached_check_c_compiler_flag(-Wundef HAVE_GCC_WUNDEF)
if(HAVE_GCC_WUNDEF)
sdl_target_compile_option_all_languages(${TARGET} "-Wundef")
endif()
- check_c_compiler_flag(-Wfloat-conversion HAVE_GCC_WFLOAT_CONVERSION)
+ cached_check_c_compiler_flag(-Wfloat-conversion HAVE_GCC_WFLOAT_CONVERSION)
if(HAVE_GCC_WFLOAT_CONVERSION)
sdl_target_compile_option_all_languages(${TARGET} "-Wfloat-conversion")
endif()
- check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING)
+ cached_check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING)
if(HAVE_GCC_NO_STRICT_ALIASING)
sdl_target_compile_option_all_languages(${TARGET} "-fno-strict-aliasing")
endif()
- check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION)
+ cached_check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION)
if(HAVE_GCC_WDOCUMENTATION)
if(SDL_WERROR)
- check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION)
+ cached_check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION)
if(HAVE_GCC_WERROR_DOCUMENTATION)
sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation")
endif()
@@ -93,10 +99,10 @@ function(SDL_AddCommonCompilerFlags TARGET)
sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation")
endif()
- check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
+ cached_check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
if(HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
if(SDL_WERROR)
- check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
+ cached_check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
if(HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation-unknown-command")
endif()
@@ -104,27 +110,27 @@ function(SDL_AddCommonCompilerFlags TARGET)
sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation-unknown-command")
endif()
- check_c_compiler_flag(-fcomment-block-commands=threadsafety HAVE_GCC_COMMENT_BLOCK_COMMANDS)
+ cached_check_c_compiler_flag(-fcomment-block-commands=threadsafety HAVE_GCC_COMMENT_BLOCK_COMMANDS)
if(HAVE_GCC_COMMENT_BLOCK_COMMANDS)
sdl_target_compile_option_all_languages(${TARGET} "-fcomment-block-commands=threadsafety")
else()
- check_c_compiler_flag(/clang:-fcomment-block-commands=threadsafety HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
+ cached_check_c_compiler_flag(/clang:-fcomment-block-commands=threadsafety HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
if(HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
sdl_target_compile_option_all_languages(${TARGET} "/clang:-fcomment-block-commands=threadsafety")
endif()
endif()
- check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
+ cached_check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
if(HAVE_GCC_WSHADOW)
sdl_target_compile_option_all_languages(${TARGET} "-Wshadow")
endif()
- check_c_compiler_flag(-Wunused-local-typedefs HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
+ cached_check_c_compiler_flag(-Wunused-local-typedefs HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
if(HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
sdl_target_compile_option_all_languages(${TARGET} "-Wno-unused-local-typedefs")
endif()
- check_c_compiler_flag(-Wimplicit-fallthrough HAVE_GCC_WIMPLICIT_FALLTHROUGH)
+ cached_check_c_compiler_flag(-Wimplicit-fallthrough HAVE_GCC_WIMPLICIT_FALLTHROUGH)
if(HAVE_GCC_WIMPLICIT_FALLTHROUGH)
sdl_target_compile_option_all_languages(${TARGET} "-Wimplicit-fallthrough")
endif()
@@ -132,12 +138,12 @@ function(SDL_AddCommonCompilerFlags TARGET)
if(SDL_WERROR)
if(MSVC)
- check_c_compiler_flag(/WX HAVE_WX)
+ cached_check_c_compiler_flag(/WX HAVE_WX)
if(HAVE_WX)
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:/WX>")
endif()
elseif(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QNX)
- check_c_compiler_flag(-Werror HAVE_WERROR)
+ cached_check_c_compiler_flag(-Werror HAVE_WERROR)
if(HAVE_WERROR)
sdl_target_compile_option_all_languages(${TARGET} "-Werror")
endif()
@@ -152,12 +158,12 @@ function(SDL_AddCommonCompilerFlags TARGET)
endif()
if(USE_CLANG)
- check_c_compiler_flag("-fcolor-diagnostics" COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
+ cached_check_c_compiler_flag("-fcolor-diagnostics" COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
if(COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
sdl_target_compile_option_all_languages(${TARGET} "-fcolor-diagnostics")
endif()
else()
- check_c_compiler_flag("-fdiagnostics-color=always" COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
+ cached_check_c_compiler_flag("-fdiagnostics-color=always" COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
if(COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
sdl_target_compile_option_all_languages(${TARGET} "-fdiagnostics-color=always")
endif()