FindThreads.cmake 7.3 KB

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