FindThreads.cmake 7.9 KB

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