Subject: [bug#62307] [PATCH core-updates 03/15] gcc-11: Patch libstdc++ libpthread issue for Hurd only.

From: Josselin Poiret <[email-masked]>
Date: Mon, 20 Mar 2023 23:10:38 +0100

* gnu/packages/gcc.scm (gcc-11): Add ugly conditional patching, to avoid a
world-rebuild.
* gnu/packages/cross-base.scm (cross-gcc):
* gnu/packages/commencement.scm (gcc-boot0): Pass the patch there as well.
* gnu/packages/patches/gcc-11-libstdc++-hurd-libpthread.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register it.
---
 gnu/local.mk                                  |  1 +
 gnu/packages/commencement.scm                 | 10 ++-
 gnu/packages/cross-base.scm                   | 21 ++++---
 gnu/packages/gcc.scm                          | 32 ++++++++++
 .../gcc-11-libstdc++-hurd-libpthread.patch    | 62 +++++++++++++++++++
 5 files changed, 118 insertions(+), 8 deletions(-)
 create mode 100644 gnu/packages/patches/gcc-11-libstdc++-hurd-libpthread.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 0b2c8538c4..d0f89ace6c 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1181,6 +1181,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/gcc-9-strmov-store-file-names.patch	\
   %D%/packages/patches/gcc-12-strmov-store-file-names.patch	\
   %D%/packages/patches/gcc-10-cross-environment-variables.patch \
+  %D%/packages/patches/gcc-11-libstdc++-hurd-libpthread.patch   \
   %D%/packages/patches/gcc-12-cross-environment-variables.patch \
   %D%/packages/patches/gcc-10-tree-sra-union-handling.patch	\
   %D%/packages/patches/gcolor3-update-libportal-usage.patch	\
diff --git a/gnu/packages/commencement.scm b/gnu/packages/commencement.scm
index 24cffc4252..2d947ea911 100644
--- a/gnu/packages/commencement.scm
+++ b/gnu/packages/commencement.scm
@@ -2452,7 +2452,15 @@ (define gcc-boot0
               ,@(alist-delete "libc" (%boot0-inputs))))
 
     ;; No need for the native-inputs to build the documentation at this stage.
-    (native-inputs `())))
+    (native-inputs
+     (cond
+      ;; FIXME This patch is not in the origin because it is conditionally
+      ;; applied, to avoid rebuilds at the end of the core-updates cycle.
+      ;; Move to gcc-11's origin ASAP.
+      ((target-hurd? (or (%current-target-system)
+                         (%current-system)))
+       (list patch-boot0 (search-patch "gcc-11-libstdc++-hurd-libpthread.patch")))
+      (#t '())))))
 
 (define perl-boot0
   (package
diff --git a/gnu/packages/cross-base.scm b/gnu/packages/cross-base.scm
index 2959616af6..30bf1c6bd0 100644
--- a/gnu/packages/cross-base.scm
+++ b/gnu/packages/cross-base.scm
@@ -319,14 +319,21 @@ (define* (cross-gcc target
                    ("libc" ,libc))
                  `(,@inputs
                    ("mingw-source" ,(package-source mingw-w64)))))
