Acurite minor bugfixes
[rtl-433.git] / cmake / Modules / FindThreads.cmake
1 # Updated FindThreads.cmake that supports pthread-win32
2 # Downloaded from http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=6399
3
4 # - This module determines the thread library of the system.
5 #
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
12 #
13 # If use of pthreads-win32 is desired, the following variables
14 # can be set.
15 #
16 #  THREADS_USE_PTHREADS_WIN32 -
17 #    Setting this to true searches for the pthreads-win32
18 #    port (since CMake 2.8.0)
19 #
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.)
29 #
30 #======================================================
31 # Example usage where threading library
32 # is provided by the system:
33 #
34 #   find_package(Threads REQUIRED)
35 #   add_executable(foo foo.cc)
36 #   target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT})
37 #
38 # Example usage if pthreads-win32 is desired on Windows
39 # or a system provided thread library:
40 #
41 #   set(THREADS_USE_PTHREADS_WIN32 true)
42 #   find_package(Threads REQUIRED)
43 #   include_directories(${THREADS_PTHREADS_INCLUDE_DIR})
44 #
45 #   add_executable(foo foo.cc)
46 #   target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT})
47 #
48
49 INCLUDE (CheckIncludeFiles)
50 INCLUDE (CheckLibraryExists)
51 SET(Threads_FOUND FALSE)
52
53 IF(WIN32 AND NOT CYGWIN AND THREADS_USE_PTHREADS_WIN32)
54   SET(_Threads_ptwin32 true)
55 ENDIF()
56
57 # Do we have sproc?
58 IF(CMAKE_SYSTEM MATCHES IRIX)
59   CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h"  CMAKE_HAVE_SPROC_H)
60 ENDIF()
61
62 IF(CMAKE_HAVE_SPROC_H)
63   # We have sproc
64   SET(CMAKE_USE_SPROC_INIT 1)
65
66 ELSEIF(_Threads_ptwin32)
67
68   IF(NOT DEFINED THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME)
69     # Assign the default scheme
70     SET(THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME "C")
71   ELSE()
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")
77     ENDIF()
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")
81   ENDIF()
82
83   FIND_PATH(THREADS_PTHREADS_INCLUDE_DIR pthread.h)
84
85   # Determine the library filename
86   IF(MSVC)
87     SET(_Threads_pthreads_libname
88         pthreadV${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2)
89   ELSEIF(MINGW)
90     SET(_Threads_pthreads_libname
91         pthreadG${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2)
92   ELSE()
93     MESSAGE(FATAL_ERROR "This should never happen")
94   ENDIF()
95
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)
102   ENDIF()
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"
107                NO_SYSTEM_PATH
108                )
109
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)
115   ENDIF()
116
117   MARK_AS_ADVANCED(THREADS_PTHREADS_WIN32_LIBRARY)
118
119 ELSE()
120   # Do we have pthreads?
121   CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
122   IF(CMAKE_HAVE_PTHREAD_H)
123
124     #
125     # We have pthread.h
126     # Let's check for the library now.
127     #
128     SET(CMAKE_HAVE_THREADS_LIBRARY)
129     IF(NOT THREADS_HAVE_PTHREAD_ARG)
130
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)
137       ENDIF()
138
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)
145       ENDIF()
146
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)
154         ENDIF()
155       ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*")
156
157     ENDIF(NOT THREADS_HAVE_PTHREAD_ARG)
158
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
164           ${CMAKE_BINARY_DIR}
165           ${CMAKE_ROOT}/Modules/CheckForPthreads.c
166           CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
167           COMPILE_OUTPUT_VARIABLE OUTPUT)
168
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")
173           ELSE()
174             MESSAGE(STATUS "Check if compiler accepts -pthread - no")
175             FILE(APPEND
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")
178           ENDIF()
179         ELSE()
180           MESSAGE(STATUS "Check if compiler accepts -pthread - no")
181           FILE(APPEND
182             ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
183             "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
184         ENDIF()
185
186       ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
187
188       IF(THREADS_HAVE_PTHREAD_ARG)
189         SET(Threads_FOUND TRUE)
190         SET(CMAKE_THREAD_LIBS_INIT "-pthread")
191       ENDIF()
192
193     ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY)
194   ENDIF(CMAKE_HAVE_PTHREAD_H)
195 ENDIF()
196
197 IF(CMAKE_THREAD_LIBS_INIT)
198   SET(CMAKE_USE_PTHREADS_INIT 1)
199   SET(Threads_FOUND TRUE)
200 ENDIF()
201
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)
206 ENDIF()
207
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
217     # are available.
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)
225   ENDIF()
226
227   IF(CMAKE_SYSTEM MATCHES "OSF1-V*")
228     SET(CMAKE_USE_PTHREADS_INIT 0)
229     SET(CMAKE_THREAD_LIBS_INIT )
230   ENDIF()
231
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)
237   ENDIF()
238 ENDIF(CMAKE_USE_PTHREADS_INIT)
239
240 INCLUDE(FindPackageHandleStandardArgs)
241 IF(_Threads_ptwin32)
242   FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG
243     THREADS_PTHREADS_WIN32_LIBRARY THREADS_PTHREADS_INCLUDE_DIR)
244 ELSE()
245   FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
246 ENDIF()