FindThreads: use a macro for the repetitive thread library checks

This commit is contained in:
Rolf Eike Beer 2014-10-02 22:23:20 +02:00
parent 99d34f46fa
commit 94a061d372
1 changed files with 17 additions and 23 deletions

View File

@ -45,6 +45,19 @@ if(CMAKE_SYSTEM_NAME MATCHES IRIX AND NOT CMAKE_THREAD_PREFER_PTHREAD)
CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H) CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
endif() endif()
# Internal helper macro.
# Do NOT even think about using it outside of this file!
macro(_check_threads_lib LIBNAME FUNCNAME VARNAME)
if(NOT CMAKE_HAVE_THREADS_LIBRARY)
CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME})
if(${VARNAME})
set(CMAKE_THREAD_LIBS_INIT "-l${LIBNAME}")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
endif()
endif ()
endmacro()
if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD) if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
# We have sproc # We have sproc
set(CMAKE_USE_SPROC_INIT 1) set(CMAKE_USE_SPROC_INIT 1)
@ -67,30 +80,11 @@ else()
set(Threads_FOUND TRUE) set(Threads_FOUND TRUE)
else() else()
# Do we have -lpthreads _check_threads_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE)
CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE) _check_threads_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE)
if(CMAKE_HAVE_PTHREADS_CREATE) if(CMAKE_SYSTEM_NAME MATCHES "SunOS")
set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
else()
# Ok, how about -lpthread
CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
if(CMAKE_HAVE_PTHREAD_CREATE)
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
elseif(CMAKE_SYSTEM_NAME MATCHES "SunOS")
# On sun also check for -lthread # On sun also check for -lthread
CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE) _check_threads_lib(thread thr_create CMAKE_HAVE_THR_CREATE)
if(CMAKE_HAVE_THR_CREATE)
set(CMAKE_THREAD_LIBS_INIT "-lthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
endif()
endif()
endif() endif()
endif() endif()
endif() endif()