FindThreads.cmake 6.1 KB

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