FindThreads.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #.rst:
  2. # FindThreads
  3. # -----------
  4. #
  5. # This module determines the thread library of the system.
  6. #
  7. # The following variables are set
  8. #
  9. # ::
  10. #
  11. # CMAKE_THREAD_LIBS_INIT - the thread library
  12. # CMAKE_USE_SPROC_INIT - are we using sproc?
  13. # CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads?
  14. # CMAKE_USE_PTHREADS_INIT - are we using pthreads
  15. # CMAKE_HP_PTHREADS_INIT - are we using hp pthreads
  16. #
  17. # The following import target is created
  18. #
  19. # ::
  20. #
  21. # Threads::Threads
  22. #
  23. # For systems with multiple thread libraries, caller can set
  24. #
  25. # ::
  26. #
  27. # CMAKE_THREAD_PREFER_PTHREAD
  28. #
  29. # If the use of the -pthread compiler and linker flag is prefered then the
  30. # caller can set
  31. #
  32. # ::
  33. #
  34. # THREADS_PREFER_PTHREAD_FLAG
  35. #
  36. # Please note that the compiler flag can only be used with the imported
  37. # target. Use of both the imported target as well as this switch is highly
  38. # recommended for new code.
  39. #=============================================================================
  40. # Copyright 2002-2009 Kitware, Inc.
  41. # Copyright 2011-2014 Rolf Eike Beer <[email protected]>
  42. #
  43. # Distributed under the OSI-approved BSD License (the "License");
  44. # see accompanying file Copyright.txt for details.
  45. #
  46. # This software is distributed WITHOUT ANY WARRANTY; without even the
  47. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  48. # See the License for more information.
  49. #=============================================================================
  50. # (To distribute this file outside of CMake, substitute the full
  51. # License text for the above reference.)
  52. include (CheckIncludeFiles)
  53. include (CheckLibraryExists)
  54. include (CheckSymbolExists)
  55. set(Threads_FOUND FALSE)
  56. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  57. set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
  58. # Do we have sproc?
  59. if(CMAKE_SYSTEM_NAME MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  60. CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
  61. endif()
  62. # Internal helper macro.
  63. # Do NOT even think about using it outside of this file!
  64. macro(_check_threads_lib LIBNAME FUNCNAME VARNAME)
  65. if(NOT Threads_FOUND)
  66. CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME})
  67. if(${VARNAME})
  68. set(CMAKE_THREAD_LIBS_INIT "-l${LIBNAME}")
  69. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  70. set(Threads_FOUND TRUE)
  71. endif()
  72. endif ()
  73. endmacro()
  74. # Internal helper macro.
  75. # Do NOT even think about using it outside of this file!
  76. macro(_check_pthreads_flag)
  77. if(NOT Threads_FOUND)
  78. # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
  79. if(NOT DEFINED THREADS_HAVE_PTHREAD_ARG)
  80. message(STATUS "Check if compiler accepts -pthread")
  81. try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
  82. ${CMAKE_BINARY_DIR}
  83. ${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c
  84. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  85. COMPILE_OUTPUT_VARIABLE OUTPUT)
  86. if(THREADS_HAVE_PTHREAD_ARG)
  87. if(THREADS_PTHREAD_ARG STREQUAL "2")
  88. set(Threads_FOUND TRUE)
  89. message(STATUS "Check if compiler accepts -pthread - yes")
  90. else()
  91. message(STATUS "Check if compiler accepts -pthread - no")
  92. file(APPEND
  93. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  94. "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
  95. endif()
  96. else()
  97. message(STATUS "Check if compiler accepts -pthread - no")
  98. file(APPEND
  99. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  100. "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
  101. endif()
  102. endif()
  103. if(THREADS_HAVE_PTHREAD_ARG)
  104. set(Threads_FOUND TRUE)
  105. set(CMAKE_THREAD_LIBS_INIT "-pthread")
  106. endif()
  107. endif()
  108. endmacro()
  109. if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  110. # We have sproc
  111. set(CMAKE_USE_SPROC_INIT 1)
  112. else()
  113. # Do we have pthreads?
  114. CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
  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()