mod: Update gh-pages
[platform/upstream/gflags.git] / cmake / FindThreadsCXX.cmake
1 #.rst:
2 # FindThreads
3 # -----------
4 #
5 # This module determines the thread library of the system.
6 #
7 # The following variables are set
8 #
9 # ::
10 #
11 #   CMAKE_THREAD_LIBS_INIT     - the thread library
12 #   CMAKE_USE_SPROC_INIT       - are we using sproc?
13 #   CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads?
14 #   CMAKE_USE_PTHREADS_INIT    - are we using pthreads
15 #   CMAKE_HP_PTHREADS_INIT     - are we using hp pthreads
16 #
17 # For systems with multiple thread libraries, caller can set
18 #
19 # ::
20 #
21 #   CMAKE_THREAD_PREFER_PTHREAD
22
23 #=============================================================================
24 # Copyright 2002-2009 Kitware, Inc.
25 #
26 # Distributed under the OSI-approved BSD License (the "License");
27 # see accompanying file Copyright.txt for details.
28 #
29 # This software is distributed WITHOUT ANY WARRANTY; without even the
30 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31 # See the License for more information.
32 #=============================================================================
33 # (To distribute this file outside of CMake, substitute the full
34 #  License text for the above reference.)
35
36 include (CheckIncludeFileCXX)
37 include (CheckCXXLibraryExists)
38 include (CheckCXXSymbolExists)
39 set(Threads_FOUND FALSE)
40
41
42 # Do we have sproc?
43 if(CMAKE_SYSTEM MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
44   CHECK_INCLUDE_FILES_CXX("sys/types.h;sys/prctl.h"  CMAKE_HAVE_SPROC_H)
45 endif()
46
47 if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
48   # We have sproc
49   set(CMAKE_USE_SPROC_INIT 1)
50 else()
51   # Do we have pthreads?
52   CHECK_INCLUDE_FILE_CXX("pthread.h" CMAKE_HAVE_PTHREAD_H)
53   if(CMAKE_HAVE_PTHREAD_H)
54
55     #
56     # We have pthread.h
57     # Let's check for the library now.
58     #
59     set(CMAKE_HAVE_THREADS_LIBRARY)
60     if(NOT THREADS_HAVE_PTHREAD_ARG)
61       # Check if pthread functions are in normal C library
62       CHECK_CXX_SYMBOL_EXISTS(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
63       if(CMAKE_HAVE_LIBC_CREATE)
64         set(CMAKE_THREAD_LIBS_INIT "")
65         set(CMAKE_HAVE_THREADS_LIBRARY 1)
66         set(Threads_FOUND TRUE)
67       endif()
68
69       if(NOT CMAKE_HAVE_THREADS_LIBRARY)
70         # Do we have -lpthreads
71         CHECK_CXX_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
72         if(CMAKE_HAVE_PTHREADS_CREATE)
73           set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
74           set(CMAKE_HAVE_THREADS_LIBRARY 1)
75           set(Threads_FOUND TRUE)
76         endif()
77
78         # Ok, how about -lpthread
79         CHECK_CXX_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
80         if(CMAKE_HAVE_PTHREAD_CREATE)
81           set(CMAKE_THREAD_LIBS_INIT "-lpthread")
82           set(CMAKE_HAVE_THREADS_LIBRARY 1)
83           set(Threads_FOUND TRUE)
84         endif()
85
86         if(CMAKE_SYSTEM MATCHES "SunOS.*")
87           # On sun also check for -lthread
88           CHECK_CXX_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
89           if(CMAKE_HAVE_THR_CREATE)
90             set(CMAKE_THREAD_LIBS_INIT "-lthread")
91             set(CMAKE_HAVE_THREADS_LIBRARY 1)
92             set(Threads_FOUND TRUE)
93           endif()
94         endif()
95       endif()
96     endif()
97
98     if(NOT CMAKE_HAVE_THREADS_LIBRARY)
99       # If we did not found -lpthread, -lpthreads, or -lthread, look for -pthread
100       if("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
101         message(STATUS "Check if compiler accepts -pthread")
102         configure_file ("${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.cxx"
103                         "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckForPthreads.cxx" COPYONLY)
104         try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
105           ${CMAKE_BINARY_DIR}
106           ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckForPthreads.cxx
107           CMAKE_FLAGS "-DLINK_LIBRARIES:STRING=-pthread;-DCMAKE_CXX_FLAGS:STRING=-fpermissive"
108           COMPILE_OUTPUT_VARIABLE OUTPUT)
109
110         if(THREADS_HAVE_PTHREAD_ARG)
111           if(THREADS_PTHREAD_ARG STREQUAL "2")
112             set(Threads_FOUND TRUE)
113             message(STATUS "Check if compiler accepts -pthread - yes")
114           else()
115             message(STATUS "Check if compiler accepts -pthread - no")
116             file(APPEND
117               ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
118               "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
119           endif()
120         else()
121           message(STATUS "Check if compiler accepts -pthread - no")
122           file(APPEND
123             ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
124             "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
125         endif()
126
127       endif()
128
129       if(THREADS_HAVE_PTHREAD_ARG)
130         set(Threads_FOUND TRUE)
131         set(CMAKE_THREAD_LIBS_INIT "-pthread")
132       endif()
133
134     endif()
135   endif()
136 endif()
137
138 if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_CREATE)
139   set(CMAKE_USE_PTHREADS_INIT 1)
140   set(Threads_FOUND TRUE)
141 endif()
142
143 if(CMAKE_SYSTEM MATCHES "Windows")
144   set(CMAKE_USE_WIN32_THREADS_INIT 1)
145   set(Threads_FOUND TRUE)
146 endif()
147
148 if(CMAKE_USE_PTHREADS_INIT)
149   if(CMAKE_SYSTEM MATCHES "HP-UX-*")
150     # Use libcma if it exists and can be used.  It provides more
151     # symbols than the plain pthread library.  CMA threads
152     # have actually been deprecated:
153     #   http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
154     #   http://docs.hp.com/en/947/d8.html
155     # but we need to maintain compatibility here.
156     # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
157     # are available.
158     CHECK_CXX_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
159     if(CMAKE_HAVE_HP_CMA)
160       set(CMAKE_THREAD_LIBS_INIT "-lcma")
161       set(CMAKE_HP_PTHREADS_INIT 1)
162       set(Threads_FOUND TRUE)
163     endif()
164     set(CMAKE_USE_PTHREADS_INIT 1)
165   endif()
166
167   if(CMAKE_SYSTEM MATCHES "OSF1-V*")
168     set(CMAKE_USE_PTHREADS_INIT 0)
169     set(CMAKE_THREAD_LIBS_INIT )
170   endif()
171
172   if(CMAKE_SYSTEM MATCHES "CYGWIN_NT*")
173     set(CMAKE_USE_PTHREADS_INIT 1)
174     set(Threads_FOUND TRUE)
175     set(CMAKE_THREAD_LIBS_INIT )
176     set(CMAKE_USE_WIN32_THREADS_INIT 0)
177   endif()
178 endif()
179
180 include(FindPackageHandleStandardArgs)
181 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)