FindThreads.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #
  7. # This module determines the thread library of the system.
  8. #
  9. # The following variables are set
  10. #
  11. # ::
  12. #
  13. # CMAKE_THREAD_LIBS_INIT - the thread library
  14. # CMAKE_USE_SPROC_INIT - are we using sproc?
  15. # CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads?
  16. # CMAKE_USE_PTHREADS_INIT - are we using pthreads
  17. # CMAKE_HP_PTHREADS_INIT - are we using hp pthreads
  18. #
  19. # The following import target is created
  20. #
  21. # ::
  22. #
  23. # Threads::Threads
  24. #
  25. # For systems with multiple thread libraries, caller can set
  26. #
  27. # ::
  28. #
  29. # CMAKE_THREAD_PREFER_PTHREAD
  30. #
  31. # If the use of the -pthread compiler and linker flag is preferred then the
  32. # caller can set
  33. #
  34. # ::
  35. #
  36. # THREADS_PREFER_PTHREAD_FLAG
  37. #
  38. # Please note that the compiler flag can only be used with the imported
  39. # target. Use of both the imported target as well as this switch is highly
  40. # recommended for new code.
  41. include (CheckLibraryExists)
  42. include (CheckSymbolExists)
  43. set(Threads_FOUND FALSE)
  44. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  45. set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
  46. if(CMAKE_C_COMPILER_LOADED)
  47. include (CheckIncludeFile)
  48. elseif(CMAKE_CXX_COMPILER_LOADED)
  49. include (CheckIncludeFileCXX)
  50. else()
  51. message(FATAL_ERROR "FindThreads only works if either C or CXX language is enabled")
  52. endif()
  53. # Do we have sproc?
  54. if(CMAKE_SYSTEM_NAME MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  55. include (CheckIncludeFiles)
  56. CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
  57. endif()
  58. # Internal helper macro.
  59. # Do NOT even think about using it outside of this file!
  60. macro(_check_threads_lib LIBNAME FUNCNAME VARNAME)
  61. if(NOT Threads_FOUND)
  62. CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME})
  63. if(${VARNAME})
  64. set(CMAKE_THREAD_LIBS_INIT "-l${LIBNAME}")
  65. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  66. set(Threads_FOUND TRUE)
  67. endif()
  68. endif ()
  69. endmacro()
  70. # Internal helper macro.
  71. # Do NOT even think about using it outside of this file!
  72. macro(_check_pthreads_flag)
  73. if(NOT Threads_FOUND)
  74. # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
  75. if(NOT DEFINED THREADS_HAVE_PTHREAD_ARG)
  76. message(STATUS "Check if compiler accepts -pthread")
  77. if(CMAKE_C_COMPILER_LOADED)
  78. set(_threads_src ${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c)
  79. elseif(CMAKE_CXX_COMPILER_LOADED)
  80. set(_threads_src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindThreads/CheckForPthreads.cxx)
  81. configure_file(${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c "${_threads_src}" COPYONLY)
  82. endif()
  83. try_compile(THREADS_HAVE_PTHREAD_ARG
  84. ${CMAKE_BINARY_DIR}
  85. ${_threads_src}
  86. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  87. OUTPUT_VARIABLE OUTPUT)
  88. unset(_threads_src)
  89. if(THREADS_HAVE_PTHREAD_ARG)
  90. set(Threads_FOUND TRUE)
  91. message(STATUS "Check if compiler accepts -pthread - yes")
  92. else()
  93. message(STATUS "Check if compiler accepts -pthread - no")
  94. file(APPEND
  95. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  96. "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
  97. endif()
  98. endif()
  99. if(THREADS_HAVE_PTHREAD_ARG)
  100. set(Threads_FOUND TRUE)
  101. set(CMAKE_THREAD_LIBS_INIT "-pthread")
  102. endif()
  103. endif()
  104. endmacro()
  105. if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  106. # We have sproc
  107. set(CMAKE_USE_SPROC_INIT 1)
  108. else()
  109. # Do we have pthreads?
  110. if(CMAKE_C_COMPILER_LOADED)
  111. CHECK_INCLUDE_FILE("pthread.h" CMAKE_HAVE_PTHREAD_H)
  112. else()
  113. CHECK_INCLUDE_FILE_CXX("pthread.h" CMAKE_HAVE_PTHREAD_H)
  114. endif()
  115. if(CMAKE_HAVE_PTHREAD_H)
  116. #
  117. # We have pthread.h
  118. # Let's check for the library now.
  119. #
  120. set(CMAKE_HAVE_THREADS_LIBRARY)
  121. if(NOT THREADS_HAVE_PTHREAD_ARG)
  122. # Check if pthread functions are in normal C library
  123. CHECK_SYMBOL_EXISTS(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
  124. if(CMAKE_HAVE_LIBC_CREATE)
  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. _check_threads_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE)
  136. _check_threads_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE)
  137. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  138. # On sun also check for -lthread
  139. _check_threads_lib(thread thr_create CMAKE_HAVE_THR_CREATE)
  140. endif()
  141. endif()
  142. endif()
  143. _check_pthreads_flag()
  144. endif()
  145. endif()
  146. if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_CREATE)
  147. set(CMAKE_USE_PTHREADS_INIT 1)
  148. set(Threads_FOUND TRUE)
  149. endif()
  150. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  151. set(CMAKE_USE_WIN32_THREADS_INIT 1)
  152. set(Threads_FOUND TRUE)
  153. endif()
  154. if(CMAKE_USE_PTHREADS_INIT)
  155. if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  156. # Use libcma if it exists and can be used. It provides more
  157. # symbols than the plain pthread library. CMA threads
  158. # have actually been deprecated:
  159. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  160. # http://docs.hp.com/en/947/d8.html
  161. # but we need to maintain compatibility here.
  162. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  163. # are available.
  164. CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  165. if(CMAKE_HAVE_HP_CMA)
  166. set(CMAKE_THREAD_LIBS_INIT "-lcma")
  167. set(CMAKE_HP_PTHREADS_INIT 1)
  168. set(Threads_FOUND TRUE)
  169. endif()
  170. set(CMAKE_USE_PTHREADS_INIT 1)
  171. endif()
  172. if(CMAKE_SYSTEM MATCHES "OSF1-V")
  173. set(CMAKE_USE_PTHREADS_INIT 0)
  174. set(CMAKE_THREAD_LIBS_INIT )
  175. endif()
  176. if(CMAKE_SYSTEM MATCHES "CYGWIN_NT")
  177. set(CMAKE_USE_PTHREADS_INIT 1)
  178. set(Threads_FOUND TRUE)
  179. set(CMAKE_THREAD_LIBS_INIT )
  180. set(CMAKE_USE_WIN32_THREADS_INIT 0)
  181. endif()
  182. endif()
  183. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  184. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  185. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
  186. if(THREADS_FOUND AND NOT TARGET Threads::Threads)
  187. add_library(Threads::Threads INTERFACE IMPORTED)
  188. if(THREADS_HAVE_PTHREAD_ARG)
  189. set_property(TARGET Threads::Threads PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
  190. endif()
  191. if(CMAKE_THREAD_LIBS_INIT)
  192. set_property(TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
  193. endif()
  194. endif()