FindThreads.cmake 4.8 KB

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