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