Browse Source

FindThreads: Honor THREADS_PREFER_PTHREAD_FLAG when pthread is found in libc

The `-pthread` flag tells the compiler/linker to link to additional
libraries needed for thread support (e.g. libatomic on riscv64). The
flag therefore should be used if requested using
`THREADS_PREFER_PTHREAD_FLAG` also when the pthread functions are
found in libc.
Mattias Ellert 4 years ago
parent
commit
5efb6fb516
1 changed files with 11 additions and 8 deletions
  1. 11 8
      Modules/FindThreads.cmake

+ 11 - 8
Modules/FindThreads.cmake

@@ -164,18 +164,21 @@ if(CMAKE_HAVE_PTHREAD_H)
     elseif(CMAKE_CXX_COMPILER_LOADED)
       CHECK_CXX_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD)
     endif()
-    if(CMAKE_HAVE_LIBC_PTHREAD)
+
+    # Check for -pthread first if enabled. This is the recommended
+    # way, but not backwards compatible as one must also pass -pthread
+    # as compiler flag then.
+    if(THREADS_PREFER_PTHREAD_FLAG)
+      _check_pthreads_flag()
+    endif()
+
+    if(Threads_FOUND)
+      # do nothing, we are done
+    elseif(CMAKE_HAVE_LIBC_PTHREAD)
       set(CMAKE_THREAD_LIBS_INIT "")
       set(CMAKE_HAVE_THREADS_LIBRARY 1)
       set(Threads_FOUND TRUE)
     else()
-      # Check for -pthread first if enabled. This is the recommended
-      # way, but not backwards compatible as one must also pass -pthread
-      # as compiler flag then.
-      if (THREADS_PREFER_PTHREAD_FLAG)
-         _check_pthreads_flag()
-      endif ()
-
       if(CMAKE_SYSTEM MATCHES "GHS-MULTI")
         _check_threads_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE)
       endif()