FindThreads.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # Updated FindThreads.cmake that supports pthread-win32
  2. # Downloaded from http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=6399
  3. # - This module determines the thread library of the system.
  4. #
  5. # The following variables are set
  6. # CMAKE_THREAD_LIBS_INIT - the thread library
  7. # CMAKE_USE_SPROC_INIT - are we using sproc?
  8. # CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads?
  9. # CMAKE_USE_PTHREADS_INIT - are we using pthreads
  10. # CMAKE_HP_PTHREADS_INIT - are we using hp pthreads
  11. #
  12. # If use of pthreads-win32 is desired, the following variables
  13. # can be set.
  14. #
  15. # THREADS_USE_PTHREADS_WIN32 -
  16. # Setting this to true searches for the pthreads-win32
  17. # port (since CMake 2.8.0)
  18. #
  19. # THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME
  20. # C = no exceptions (default)
  21. # (NOTE: This is the default scheme on most POSIX thread
  22. # implementations and what you should probably be using)
  23. # CE = C++ Exception Handling
  24. # SE = Structure Exception Handling (MSVC only)
  25. # (NOTE: Changing this option from the default may affect
  26. # the portability of your application. See pthreads-win32
  27. # documentation for more details.)
  28. #
  29. #======================================================
  30. # Example usage where threading library
  31. # is provided by the system:
  32. #
  33. # find_package(Threads REQUIRED)
  34. # add_executable(foo foo.cc)
  35. # target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT})
  36. #
  37. # Example usage if pthreads-win32 is desired on Windows
  38. # or a system provided thread library:
  39. #
  40. # set(THREADS_USE_PTHREADS_WIN32 true)
  41. # find_package(Threads REQUIRED)
  42. # include_directories(${THREADS_PTHREADS_INCLUDE_DIR})
  43. #
  44. # add_executable(foo foo.cc)
  45. # target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT})
  46. #
  47. INCLUDE (CheckIncludeFiles)
  48. INCLUDE (CheckLibraryExists)
  49. SET(Threads_FOUND FALSE)
  50. IF(WIN32 AND NOT CYGWIN AND THREADS_USE_PTHREADS_WIN32)
  51. SET(_Threads_ptwin32 true)
  52. ENDIF()
  53. # Do we have sproc?
  54. IF(CMAKE_SYSTEM MATCHES IRIX)
  55. CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
  56. ENDIF()
  57. IF(CMAKE_HAVE_SPROC_H)
  58. # We have sproc
  59. SET(CMAKE_USE_SPROC_INIT 1)
  60. ELSEIF(_Threads_ptwin32)
  61. IF(NOT DEFINED THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME)
  62. # Assign the default scheme
  63. SET(THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME "C")
  64. ELSE()
  65. # Validate the scheme specified by the user
  66. IF(NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "C" AND
  67. NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "CE" AND
  68. NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE")
  69. MESSAGE(FATAL_ERROR "See documentation for FindPthreads.cmake, only C, CE, and SE modes are allowed")
  70. ENDIF()
  71. IF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE")
  72. MESSAGE(FATAL_ERROR "Structured Exception Handling is only allowed for MSVC")
  73. ENDIF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE")
  74. ENDIF()
  75. FIND_PATH(THREADS_PTHREADS_INCLUDE_DIR pthread.h)
  76. # Determine the library filename
  77. IF(MSVC)
  78. SET(_Threads_pthreads_libname
  79. pthreadV${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2)
  80. ELSEIF(MINGW)
  81. SET(_Threads_pthreads_libname
  82. pthreadG${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2)
  83. ELSE()
  84. MESSAGE(FATAL_ERROR "This should never happen")
  85. ENDIF()
  86. # Use the include path to help find the library if possible
  87. SET(_Threads_lib_paths "")
  88. IF(THREADS_PTHREADS_INCLUDE_DIR)
  89. GET_FILENAME_COMPONENT(_Threads_root_dir
  90. ${THREADS_PTHREADS_INCLUDE_DIR} PATH)
  91. SET(_Threads_lib_paths ${_Threads_root_dir}/lib)
  92. ENDIF()
  93. FIND_LIBRARY(THREADS_PTHREADS_WIN32_LIBRARY
  94. NAMES ${_Threads_pthreads_libname}
  95. PATHS ${_Threads_lib_paths}
  96. DOC "The Portable Threads Library for Win32"
  97. NO_SYSTEM_PATH
  98. )
  99. IF(THREADS_PTHREADS_INCLUDE_DIR AND THREADS_PTHREADS_WIN32_LIBRARY)
  100. MARK_AS_ADVANCED(THREADS_PTHREADS_INCLUDE_DIR)
  101. SET(CMAKE_THREAD_LIBS_INIT ${THREADS_PTHREADS_WIN32_LIBRARY})
  102. SET(CMAKE_HAVE_THREADS_LIBRARY 1)
  103. SET(Threads_FOUND TRUE)
  104. ENDIF()
  105. MARK_AS_ADVANCED(THREADS_PTHREADS_WIN32_LIBRARY)
  106. ELSE()
  107. # Do we have pthreads?
  108. CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
  109. IF(CMAKE_HAVE_PTHREAD_H)
  110. #
  111. # We have pthread.h
  112. # Let's check for the library now.
  113. #
  114. SET(CMAKE_HAVE_THREADS_LIBRARY)
  115. IF(NOT THREADS_HAVE_PTHREAD_ARG)
  116. # Do we have -lpthreads
  117. CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
  118. IF(CMAKE_HAVE_PTHREADS_CREATE)
  119. SET(CMAKE_THREAD_LIBS_INIT "-lpthreads")
  120. SET(CMAKE_HAVE_THREADS_LIBRARY 1)
  121. SET(Threads_FOUND TRUE)
  122. ENDIF()
  123. # Ok, how about -lpthread
  124. CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
  125. IF(CMAKE_HAVE_PTHREAD_CREATE)
  126. SET(CMAKE_THREAD_LIBS_INIT "-lpthread")
  127. SET(Threads_FOUND TRUE)
  128. SET(CMAKE_HAVE_THREADS_LIBRARY 1)
  129. ENDIF()
  130. IF(CMAKE_SYSTEM MATCHES "SunOS.*")
  131. # On sun also check for -lthread
  132. CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
  133. IF(CMAKE_HAVE_THR_CREATE)
  134. SET(CMAKE_THREAD_LIBS_INIT "-lthread")
  135. SET(CMAKE_HAVE_THREADS_LIBRARY 1)
  136. SET(Threads_FOUND TRUE)
  137. ENDIF()
  138. ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*")
  139. ENDIF(NOT THREADS_HAVE_PTHREAD_ARG)
  140. IF(NOT CMAKE_HAVE_THREADS_LIBRARY)
  141. # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
  142. IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
  143. MESSAGE(STATUS "Check if compiler accepts -pthread")
  144. TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
  145. ${CMAKE_BINARY_DIR}
  146. ${CMAKE_ROOT}/Modules/CheckForPthreads.c
  147. CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
  148. COMPILE_OUTPUT_VARIABLE OUTPUT)
  149. IF(THREADS_HAVE_PTHREAD_ARG)
  150. IF(THREADS_PTHREAD_ARG MATCHES "^2$")
  151. SET(Threads_FOUND TRUE)
  152. MESSAGE(STATUS "Check if compiler accepts -pthread - yes")
  153. ELSE()
  154. MESSAGE(STATUS "Check if compiler accepts -pthread - no")
  155. FILE(APPEND
  156. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  157. "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
  158. ENDIF()
  159. ELSE()
  160. MESSAGE(STATUS "Check if compiler accepts -pthread - no")
  161. FILE(APPEND
  162. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  163. "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
  164. ENDIF()
  165. ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
  166. IF(THREADS_HAVE_PTHREAD_ARG)
  167. SET(Threads_FOUND TRUE)
  168. SET(CMAKE_THREAD_LIBS_INIT "-pthread")
  169. ENDIF()
  170. ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY)
  171. ENDIF(CMAKE_HAVE_PTHREAD_H)
  172. ENDIF()
  173. IF(CMAKE_THREAD_LIBS_INIT)
  174. SET(CMAKE_USE_PTHREADS_INIT 1)
  175. SET(Threads_FOUND TRUE)
  176. ENDIF()
  177. IF(CMAKE_SYSTEM MATCHES "Windows"
  178. AND NOT THREADS_USE_PTHREADS_WIN32)
  179. SET(CMAKE_USE_WIN32_THREADS_INIT 1)
  180. SET(Threads_FOUND TRUE)
  181. ENDIF()
  182. IF(CMAKE_USE_PTHREADS_INIT)
  183. IF(CMAKE_SYSTEM MATCHES "HP-UX-*")
  184. # Use libcma if it exists and can be used. It provides more
  185. # symbols than the plain pthread library. CMA threads
  186. # have actually been deprecated:
  187. # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
  188. # http://docs.hp.com/en/947/d8.html
  189. # but we need to maintain compatibility here.
  190. # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
  191. # are available.
  192. CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
  193. IF(CMAKE_HAVE_HP_CMA)
  194. SET(CMAKE_THREAD_LIBS_INIT "-lcma")
  195. SET(CMAKE_HP_PTHREADS_INIT 1)
  196. SET(Threads_FOUND TRUE)
  197. ENDIF(CMAKE_HAVE_HP_CMA)
  198. SET(CMAKE_USE_PTHREADS_INIT 1)
  199. ENDIF()
  200. IF(CMAKE_SYSTEM MATCHES "OSF1-V*")
  201. SET(CMAKE_USE_PTHREADS_INIT 0)
  202. SET(CMAKE_THREAD_LIBS_INIT )
  203. ENDIF()
  204. IF(CMAKE_SYSTEM MATCHES "CYGWIN_NT*")
  205. SET(CMAKE_USE_PTHREADS_INIT 1)
  206. SET(Threads_FOUND TRUE)
  207. SET(CMAKE_THREAD_LIBS_INIT )
  208. SET(CMAKE_USE_WIN32_THREADS_INIT 0)
  209. ENDIF()
  210. ENDIF(CMAKE_USE_PTHREADS_INIT)
  211. INCLUDE(FindPackageHandleStandardArgs)
  212. IF(_Threads_ptwin32)
  213. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG
  214. THREADS_PTHREADS_WIN32_LIBRARY THREADS_PTHREADS_INCLUDE_DIR)
  215. ELSE()
  216. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
  217. ENDIF()