From ab1bfc33c87726dc1fd85c351191380c6c463143 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Thu, 18 Aug 2022 00:31:05 +0300 Subject: Link with rt library to get clock_gettime where necessary Issue #450 (bdwgc). This patch adds the logic to CMake and configure scripts to pass -lrt option during linking to avoid unresolved clock_gettime on some hosts. * CMakeLists.txt [!NEED_LIB_RT] (HAVE_CLOCK_GETTIME_DIRECTLY): Set variable by check_function_exists(clock_gettime). * CMakeLists.txt [!NEED_LIB_RT && !HAVE_CLOCK_GETTIME_DIRECTLY] (NEED_LIB_RT): Set to ON. * CMakeLists.txt (LIBRT): Refine comment. * CMakeLists.txt (gc): Specify target_link_libraries regardless of enable_threads. * Makefile.am (libgc_la_LIBADD): Update comment. * configure.ac (need_lib_rt): Define variable (and set to false). * configure.ac [$THREADS==pthreads && ($host==*-*-hpux11* || $host==*-*-netbsd* || $host== *-*-solaris* || $host==*-*-osf*)] (THREADDLLIBS): Do not add "-lrt" directly. * configure.ac [$THREADS==pthreads && ($host==*-*-hpux11* || $host==*-*-netbsd* || $host== *-*-solaris* || $host==*-*-osf*)] (need_lib_rt): Set to true. * configure.ac [!need_lib_rt && !AC_LINK_IFELSE(clock_gettime) && AC_CHECK_LIB(rt,clock_gettime)] (need_lib_rt): Set to true. * configure.ac [need_lib_rt] (THREADDLLIBS): Append "-lrt". --- configure.ac | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'configure.ac') diff --git a/configure.ac b/configure.ac index 2a9e3898..1293b037 100644 --- a/configure.ac +++ b/configure.ac @@ -190,6 +190,7 @@ CFLAGS="$old_CFLAGS" THREADDLLIBS= need_atomic_ops_asm=false +need_lib_rt=false compile_asm=false use_parallel_mark=no use_thread_local_alloc=no @@ -221,8 +222,9 @@ case "$THREADS" in *-*-hpux11*) AC_MSG_WARN("Only HP/UX 11 POSIX threads are supported.") AC_DEFINE(_POSIX_C_SOURCE,199506L) - THREADDLLIBS="-lpthread -lrt" + THREADDLLIBS="-lpthread" # HPUX needs REENTRANT for the _r calls. + need_lib_rt=true ;; *-*-openbsd*) AM_CFLAGS="$AM_CFLAGS -pthread" @@ -241,10 +243,12 @@ case "$THREADS" in *-*-netbsd*) AC_MSG_WARN("Only on NetBSD 2.0 or later.") AC_DEFINE(_PTHREADS) - THREADDLLIBS="-lpthread -lrt" + THREADDLLIBS="-lpthread" + need_lib_rt=true ;; *-*-solaris*) - THREADDLLIBS="-lpthread -lrt" + THREADDLLIBS="-lpthread" + need_lib_rt=true ;; *-*-cygwin* | *-*-msys*) # Cygwin doesn't have a real libpthread, so Libtool can't link @@ -265,7 +269,8 @@ case "$THREADS" in ;; *-*-osf*) AM_CFLAGS="$AM_CFLAGS -pthread" - THREADDLLIBS="-lpthread -lrt" + THREADDLLIBS="-lpthread" + need_lib_rt=true ;; *) AS_IF([test x$default_threadlibs != xtrue], @@ -328,7 +333,22 @@ case "$THREADS" in AC_MSG_ERROR($THREADS is an unknown thread package) ;; esac + +# Check whether -lrt linker option is needed to use clock_gettime. +if test "x$need_lib_rt" != xtrue; then + AC_MSG_CHECKING(for clock_gettime without additional libraries) + AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ], + [struct timespec t; clock_gettime(CLOCK_REALTIME, &t)])], + [AC_MSG_RESULT(yes)], + [AC_MSG_RESULT(no) + AC_CHECK_LIB(rt, clock_gettime, [need_lib_rt=true])]) +fi + +if test "x$need_lib_rt" = xtrue; then + THREADDLLIBS="$THREADDLLIBS -lrt" +fi AC_SUBST(THREADDLLIBS) + AM_CONDITIONAL(THREADS, test x$THREADS != xnone) AM_CONDITIONAL(PTHREADS, test x$THREADS = xposix) AM_CONDITIONAL(DARWIN_THREADS, test x$darwin_threads = xtrue) -- cgit v1.2.1