SDL-1.2: look for clock_gettime() in libc first, then in librt if not found.

From 52c714024e2d5a5383f64f9b119ea96cb46f9af2 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Sun, 7 May 2023 07:56:04 +0300
Subject: [PATCH] look for clock_gettime() in libc first, then in librt if not
 found.

---
 configure    | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----
 configure.ac |  9 +++++++--
 2 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/configure b/configure
index 7002238a..aeb34733 100755
--- a/configure
+++ b/configure
@@ -21737,7 +21737,51 @@ else
 fi
 
     if test x$enable_clock_gettime = xyes; then
-        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_gettime in -lrt" >&5
+        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_gettime in -lc" >&5
+$as_echo_n "checking for clock_gettime in -lc... " >&6; }
+if ${ac_cv_lib_c_clock_gettime+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lc  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char clock_gettime ();
+int
+main ()
+{
+return clock_gettime ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_lib_c_clock_gettime=yes
+else
+  ac_cv_lib_c_clock_gettime=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_c_clock_gettime" >&5
+$as_echo "$ac_cv_lib_c_clock_gettime" >&6; }
+if test "x$ac_cv_lib_c_clock_gettime" = xyes; then :
+  have_clock_gettime=yes
+fi
+
+        if test x$have_clock_gettime = xyes; then
+            $as_echo "#define HAVE_CLOCK_GETTIME 1" >>confdefs.h
+
+        else
+            { $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_gettime in -lrt" >&5
 $as_echo_n "checking for clock_gettime in -lrt... " >&6; }
 if ${ac_cv_lib_rt_clock_gettime+:} false; then :
   $as_echo_n "(cached) " >&6
@@ -21777,10 +21821,11 @@ if test "x$ac_cv_lib_rt_clock_gettime" = xyes; then :
   have_clock_gettime=yes
 fi
 
-        if test x$have_clock_gettime = xyes; then
-            $as_echo "#define HAVE_CLOCK_GETTIME 1" >>confdefs.h
+            if test x$have_clock_gettime = xyes; then
+                EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lrt"
+                $as_echo "#define HAVE_CLOCK_GETTIME 1" >>confdefs.h
 
-            EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lrt"
+            fi
         fi
     fi
 }
diff --git a/configure.ac b/configure.ac
index 667b197b..458ee6d2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2348,10 +2348,15 @@ CheckClockGettime()
 [AS_HELP_STRING([--enable-clock_gettime], [use clock_gettime() instead of gettimeofday() on UNIX [default=no]])],
                   , enable_clock_gettime=no)
     if test x$enable_clock_gettime = xyes; then
-        AC_CHECK_LIB(rt, clock_gettime, have_clock_gettime=yes)
+        AC_CHECK_LIB(c, clock_gettime, have_clock_gettime=yes)
         if test x$have_clock_gettime = xyes; then
             AC_DEFINE(HAVE_CLOCK_GETTIME)
-            EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lrt"
+        else
+            AC_CHECK_LIB(rt, clock_gettime, have_clock_gettime=yes)
+            if test x$have_clock_gettime = xyes; then
+                EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lrt"
+                AC_DEFINE(HAVE_CLOCK_GETTIME)
+            fi
         fi
     fi
 }