ENH: move EstimateFormatLength to kwsys
[cmake.git] / Modules / FindThreads.cmake
blobe0bc48088761824c722af9f15b1f88c68901881d
1 # This module determines the thread library of the system.
2 # The following variables are set
3 # CMAKE_THREAD_LIBS_INIT        - the thread library to link into an application (-lpthread )
4 # CMAKE_USE_SPROC_INIT          - are we using sproc?
5 # CMAKE_USE_WIN32_THREADS_INIT  - are we using WIN32 threads
6 # CMAKE_USE_PTHREADS_INIT       - are we using pthreads
7 # CMAKE_HP_PTHREADS_INIT             - are we using hp pthreads
9 INCLUDE (CheckIncludeFile)
10 INCLUDE (CheckLibraryExists)
12 # Do we have sproc?
13 IF(CMAKE_SYSTEM MATCHES IRIX)
14   CHECK_INCLUDE_FILE("sys/prctl.h"  CMAKE_HAVE_SPROC_H)
15 ENDIF(CMAKE_SYSTEM MATCHES IRIX)
17 IF(CMAKE_HAVE_SPROC_H)
18   # We have sproc
19   SET(CMAKE_USE_SPROC_INIT 1)
20 ELSE(CMAKE_HAVE_SPROC_H)
21   # Do we have pthreads?
22   CHECK_INCLUDE_FILE("pthread.h" CMAKE_HAVE_PTHREAD_H)
23   IF(CMAKE_HAVE_PTHREAD_H)
24     # We have pthread.h
25     # Let's check for the library now.
26     SET(CMAKE_HAVE_THREADS_LIBRARY)
27     IF(NOT THREADS_HAVE_PTHREAD_ARG)
28       # Do we have -lpthreads
29       CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
30       IF(CMAKE_HAVE_PTHREADS_CREATE)
31         SET(CMAKE_THREAD_LIBS_INIT "-lpthreads")
32         SET(CMAKE_HAVE_THREADS_LIBRARY 1)
33       ENDIF(CMAKE_HAVE_PTHREADS_CREATE)
34       # Ok, how about -lpthread
35       CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
36       IF(CMAKE_HAVE_PTHREAD_CREATE)
37         SET(CMAKE_THREAD_LIBS_INIT "-lpthread")
38         SET(CMAKE_HAVE_THREADS_LIBRARY 1)
39       ENDIF(CMAKE_HAVE_PTHREAD_CREATE)
40       IF(CMAKE_SYSTEM MATCHES "SunOS.*")
41         # On sun also check for -lthread
42         CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
43         IF(CMAKE_HAVE_THR_CREATE)
44           SET(CMAKE_THREAD_LIBS_INIT "-lthread")
45           SET(CMAKE_HAVE_THREADS_LIBRARY 1)
46         ENDIF(CMAKE_HAVE_THR_CREATE)
47       ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*")
48     ENDIF(NOT THREADS_HAVE_PTHREAD_ARG)
50     IF(NOT CMAKE_HAVE_THREADS_LIBRARY)
51       # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
52       IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
53         MESSAGE(STATUS "Check if compiler accepts -pthread")
54         TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
55           ${CMAKE_BINARY_DIR}
56           ${CMAKE_ROOT}/Modules/CheckForPthreads.c
57           CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
58           OUTPUT_VARIABLE OUTPUT)
59         IF(THREADS_HAVE_PTHREAD_ARG)
60           IF(THREADS_PTHREAD_ARG MATCHES "^2$")
61             MESSAGE(STATUS "Check if compiler accepts -pthread - yes")
62           ELSE(THREADS_PTHREAD_ARG MATCHES "^2$")
63             MESSAGE(STATUS "Check if compiler accepts -pthread - no")
64             FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeError.log 
65               "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
66           ENDIF(THREADS_PTHREAD_ARG MATCHES "^2$")
67         ELSE(THREADS_HAVE_PTHREAD_ARG)
68           MESSAGE(STATUS "Check if compiler accepts -pthread - no")
69           FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeError.log 
70             "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
71         ENDIF(THREADS_HAVE_PTHREAD_ARG)
72       ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
73       IF(THREADS_HAVE_PTHREAD_ARG)
74         SET(CMAKE_THREAD_LIBS_INIT "-pthread")
75       ELSE(THREADS_HAVE_PTHREAD_ARG)
76       ENDIF(THREADS_HAVE_PTHREAD_ARG)
77     ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY)
78   ENDIF(CMAKE_HAVE_PTHREAD_H)
79 ENDIF(CMAKE_HAVE_SPROC_H)
81 IF(CMAKE_THREAD_LIBS_INIT)
82   SET(CMAKE_USE_PTHREADS_INIT 1)
83 ENDIF(CMAKE_THREAD_LIBS_INIT)
85 IF(CMAKE_SYSTEM MATCHES "Windows")
86   SET(CMAKE_USE_WIN32_THREADS_INIT 1)
87 ENDIF(CMAKE_SYSTEM MATCHES "Windows")
89 IF(CMAKE_USE_PTHREADS_INIT)
90   IF(CMAKE_SYSTEM MATCHES "HP-UX-*")
91     SET(CMAKE_THREAD_LIBS_INIT "-lcma")
92     SET(CMAKE_USE_PTHREADS_INIT 1)
93     SET(CMAKE_HP_PTHREADS_INIT 1)
94   ENDIF(CMAKE_SYSTEM MATCHES "HP-UX-*")
96   IF(CMAKE_SYSTEM MATCHES "OSF1-V*")
97     SET(CMAKE_USE_PTHREADS_INIT 0)
98     SET(CMAKE_THREAD_LIBS_INIT )
99   ENDIF(CMAKE_SYSTEM MATCHES "OSF1-V*")
101   IF(CMAKE_SYSTEM MATCHES "CYGWIN_NT*")
102     SET(CMAKE_USE_PTHREADS_INIT 1)
103     SET(CMAKE_THREAD_LIBS_INIT )
104     SET(CMAKE_USE_WIN32_THREADS_INIT 0)
105   ENDIF(CMAKE_SYSTEM MATCHES "CYGWIN_NT*")
106 ENDIF(CMAKE_USE_PTHREADS_INIT)