Commit aa4a6b0b88b2e0b1206a147570bb7af3aff15350

Ozkan Sezer 2021-01-25T04:11:40

better check for clock_gettime_nsec_np() -- cf. bug #5467.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6835785..39adf31 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -873,7 +873,18 @@ if(LIBC)
     else()
       set(HAVE_ALLOCA_H 1)
       set(HAVE_ALLOCA 1)
-      check_symbol_exists(clock_gettime_nsec_np "time.h" HAVE_CLOCK_GETTIME_NSEC_NP)
+      if(IOS OR TVOS)   # assuming iOS / TVOS >= 10.0
+        set(HAVE_CLOCK_GETTIME_NSEC_NP 1)
+      else()
+        CHECK_C_SOURCE_COMPILES("
+          #include <AvailabilityMacros.h>
+          #include <time.h>
+          #if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
+          #error Need 10.12 or newer target
+          #endif
+          int main(int argc, char **argv) { return !clock_gettime_nsec_np(CLOCK_MONOTONIC); }
+        " HAVE_CLOCK_GETTIME_NSEC_NP)
+      endif()
     endif()
 
     check_struct_has_member("struct sigaction" "sa_sigaction" "signal.h" HAVE_SA_SIGACTION)
diff --git a/configure b/configure
index 4c3cdea..5b477e0 100755
--- a/configure
+++ b/configure
@@ -17068,7 +17068,7 @@ fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
 fi
 
-    for ac_func in malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info clock_gettime_nsec_np poll _Exit
+    for ac_func in malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info poll _Exit
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -24748,6 +24748,47 @@ $as_echo "#define HAVE_CLOCK_GETTIME 1" >>confdefs.h
     fi
 }
 
+CheckClockGettimeAPPLE()
+{
+    have_clock_gettime_apple=no
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_gettime_nsec_np" >&5
+$as_echo_n "checking for clock_gettime_nsec_np... " >&6; }
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+#include <AvailabilityMacros.h>
+#include <time.h>
+
+int
+main ()
+{
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
+#error Need 10.12 or newer target
+#endif
+        return !clock_gettime_nsec_np(CLOCK_MONOTONIC);
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+
+        have_clock_gettime_apple=yes
+
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_clock_gettime_apple" >&5
+$as_echo "$have_clock_gettime_apple" >&6; }
+
+    if test x$have_clock_gettime_apple = xyes; then
+
+$as_echo "#define HAVE_CLOCK_GETTIME_NSEC_NP 1" >>confdefs.h
+
+    fi
+}
+
 CheckLinuxVersion()
 {
     ac_fn_c_check_header_mongrel "$LINENO" "linux/version.h" "ac_cv_header_linux_version_h" "$ac_includes_default"
@@ -25442,6 +25483,10 @@ fi
         CheckVulkan
         CheckPTHREAD
 
+
+$as_echo "#define HAVE_CLOCK_GETTIME_NSEC_NP 1" >>confdefs.h
+
+
         SOURCES="$SOURCES $srcdir/src/misc/ios/*.m"
         have_misc=yes
 
@@ -25559,6 +25604,7 @@ $as_echo "#define SDL_VIDEO_RENDER_OGL_ES2 1" >>confdefs.h
 
         CheckVisibilityHidden
         CheckDeclarationAfterStatement
+        CheckClockGettimeAPPLE
         CheckDummyVideo
         CheckDiskAudio
         CheckDummyAudio
@@ -26010,7 +26056,6 @@ fi
 
 
 
-
 cat >Makefile.rules <<__EOF__
 
 # Build rules for objects
diff --git a/configure.ac b/configure.ac
index 3418f0a..976314a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -345,7 +345,7 @@ if test x$enable_libc = xyes; then
         AC_DEFINE(HAVE_MPROTECT, 1, [ ])
         ]),
     )
-    AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info clock_gettime_nsec_np poll _Exit)
+    AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info poll _Exit)
 
     AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"])
     AC_CHECK_FUNCS(acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf trunc truncf fmod fmodf log logf log10 log10f lround lroundf pow powf round roundf scalbn scalbnf sin sinf sqrt sqrtf tan tanf)
@@ -3486,6 +3486,29 @@ AS_HELP_STRING([--enable-clock_gettime], [use clock_gettime() instead of gettime
     fi
 }
 
+dnl Check for clock_gettime_nsec_np()
+CheckClockGettimeAPPLE()
+{
+    have_clock_gettime_apple=no
+    AC_MSG_CHECKING(for clock_gettime_nsec_np)
+    AC_TRY_LINK([
+#include <AvailabilityMacros.h>
+#include <time.h>
+      ],[
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
+#error Need 10.12 or newer target
+#endif
+        return !clock_gettime_nsec_np(CLOCK_MONOTONIC);
+      ],[
+        have_clock_gettime_apple=yes
+    ])
+    AC_MSG_RESULT($have_clock_gettime_apple)
+
+    if test x$have_clock_gettime_apple = xyes; then
+        AC_DEFINE(HAVE_CLOCK_GETTIME_NSEC_NP, 1, [ ])
+    fi
+}
+
 dnl Check for a valid linux/version.h
 CheckLinuxVersion()
 {
@@ -4021,6 +4044,9 @@ AS_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
         CheckVulkan
         CheckPTHREAD
 
+        dnl assuming iOS >= 10.0
+        AC_DEFINE(HAVE_CLOCK_GETTIME_NSEC_NP, 1, [ ])
+
         SOURCES="$SOURCES $srcdir/src/misc/ios/*.m"
         have_misc=yes
 
@@ -4116,6 +4142,7 @@ AS_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
 
         CheckVisibilityHidden
         CheckDeclarationAfterStatement
+        CheckClockGettimeAPPLE
         CheckDummyVideo
         CheckDiskAudio
         CheckDummyAudio
diff --git a/include/SDL_config_iphoneos.h b/include/SDL_config_iphoneos.h
index 9a748be..08524b4 100644
--- a/include/SDL_config_iphoneos.h
+++ b/include/SDL_config_iphoneos.h
@@ -53,6 +53,7 @@
 #define HAVE_REALLOC    1
 #define HAVE_FREE   1
 #define HAVE_ALLOCA 1
+#define HAVE_CLOCK_GETTIME_NSEC_NP 1
 #define HAVE_GETENV 1
 #define HAVE_SETENV 1
 #define HAVE_PUTENV 1
diff --git a/include/SDL_config_macosx.h b/include/SDL_config_macosx.h
index 9a8f720..464b197 100644
--- a/include/SDL_config_macosx.h
+++ b/include/SDL_config_macosx.h
@@ -56,6 +56,9 @@
 #define HAVE_CALLOC 1
 #define HAVE_REALLOC    1
 #define HAVE_FREE   1
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
+#define HAVE_CLOCK_GETTIME_NSEC_NP 1
+#endif
 #define HAVE_ALLOCA 1
 #define HAVE_GETENV 1
 #define HAVE_SETENV 1