FindThreads.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 preferred 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-2015 Rolf Eike Beer <eike@sf-mail.de>
  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 (CheckLibraryExists)
  53. include (CheckSymbolExists)
  54. set(Threads_FOUND FALSE)
  55. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  56. set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
  57. if(CMAKE_C_COMPILER_LOADED)
  58. include (CheckIncludeFile)
  59. elseif(CMAKE_CXX_COMPILER_LOADED)
  60. include (CheckIncludeFileCXX)
  61. else()
  62. message(FATAL_ERROR "FindThreads only works if either C or CXX language is enabled")
  63. endif()
  64. # Do we have sproc?
  65. if(CMAKE_SYSTEM_NAME MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  66. include (CheckIncludeFiles)
  67. CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
  68. endif()
  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(STATUS "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_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
  95. ${CMAKE_BINARY_DIR}
  96. ${_threads_src}
  97. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  98. COMPILE_OUTPUT_VARIABLE OUTPUT)
  99. unset(_threads_src)
  100. if(THREADS_HAVE_PTHREAD_ARG)
  101. if(THREADS_PTHREAD_ARG STREQUAL "2")
  102. set(Threads_FOUND TRUE)
  103. message(STATUS "Check if compiler accepts -pthread - yes")
  104. else()
  105. message(STATUS "Check if compiler accepts -pthread - no")
  106. file(APPEND
  107. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  108. "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
  109. endif()
  110. else()
  111. message(STATUS "Check if compiler accepts -pthread - no")
  112. file(APPEND
  113. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  114. "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
  115. endif()
  116. endif()
  117. if(THREADS_HAVE_PTHREAD_ARG)
  118. set(Threads_FOUND TRUE)
  119. set(CMAKE_THREAD_LIBS_INIT "-pthread")
  120. endif()
  121. endif()
  122. endmacro()
  123. if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  124. # We have sproc
  125. set(CMAKE_USE_SPROC_INIT 1)
  126. else()
  127. # Do we have pthreads?
  128. if(CMAKE_C_COMPILER_LOADED)
  129. CHECK_INCLUDE_FILE("pthread.h" CMAKE_HAVE_PTHREAD_H)
  130. else()
  131. CHECK_INCLUDE_FILE_CXX("pthread.h" CMAKE_HAVE_PTHREAD_H)
  132. endif()
  133. if(CMAKE_HAVE_PTHREAD_H)
  134. #
  135. # We have pthread.h
  136. # Let's check for the library now.
  137. #
  138. set(CMAKE_HAVE_THREADS_LIBRARY)
  139. if(NOT THREADS_HAVE_PTHREAD_ARG)
  140. # Check if pthread functions are in normal C library
  141. CHECK_SYMBOL_EXISTS(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
  142. if(CMAKE_HAVE_LIBC_CREATE)
  143. set(CMAKE_THREAD_LIBS_INIT "")
  144. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  145. set(Threads_FOUND TRUE)
  146. else()
  147. # Check for -pthread first if enabled. This is the recommended
  148. # way, but not backwards compatible as one must also pass -pthread
  149. # as compiler flag then.
  150. if (THREADS_PREFER_PTHREAD_FLAG)
  151. _check_pthreads_flag()
  152. endif ()
  153. _check_threads_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE)
  154. _check_threads_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE)
  155. if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  156. # On sun also check for -lthread
  157. _check_threads_lib(thread thr_create CMAKE_HAVE_THR_CREATE)
  158. endif()
  159. endif()
  160. endif()
  161. _check_pthreads_flag()
  162. endif()
  163. endif()
  164. if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_CREATE)
  165. set(CMAKE_USE_PTHREADS_INIT 1)
  166. set(Threads_FOUND TRUE)
  167. endif()
  168. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  169. set(CMAKE_USE_WIN32_THREADS_INIT 1)
  170. set(Threads_FOUND TRUE)
  171. endif()
  172. if(CMAKE_USE_PTHREADS_INIT)
  173. if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  174. # Use libcma if it exists and can be used. It provides more
  175. # symbols than the plain pthread library. CMA threads
  176. # have actually been deprecated:
  177. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  178. # http://docs.hp.com/en/947/d8.html
  179. # but we need to maintain compatibility here.
  180. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  181. # are available.
  182. CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  183. if(CMAKE_HAVE_HP_CMA)
  184. set(CMAKE_THREAD_LIBS_INIT "-lcma")
  185. set(CMAKE_HP_PTHREADS_INIT 1)
  186. set(Threads_FOUND TRUE)
  187. endif()
  188. set(CMAKE_USE_PTHREADS_INIT 1)
  189. endif()
  190. if(CMAKE_SYSTEM MATCHES "OSF1-V")
  191. set(CMAKE_USE_PTHREADS_INIT 0)
  192. set(CMAKE_THREAD_LIBS_INIT )
  193. endif()
  194. if(CMAKE_SYSTEM MATCHES "CYGWIN_NT")
  195. set(CMAKE_USE_PTHREADS_INIT 1)
  196. set(Threads_FOUND TRUE)
  197. set(CMAKE_THREAD_LIBS_INIT )
  198. set(CMAKE_USE_WIN32_THREADS_INIT 0)
  199. endif()
  200. endif()
  201. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  202. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  203. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
  204. if(THREADS_FOUND AND NOT TARGET Threads::Threads)
  205. add_library(Threads::Threads INTERFACE IMPORTED)
  206. if(THREADS_HAVE_PTHREAD_ARG)
  207. set_property(TARGET Threads::Threads PROPERTY INTERFACE_COMPILE_OPTIONS "-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()