1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 This module determines the thread library of the system.
15 This module defines the following :prop_tgt:`IMPORTED` target:
18 The thread library, if found.
23 The following variables are set:
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
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.
38 Variables Affecting Behavior
39 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41 .. variable:: THREADS_PREFER_PTHREAD_FLAG
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.
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 #]=======================================================================]
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})
59 if(CMAKE_C_COMPILER_LOADED)
60 include (CheckIncludeFile)
61 include (CheckCSourceCompiles)
62 elseif(CMAKE_CXX_COMPILER_LOADED)
63 include (CheckIncludeFileCXX)
64 include (CheckCXXSourceCompiles)
66 message(FATAL_ERROR "FindThreads only works if either C or CXX language is enabled")
69 # simple pthread test code
70 set(PTHREAD_C_CXX_TEST_SOURCE [====[
73 static void* test_func(void* data)
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);
92 # Internal helper macro.
93 # Do NOT even think about using it outside of this file!
94 macro(_threads_check_libc)
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)
101 if(CMAKE_HAVE_LIBC_PTHREAD)
102 set(CMAKE_THREAD_LIBS_INIT "")
103 set(Threads_FOUND TRUE)
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})
114 set(CMAKE_THREAD_LIBS_INIT "-l${LIBNAME}")
115 set(Threads_FOUND TRUE)
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.
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 CheckForPthreads.c)
133 elseif(CMAKE_CXX_COMPILER_LOADED)
134 set(_threads_src CheckForPthreads.cxx)
136 try_compile(THREADS_HAVE_PTHREAD_ARG
137 SOURCE_FROM_FILE "${_threads_src}" "${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c"
138 CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
143 if(THREADS_HAVE_PTHREAD_ARG)
144 set(Threads_FOUND TRUE)
145 message(CHECK_PASS "yes")
147 message(CHECK_FAIL "no")
152 if(THREADS_HAVE_PTHREAD_ARG)
153 set(Threads_FOUND TRUE)
154 set(CMAKE_THREAD_LIBS_INIT "-pthread")
159 # Check if pthread functions are in normal C library.
160 # We list some pthread functions in PTHREAD_C_CXX_TEST_SOURCE test code.
161 # If the pthread functions already exist in C library, we could just use
162 # them instead of linking to the additional pthread library.
163 _threads_check_libc()
165 # Check for -pthread first if enabled. This is the recommended
166 # way, but not backwards compatible as one must also pass -pthread
167 # as compiler flag then.
168 if (THREADS_PREFER_PTHREAD_FLAG)
169 _threads_check_flag_pthread()
172 if(CMAKE_SYSTEM MATCHES "GHS-MULTI")
173 _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE)
175 _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE)
176 _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE)
178 if (NOT THREADS_PREFER_PTHREAD_FLAG)
179 _threads_check_flag_pthread()
182 if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_PTHREAD)
183 set(CMAKE_USE_PTHREADS_INIT 1)
184 set(Threads_FOUND TRUE)
187 if(CMAKE_SYSTEM_NAME MATCHES "Windows")
188 set(CMAKE_USE_WIN32_THREADS_INIT 1)
189 set(Threads_FOUND TRUE)
192 if(CMAKE_USE_PTHREADS_INIT)
193 if(CMAKE_SYSTEM_NAME MATCHES "HP-UX")
194 # Use libcma if it exists and can be used. It provides more
195 # symbols than the plain pthread library. CMA threads
196 # have actually been deprecated:
197 # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
198 # http://docs.hp.com/en/947/d8.html
199 # but we need to maintain compatibility here.
200 # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
202 check_library_exists(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
203 if(CMAKE_HAVE_HP_CMA)
204 set(CMAKE_THREAD_LIBS_INIT "-lcma")
205 set(CMAKE_HP_PTHREADS_INIT 1)
206 set(Threads_FOUND TRUE)
208 set(CMAKE_USE_PTHREADS_INIT 1)
211 if(CMAKE_SYSTEM MATCHES "OSF1-V")
212 set(CMAKE_USE_PTHREADS_INIT 0)
213 set(CMAKE_THREAD_LIBS_INIT )
216 if(CMAKE_SYSTEM MATCHES "CYGWIN_NT" OR CMAKE_SYSTEM MATCHES "MSYS_NT")
217 set(CMAKE_USE_PTHREADS_INIT 1)
218 set(Threads_FOUND TRUE)
219 set(CMAKE_THREAD_LIBS_INIT )
220 set(CMAKE_USE_WIN32_THREADS_INIT 0)
224 set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
225 include(FindPackageHandleStandardArgs)
226 find_package_handle_standard_args(Threads DEFAULT_MSG Threads_FOUND)
228 if(THREADS_FOUND AND NOT TARGET Threads::Threads)
229 add_library(Threads::Threads INTERFACE IMPORTED)
231 if(THREADS_HAVE_PTHREAD_ARG)
232 set_property(TARGET Threads::Threads
233 PROPERTY INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:SHELL:-Xcompiler -pthread>"
234 "$<$<AND:$<NOT:$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>>,$<NOT:$<COMPILE_LANGUAGE:Swift>>>:-pthread>")
237 if(CMAKE_THREAD_LIBS_INIT)
238 set_property(TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")