FindThreads.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #.rst:
  2. # FindThreads
  3. # -----------
  4. #
  5. # This module determines the thread library of the system.
  6. #
  7. # The following import target is created
  8. #
  9. # ::
  10. #
  11. # CMake::Threads
  12. #
  13. # The following variables are set
  14. #
  15. # ::
  16. #
  17. # THREADS_FOUND
  18. # THREADS_LIBRARIES
  19. # THREADS_DEFINITIONS
  20. #
  21. # Legacy variables
  22. #
  23. # ::
  24. #
  25. # CMAKE_THREAD_LIBS_INIT - the thread library
  26. # CMAKE_THREAD_DEFS_INIT - the thread compile definitions
  27. # CMAKE_USE_SPROC_INIT - are we using sproc?
  28. # CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads?
  29. # CMAKE_USE_PTHREADS_INIT - are we using pthreads
  30. # CMAKE_HP_PTHREADS_INIT - are we using hp pthreads
  31. #
  32. # For systems with multiple thread libraries, caller can set
  33. #
  34. # ::
  35. #
  36. # CMAKE_THREAD_PREFER_PTHREAD
  37. #=============================================================================
  38. # Copyright 2002-2014 Kitware, Inc.
  39. #
  40. # Distributed under the OSI-approved BSD License (the "License");
  41. # see accompanying file Copyright.txt for details.
  42. #
  43. # This software is distributed WITHOUT ANY WARRANTY; without even the
  44. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  45. # See the License for more information.
  46. #=============================================================================
  47. # (To distribute this file outside of CMake, substitute the full
  48. # License text for the above reference.)
  49. include(CheckIncludeFiles)
  50. include(CheckLibraryExists)
  51. include(CheckSymbolExists)
  52. set(Threads_FOUND FALSE)
  53. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  54. set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
  55. # Do we have sproc?
  56. if(CMAKE_SYSTEM_NAME MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  57. check_include_files("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
  58. endif()
  59. if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  60. # We have sproc
  61. set(CMAKE_USE_SPROC_INIT 1)
  62. else()
  63. # Do we have pthreads?
  64. check_include_files("pthread.h" CMAKE_HAVE_PTHREAD_H)
  65. if(CMAKE_HAVE_PTHREAD_H)
  66. #
  67. # We have pthread.h
  68. # Let's check for the library now.
  69. #
  70. set(CMAKE_HAVE_THREADS_LIBRARY)
  71. # Check if pthread functions are in normal C library
  72. check_symbol_exists(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
  73. if(CMAKE_HAVE_LIBC_CREATE)
  74. set(CMAKE_THREAD_LIBS_INIT "")
  75. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  76. set(Threads_FOUND TRUE)
  77. endif()
  78. if(NOT Threads_FOUND AND NOT THREADS_HAVE_PTHREAD_ARG)
  79. message(STATUS "Check if compiler accepts -pthread")
  80. try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
  81. ${CMAKE_BINARY_DIR}
  82. ${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c
  83. COMPILE_DEFINITIONS -pthread
  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. 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 returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
  94. set(THREADS_HAVE_PTHREAD_ARG)
  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(NOT Threads_FOUND AND THREADS_HAVE_PTHREAD_ARG)
  104. set(Threads_FOUND TRUE)
  105. set(CMAKE_THREAD_LIBS_INIT "-pthread")
  106. set(CMAKE_THREAD_DEFS_INIT "-pthread")
  107. endif()
  108. if(NOT Threads_FOUND)
  109. if(NOT CMAKE_HAVE_THREADS_LIBRARY)
  110. # Do we have -lpthreads
  111. check_library_exists(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
  112. if(CMAKE_HAVE_PTHREADS_CREATE)
  113. set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
  114. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  115. set(Threads_FOUND TRUE)
  116. endif()
  117. # Ok, how about -lpthread
  118. check_library_exists(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
  119. if(CMAKE_HAVE_PTHREAD_CREATE)
  120. set(CMAKE_THREAD_LIBS_INIT "-lpthread")
  121. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  122. set(Threads_FOUND TRUE)
  123. endif()
  124. if(CMAKE_SYSTEM MATCHES "SunOS.*")
  125. # On sun also check for -lthread
  126. check_library_exists(thread thr_create "" CMAKE_HAVE_THR_CREATE)
  127. if(CMAKE_HAVE_THR_CREATE)
  128. set(CMAKE_THREAD_LIBS_INIT "-lthread")
  129. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  130. set(Threads_FOUND TRUE)
  131. endif()
  132. endif()
  133. endif()
  134. endif()
  135. endif()
  136. endif()
  137. if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_CREATE)
  138. set(CMAKE_USE_PTHREADS_INIT 1)
  139. set(Threads_FOUND TRUE)
  140. endif()
  141. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  142. set(CMAKE_USE_WIN32_THREADS_INIT 1)
  143. set(Threads_FOUND TRUE)
  144. endif()
  145. if(CMAKE_USE_PTHREADS_INIT)
  146. if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  147. # Use libcma if it exists and can be used. It provides more
  148. # symbols than the plain pthread library. CMA threads
  149. # have actually been deprecated:
  150. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  151. # http://docs.hp.com/en/947/d8.html
  152. # but we need to maintain compatibility here.
  153. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  154. # are available.
  155. check_library_exists(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  156. if(CMAKE_HAVE_HP_CMA)
  157. set(CMAKE_THREAD_LIBS_INIT "-lcma")
  158. set(CMAKE_HP_PTHREADS_INIT 1)
  159. set(Threads_FOUND TRUE)
  160. endif()
  161. set(CMAKE_USE_PTHREADS_INIT 1)
  162. endif()
  163. if(CMAKE_SYSTEM MATCHES "OSF1-V")
  164. set(CMAKE_USE_PTHREADS_INIT 0)
  165. set(CMAKE_THREAD_LIBS_INIT )
  166. endif()
  167. if(CMAKE_SYSTEM MATCHES "CYGWIN_NT")
  168. set(CMAKE_USE_PTHREADS_INIT 1)
  169. set(Threads_FOUND TRUE)
  170. set(CMAKE_THREAD_LIBS_INIT )
  171. set(CMAKE_USE_WIN32_THREADS_INIT 0)
  172. endif()
  173. endif()
  174. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  175. include(FindPackageHandleStandardArgs)
  176. find_package_handle_standard_args(Threads DEFAULT_MSG Threads_FOUND)
  177. if(THREADS_FOUND)
  178. set(THREADS_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
  179. set(THREADS_DEFINITIONS "${CMAKE_THREAD_DEFS_INIT}")
  180. endif()