FindThreads.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindThreads
  5. -----------
  6. This module determines the thread library of the system.
  7. The following variables are set
  8. ::
  9. CMAKE_THREAD_LIBS_INIT - the thread library
  10. CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads?
  11. CMAKE_USE_PTHREADS_INIT - are we using pthreads
  12. CMAKE_HP_PTHREADS_INIT - are we using hp pthreads
  13. The following import target is created
  14. ::
  15. Threads::Threads
  16. If the use of the -pthread compiler and linker flag is preferred then the
  17. caller can set
  18. ::
  19. THREADS_PREFER_PTHREAD_FLAG
  20. The compiler flag can only be used with the imported
  21. target. Use of both the imported target as well as this switch is highly
  22. recommended for new code.
  23. This module is not needed for C++11 and later if threading is done using
  24. ``std::thread`` from the standard library.
  25. #]=======================================================================]
  26. include (CheckLibraryExists)
  27. include (CheckSymbolExists)
  28. set(Threads_FOUND FALSE)
  29. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  30. set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
  31. if(CMAKE_C_COMPILER_LOADED)
  32. include (CheckIncludeFile)
  33. elseif(CMAKE_CXX_COMPILER_LOADED)
  34. include (CheckIncludeFileCXX)
  35. else()
  36. message(FATAL_ERROR "FindThreads only works if either C or CXX language is enabled")
  37. endif()
  38. # Internal helper macro.
  39. # Do NOT even think about using it outside of this file!
  40. macro(_check_threads_lib LIBNAME FUNCNAME VARNAME)
  41. if(NOT Threads_FOUND)
  42. CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME})
  43. if(${VARNAME})
  44. set(CMAKE_THREAD_LIBS_INIT "-l${LIBNAME}")
  45. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  46. set(Threads_FOUND TRUE)
  47. endif()
  48. endif ()
  49. endmacro()
  50. # Internal helper macro.
  51. # Do NOT even think about using it outside of this file!
  52. macro(_check_pthreads_flag)
  53. if(NOT Threads_FOUND)
  54. # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
  55. if(NOT DEFINED THREADS_HAVE_PTHREAD_ARG)
  56. message(STATUS "Check if compiler accepts -pthread")
  57. if(CMAKE_C_COMPILER_LOADED)
  58. set(_threads_src ${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c)
  59. elseif(CMAKE_CXX_COMPILER_LOADED)
  60. set(_threads_src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindThreads/CheckForPthreads.cxx)
  61. configure_file(${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c "${_threads_src}" COPYONLY)
  62. endif()
  63. try_compile(THREADS_HAVE_PTHREAD_ARG
  64. ${CMAKE_BINARY_DIR}
  65. ${_threads_src}
  66. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  67. OUTPUT_VARIABLE OUTPUT)
  68. unset(_threads_src)
  69. if(THREADS_HAVE_PTHREAD_ARG)
  70. set(Threads_FOUND TRUE)
  71. message(STATUS "Check if compiler accepts -pthread - yes")
  72. else()
  73. message(STATUS "Check if compiler accepts -pthread - no")
  74. file(APPEND
  75. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  76. "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
  77. endif()
  78. endif()
  79. if(THREADS_HAVE_PTHREAD_ARG)
  80. set(Threads_FOUND TRUE)
  81. set(CMAKE_THREAD_LIBS_INIT "-pthread")
  82. endif()
  83. endif()
  84. endmacro()
  85. # Do we have pthreads?
  86. if(CMAKE_C_COMPILER_LOADED)
  87. CHECK_INCLUDE_FILE("pthread.h" CMAKE_HAVE_PTHREAD_H)
  88. else()
  89. CHECK_INCLUDE_FILE_CXX("pthread.h" CMAKE_HAVE_PTHREAD_H)
  90. endif()
  91. if(CMAKE_HAVE_PTHREAD_H)
  92. #
  93. # We have pthread.h
  94. # Let's check for the library now.
  95. #
  96. set(CMAKE_HAVE_THREADS_LIBRARY)
  97. if(NOT THREADS_HAVE_PTHREAD_ARG)
  98. # Check if pthread functions are in normal C library.
  99. # If the pthread functions already exist in C library, we could just use
  100. # them instead of linking to the additional pthread library. We could
  101. # try to check any pthread symbol name, but here is an exception. If we
  102. # use clang asan build, we will find the pthread_create() symbol in the
  103. # libc(libasan). However, it doesn't have the full pthread implementation.
  104. # So, we can't assume that we have the pthread implementation in libc
  105. # using the pthread_create() checking here. Then, we turn to check the
  106. # pthread_kill() symbol instead.
  107. CHECK_SYMBOL_EXISTS(pthread_kill pthread.h CMAKE_HAVE_LIBC_PTHREAD_KILL)
  108. if(CMAKE_HAVE_LIBC_PTHREAD_KILL)
  109. set(CMAKE_THREAD_LIBS_INIT "")
  110. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  111. set(Threads_FOUND TRUE)
  112. else()
  113. # Check for -pthread first if enabled. This is the recommended
  114. # way, but not backwards compatible as one must also pass -pthread
  115. # as compiler flag then.
  116. if (THREADS_PREFER_PTHREAD_FLAG)
  117. _check_pthreads_flag()
  118. endif ()
  119. _check_threads_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE)
  120. _check_threads_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE)
  121. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  122. # On sun also check for -lthread
  123. _check_threads_lib(thread thr_create CMAKE_HAVE_THR_CREATE)
  124. endif()
  125. endif()
  126. endif()
  127. _check_pthreads_flag()
  128. endif()
  129. if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_PTHREAD_KILL)
  130. set(CMAKE_USE_PTHREADS_INIT 1)
  131. set(Threads_FOUND TRUE)
  132. endif()
  133. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  134. set(CMAKE_USE_WIN32_THREADS_INIT 1)
  135. set(Threads_FOUND TRUE)
  136. endif()
  137. if(CMAKE_USE_PTHREADS_INIT)
  138. if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  139. # Use libcma if it exists and can be used. It provides more
  140. # symbols than the plain pthread library. CMA threads
  141. # have actually been deprecated:
  142. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  143. # http://docs.hp.com/en/947/d8.html
  144. # but we need to maintain compatibility here.
  145. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  146. # are available.
  147. CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  148. if(CMAKE_HAVE_HP_CMA)
  149. set(CMAKE_THREAD_LIBS_INIT "-lcma")
  150. set(CMAKE_HP_PTHREADS_INIT 1)
  151. set(Threads_FOUND TRUE)
  152. endif()
  153. set(CMAKE_USE_PTHREADS_INIT 1)
  154. endif()
  155. if(CMAKE_SYSTEM MATCHES "OSF1-V")
  156. set(CMAKE_USE_PTHREADS_INIT 0)
  157. set(CMAKE_THREAD_LIBS_INIT )
  158. endif()
  159. if(CMAKE_SYSTEM MATCHES "CYGWIN_NT")
  160. set(CMAKE_USE_PTHREADS_INIT 1)
  161. set(Threads_FOUND TRUE)
  162. set(CMAKE_THREAD_LIBS_INIT )
  163. set(CMAKE_USE_WIN32_THREADS_INIT 0)
  164. endif()
  165. endif()
  166. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  167. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  168. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
  169. if(THREADS_FOUND AND NOT TARGET Threads::Threads)
  170. add_library(Threads::Threads INTERFACE IMPORTED)
  171. if(THREADS_HAVE_PTHREAD_ARG)
  172. set_property(TARGET Threads::Threads
  173. PROPERTY INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler -pthread>"
  174. "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-pthread>")
  175. endif()
  176. if(CMAKE_THREAD_LIBS_INIT)
  177. set_property(TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
  178. endif()
  179. endif()