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