From e256508e4bd9201a9e3949b5283cf1239eee2696 Mon Sep 17 00:00:00 2001 From: Roman-Byshliaha-Bose <132906099+Roman-Byshliaha-Bose@users.noreply.github.com> Date: Wed, 1 Nov 2023 00:09:58 +0100 Subject: [PATCH] [CMake] Link with librt only when platform toolchain has it (#34255) Not all POSIX platforms have rt as a separate library. QNX has rt as part of libc (same as pthreads). Add condition to check if the library can be found and link with it only in positive scenario. A solution for: https://github.com/grpc/grpc/issues/34254 --- CMakeLists.txt | 5 ++++- templates/CMakeLists.txt.template | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 925c7f4cb24..a00adcd8dd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -301,7 +301,10 @@ if(UNIX AND NOT HAIKU) find_package(Threads) set(_gRPC_ALLTARGETS_LIBRARIES ${CMAKE_DL_LIBS} m Threads::Threads) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_POSIX) - set(_gRPC_ALLTARGETS_LIBRARIES ${_gRPC_ALLTARGETS_LIBRARIES} rt) + find_library(LIBRT rt) + if(LIBRT) + set(_gRPC_ALLTARGETS_LIBRARIES ${_gRPC_ALLTARGETS_LIBRARIES} rt) + endif() endif() endif() diff --git a/templates/CMakeLists.txt.template b/templates/CMakeLists.txt.template index 84890f6c7f0..1b4578f8844 100644 --- a/templates/CMakeLists.txt.template +++ b/templates/CMakeLists.txt.template @@ -444,7 +444,10 @@ find_package(Threads) set(_gRPC_ALLTARGETS_LIBRARIES <%text>${CMAKE_DL_LIBS} m Threads::Threads) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_POSIX) - set(_gRPC_ALLTARGETS_LIBRARIES <%text>${_gRPC_ALLTARGETS_LIBRARIES} rt) + find_library(LIBRT rt) + if(LIBRT) + set(_gRPC_ALLTARGETS_LIBRARIES <%text>${_gRPC_ALLTARGETS_LIBRARIES} rt) + endif() endif() endif()