FindThreads.cmake 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. Imported Targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 3.1
  10. This module defines the following :prop_tgt:`IMPORTED` target:
  11. ``Threads::Threads``
  12. The thread library, if found.
  13. Result Variables
  14. ^^^^^^^^^^^^^^^^
  15. The following variables are set:
  16. ``Threads_FOUND``
  17. If a supported thread library was found.
  18. ``CMAKE_THREAD_LIBS_INIT``
  19. The thread library to use. This may be empty if the thread functions
  20. are provided by the system libraries and no special flags are needed
  21. to use them.
  22. ``CMAKE_USE_WIN32_THREADS_INIT``
  23. If the found thread library is the win32 one.
  24. ``CMAKE_USE_PTHREADS_INIT``
  25. If the found thread library is pthread compatible.
  26. ``CMAKE_HP_PTHREADS_INIT``
  27. If the found thread library is the HP thread library.
  28. Variables Affecting Behavior
  29. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  30. .. variable:: THREADS_PREFER_PTHREAD_FLAG
  31. .. versionadded:: 3.1
  32. If the use of the -pthread compiler and linker flag is preferred then
  33. the caller can set this variable to TRUE. The compiler flag can only be
  34. used with the imported target. Use of both the imported target as well
  35. as this switch is highly recommended for new code.
  36. This variable has no effect if the system libraries provide the
  37. thread functions, i.e. when ``CMAKE_THREAD_LIBS_INIT`` will be empty.
  38. #]=======================================================================]
  39. include (CheckLibraryExists)
  40. set(Threads_FOUND FALSE)
  41. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  42. set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
  43. if(CMAKE_C_COMPILER_LOADED)
  44. include (CheckIncludeFile)
  45. include (CheckCSourceCompiles)
  46. elseif(CMAKE_CXX_COMPILER_LOADED)
  47. include (CheckIncludeFileCXX)
  48. include (CheckCXXSourceCompiles)
  49. else()
  50. message(FATAL_ERROR "FindThreads only works if either C or CXX language is enabled")
  51. endif()
  52. # simple pthread test code
  53. set(PTHREAD_C_CXX_TEST_SOURCE [====[
  54. #include <pthread.h>
  55. static void* test_func(void* data)
  56. {
  57. return data;
  58. }
  59. int main(void)
  60. {
  61. pthread_t thread;
  62. pthread_create(&thread, NULL, test_func, NULL);
  63. pthread_detach(thread);
  64. pthread_cancel(thread);
  65. pthread_join(thread, NULL);
  66. pthread_atfork(NULL, NULL, NULL);
  67. pthread_exit(NULL);
  68. return 0;
  69. }
  70. ]====])
  71. # Internal helper macro.
  72. # Do NOT even think about using it outside of this file!
  73. macro(_threads_check_libc)
  74. if(NOT Threads_FOUND)
  75. if(CMAKE_C_COMPILER_LOADED)
  76. CHECK_C_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD)
  77. elseif(CMAKE_CXX_COMPILER_LOADED)
  78. CHECK_CXX_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD)
  79. endif()
  80. if(CMAKE_HAVE_LIBC_PTHREAD)
  81. set(CMAKE_THREAD_LIBS_INIT "")
  82. set(Threads_FOUND TRUE)
  83. endif()
  84. endif ()
  85. endmacro()
  86. # Internal helper macro.
  87. # Do NOT even think about using it outside of this file!
  88. macro(_threads_check_lib LIBNAME FUNCNAME VARNAME)
  89. if(NOT Threads_FOUND)
  90. CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME})
  91. if(${VARNAME})
  92. set(CMAKE_THREAD_LIBS_INIT "-l${LIBNAME}")
  93. set(Threads_FOUND TRUE)
  94. endif()
  95. endif ()
  96. endmacro()
  97. # Internal helper macro.
  98. # Do NOT even think about using it outside of this file!
  99. macro(_threads_check_flag_pthread)
  100. if(NOT Threads_FOUND)
  101. # If we did not find -lpthreads, -lpthread, or -lthread, look for -pthread
  102. # except on compilers known to not have it.
  103. if(MSVC)
  104. # Compilers targeting the MSVC ABI do not have a -pthread flag.
  105. set(THREADS_HAVE_PTHREAD_ARG FALSE)
  106. elseif(NOT DEFINED THREADS_HAVE_PTHREAD_ARG)
  107. message(CHECK_START "Check if compiler accepts -pthread")
  108. if(CMAKE_C_COMPILER_LOADED)
  109. set(_threads_src CheckForPthreads.c)
  110. elseif(CMAKE_CXX_COMPILER_LOADED)
  111. set(_threads_src CheckForPthreads.cxx)
  112. endif()
  113. try_compile(THREADS_HAVE_PTHREAD_ARG
  114. SOURCE_FROM_FILE "${_threads_src}" "${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c"
  115. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  116. )
  117. unset(_threads_src)
  118. if(THREADS_HAVE_PTHREAD_ARG)
  119. set(Threads_FOUND TRUE)
  120. message(CHECK_PASS "yes")
  121. else()
  122. message(CHECK_FAIL "no")
  123. endif()
  124. endif()
  125. if(THREADS_HAVE_PTHREAD_ARG)
  126. set(Threads_FOUND TRUE)
  127. set(CMAKE_THREAD_LIBS_INIT "-pthread")
  128. endif()
  129. endif()
  130. endmacro()
  131. # Check if pthread functions are in normal C library.
  132. # We list some pthread functions in PTHREAD_C_CXX_TEST_SOURCE test code.
  133. # If the pthread functions already exist in C library, we could just use
  134. # them instead of linking to the additional pthread library.
  135. _threads_check_libc()
  136. # Check for -pthread first if enabled. This is the recommended
  137. # way, but not backwards compatible as one must also pass -pthread
  138. # as compiler flag then.
  139. if (THREADS_PREFER_PTHREAD_FLAG)
  140. _threads_check_flag_pthread()
  141. endif ()
  142. if(CMAKE_SYSTEM MATCHES "GHS-MULTI")
  143. _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE)
  144. endif()
  145. _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE)
  146. _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE)
  147. if (NOT THREADS_PREFER_PTHREAD_FLAG)
  148. _threads_check_flag_pthread()
  149. endif()
  150. if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_PTHREAD)
  151. set(CMAKE_USE_PTHREADS_INIT 1)
  152. set(Threads_FOUND TRUE)
  153. endif()
  154. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  155. set(CMAKE_USE_WIN32_THREADS_INIT 1)
  156. set(Threads_FOUND TRUE)
  157. endif()
  158. if(CMAKE_USE_PTHREADS_INIT)
  159. if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  160. # Use libcma if it exists and can be used. It provides more
  161. # symbols than the plain pthread library. CMA threads
  162. # have actually been deprecated:
  163. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  164. # http://docs.hp.com/en/947/d8.html
  165. # but we need to maintain compatibility here.
  166. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  167. # are available.
  168. CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  169. if(CMAKE_HAVE_HP_CMA)
  170. set(CMAKE_THREAD_LIBS_INIT "-lcma")
  171. set(CMAKE_HP_PTHREADS_INIT 1)
  172. set(Threads_FOUND TRUE)
  173. endif()
  174. set(CMAKE_USE_PTHREADS_INIT 1)
  175. endif()
  176. if(CMAKE_SYSTEM MATCHES "OSF1-V")
  177. set(CMAKE_USE_PTHREADS_INIT 0)
  178. set(CMAKE_THREAD_LIBS_INIT )
  179. endif()
  180. if(CMAKE_SYSTEM MATCHES "CYGWIN_NT" OR CMAKE_SYSTEM MATCHES "MSYS_NT")
  181. set(CMAKE_USE_PTHREADS_INIT 1)
  182. set(Threads_FOUND TRUE)
  183. set(CMAKE_THREAD_LIBS_INIT )
  184. set(CMAKE_USE_WIN32_THREADS_INIT 0)
  185. endif()
  186. endif()
  187. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  188. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  189. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
  190. if(THREADS_FOUND AND NOT TARGET Threads::Threads)
  191. add_library(Threads::Threads INTERFACE IMPORTED)
  192. if(THREADS_HAVE_PTHREAD_ARG)
  193. set_property(TARGET Threads::Threads
  194. PROPERTY INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:SHELL:-Xcompiler -pthread>"
  195. "$<$<NOT:$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>>:-pthread>")
  196. endif()
  197. if(CMAKE_THREAD_LIBS_INIT)
  198. set_property(TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
  199. endif()
  200. endif()