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