a675fd6755184fa5516ef544063de52503000124
[platform/upstream/cmake.git] / Modules / FindThreads.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindThreads
6 -----------
7
8 This module determines the thread library of the system.
9
10 Imported Targets
11 ^^^^^^^^^^^^^^^^
12
13 .. versionadded:: 3.1
14
15 This module defines the following :prop_tgt:`IMPORTED` target:
16
17 ``Threads::Threads``
18   The thread library, if found.
19
20 Result Variables
21 ^^^^^^^^^^^^^^^^
22
23 The following variables are set:
24
25 ``Threads_FOUND``
26   If a supported thread library was found.
27 ``CMAKE_THREAD_LIBS_INIT``
28   The thread library to use. This may be empty if the thread functions
29   are provided by the system libraries and no special flags are needed
30   to use them.
31 ``CMAKE_USE_WIN32_THREADS_INIT``
32   If the found thread library is the win32 one.
33 ``CMAKE_USE_PTHREADS_INIT``
34   If the found thread library is pthread compatible.
35 ``CMAKE_HP_PTHREADS_INIT``
36   If the found thread library is the HP thread library.
37
38 Variables Affecting Behavior
39 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40
41 .. variable:: THREADS_PREFER_PTHREAD_FLAG
42
43   .. versionadded:: 3.1
44
45   If the use of the -pthread compiler and linker flag is preferred then
46   the caller can set this variable to TRUE. The compiler flag can only be
47   used with the imported target. Use of both the imported target as well
48   as this switch is highly recommended for new code.
49
50   This variable has no effect if the system libraries provide the
51   thread functions, i.e. when ``CMAKE_THREAD_LIBS_INIT`` will be empty.
52 #]=======================================================================]
53
54 include (CheckLibraryExists)
55 set(Threads_FOUND FALSE)
56 set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
57 set(CMAKE_REQUIRED_QUIET ${Threads_FIND_QUIETLY})
58
59 if(CMAKE_C_COMPILER_LOADED)
60   include (CheckIncludeFile)
61   include (CheckCSourceCompiles)
62 elseif(CMAKE_CXX_COMPILER_LOADED)
63   include (CheckIncludeFileCXX)
64   include (CheckCXXSourceCompiles)
65 else()
66   message(FATAL_ERROR "FindThreads only works if either C or CXX language is enabled")
67 endif()
68
69 # simple pthread test code
70 set(PTHREAD_C_CXX_TEST_SOURCE [====[
71 #include <pthread.h>
72
73 static void* test_func(void* data)
74 {
75   return data;
76 }
77
78 int main(void)
79 {
80   pthread_t thread;
81   pthread_create(&thread, NULL, test_func, NULL);
82   pthread_detach(thread);
83   pthread_cancel(thread);
84   pthread_join(thread, NULL);
85   pthread_atfork(NULL, NULL, NULL);
86   pthread_exit(NULL);
87
88   return 0;
89 }
90 ]====])
91
92 # Internal helper macro.
93 # Do NOT even think about using it outside of this file!
94 macro(_threads_check_libc)
95   if(NOT Threads_FOUND)
96     if(CMAKE_C_COMPILER_LOADED)
97       CHECK_C_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD)
98     elseif(CMAKE_CXX_COMPILER_LOADED)
99       CHECK_CXX_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD)
100     endif()
101     if(CMAKE_HAVE_LIBC_PTHREAD)
102       set(CMAKE_THREAD_LIBS_INIT "")
103       set(Threads_FOUND TRUE)
104     endif()
105   endif ()
106 endmacro()
107
108 # Internal helper macro.
109 # Do NOT even think about using it outside of this file!
110 macro(_threads_check_lib LIBNAME FUNCNAME VARNAME)
111   if(NOT Threads_FOUND)
112      CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME})
113      if(${VARNAME})
114        set(CMAKE_THREAD_LIBS_INIT "-l${LIBNAME}")
115        set(Threads_FOUND TRUE)
116      endif()
117   endif ()
118 endmacro()
119
120 # Internal helper macro.
121 # Do NOT even think about using it outside of this file!
122 macro(_threads_check_flag_pthread)
123   if(NOT Threads_FOUND)
124     # If we did not find -lpthreads, -lpthread, or -lthread, look for -pthread
125     # except on compilers known to not have it.
126     if(MSVC)
127       # Compilers targeting the MSVC ABI do not have a -pthread flag.
128       set(THREADS_HAVE_PTHREAD_ARG FALSE)
129     elseif(NOT DEFINED THREADS_HAVE_PTHREAD_ARG)
130       message(CHECK_START "Check if compiler accepts -pthread")
131       if(CMAKE_C_COMPILER_LOADED)
132         set(_threads_src ${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c)
133       elseif(CMAKE_CXX_COMPILER_LOADED)
134         set(_threads_src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindThreads/CheckForPthreads.cxx)
135         configure_file(${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c "${_threads_src}" COPYONLY)
136       endif()
137       try_compile(THREADS_HAVE_PTHREAD_ARG
138         ${CMAKE_BINARY_DIR}
139         ${_threads_src}
140         CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
141         OUTPUT_VARIABLE _cmake_check_pthreads_output)
142
143       string(APPEND _cmake_find_threads_output "${_cmake_check_pthreads_output}")
144       unset(_cmake_check_pthreads_output)
145       unset(_threads_src)
146
147       if(THREADS_HAVE_PTHREAD_ARG)
148         set(Threads_FOUND TRUE)
149         message(CHECK_PASS "yes")
150       else()
151         message(CHECK_FAIL "no")
152       endif()
153
154     endif()
155
156     if(THREADS_HAVE_PTHREAD_ARG)
157       set(Threads_FOUND TRUE)
158       set(CMAKE_THREAD_LIBS_INIT "-pthread")
159     endif()
160   endif()
161 endmacro()
162
163 # Check if pthread functions are in normal C library.
164 # We list some pthread functions in PTHREAD_C_CXX_TEST_SOURCE test code.
165 # If the pthread functions already exist in C library, we could just use
166 # them instead of linking to the additional pthread library.
167 _threads_check_libc()
168
169 # Check for -pthread first if enabled. This is the recommended
170 # way, but not backwards compatible as one must also pass -pthread
171 # as compiler flag then.
172 if (THREADS_PREFER_PTHREAD_FLAG)
173   _threads_check_flag_pthread()
174 endif ()
175
176 if(CMAKE_SYSTEM MATCHES "GHS-MULTI")
177   _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE)
178 endif()
179 _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE)
180 _threads_check_lib(pthread  pthread_create CMAKE_HAVE_PTHREAD_CREATE)
181
182 if (NOT THREADS_PREFER_PTHREAD_FLAG)
183   _threads_check_flag_pthread()
184 endif()
185
186 if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_PTHREAD)
187   set(CMAKE_USE_PTHREADS_INIT 1)
188   set(Threads_FOUND TRUE)
189 endif()
190
191 if(CMAKE_SYSTEM_NAME MATCHES "Windows")
192   set(CMAKE_USE_WIN32_THREADS_INIT 1)
193   set(Threads_FOUND TRUE)
194 endif()
195
196 if(CMAKE_USE_PTHREADS_INIT)
197   if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
198     # Use libcma if it exists and can be used.  It provides more
199     # symbols than the plain pthread library.  CMA threads
200     # have actually been deprecated:
201     #   http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
202     #   http://docs.hp.com/en/947/d8.html
203     # but we need to maintain compatibility here.
204     # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
205     # are available.
206     CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
207     if(CMAKE_HAVE_HP_CMA)
208       set(CMAKE_THREAD_LIBS_INIT "-lcma")
209       set(CMAKE_HP_PTHREADS_INIT 1)
210       set(Threads_FOUND TRUE)
211     endif()
212     set(CMAKE_USE_PTHREADS_INIT 1)
213   endif()
214
215   if(CMAKE_SYSTEM MATCHES "OSF1-V")
216     set(CMAKE_USE_PTHREADS_INIT 0)
217     set(CMAKE_THREAD_LIBS_INIT )
218   endif()
219
220   if(CMAKE_SYSTEM MATCHES "CYGWIN_NT" OR CMAKE_SYSTEM MATCHES "MSYS_NT")
221     set(CMAKE_USE_PTHREADS_INIT 1)
222     set(Threads_FOUND TRUE)
223     set(CMAKE_THREAD_LIBS_INIT )
224     set(CMAKE_USE_WIN32_THREADS_INIT 0)
225   endif()
226 endif()
227
228 set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
229 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
230 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
231
232 if(THREADS_FOUND AND NOT TARGET Threads::Threads)
233   add_library(Threads::Threads INTERFACE IMPORTED)
234
235   if(THREADS_HAVE_PTHREAD_ARG)
236     set_property(TARGET Threads::Threads
237                  PROPERTY INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:SHELL:-Xcompiler -pthread>"
238                                                     "$<$<NOT:$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>>:-pthread>")
239   endif()
240
241   if(CMAKE_THREAD_LIBS_INIT)
242     set_property(TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
243   endif()
244 elseif(NOT THREADS_FOUND AND _cmake_find_threads_output)
245   file(APPEND
246     ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
247     "Determining if compiler accepts -pthread failed with the following output:\n${_cmake_find_threads_output}\n\n")
248 endif()
249
250 unset(_cmake_find_threads_output)