-            (libc
+            (else
              `(,@inputs
-               ("libc" ,libc)
-               ("libc:static" ,libc "static")
-               ("xkernel-headers"       ;the target headers
-                ,@(assoc-ref (package-propagated-inputs libc)
-                             "kernel-headers"))))
-            (else inputs)))))
+               ;; FIXME Kludge to only apply the patch for hurd to avoid rebuilds
+               ;; remove ASAP, along with the accompanying changes.
+               ,@(if (target-hurd? target)
+                     `(("patch" ,patch)
+                       ("_" ,(search-patch "gcc-11-libstdc++-hurd-libpthread.patch")))
+                     '())
+               ,@(if libc
+                     `(("libc" ,libc)
+                       ("libc:static" ,libc "static")
+                       ("xkernel-headers" ;the target headers
+                        ,@(assoc-ref (package-propagated-inputs libc)
+                                     "kernel-headers")))
+                     '())))))))
 
     (inputs '())
 
diff --git a/gnu/packages/gcc.scm b/gnu/packages/gcc.scm
index ce6e3e7a83..55d0f7a162 100644
--- a/gnu/packages/gcc.scm
+++ b/gnu/packages/gcc.scm
@@ -707,6 +707,38 @@ (define-public gcc-11
             (modules '((guix build utils)))
             (snippet gcc-canadian-cross-objdump-snippet)))
 
+   (arguments
+    (cond
+     ;; FIXME We conditionally apply a patch passed in native-inputs, to avoid
+     ;; rebuilds at the end of the core-updates cycle.  Move this to a regular
+     ;; patch ASAP.
+     ((target-hurd? (or (%current-target-system)
+                        (%current-system)))
+      (substitute-keyword-arguments (package-arguments gcc-8)
+        ((#:phases phases)
+         `(modify-phases ,phases
+            (add-after 'unpack 'patch-libpthread-hurd
+              (lambda* (#:key inputs native-inputs #:allow-other-keys)
+                (define patch (search-input-file (or native-inputs inputs) "/bin/patch"))
+                (define the-patch
+                  (cdr
+                   (car
+                    (filter (lambda (input)
+                              (string-suffix? "gcc-11-libstdc++-hurd-libpthread.patch"
+                                              (cdr input)))
+                            (or native-inputs inputs)))))
+                (invoke patch "--verbose"
+                        "--force" "--no-backup-if-mismatch"
+                        "-p1" "--input" the-patch)))))))
+     (#t (package-arguments gcc-8))))
+   (native-inputs
+    (cond
+     ((target-hurd? (or (%current-target-system)
+                        (%current-system)))
+      (modify-inputs (package-native-inputs gcc-8)
+        (prepend (module-ref (resolve-interface '(gnu packages base)) 'patch)
+                 (search-patch "gcc-11-libstdc++-hurd-libpthread.patch"))))
+     (#t (package-native-inputs gcc-8))))
    (properties
     `((compiler-cpu-architectures
        ("aarch64" ,@%gcc-11-aarch64-micro-architectures)
diff --git a/gnu/packages/patches/gcc-11-libstdc++-hurd-libpthread.patch b/gnu/packages/patches/gcc-11-libstdc++-hurd-libpthread.patch
new file mode 100644
index 0000000000..1845583ac5
--- /dev/null
+++ b/gnu/packages/patches/gcc-11-libstdc++-hurd-libpthread.patch
@@ -0,0 +1,62 @@
+From f0065f207cf19cd960b33d961472c6d69514336f Mon Sep 17 00:00:00 2001
+From: Samuel Thibault <[email-masked]>
+Date: Fri, 7 Oct 2022 22:45:06 +0200
+Subject: [PATCH] libstdc++: Mark pieces of gnu-linux/os_support.h
+ linux-specific
+
+This is notably needed because in glibc 2.34, the move of pthread functions
+into libc.so happened for Linux only, not GNU/Hurd.
+
+The pthread_self() function can also always be used fine as it is on
+GNU/Hurd.
+
+libstdc++-v3/ChangeLog:
+
+	* config/os/gnu-linux/os_defines.h [!__linux__]
+	(_GLIBCXX_NATIVE_THREAD_ID, _GLIBCXX_GTHREAD_USE_WEAK): Do not define.
+
+Reviewed-by: Jonathan Wakely <[email-masked]>
+---
+ libstdc++-v3/config/os/gnu-linux/os_defines.h | 22 +++++++++++--------
+ 1 file changed, 13 insertions(+), 9 deletions(-)
+
+diff --git a/libstdc++-v3/config/os/gnu-linux/os_defines.h b/libstdc++-v3/config/os/gnu-linux/os_defines.h
+index 3064f8d908ac2..87317031fcd71 100644
+--- a/libstdc++-v3/config/os/gnu-linux/os_defines.h
++++ b/libstdc++-v3/config/os/gnu-linux/os_defines.h
+@@ -60,22 +60,26 @@
+ # define _GLIBCXX_HAVE_FLOAT128_MATH 1
+ #endif
+ 
+-#if __GLIBC_PREREQ(2, 27)
++#ifdef __linux__
++// The following libpthread properties only apply to Linux, not GNU/Hurd.
++
++# if __GLIBC_PREREQ(2, 27)
+ // Since glibc 2.27 pthread_self() is usable without linking to libpthread.
+-# define _GLIBCXX_NATIVE_THREAD_ID pthread_self()
+-#else
++#  define _GLIBCXX_NATIVE_THREAD_ID pthread_self()
++# else
+ // Before then it was in libc.so.6 but not libc.a, and always returns 0,
+ // which breaks the invariant this_thread::get_id() != thread::id{}.
+ // So only use it if we know the libpthread version is available.
+ // Otherwise use (__gthread_t)1 as the ID of the main (and only) thread.
+-# define _GLIBCXX_NATIVE_THREAD_ID \
+-  (__gthread_active_p() ? __gthread_self() : (__gthread_t)1)
+-#endif
++#  define _GLIBCXX_NATIVE_THREAD_ID \
++   (__gthread_active_p() ? __gthread_self() : (__gthread_t)1)
++# endif
+ 
+-#if __GLIBC_PREREQ(2, 34)
++# if __GLIBC_PREREQ(2, 34)
+ // Since glibc 2.34 all pthreads functions are usable without linking to
+ // libpthread.
+-# define _GLIBCXX_GTHREAD_USE_WEAK 0
+-#endif
++#  define _GLIBCXX_GTHREAD_USE_WEAK 0
++# endif
++#endif // __linux__
+ 
+ #endif
-- 
2.39.2