Subject: [PATCH 1/1] gnu: p11-kit: Fix mingw cross-compilation.

From: Vivien Kraus <[email-masked]>
Date: Thu, 5 Jan 2023 23:08:18 +0100

* gnu/packages/patches/p11-kit-fix-mingw.patch: New patch. It removes
undefined symbol errors.
* gnu/packages/tls.scm (p11-kit) [native-inputs]: Add libtasn1 as a dependency
for a code generator. The autotools are added when compiling for mingw.
[inputs]: Add the mingw patch, similar to the hurd patch.
[phases]: Add a new apply-mingw-patch phase, similar to apply-hurd-patch. The
'bootstrap phase replacement happens both for hurd and mingw.
---
 gnu/local.mk                                 |  1 +
 gnu/packages/patches/p11-kit-fix-mingw.patch | 92 ++++++++++++++++++++
 gnu/packages/tls.scm                         | 22 ++++-
 3 files changed, 111 insertions(+), 4 deletions(-)
 create mode 100644 gnu/packages/patches/p11-kit-fix-mingw.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 4a94c3b8c8..a35c2d2f33 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1649,6 +1649,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/sqlite-hurd.patch			\
   %D%/packages/patches/strace-readlink-tests.patch		\
   %D%/packages/patches/sunxi-tools-remove-sys-io.patch	\
+  %D%/packages/patches/p11-kit-fix-mingw.patch			\
   %D%/packages/patches/p11-kit-hurd.patch			\
   %D%/packages/patches/patch-hurd-path-max.patch		\
   %D%/packages/patches/perl-5.14-autosplit-default-time.patch	\
