From c9bd462c0887bbb7f9660b81ee2874c94f1e409b Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 14 Apr 2022 12:59:27 -0400 Subject: FindThreads: Drop unused internal CMAKE_HAVE_THREADS_LIBRARY variable This internal variable has not been used since commit 46368eddfd (FindThreads: move checking of the -pthread compiler flag into a macro, 2014-10-06, v3.1.0-rc1~21^2~2). It has never been documented for public use. --- Modules/FindThreads.cmake | 3 --- 1 file changed, 3 deletions(-) diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index e4d6cf3..15f29f7 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -96,7 +96,6 @@ macro(_check_threads_lib LIBNAME FUNCNAME VARNAME) 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 () @@ -153,7 +152,6 @@ if(CMAKE_HAVE_PTHREAD_H) # We have pthread.h # Let's check for the library now. # - set(CMAKE_HAVE_THREADS_LIBRARY) if(NOT THREADS_HAVE_PTHREAD_ARG) # Check if pthread functions are in normal C library. # We list some pthread functions in PTHREAD_C_CXX_TEST_SOURCE test code. @@ -166,7 +164,6 @@ if(CMAKE_HAVE_PTHREAD_H) endif() if(CMAKE_HAVE_LIBC_PTHREAD) set(CMAKE_THREAD_LIBS_INIT "") - set(CMAKE_HAVE_THREADS_LIBRARY 1) set(Threads_FOUND TRUE) else() # Check for -pthread first if enabled. This is the recommended -- cgit v0.12 From 5b5cd92363877e790d09f38d6364f42345e7f327 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 14 Apr 2022 14:45:11 -0400 Subject: FindThreads: Drop strange SunOS+pthread.h+thr_create combination Solaris has provided pthreads for a long time, so we probably never get to this combination anyway. --- Modules/FindThreads.cmake | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index 15f29f7..30f8580 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -178,10 +178,6 @@ if(CMAKE_HAVE_PTHREAD_H) endif() _check_threads_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) _check_threads_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) - if(CMAKE_SYSTEM_NAME MATCHES "SunOS") - # On sun also check for -lthread - _check_threads_lib(thread thr_create CMAKE_HAVE_THR_CREATE) - endif() endif() endif() -- cgit v0.12 From 1ee8c545ddb270c282f896157616e91c41c80880 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 14 Apr 2022 13:16:05 -0400 Subject: FindThreads: Clarify internal helper macro names --- Modules/FindThreads.cmake | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index 30f8580..13c8c5c 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -91,7 +91,7 @@ int main(void) # Internal helper macro. # Do NOT even think about using it outside of this file! -macro(_check_threads_lib LIBNAME FUNCNAME VARNAME) +macro(_threads_check_lib LIBNAME FUNCNAME VARNAME) if(NOT Threads_FOUND) CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME}) if(${VARNAME}) @@ -103,7 +103,7 @@ endmacro() # Internal helper macro. # Do NOT even think about using it outside of this file! -macro(_check_pthreads_flag) +macro(_threads_check_flag_pthread) if(NOT Threads_FOUND) # If we did not find -lpthreads, -lpthread, or -lthread, look for -pthread if(NOT DEFINED THREADS_HAVE_PTHREAD_ARG) @@ -170,18 +170,18 @@ if(CMAKE_HAVE_PTHREAD_H) # way, but not backwards compatible as one must also pass -pthread # as compiler flag then. if (THREADS_PREFER_PTHREAD_FLAG) - _check_pthreads_flag() + _threads_check_flag_pthread() endif () if(CMAKE_SYSTEM MATCHES "GHS-MULTI") - _check_threads_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE) + _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE) endif() - _check_threads_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) - _check_threads_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) + _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) + _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) endif() endif() - _check_pthreads_flag() + _threads_check_flag_pthread() endif() if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_PTHREAD) -- cgit v0.12 From cbe7550d998def869d297b2bf6e3d9b8689e33de Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 14 Apr 2022 13:04:43 -0400 Subject: FindThreads: Remove unnecessary condition The `THREADS_HAVE_PTHREAD_ARG` cache entry cannot be defined unless FindThreads has already been executed, perhaps by a previous run of CMake, or a previous `find_package(Threads)` call. In that case, the other alternatives will also already have been checked and results cached. --- Modules/FindThreads.cmake | 51 +++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index 13c8c5c..ed0bb8b 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -152,33 +152,32 @@ if(CMAKE_HAVE_PTHREAD_H) # We have pthread.h # Let's check for the library now. # - if(NOT THREADS_HAVE_PTHREAD_ARG) - # Check if pthread functions are in normal C library. - # We list some pthread functions in PTHREAD_C_CXX_TEST_SOURCE test code. - # If the pthread functions already exist in C library, we could just use - # them instead of linking to the additional pthread library. - if(CMAKE_C_COMPILER_LOADED) - CHECK_C_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) - elseif(CMAKE_CXX_COMPILER_LOADED) - CHECK_CXX_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) - endif() - if(CMAKE_HAVE_LIBC_PTHREAD) - set(CMAKE_THREAD_LIBS_INIT "") - set(Threads_FOUND TRUE) - else() - # Check for -pthread first if enabled. This is the recommended - # way, but not backwards compatible as one must also pass -pthread - # as compiler flag then. - if (THREADS_PREFER_PTHREAD_FLAG) - _threads_check_flag_pthread() - endif () - - if(CMAKE_SYSTEM MATCHES "GHS-MULTI") - _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE) - endif() - _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) - _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) + + # Check if pthread functions are in normal C library. + # We list some pthread functions in PTHREAD_C_CXX_TEST_SOURCE test code. + # If the pthread functions already exist in C library, we could just use + # them instead of linking to the additional pthread library. + if(CMAKE_C_COMPILER_LOADED) + CHECK_C_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) + elseif(CMAKE_CXX_COMPILER_LOADED) + CHECK_CXX_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) + endif() + if(CMAKE_HAVE_LIBC_PTHREAD) + set(CMAKE_THREAD_LIBS_INIT "") + set(Threads_FOUND TRUE) + else() + # Check for -pthread first if enabled. This is the recommended + # way, but not backwards compatible as one must also pass -pthread + # as compiler flag then. + if (THREADS_PREFER_PTHREAD_FLAG) + _threads_check_flag_pthread() + endif () + + if(CMAKE_SYSTEM MATCHES "GHS-MULTI") + _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE) endif() + _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) + _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) endif() _threads_check_flag_pthread() -- cgit v0.12 From 41ef904e81a6ef6758b3b9ccf29b69c784cf67b7 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 14 Apr 2022 13:10:30 -0400 Subject: FindThreads: Simplify conditions for not-in-libc checks If `CMAKE_HAVE_LIBC_PTHREAD` is true, we also set `Threads_FOUND` to true, which blocks all the other checks from running. --- Modules/FindThreads.cmake | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index ed0bb8b..52a8a5f 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -165,21 +165,21 @@ if(CMAKE_HAVE_PTHREAD_H) if(CMAKE_HAVE_LIBC_PTHREAD) set(CMAKE_THREAD_LIBS_INIT "") set(Threads_FOUND TRUE) - else() - # Check for -pthread first if enabled. This is the recommended - # way, but not backwards compatible as one must also pass -pthread - # as compiler flag then. - if (THREADS_PREFER_PTHREAD_FLAG) - _threads_check_flag_pthread() - endif () - - if(CMAKE_SYSTEM MATCHES "GHS-MULTI") - _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE) - endif() - _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) - _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) endif() + # Check for -pthread first if enabled. This is the recommended + # way, but not backwards compatible as one must also pass -pthread + # as compiler flag then. + if (THREADS_PREFER_PTHREAD_FLAG) + _threads_check_flag_pthread() + endif () + + if(CMAKE_SYSTEM MATCHES "GHS-MULTI") + _threads_check_lib(posix pthread_create CMAKE_HAVE_PTHREADS_CREATE) + endif() + _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) + _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) + _threads_check_flag_pthread() endif() -- cgit v0.12 From 564385194b53b656fc9d010329575ca957569401 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 14 Apr 2022 13:13:04 -0400 Subject: FindThreads: Avoid repeating check for -pthread flag When `THREADS_PREFER_PTHREAD_FLAG` is enabled, we check for it before the thread libraries. We do not need to check after them too. --- Modules/FindThreads.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index 52a8a5f..fb99094 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -180,7 +180,9 @@ if(CMAKE_HAVE_PTHREAD_H) _threads_check_lib(pthreads pthread_create CMAKE_HAVE_PTHREADS_CREATE) _threads_check_lib(pthread pthread_create CMAKE_HAVE_PTHREAD_CREATE) - _threads_check_flag_pthread() + if (NOT THREADS_PREFER_PTHREAD_FLAG) + _threads_check_flag_pthread() + endif() endif() if(CMAKE_THREAD_LIBS_INIT OR CMAKE_HAVE_LIBC_PTHREAD) -- cgit v0.12 From c53748a3772ab7419e82e27fd49bf30ca7821383 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 14 Apr 2022 13:24:16 -0400 Subject: FindThreads: Factor out helper macro for libc check --- Modules/FindThreads.cmake | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index fb99094..c1531a4 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -91,6 +91,22 @@ int main(void) # Internal helper macro. # Do NOT even think about using it outside of this file! +macro(_threads_check_libc) + if(NOT Threads_FOUND) + if(CMAKE_C_COMPILER_LOADED) + CHECK_C_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) + elseif(CMAKE_CXX_COMPILER_LOADED) + CHECK_CXX_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) + endif() + if(CMAKE_HAVE_LIBC_PTHREAD) + set(CMAKE_THREAD_LIBS_INIT "") + set(Threads_FOUND TRUE) + endif() + endif () +endmacro() + +# Internal helper macro. +# Do NOT even think about using it outside of this file! macro(_threads_check_lib LIBNAME FUNCNAME VARNAME) if(NOT Threads_FOUND) CHECK_LIBRARY_EXISTS(${LIBNAME} ${FUNCNAME} "" ${VARNAME}) @@ -157,15 +173,7 @@ if(CMAKE_HAVE_PTHREAD_H) # We list some pthread functions in PTHREAD_C_CXX_TEST_SOURCE test code. # If the pthread functions already exist in C library, we could just use # them instead of linking to the additional pthread library. - if(CMAKE_C_COMPILER_LOADED) - CHECK_C_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) - elseif(CMAKE_CXX_COMPILER_LOADED) - CHECK_CXX_SOURCE_COMPILES("${PTHREAD_C_CXX_TEST_SOURCE}" CMAKE_HAVE_LIBC_PTHREAD) - endif() - if(CMAKE_HAVE_LIBC_PTHREAD) - set(CMAKE_THREAD_LIBS_INIT "") - set(Threads_FOUND TRUE) - endif() + _threads_check_libc() # Check for -pthread first if enabled. This is the recommended # way, but not backwards compatible as one must also pass -pthread -- cgit v0.12