SQLite User Forum

[PATCH] configure: make pthread bits optional
Login

[PATCH] configure: make pthread bits optional

(1) By Kleis Auke Wolthuizen (kleisauke) on 2025-02-14 17:51:43 [source]

See attached patch.

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Kleis Auke Wolthuizen <github@kleisauke.nl>
Date: Fri, 14 Feb 2025 17:20:40 +0100
Subject: [PATCH 1/1] configure: make pthread bits optional

Specifically Windows does not need require this.

Regressed since the migration of the build system to autosetup.

diff --git a/autosetup/sqlite-config.tcl b/autosetup/sqlite-config.tcl
index 1111111..2222222 100644
--- a/autosetup/sqlite-config.tcl
+++ b/autosetup/sqlite-config.tcl
@@ -383,7 +383,7 @@ proc sqlite-handle-soname {} {
 }
 
 ########################################################################
-# If --enable-thresafe is set, this adds -DSQLITE_THREADSAFE=1 to
+# If --enable-threadsafe is set, this adds -DSQLITE_THREADSAFE=1 to
 # OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD to the linker flags
 # needed for linking pthread. If --enable-threadsafe is not set, adds
 # -DSQLITE_THREADSAFE=0 to OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD
@@ -393,14 +393,15 @@ proc sqlite-handle-threadsafe {} {
   proj-if-opt-truthy threadsafe {
     msg-result yes
     sqlite-add-feature-flag -DSQLITE_THREADSAFE=1
-    if {![proj-check-function-in-lib pthread_create pthread]
-        || ![proj-check-function-in-lib pthread_mutexattr_init pthread]} {
-      user-error "Missing required pthread bits"
+    proj-check-function-in-lib pthread_mutexattr_init pthread
+    if {[proj-check-function-in-lib pthread_create pthread]} {
+      define LDFLAGS_PTHREAD [get-define lib_pthread_create]
+      undefine lib_pthread_create
+      # Recall that LDFLAGS_PTHREAD might be empty even if pthreads if
+      # found because it's in -lc on some platforms.
+    } else {
+      define LDFLAGS_PTHREAD ""
     }
-    define LDFLAGS_PTHREAD [get-define lib_pthread_create]
-    undefine lib_pthread_create
-    # Recall that LDFLAGS_PTHREAD might be empty even if pthreads if
-    # found because it's in -lc on some platforms.
   } {
     msg-result no
     sqlite-add-feature-flag -DSQLITE_THREADSAFE=0

(2) By Stephan Beal (stephan) on 2025-02-14 18:10:50 in reply to 1 [link] [source]

make pthread bits optional

An equivalent patch was just applied to trunk and the 3.49 branch. Summary: failure to find pthreads is only fatal if --enable-threadsafe is specifically passed in.

(Ah, there's a missing "yes" in that output in one case but that'll be fixed soon.)

Thank you for the report!

(3) By Kleis Auke Wolthuizen (kleisauke) on 2025-02-14 18:26:53 in reply to 2 [link] [source]

Thanks for the quick response and patch.

However, I don't think that patch is entirely correct for Win32. The SQLITE_THREADSAFE feature flag shouldn't be disabled if pthreads is unavailable, as Windows can use _beginthreadex() and _endthreadex() instead (see the SQLITE_OS_WIN_THREADS definition for reference).

(4) By Stephan Beal (stephan) on 2025-02-14 18:35:59 in reply to 3 [link] [source]

However, I don't think that patch is entirely correct for Win32. The SQLITE_THREADSAFE feature flag shouldn't be disabled if pthreads is unavailable, as Windows can use _beginthreadex() and _endthreadex() instead (see the SQLITE_OS_WIN_THREADS definition for reference).

Fair point. We don't officially support non-native Windows builds and the native Windows builds don't use the configure script, but i will tinker with this on msys2 over the weekend.

(5) By Stephan Beal (stephan) on 2025-02-15 14:52:46 in reply to 4 [link] [source]

but i will tinker with this on msys2 over the weekend.

@Kleis it turns out that this patch coincidentally works as-is on msys2 and cygwin. Both give me the same results:

$ ./configure --all
...
Support threadsafe operation? Checking for libs...
Checking libs for pthread_create...none needed
Checking libs for pthread_mutexattr_init...none needed
...
Library feature flags: ...<snip>... -DSQLITE_THREADSAFE=1

Tested environments:

$ uname -a
MSYS_NT-10.0-26100 hplite 3.5.4-0bc1222b.x86_64 2024-12-05 09:27 UTC x86_64 Msys

$ uname -a
CYGWIN_NT-10.0-26100 hplite 3.5.7-1.x86_64 2025-01-29 19:46 UTC x86_64 Cygwin

Did you have another environment in mind where pthreads is not available but _beginthreadex() and friends are?

The native/MSVC builds don't use the configure script, so will use whatever options are defined in Makefile.msc.

(6) By Kleis Auke Wolthuizen (kleisauke) on 2025-02-15 15:17:03 in reply to 5 [link] [source]

This is for https://github.com/libvips/build-win64-mxe, which uses https://github.com/mxe/mxe as its base environment but swaps GCC and binutils with Clang and other LLVM-based tools.

It appears that MSYS2 provides libwinpthread-1.dll, with many packages depending on it:
https://packages.msys2.org/packages/mingw-w64-x86_64-libwinpthread-git

However, I intentionally avoided using it in our repository to prevent shipping libwinpthread-1.dll to end users, especially since all our dependencies support native Win32 thread/mutex implementations.

For full transparency, SQLite also needed this patch:

--- a/main.mk
+++ b/main.mk
@@ -1438,8 +1438,9 @@ all: so
 # unnecessary unless we want to set SONAME to libsqlite3.so.3, which
 # is also unnecessary.
 #
-# N.B. different transformations are applied on systems where $(T.dll)
-# is ".dylib" and none of the following docs apply on such systems.
+# N.B. this is skipped entirely where $(T.dll) is ".dll" or different
+# transformations are applied on systems where $(T.dll) is ".dylib"
+# and none of the following docs apply on such systems.
 #
 # The link named libsqlite3.so.0 is provided in an attempt to reduce
 # downstream disruption when performing upgrades from pre-3.48 to a
@@ -1482,7 +1483,9 @@ install-so-1: $(install-dir.lib) $(libsqlite3.SO)
 	fi
 	@echo "Setting up $(libsqlite3.SO) version symlinks..."; \
 	cd "$(install-dir.lib)" || exit $$?; \
-	if [ x.dylib = x$(T.dll) ]; then \
+	if [ x.dll = x$(T.dll) ]; then \
+		echo "... none needed"; \
+	elif [ x.dylib = x$(T.dll) ]; then \
 		rm -f libsqlite3.0$(T.dll) libsqlite3.$(PACKAGE_VERSION)$(T.dll) || exit $$?; \
 		dllname=libsqlite3.$(PACKAGE_VERSION)$(T.dll); \
 		mv $(libsqlite3.SO) $$dllname || exit $$?; \

See e.g. commit https://github.com/libvips/build-win64-mxe/commit/098b4c91a0815f3a4ea1b7f00db58ef04822d918 for reference.

(7) By Stephan Beal (stephan) on 2025-02-15 15:40:50 in reply to 6 [link] [source]

This is for https://github.com/libvips/build-win64-mxe, which uses https://github.com/mxe/mxe as its base environment but swaps GCC and binutils with Clang and other LLVM-based tools.

We admittedly don't put much effort into supporting cross-compilation because we don't have the collective bandwidth to test and support even a small subset of the potential platform combinations. Our stance is that "custom builds require custom solutions," and the amalgamation source distribution makes it trivial to build the library however a given user might need.

+ if [ x.dll = x$(T.dll) ]; then

After looking more closely at msys2 and cygwin, that is apparently the preferred approach for those, so the install rules/docs will be patched for that today.

Thank you!