SDL: cmake: check for valid isinf/isinff/isnan/isnanf macros

From d52ea209165222f2a91e7d71ce9f1787c9b7119c Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 24 Jun 2024 21:47:19 +0200
Subject: [PATCH] cmake: check for valid isinf/isinff/isnan/isnanf macros

---
 CMakeLists.txt                                | 37 +++++++++++++++++++
 cmake/PreseedMSVCCache.cmake                  |  6 +++
 include/build_config/SDL_build_config.h.cmake |  2 +
 3 files changed, 45 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7ddb476c841ca..0472fd8dc7d4b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1100,6 +1100,43 @@ if(SDL_LIBC)
   endforeach()
   cmake_pop_check_state()
 
+  cmake_push_check_state()
+  if(MSVC)
+    string(APPEND CMAKE_REQUIRED_FLAGS " -we4244 -WX")  # 'conversion' conversion from 'type1' to 'type2', possible loss of data
+  else()
+    string(APPEND CMAKE_REQUIRED_FLAGS " -Wfloat-conversion -Werror")
+  endif()
+  foreach(math_fn isinf isnan)
+    string(TOUPPER "${math_fn}" MATH_FN)
+    check_c_source_compiles("
+        #include <math.h>
+        int main() {
+          double d = 3.14159;
+          return ${math_fn}(d);
+        }
+      " LIBC_HAS_${MATH_FN})
+    set(HAVE_${MATH_FN} ${LIBC_HAS_${MATH_FN}})
+
+    check_c_source_compiles("
+      #include <math.h>
+      int main() {
+        float f = 3.14159f;
+        return ${math_fn}(f);
+      }
+    " LIBC_${MATH_FN}_HANDLES_FLOAT)
+    set(HAVE_${MATH_FN}_FLOAT_MACRO ${LIBC_${MATH_FN}_HANDLES_FLOAT})
+
+    check_c_source_compiles("
+      #include <math.h>
+      int main() {
+        float f = 3.14159f;
+        return ${math_fn}f(f);
+      }
+    " LIBC_HAS_${MATH_FN}F)
+    set(HAVE_${MATH_FN}F "${LIBC_HAS_${MATH_FN}}")
+  endforeach()
+  cmake_pop_check_state()
+
   if(NOT WINDOWS)
     check_symbol_exists(getpagesize "unistd.h" HAVE_GETPAGESIZE)
     check_symbol_exists(sigaction "signal.h" HAVE_SIGACTION)
diff --git a/cmake/PreseedMSVCCache.cmake b/cmake/PreseedMSVCCache.cmake
index 61980f118b4a4..2a52d56baddb5 100644
--- a/cmake/PreseedMSVCCache.cmake
+++ b/cmake/PreseedMSVCCache.cmake
@@ -55,6 +55,12 @@ function(SDL_Preseed_CMakeCache)
     set(LIBC_HAS_ICONV_H                                 ""    CACHE INTERNAL "Have include iconv.h")
     set(LIBC_HAS_INDEX                                   ""    CACHE INTERNAL "Have symbol index")
     set(LIBC_HAS_INTTYPES_H                              "1"   CACHE INTERNAL "Have include inttypes.h")
+    set(LIBC_HAS_ISINF                                   "1"   CACHE INTERNAL "Have include isinf(double)")
+    set(LIBC_ISINF_HANDLES_FLOAT                         "1"   CACHE INTERNAL "Have include isinf(float)")
+    set(LIBC_HAS_ISINFF                                  ""    CACHE INTERNAL "Have include isinff(float)")
+    set(LIBC_HAS_ISNAN                                   "1"   CACHE INTERNAL "Have include isnan(double)")
+    set(LIBC_ISNAN_HANDLES_FLOAT                         "1"   CACHE INTERNAL "Have include isnan(float)")
+    set(LIBC_HAS_ISNANF                                  ""    CACHE INTERNAL "Have include isnanf(float)")
     set(LIBC_HAS_ITOA                                    "1"   CACHE INTERNAL "Have symbol itoa")
     set(LIBC_HAS_LIMITS_H                                "1"   CACHE INTERNAL "Have include limits.h")
     set(LIBC_HAS_LOG                                     "1"   CACHE INTERNAL "Have symbol log")
diff --git a/include/build_config/SDL_build_config.h.cmake b/include/build_config/SDL_build_config.h.cmake
index 69efb8fcadb70..0e63c1d645f29 100644
--- a/include/build_config/SDL_build_config.h.cmake
+++ b/include/build_config/SDL_build_config.h.cmake
@@ -154,8 +154,10 @@
 #cmakedefine HAVE_FMODF 1
 #cmakedefine HAVE_ISINF 1
 #cmakedefine HAVE_ISINFF 1
+#cmakedefine HAVE_ISINF_FLOAT_MACRO 1
 #cmakedefine HAVE_ISNAN 1
 #cmakedefine HAVE_ISNANF 1
+#cmakedefine HAVE_ISNAN_FLOAT_MACRO 1
 #cmakedefine HAVE_LOG 1
 #cmakedefine HAVE_LOGF 1
 #cmakedefine HAVE_LOG10 1