> I'm getting a build failure:
> 
> timevar.cc:163: undefined reference to `clock_gettime'
> 
> Our frozen build tools are intended to produce binaries that work
> "everywhere", so they're a few years old, but apparently something didn't
> configure correctly.
> 
> I see that libbacktrace configure correctly detects that clock_gettime is
> missing by default, but that it can be found in -lrt.
> 
> I'm investigating if I have a configuration problem on my end, but I think a
> configure test might be appropriate in gcc/configure, something like the one
> in the other subdirectories.

Here's a patch. I'll commit it as obvious unless someone complains.


    Add autoconf check for clock_gettime
    
    Reported by Andrew Stubbs
    
    gcc/ChangeLog:
    
            * config.in: Regenerate.
            * configure: Regenerate.
            * configure.ac: Check for HAVE_CLOCK_GETTIME.
            * timevar.cc (get_time): Use HAVE_CLOCK_GETTIME.

diff --git a/gcc/config.in b/gcc/config.in
index 3fc4666d60b5..0a506d1783a4 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -883,6 +883,12 @@
 #endif
 
 
+/* Define to 1 if you have the `clock_gettime' function. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_CLOCK_GETTIME
+#endif
+
+
 /* Define if <time.h> defines clock_t. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_CLOCK_T
diff --git a/gcc/configure b/gcc/configure
index 47c58036530f..150ab6164142 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -10613,7 +10613,8 @@ fi
 for ac_func in times clock kill getrlimit setrlimit atoq \
        popen sysconf strsignal getrusage nl_langinfo \
        gettimeofday mbstowcs wcswidth mmap posix_fallocate setlocale \
-       clearerr_unlocked feof_unlocked   ferror_unlocked fflush_unlocked 
fgetc_unlocked fgets_unlocked   fileno_unlocked fprintf_unlocked fputc_unlocked 
fputs_unlocked   fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked  
 putchar_unlocked putc_unlocked madvise mallinfo mallinfo2 fstatat getauxval
+       clearerr_unlocked feof_unlocked   ferror_unlocked fflush_unlocked 
fgetc_unlocked fgets_unlocked   fileno_unlocked fprintf_unlocked fputc_unlocked 
fputs_unlocked   fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked  
 putchar_unlocked putc_unlocked madvise mallinfo mallinfo2 fstatat getauxval \
+       clock_gettime
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_cxx_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -10626,6 +10627,54 @@ fi
 done
 
 
+# At least for glibc, clock_gettime is in librt.  But don't pull that
+# in if it still doesn't give us the function we want.
+if test $ac_cv_func_clock_gettime = no; then
+  { $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
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lrt  $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_cxx_try_link "$LINENO"; then :
+  ac_cv_lib_rt_clock_gettime=yes
+else
+  ac_cv_lib_rt_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_rt_clock_gettime" 
>&5
+$as_echo "$ac_cv_lib_rt_clock_gettime" >&6; }
+if test "x$ac_cv_lib_rt_clock_gettime" = xyes; then :
+  LIBS="-lrt $LIBS"
+
+$as_echo "#define HAVE_CLOCK_GETTIME 1" >>confdefs.h
+
+fi
+
+fi
+
 if test x$ac_cv_func_mbstowcs = xyes; then
   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether mbstowcs works" >&5
 $as_echo_n "checking whether mbstowcs works... " >&6; }
@@ -21405,7 +21454,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 21408 "configure"
+#line 21457 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -21511,7 +21560,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 21514 "configure"
+#line 21563 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
diff --git a/gcc/configure.ac b/gcc/configure.ac
index dc8346a7b823..bdb22d53e2ca 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1557,7 +1557,17 @@ define(gcc_UNLOCKED_FUNCS, clearerr_unlocked 
feof_unlocked dnl
 AC_CHECK_FUNCS(times clock kill getrlimit setrlimit atoq \
        popen sysconf strsignal getrusage nl_langinfo \
        gettimeofday mbstowcs wcswidth mmap posix_fallocate setlocale \
-       gcc_UNLOCKED_FUNCS madvise mallinfo mallinfo2 fstatat getauxval)
+       gcc_UNLOCKED_FUNCS madvise mallinfo mallinfo2 fstatat getauxval \
+       clock_gettime)
+
+# At least for glibc, clock_gettime is in librt.  But don't pull that
+# in if it still doesn't give us the function we want.
+if test $ac_cv_func_clock_gettime = no; then
+  AC_CHECK_LIB(rt, clock_gettime,
+    [LIBS="-lrt $LIBS"
+     AC_DEFINE(HAVE_CLOCK_GETTIME, 1,
+              [Define to 1 if you have the `clock_gettime' function.])])
+fi
 
 if test x$ac_cv_func_mbstowcs = xyes; then
   AC_CACHE_CHECK(whether mbstowcs works, gcc_cv_func_mbstowcs_works,
diff --git a/gcc/timevar.cc b/gcc/timevar.cc
index 4a57e74230d3..e12775e6ff3a 100644
--- a/gcc/timevar.cc
+++ b/gcc/timevar.cc
@@ -158,7 +158,7 @@ get_time (struct timevar_time_def *now)
   now->wall = 0;
   now->ggc_mem = timevar_ggc_mem_total;
 
-#if _POSIX_C_SOURCE >= 199309L
+#ifdef HAVE_CLOCK_GETTIME
   struct timespec ts;
   clock_gettime (CLOCK_MONOTONIC, &ts);
   now->wall = ts.tv_sec * 1000000000 + ts.tv_nsec;

Reply via email to