FindThreads.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. # For systems with multiple thread libraries, caller can set
  18. #
  19. # ::
  20. #
  21. # CMAKE_THREAD_PREFER_PTHREAD
  22. #=============================================================================
  23. # Copyright 2002-2009 Kitware, Inc.
  24. #
  25. # Distributed under the OSI-approved BSD License (the "License");
  26. # see accompanying file Copyright.txt for details.
  27. #
  28. # This software is distributed WITHOUT ANY WARRANTY; without even the
  29. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30. # See the License for more information.
  31. #=============================================================================
  32. # (To distribute this file outside of CMake, substitute the full
  33. # License text for the above reference.)
  34. include (CheckIncludeFiles)
  35. include (CheckLibraryExists)
  36. include (CheckSymbolExists)
  37. set(Threads_FOUND FALSE)
  38. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  39. set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
  40. # Do we have sproc?
  41. if(CMAKE_SYSTEM_NAME MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  42. CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
  43. endif()
  44. if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  45. # We have sproc
  46. set(CMAKE_USE_SPROC_INIT 1)
  47. else()
  48. # Do we have pthreads?
  49. CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
  50. if(CMAKE_HAVE_PTHREAD_H)
  51. #
  52. # We have pthread.h
  53. # Let's check for the library now.
  54. #
  55. set(CMAKE_HAVE_THREADS_LIBRARY)
  56. if(NOT THREADS_HAVE_PTHREAD_ARG)
  57. # Check if pthread functions are in normal C library
  58. CHECK_SYMBOL_EXISTS(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
  59. if(CMAKE_HAVE_LIBC_CREATE)
  60. set(CMAKE_THREAD_LIBS_INIT "")
  61. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  62. set(Threads_FOUND TRUE)
  63. else()
  64. # Do we have -lpthreads
  65. CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
  66. if(CMAKE_HAVE_PTHREADS_CREATE)
  67. set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
  68. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  69. set(Threads_FOUND TRUE)
  70. else()
  71. # Ok, how about -lpthread
  72. CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
  73. if(CMAKE_HAVE_PTHREAD_CREATE)
  74. set(CMAKE_THREAD_LIBS_INIT "-lpthread")
  75. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  76. set(Threads_FOUND TRUE)
  77. elseif(CMAKE_SYSTEM_NAME MATCHES "SunOS")
  78. # On sun also check for -lthread
  79. CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
  80. if(CMAKE_HAVE_THR_CREATE)
  81. set(CMAKE_THREAD_LIBS_INIT "-lthread")
  82. set(CMAKE_HAVE_THREADS_LIBRARY 1)
  83. set(Threads_FOUND TRUE)
  84. endif()
  85. endif()
  86. endif()
  87. endif()
  88. endif()
  89. if(NOT CMAKE_HAVE_THREADS_LIBRARY)
  90. # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
  91. if("x${THREADS_HAVE_PTHREAD_ARG}" STREQUAL "x")
  92. message(STATUS "Check if compiler accepts -pthread")
  93. try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
  94. ${CMAKE_BINARY_DIR}
  95. ${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c
  96. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  97. COMPILE_OUTPUT_VARIABLE OUTPUT)
  98. if(THREADS_HAVE_PTHREAD_ARG)
  99. if(THREADS_PTHREAD_ARG STREQUAL "2")
  100. set(Threads_FOUND TRUE)
  101. message(STATUS "Check if compiler accepts -pthread - yes")
  102. else()
  103. message(STATUS "Check if compiler accepts -pthread - no")
  104. file(APPEND
  105. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  106. "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
  107. endif()
  108. else()
  109. message(STATUS "Check if compiler accepts -pthread - no")
  110. file(APPEND
  111. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  112. "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
  113. endif()
  114. endif()
  115. if(THREADS_HAVE_PTHREAD_ARG)
  116. set(Threads_FOUND TRUE)
  117. set(CMAKE_THREAD_LIBS_INIT "-pthread")
  118. endif()
  119. endif()
  120. endif()
  121. endif()
  122. if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_CREATE)
  123. set(CMAKE_USE_PTHREADS_INIT 1)
  124. set(Threads_FOUND TRUE)
  125. endif()
  126. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  127. set(CMAKE_USE_WIN32_THREADS_INIT 1)
  128. set(Threads_FOUND TRUE)
  129. endif()
  130. if(CMAKE_USE_PTHREADS_INIT)
  131. if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  132. # Use libcma if it exists and can be used. It provides more
  133. # symbols than the plain pthread library. CMA threads
  134. # have actually been deprecated:
  135. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  136. # http://docs.hp.com/en/947/d8.html
  137. # but we need to maintain compatibility here.
  138. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  139. # are available.
  140. CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  141. if(CMAKE_HAVE_HP_CMA)
  142. set(CMAKE_THREAD_LIBS_INIT "-lcma")
  143. set(CMAKE_HP_PTHREADS_INIT 1)
  144. set(Threads_FOUND TRUE)
  145. endif()
  146. set(CMAKE_USE_PTHREADS_INIT 1)
  147. endif()
  148. if(CMAKE_SYSTEM MATCHES "OSF1-V")
  149. set(CMAKE_USE_PTHREADS_INIT 0)
  150. set(CMAKE_THREAD_LIBS_INIT )
  151. endif()
  152. if(CMAKE_SYSTEM MATCHES "CYGWIN_NT")
  153. set(CMAKE_USE_PTHREADS_INIT 1)
  154. set(Threads_FOUND TRUE)
  155. set(CMAKE_THREAD_LIBS_INIT )
  156. set(CMAKE_USE_WIN32_THREADS_INIT 0)
  157. endif()
  158. endif()
  159. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  160. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  161. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)