1
0

FindThreads.cmake 5.4 KB

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