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
11 #=============================================================================
12 # Copyright 2002-2009 Kitware, Inc.
14 # Distributed under the OSI-approved BSD License (the "License");
15 # see accompanying file Copyright.txt for details.
17 # This software is distributed WITHOUT ANY WARRANTY; without even the
18 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 # See the License for more information.
20 #=============================================================================
21 # (To distribute this file outside of CMake, substitute the full
22 # License text for the above reference.)
24 include (CheckIncludeFiles)
25 include (CheckLibraryExists)
26 include (CheckSymbolExists)
27 set(Threads_FOUND FALSE)
30 if(CMAKE_SYSTEM MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
31 CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
34 if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
36 set(CMAKE_USE_SPROC_INIT 1)
38 # Do we have pthreads?
39 CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
40 if(CMAKE_HAVE_PTHREAD_H)
44 # Let's check for the library now.
46 set(CMAKE_HAVE_THREADS_LIBRARY)
47 if(NOT THREADS_HAVE_PTHREAD_ARG)
48 # Check if pthread functions are in normal C library
49 CHECK_SYMBOL_EXISTS(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
50 if(CMAKE_HAVE_LIBC_CREATE)
51 set(CMAKE_THREAD_LIBS_INIT "")
52 set(CMAKE_HAVE_THREADS_LIBRARY 1)
53 set(Threads_FOUND TRUE)
56 if(NOT CMAKE_HAVE_THREADS_LIBRARY)
57 # Do we have -lpthreads
58 CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
59 if(CMAKE_HAVE_PTHREADS_CREATE)
60 set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
61 set(CMAKE_HAVE_THREADS_LIBRARY 1)
62 set(Threads_FOUND TRUE)
65 # Ok, how about -lpthread
66 CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
67 if(CMAKE_HAVE_PTHREAD_CREATE)
68 set(CMAKE_THREAD_LIBS_INIT "-lpthread")
69 set(CMAKE_HAVE_THREADS_LIBRARY 1)
70 set(Threads_FOUND TRUE)
73 if(CMAKE_SYSTEM MATCHES "SunOS.*")
74 # On sun also check for -lthread
75 CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
76 if(CMAKE_HAVE_THR_CREATE)
77 set(CMAKE_THREAD_LIBS_INIT "-lthread")
78 set(CMAKE_HAVE_THREADS_LIBRARY 1)
79 set(Threads_FOUND TRUE)
85 if(NOT CMAKE_HAVE_THREADS_LIBRARY)
86 # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
87 if("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
88 message(STATUS "Check if compiler accepts -pthread")
89 try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
91 ${CMAKE_ROOT}/Modules/CheckForPthreads.c
92 CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
93 COMPILE_OUTPUT_VARIABLE OUTPUT)
95 if(THREADS_HAVE_PTHREAD_ARG)
96 if(THREADS_PTHREAD_ARG MATCHES "^2$")
97 set(Threads_FOUND TRUE)
98 message(STATUS "Check if compiler accepts -pthread - yes")
100 message(STATUS "Check if compiler accepts -pthread - no")
102 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
103 "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
106 message(STATUS "Check if compiler accepts -pthread - no")
108 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
109 "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
114 if(THREADS_HAVE_PTHREAD_ARG)
115 set(Threads_FOUND TRUE)
116 set(CMAKE_THREAD_LIBS_INIT "-pthread")
123 if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_CREATE)
124 set(CMAKE_USE_PTHREADS_INIT 1)
125 set(Threads_FOUND TRUE)
128 if(CMAKE_SYSTEM MATCHES "Windows")
129 set(CMAKE_USE_WIN32_THREADS_INIT 1)
130 set(Threads_FOUND TRUE)
133 if(CMAKE_USE_PTHREADS_INIT)
134 if(CMAKE_SYSTEM MATCHES "HP-UX-*")
135 # Use libcma if it exists and can be used. It provides more
136 # symbols than the plain pthread library. CMA threads
137 # have actually been deprecated:
138 # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
139 # http://docs.hp.com/en/947/d8.html
140 # but we need to maintain compatibility here.
141 # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
143 CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
144 if(CMAKE_HAVE_HP_CMA)
145 set(CMAKE_THREAD_LIBS_INIT "-lcma")
146 set(CMAKE_HP_PTHREADS_INIT 1)
147 set(Threads_FOUND TRUE)
149 set(CMAKE_USE_PTHREADS_INIT 1)
152 if(CMAKE_SYSTEM MATCHES "OSF1-V*")
153 set(CMAKE_USE_PTHREADS_INIT 0)
154 set(CMAKE_THREAD_LIBS_INIT )
157 if(CMAKE_SYSTEM MATCHES "CYGWIN_NT*")
158 set(CMAKE_USE_PTHREADS_INIT 1)
159 set(Threads_FOUND TRUE)
160 set(CMAKE_THREAD_LIBS_INIT )
161 set(CMAKE_USE_WIN32_THREADS_INIT 0)
165 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
166 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)