FindThreads.cmake 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. SET(Threads_FOUND FALSE)
  25. # Do we have sproc?
  26. IF(CMAKE_SYSTEM MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  27. CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
  28. ENDIF()
  29. IF(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  30. # We have sproc
  31. SET(CMAKE_USE_SPROC_INIT 1)
  32. ELSE()
  33. # Do we have pthreads?
  34. CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
  35. IF(CMAKE_HAVE_PTHREAD_H)
  36. #
  37. # We have pthread.h
  38. # Let's check for the library now.
  39. #
  40. SET(CMAKE_HAVE_THREADS_LIBRARY)
  41. IF(NOT THREADS_HAVE_PTHREAD_ARG)
  42. # Do we have -lpthreads
  43. CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
  44. IF(CMAKE_HAVE_PTHREADS_CREATE)
  45. SET(CMAKE_THREAD_LIBS_INIT "-lpthreads")
  46. SET(CMAKE_HAVE_THREADS_LIBRARY 1)
  47. SET(Threads_FOUND TRUE)
  48. ENDIF()
  49. # Ok, how about -lpthread
  50. CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
  51. IF(CMAKE_HAVE_PTHREAD_CREATE)
  52. SET(CMAKE_THREAD_LIBS_INIT "-lpthread")
  53. SET(Threads_FOUND TRUE)
  54. SET(CMAKE_HAVE_THREADS_LIBRARY 1)
  55. ENDIF()
  56. IF(CMAKE_SYSTEM MATCHES "SunOS.*")
  57. # On sun also check for -lthread
  58. CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
  59. IF(CMAKE_HAVE_THR_CREATE)
  60. SET(CMAKE_THREAD_LIBS_INIT "-lthread")
  61. SET(CMAKE_HAVE_THREADS_LIBRARY 1)
  62. SET(Threads_FOUND TRUE)
  63. ENDIF()
  64. ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*")
  65. ENDIF(NOT THREADS_HAVE_PTHREAD_ARG)
  66. IF(NOT CMAKE_HAVE_THREADS_LIBRARY)
  67. # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
  68. IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
  69. MESSAGE(STATUS "Check if compiler accepts -pthread")
  70. TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
  71. ${CMAKE_BINARY_DIR}
  72. ${CMAKE_ROOT}/Modules/CheckForPthreads.c
  73. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  74. COMPILE_OUTPUT_VARIABLE OUTPUT)
  75. IF(THREADS_HAVE_PTHREAD_ARG)
  76. IF(THREADS_PTHREAD_ARG MATCHES "^2$")
  77. SET(Threads_FOUND TRUE)
  78. MESSAGE(STATUS "Check if compiler accepts -pthread - yes")
  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 returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
  84. ENDIF()
  85. ELSE()
  86. MESSAGE(STATUS "Check if compiler accepts -pthread - no")
  87. FILE(APPEND
  88. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  89. "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
  90. ENDIF()
  91. ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
  92. IF(THREADS_HAVE_PTHREAD_ARG)
  93. SET(Threads_FOUND TRUE)
  94. SET(CMAKE_THREAD_LIBS_INIT "-pthread")
  95. ENDIF()
  96. ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY)
  97. ENDIF(CMAKE_HAVE_PTHREAD_H)
  98. ENDIF()
  99. IF(CMAKE_THREAD_LIBS_INIT)
  100. SET(CMAKE_USE_PTHREADS_INIT 1)
  101. SET(Threads_FOUND TRUE)
  102. ENDIF()
  103. IF(CMAKE_SYSTEM MATCHES "Windows")
  104. SET(CMAKE_USE_WIN32_THREADS_INIT 1)
  105. SET(Threads_FOUND TRUE)
  106. ENDIF()
  107. IF(CMAKE_USE_PTHREADS_INIT)
  108. IF(CMAKE_SYSTEM MATCHES "HP-UX-*")
  109. # Use libcma if it exists and can be used. It provides more
  110. # symbols than the plain pthread library. CMA threads
  111. # have actually been deprecated:
  112. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  113. # http://docs.hp.com/en/947/d8.html
  114. # but we need to maintain compatibility here.
  115. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  116. # are available.
  117. CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  118. IF(CMAKE_HAVE_HP_CMA)
  119. SET(CMAKE_THREAD_LIBS_INIT "-lcma")
  120. SET(CMAKE_HP_PTHREADS_INIT 1)
  121. SET(Threads_FOUND TRUE)
  122. ENDIF(CMAKE_HAVE_HP_CMA)
  123. SET(CMAKE_USE_PTHREADS_INIT 1)
  124. ENDIF()
  125. IF(CMAKE_SYSTEM MATCHES "OSF1-V*")
  126. SET(CMAKE_USE_PTHREADS_INIT 0)
  127. SET(CMAKE_THREAD_LIBS_INIT )
  128. ENDIF()
  129. IF(CMAKE_SYSTEM MATCHES "CYGWIN_NT*")
  130. SET(CMAKE_USE_PTHREADS_INIT 1)
  131. SET(Threads_FOUND TRUE)
  132. SET(CMAKE_THREAD_LIBS_INIT )
  133. SET(CMAKE_USE_WIN32_THREADS_INIT 0)
  134. ENDIF()
  135. ENDIF(CMAKE_USE_PTHREADS_INIT)
  136. INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  137. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)