cmake: update threads

For consistency with other backend/provider selection, allow
`USE_THREADS` to select the threads provider.
This commit is contained in:
Edward Thomson
2024-12-31 11:40:44 +00:00
parent 890d70856c
commit 2b581ef517
6 changed files with 54 additions and 20 deletions

View File

@@ -25,10 +25,8 @@ option(BUILD_CLI "Build the command-line interface"
option(BUILD_EXAMPLES "Build library usage example apps" OFF)
option(BUILD_FUZZERS "Build the fuzz targets" OFF)
# Suggested functionality that may not be available on a per-platform basis
option(USE_THREADS "Use threads for parallel processing when possible" ON)
# Feature enablement and backend selection
set(USE_THREADS "" CACHE STRING "Use threads for parallel processing when possible. One of ON, OFF, or a specific provider: pthreads or win32. (Defaults to ON.)")
set(USE_SSH "" CACHE STRING "Enables SSH support and optionally selects provider. One of ON, OFF, or a specific provider: libssh2 or exec. (Defaults to OFF.)")
set(USE_HTTPS "" CACHE STRING "Enable HTTPS support and optionally selects the provider. One of ON, OFF, or a specific provider: OpenSSL, OpenSSL-FIPS, OpenSSL-Dynamic, mbedTLS, SecureTransport, Schannel, or WinHTTP. (Defaults to ON.)")
set(USE_SHA1 "" CACHE STRING "Selects SHA1 provider. One of builtin, HTTPS, or a specific provider. (Defaults to builtin.)")

41
cmake/SelectThreads.cmake Normal file
View File

@@ -0,0 +1,41 @@
include(SanitizeBool)
sanitizebool(USE_THREADS)
if(NOT WIN32)
find_package(Threads)
endif()
if((USE_THREADS STREQUAL ON OR USE_THREADS STREQUAL "") AND THREADS_FOUND)
set(USE_THREADS "pthreads")
elseif((USE_THREADS STREQUAL ON OR USE_THREADS STREQUAL "") AND WIN32)
set(USE_THREADS "win32")
elseif(USE_THREADS STREQUAL "")
set(USE_THREADS OFF)
endif()
if(USE_THREADS STREQUAL "pthreads")
if(NOT THREADS_FOUND)
message(FATAL_ERROR "pthreads were requested but not found")
endif()
list(APPEND LIBGIT2_SYSTEM_LIBS ${CMAKE_THREAD_LIBS_INIT})
list(APPEND LIBGIT2_PC_LIBS ${CMAKE_THREAD_LIBS_INIT})
set(GIT_THREADS 1)
set(GIT_THREADS_PTHREADS 1)
add_feature_info("Threads" ON "using pthreads")
elseif(USE_THREADS STREQUAL "win32")
if(NOT WIN32)
message(FATAL_ERROR "Win32 API support is not available on this platform")
endif()
set(GIT_THREADS 1)
set(GIT_THREADS_WIN32 1)
add_feature_info("Threads" ON "using Win32 APIs")
elseif(USE_THREADS STREQUAL OFF)
set(GIT_THREADS 0)
add_feature_info("Threads" OFF "threads support is disabled")
else()
message(FATAL_ERROR "unknown threads option: ${USE_THREADS}")
endif()

View File

@@ -36,6 +36,7 @@ add_feature_info(debugopen GIT_DEBUG_STRICT_OPEN "path validation in open")
# Optional feature enablement
#
include(SelectThreads)
include(SelectNsec)
include(SelectHTTPSBackend)
include(SelectHashes)
@@ -141,19 +142,6 @@ if(AMIGA)
add_definitions(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP)
endif()
# threads
if(USE_THREADS)
if(NOT WIN32)
find_package(Threads REQUIRED)
list(APPEND LIBGIT2_SYSTEM_LIBS ${CMAKE_THREAD_LIBS_INIT})
list(APPEND LIBGIT2_PC_LIBS ${CMAKE_THREAD_LIBS_INIT})
endif()
set(GIT_THREADS 1)
endif()
add_feature_info(threadsafe USE_THREADS "threadsafe support")
#
# Include child projects
#

View File

@@ -116,10 +116,12 @@ const char *git_libgit2_feature_backend(git_feature_t feature)
{
switch (feature) {
case GIT_FEATURE_THREADS:
#if defined(GIT_THREADS) && defined(GIT_WIN32)
#if defined(GIT_THREADS_PTHREADS)
return "pthread";
#elif defined(GIT_THREADS_WIN32)
return "win32";
#elif defined(GIT_THREADS)
return "pthread";
GIT_ASSERT_WITH_RETVAL(!"Unknown threads backend", NULL);
#endif
break;

View File

@@ -6,6 +6,9 @@
#cmakedefine GIT_DEBUG_STRICT_OPEN 1
#cmakedefine GIT_THREADS 1
#cmakedefine GIT_THREADS_PTHREADS 1
#cmakedefine GIT_THREADS_WIN32 1
#cmakedefine GIT_WIN32_LEAKCHECK 1
#cmakedefine GIT_ARCH_64 1

View File

@@ -82,10 +82,12 @@ void test_core_features__backends(void)
const char *sha1 = git_libgit2_feature_backend(GIT_FEATURE_SHA1);
const char *sha256 = git_libgit2_feature_backend(GIT_FEATURE_SHA256);
#if defined(GIT_THREADS) && defined(GIT_WIN32)
#if defined(GIT_THREADS_WIN32)
cl_assert_equal_s("win32", threads);
#elif defined(GIT_THREADS)
#elif defined(GIT_THREADS_PTHREADS)
cl_assert_equal_s("pthread", threads);
#elif defined(GIT_THREADS)
cl_assert(0);
#else
cl_assert(threads == NULL);
#endif