[llvm] [Flang] explicitly link the pthread library when building shared flang-rt. (PR #129956)

Daniel Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 6 14:24:42 PST 2025


https://github.com/DanielCChen updated https://github.com/llvm/llvm-project/pull/129956

>From c9bab027c61e8efdaf4040789da5788a0ad07e48 Mon Sep 17 00:00:00 2001
From: Daniel Chen <cdchen at ca.ibm.com>
Date: Wed, 5 Mar 2025 18:31:22 -0500
Subject: [PATCH 1/4] [Flang] explicitly link the pthread library when building
 shared flang-rt.

---
 flang-rt/cmake/modules/AddFlangRT.cmake | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index a43f1c332187a..59c011b87b131 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -142,6 +142,8 @@ function (add_flangrt_library name)
   endif ()
   if (build_shared)
     add_library("${name_shared}" SHARED ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
+    find_package(Threads REQUIRED)
+    target_link_libraries(${name_shared} PUBLIC Threads::Threads)
   endif ()
 
   if (libtargets)

>From c03d1b0b239465600529355638394b22ceb8f1e1 Mon Sep 17 00:00:00 2001
From: Daniel Chen <cdchen at ca.ibm.com>
Date: Thu, 6 Mar 2025 11:14:06 -0500
Subject: [PATCH 2/4] To move the find_package requirment to the top leverl
 cmake file.

---
 flang-rt/CMakeLists.txt                 | 2 ++
 flang-rt/cmake/modules/AddFlangRT.cmake | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index 9f890a015621a..fb93b043db2ad 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -245,6 +245,8 @@ check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
 # Check whether -fno-lto is supported.
 check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG)
 
+# Search for pthread library
+find_package(Threads REQUIRED)
 
 # function checks
 find_package(Backtrace)
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index 59c011b87b131..68e53369e129d 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -142,7 +142,6 @@ function (add_flangrt_library name)
   endif ()
   if (build_shared)
     add_library("${name_shared}" SHARED ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
-    find_package(Threads REQUIRED)
     target_link_libraries(${name_shared} PUBLIC Threads::Threads)
   endif ()
 

>From e1d8cd0a4f72082e61c9c2bc3016aabea815c119 Mon Sep 17 00:00:00 2001
From: Daniel Chen <cdchen at ca.ibm.com>
Date: Thu, 6 Mar 2025 15:50:55 -0500
Subject: [PATCH 3/4] [Flang] Improve the comments to state the reason why
 needing Thread.

---
 flang-rt/CMakeLists.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index fb93b043db2ad..90484965f1213 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -245,7 +245,9 @@ check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
 # Check whether -fno-lto is supported.
 check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG)
 
-# Search for pthread library
+# Different platform may have different name for the POSIX thread library.
+# For example, libpthread.a on AIX. Search for it as it is needed when
+# building the shared flang_rt.runtime.so.
 find_package(Threads REQUIRED)
 
 # function checks

>From 8b8df2edef2d9b5dbd691b7ecf24a21141c9a12e Mon Sep 17 00:00:00 2001
From: Daniel Chen <cdchen at ca.ibm.com>
Date: Thu, 6 Mar 2025 17:24:24 -0500
Subject: [PATCH 4/4] Remove REQUIRED as not all the platform has pthread
 library.

---
 flang-rt/CMakeLists.txt                 | 2 +-
 flang-rt/cmake/modules/AddFlangRT.cmake | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index 90484965f1213..b516a5a1d0131 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -248,7 +248,7 @@ check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG)
 # Different platform may have different name for the POSIX thread library.
 # For example, libpthread.a on AIX. Search for it as it is needed when
 # building the shared flang_rt.runtime.so.
-find_package(Threads REQUIRED)
+find_package(Threads)
 
 # function checks
 find_package(Backtrace)
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index 68e53369e129d..c9a180e16163a 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -142,7 +142,9 @@ function (add_flangrt_library name)
   endif ()
   if (build_shared)
     add_library("${name_shared}" SHARED ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
-    target_link_libraries(${name_shared} PUBLIC Threads::Threads)
+    if (Threads_FOUND) 
+      target_link_libraries(${name_shared} PUBLIC Threads::Threads)
+    endif ()
   endif ()
 
   if (libtargets)



More information about the llvm-commits mailing list