[llvm] [cmake] Add pthread to required libraries before dl and rt checks (PR #203054)

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 12:19:07 PDT 2026


https://github.com/rnk updated https://github.com/llvm/llvm-project/pull/203054

>From f7090f6d4d500e0e9e6bca6b6220c3d06249bc34 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rkleckner at nvidia.com>
Date: Wed, 10 Jun 2026 16:59:25 +0000
Subject: [PATCH] [cmake] Add pthread to required libraries before dl and rt
 checks

This fixes detection of shm_open on glibc < 2.34.

On glibc before 2.34 (released 2021), librt may depend on symbols from
libpthread. LLVM's HAVE_LIBRT probe uses check_library_exists(rt
shm_open ...), which links a small test against librt and
CMAKE_REQUIRED_LIBRARIES. If pthread is not included in the test link,
the probe can fail even though shm_open is available and LLVM targets
later link with pthread.

Move the dl and rt library checks after pthread detection and add the
detected pthread library to CMAKE_REQUIRED_LIBRARIES. This also lets us
remove later logic that temporarily made pthread a required library for
pthread symbol checks.

Modern C libraries (glibc 2.34+ and musl) integrate pthread-related
functionality into libc, so there is little practical benefit to
avoiding a libpthread dependency on pre-2021 glibc versions when
LLVM_ENABLE_THREADS is disabled.
---
 llvm/cmake/config-ix.cmake | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/llvm/cmake/config-ix.cmake b/llvm/cmake/config-ix.cmake
index 415b09e298075..aca87f154254a 100644
--- a/llvm/cmake/config-ix.cmake
+++ b/llvm/cmake/config-ix.cmake
@@ -145,13 +145,8 @@ if(NOT WIN32)
       check_library_exists(c pthread_mutex_lock "" HAVE_PTHREAD_MUTEX_LOCK)
     endif()
   endif()
-  check_library_exists(dl dlopen "" HAVE_LIBDL)
-  check_library_exists(rt shm_open "" HAVE_LIBRT)
 endif()
 
-# Check for libpfm.
-include(FindLibpfm)
-
 if(HAVE_LIBPTHREAD)
   # We want to find pthreads library and at the moment we do want to
   # have it reported as '-l<lib>' instead of '-pthread'.
@@ -160,8 +155,22 @@ if(HAVE_LIBPTHREAD)
   set(THREADS_HAVE_PTHREAD_ARG Off)
   find_package(Threads REQUIRED)
   set(LLVM_PTHREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
+  if(LLVM_PTHREAD_LIB)
+    list(APPEND CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
+  endif()
+endif()
+
+# Keep the dlopen and rt library checks after FindThreads so that
+# CMAKE_REQUIRED_LIBRARIES includes pthread. glibc versions before 2.34 may
+# need pthread to satisfy librt dependencies.
+if(NOT WIN32)
+  check_library_exists(dl dlopen "" HAVE_LIBDL)
+  check_library_exists(rt shm_open "" HAVE_LIBRT)
 endif()
 
+# Check for libpfm.
+include(FindLibpfm)
+
 if(LLVM_ENABLE_ZLIB)
   if(LLVM_ENABLE_ZLIB STREQUAL FORCE_ON)
     find_package(ZLIB REQUIRED)
@@ -442,16 +451,10 @@ else()
 endif()
 
 if (NOT WIN32)
-  if (LLVM_PTHREAD_LIB)
-    list(APPEND CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
-  endif()
   check_symbol_exists(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
   check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
   check_symbol_exists(pthread_get_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_GET_NAME_NP)
   check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SET_NAME_NP)
-  if (LLVM_PTHREAD_LIB)
-    list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
-  endif()
 
   if( HAVE_LIBDL )
     list(APPEND CMAKE_REQUIRED_LIBRARIES dl)



More information about the llvm-commits mailing list