FindThreads.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(CMAKE_SYSTEM MATCHES "SunOS.*")
  74. ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY)
  75. ENDIF(NOT THREADS_HAVE_PTHREAD_ARG)
  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 MATCHES "^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("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
  102. IF(THREADS_HAVE_PTHREAD_ARG)
  103. SET(Threads_FOUND TRUE)
  104. SET(CMAKE_THREAD_LIBS_INIT "-pthread")
  105. ENDIF()
  106. ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY)
  107. ENDIF(CMAKE_HAVE_PTHREAD_H)
  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(CMAKE_HAVE_HP_CMA)
  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(CMAKE_USE_PTHREADS_INIT)
  146. INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  147. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)