diff --git a/gnu/packages/patches/p11-kit-fix-mingw.patch b/gnu/packages/patches/p11-kit-fix-mingw.patch
new file mode 100644
index 0000000000..cfb8a3c9ba
--- /dev/null
+++ b/gnu/packages/patches/p11-kit-fix-mingw.patch
@@ -0,0 +1,92 @@
+From de5a3522cc7dd28d9b12f26ad29803ae623dcf24 Mon Sep 17 00:00:00 2001
+From: Vivien Kraus <[email-masked]>
+Date: Thu, 5 Jan 2023 22:56:35 +0100
+Subject: [PATCH] Make sure that libp11-kit does not have undefined symbols
+
+Under mingw, as a DLL, libp11-kit is not allowed to have undefined
+symbols. It means that it should also add the COMMON_LIBS in addition
+to the internal library.
+
+The programs must do that too, otherwise they have undefined symbols.
+---
+ p11-kit/Makefile.am | 13 ++++++++++---
+ trust/Makefile.am   |  3 ++-
+ 2 files changed, 12 insertions(+), 4 deletions(-)
+
+diff --git a/p11-kit/Makefile.am b/p11-kit/Makefile.am
+index 7fe7a6c..11ec3da 100644
+--- a/p11-kit/Makefile.am
++++ b/p11-kit/Makefile.am
+@@ -237,6 +237,8 @@ EXTRA_DIST += \
+ 
+ bin_PROGRAMS += p11-kit/p11-kit
+ 
++p11_kit_p11_kit_CFLAGS = $(COMMON_CFLAGS)
++
+ p11_kit_p11_kit_SOURCES = \
+ 	p11-kit/lists.c \
+ 	p11-kit/p11-kit.c \
+@@ -244,8 +246,9 @@ p11_kit_p11_kit_SOURCES = \
+ 
+ p11_kit_p11_kit_LDADD = \
+ 	libp11-kit.la \
++	libp11-kit-internal.la \
+ 	libp11-tool.la \
+-	libp11-common.la \
++	$(COMMON_LIBS) \
+ 	$(LTLIBINTL) \
+ 	$(NULL)
+ 
+@@ -257,12 +260,14 @@ private_PROGRAMS += p11-kit/p11-kit-remote
+ 
+ p11_kit_p11_kit_remote_SOURCES = \
+ 	p11-kit/remote.c \
++	$(libp11_kit_la_SOURCES) \
+ 	$(NULL)
+ 
+ p11_kit_p11_kit_remote_LDADD = \
+ 	libp11-tool.la \
+-	libp11-common.la \
+ 	libp11-kit.la \
++	libp11-kit-internal.la \
++	$(COMMON_LIBS) \
+ 	$(NULL)
+ 
+ check_PROGRAMS += p11-kit/p11-kit-remote-testable
+@@ -278,12 +283,14 @@ private_PROGRAMS += p11-kit/p11-kit-server
+ 
+ p11_kit_p11_kit_server_SOURCES = \
+ 	p11-kit/server.c \
++	$(libp11_kit_la_SOURCES) \
+ 	$(NULL)
+ 
+ p11_kit_p11_kit_server_LDADD = \
+ 	libp11-tool.la \
+-	libp11-common.la \
+ 	libp11-kit.la \
++	libp11-kit-internal.la \
++	$(COMMON_LIBS) \
+ 	$(LIBSYSTEMD_LIBS) \
+ 	$(NULL)
+ 
+diff --git a/trust/Makefile.am b/trust/Makefile.am
+index 6ff5d12..4e99a80 100644
+--- a/trust/Makefile.am
++++ b/trust/Makefile.am
+@@ -85,11 +85,12 @@ bin_PROGRAMS += trust/trust
+ trust_trust_LDADD = \
+ 	libtrust-data.la \
+ 	libp11-kit.la \
+-	libp11-common.la \
++	libp11-kit-internal.la \
+ 	libp11-tool.la \
+ 	$(LTLIBINTL) \
+ 	$(LIBTASN1_LIBS) \
+ 	$(HASH_LIBS) \
++	$(COMMON_LIBS) \
+ 	$(NULL)
+ 
+ trust_trust_CFLAGS = \
+-- 
+2.38.1
+
diff --git a/gnu/packages/tls.scm b/gnu/packages/tls.scm
index a74b423ccf..d4220284e8 100644
--- a/gnu/packages/tls.scm
+++ b/gnu/packages/tls.scm
@@ -148,11 +148,13 @@ (define-public p11-kit
        (base32 "1dn6br4v033d3gp2max9lsr3y4q0nj6iyr1yq3kzi8ym7lal13wa"))))
     (build-system gnu-build-system)
     (native-inputs
-     `(,@(if (hurd-target?)
+     `(,@(if (or (hurd-target?) (target-mingw?))
              `(("autoconf" ,autoconf)
                ("automake" ,automake)
                ("gettext" ,gettext-minimal) ;for autopoint
-               ("libtool" ,libtool))
+               ("libtool" ,libtool)
+               ;libtasn1 is a code generator
+               ("libtasn1" ,libtasn1))
              '())
        ("pkg-config" ,pkg-config)))
     (inputs
@@ -161,6 +163,9 @@ (define-public p11-kit
              `(("libbsd" ,libbsd)
                ("hurd-patch" ,(search-patch "p11-kit-hurd.patch")))
              '())
+       ,@(if (target-mingw?)
+             `(("mingw-patch" ,(search-patch "p11-kit-fix-mingw.patch")))
+             '())
        ("libtasn1" ,libtasn1)))
     (arguments
      `(#:configure-flags '("--without-trust-paths")
@@ -170,8 +175,17 @@ (define-public p11-kit
                             (lambda* (#:key inputs #:allow-other-keys)
                               (let ((patch (assoc-ref inputs "hurd-patch")))
                                 (invoke "patch" "-p1" "--batch" "-i"
-                                        patch))))
-                          (replace 'bootstrap
+                                        patch)))))
+                        '())
+                  ,@(if (target-mingw?)
+                        '((add-after 'unpack 'apply-mingw-patch
+                            (lambda* (#:key inputs #:allow-other-keys)
+                              (let ((patch (assoc-ref inputs "mingw-patch")))
+                                (invoke "patch" "-p1" "--batch" "-i"
+                                        patch)))))
+                        '())
+                  ,@(if (or (hurd-target?) (target-mingw?))
+                        '((replace 'bootstrap
                             (lambda _
                               (invoke "autoreconf" "-fiv"))))
                         '())
-- 
2.38.1