This is the alternative patch as discussed with jon_y on IRC.


--
Best regards,
LIU Hao
From 1d0e08852d702232eca5edd885187b7db3074348 Mon Sep 17 00:00:00 2001
From: LIU Hao <[email protected]>
Date: Sat, 30 Apr 2022 21:17:38 +0800
Subject: [PATCH] winpthreads: Do not use `dllimport` when building 3rd-party
 DLLs

When using libtool to build a 3rd-party DLL that attempts to link
against winpthreads statically, `DLL_EXPORT` is defined when compiling
3rd-party source files, which used to cause winpthreads functions to be
decorated as `dllimport`. As static libraries do not export functions,
this would end up with undefined references.

We only ever need `dllexport` on winpthreads functions when building
winpthreads itself. GNU LD can import them even without the `dllimport`
attribute, so it's unnecessary.

Note: This is not a satisfactory solution. We can see that after this
commit, user code will always see `WINPTHREAD_API` as empty. I prefer to
move it from headers to source files.

Reference: https://sourceforge.net/p/mingw-w64/mailman/message/37647350/
Signed-off-by: LIU Hao <[email protected]>
---
 mingw-w64-libraries/winpthreads/README           | 10 ++++++++++
 .../winpthreads/include/pthread.h                | 16 ++++++++++------
 mingw-w64-libraries/winpthreads/include/sched.h  | 16 ++++++++++------
 .../winpthreads/include/semaphore.h              | 16 ++++++++++------
 4 files changed, 40 insertions(+), 18 deletions(-)
 create mode 100644 mingw-w64-libraries/winpthreads/README

diff --git a/mingw-w64-libraries/winpthreads/README 
b/mingw-w64-libraries/winpthreads/README
new file mode 100644
index 000000000..e5451e20f
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/README
@@ -0,0 +1,10 @@
+The Winpthreads Library
+-----------------------
+
+This library provides POSIX threading APIs for mingw-w64.
+
+Programs are usually linked against the winpthreads DLL, and winpthreads
+headers expose `dllexport` APIs by default. When linking against the
+static library, especially when building a user DLL with libtool, it is
+necessary to define the `WINPTHREAD_STATIC` macro to avoid undefined
+references.
diff --git a/mingw-w64-libraries/winpthreads/include/pthread.h 
b/mingw-w64-libraries/winpthreads/include/pthread.h
index 5aadb1cfa..d17df3352 100644
--- a/mingw-w64-libraries/winpthreads/include/pthread.h
+++ b/mingw-w64-libraries/winpthreads/include/pthread.h
@@ -83,14 +83,18 @@ extern "C" {
 /* MSB 8-bit major version, 8-bit minor version, 16-bit patch level.  */
 #define __WINPTHREADS_VERSION 0x00050000
 
-#if defined DLL_EXPORT
 #ifdef IN_WINPTHREAD
-#define WINPTHREAD_API __declspec(dllexport)
+#  ifdef DLL_EXPORT
+#    define WINPTHREAD_API __declspec(dllexport)
+#  else
+#    define WINPTHREAD_API
+#  endif
 #else
-#define WINPTHREAD_API __declspec(dllimport)
-#endif
-#else
-#define WINPTHREAD_API
+#  ifdef WINPTHREAD_STATIC
+#    define WINPTHREAD_API
+#  else
+#    define WINPTHREAD_API __declspec(dllimport)
+#  endif
 #endif
 
 /* #define WINPTHREAD_DBG 1 */
diff --git a/mingw-w64-libraries/winpthreads/include/sched.h 
b/mingw-w64-libraries/winpthreads/include/sched.h
index e77bf4cc3..2f6ce7c49 100644
--- a/mingw-w64-libraries/winpthreads/include/sched.h
+++ b/mingw-w64-libraries/winpthreads/include/sched.h
@@ -49,14 +49,18 @@ struct sched_param {
 extern "C" {
 #endif
 
-#if defined DLL_EXPORT && !defined (WINPTHREAD_EXPORT_ALL_DEBUG)
 #ifdef IN_WINPTHREAD
-#define WINPTHREAD_SCHED_API __declspec(dllexport)
+#  if defined(DLL_EXPORT) && !defined(WINPTHREAD_EXPORT_ALL_DEBUG)
+#    define WINPTHREAD_SCHED_API __declspec(dllexport)
+#  else
+#    define WINPTHREAD_SCHED_API
+#  endif
 #else
-#define WINPTHREAD_SCHED_API __declspec(dllimport)
-#endif
-#else
-#define WINPTHREAD_SCHED_API
+#  ifdef WINPTHREAD_STATIC
+#    define WINPTHREAD_SCHED_API
+#  else
+#    define WINPTHREAD_SCHED_API __declspec(dllimport)
+#  endif
 #endif
 
 int WINPTHREAD_SCHED_API sched_yield(void);
diff --git a/mingw-w64-libraries/winpthreads/include/semaphore.h 
b/mingw-w64-libraries/winpthreads/include/semaphore.h
index 14cb70371..fb08d58d7 100644
--- a/mingw-w64-libraries/winpthreads/include/semaphore.h
+++ b/mingw-w64-libraries/winpthreads/include/semaphore.h
@@ -27,14 +27,18 @@
 extern "C" {
 #endif
 
-#if defined DLL_EXPORT && !defined (WINPTHREAD_EXPORT_ALL_DEBUG)
 #ifdef IN_WINPTHREAD
-#define WINPTHREAD_SEMA_API __declspec(dllexport)
+#  if defined(DLL_EXPORT) && !defined(WINPTHREAD_EXPORT_ALL_DEBUG)
+#    define WINPTHREAD_SEMA_API __declspec(dllexport)
+#  else
+#    define WINPTHREAD_SEMA_API
+#  endif
 #else
-#define WINPTHREAD_SEMA_API __declspec(dllimport)
-#endif
-#else
-#define WINPTHREAD_SEMA_API
+#  ifdef WINPTHREAD_STATIC
+#    define WINPTHREAD_SEMA_API
+#  else
+#    define WINPTHREAD_SEMA_API __declspec(dllimport)
+#  endif
 #endif
 
 /* Set this to 0 to disable it */
-- 
2.35.3

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to