Subject: [bug#63641] [PATCH v2 5/8] gnu: Add libc-for-target and glibc/hurd.

From: Josselin Poiret <[email-masked]>
Date: Mon, 22 May 2023 11:42:26 +0200

From: Josselin Poiret <[email-masked]>

* gnu/packages/patches/glibc-2.37-hurd-clock_t_centiseconds.patch
* gnu/packages/patches/glibc-2.37-hurd-local-clock_gettime_MONOTONIC.patch
* gnu/packages/patches/glibc-2.37-ldd-x86_64.patch
* gnu/packages/patches/glibc-2.37-versioned-locpath.patch: New patches.
* gnu/local.mk (dist_patch_DATA): Register them.

* gnu/packages/base.scm (glibc/hurd, libc-for-target): New variables.
(glibc/hurd-headers): Use glibc/hurd.
* gnu/packages/commencement.scm (glibc-final-with-bootstrap-bash,
glibc-final): Use libc-for-target.
* gnu/packages/cross-base.scm (cross-libc/deprecated, cross-libc*): Use
libc-for-target.
---
 gnu/local.mk                                  |   4 +
 gnu/packages/base.scm                         |  38 ++-
 gnu/packages/commencement.scm                 |  66 ++---
 gnu/packages/cross-base.scm                   |   4 +-
 ...glibc-2.37-hurd-clock_t_centiseconds.patch |  59 +++++
 ...7-hurd-local-clock_gettime_MONOTONIC.patch | 133 ++++++++++
 .../patches/glibc-2.37-ldd-x86_64.patch       |   8 +
 .../glibc-2.37-versioned-locpath.patch        | 247 ++++++++++++++++++
 8 files changed, 521 insertions(+), 38 deletions(-)
 create mode 100644 gnu/packages/patches/glibc-2.37-hurd-clock_t_centiseconds.patch
 create mode 100644 gnu/packages/patches/glibc-2.37-hurd-local-clock_gettime_MONOTONIC.patch
 create mode 100644 gnu/packages/patches/glibc-2.37-ldd-x86_64.patch
 create mode 100644 gnu/packages/patches/glibc-2.37-versioned-locpath.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 17b07a1102..2a09d44506 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1248,6 +1248,10 @@ dist_patch_DATA =						\
   %D%/packages/patches/glibc-hidden-visibility-ldconfig.patch	\
   %D%/packages/patches/glibc-hurd-clock_gettime_monotonic.patch	\
   %D%/packages/patches/glibc-2.31-hurd-clock_gettime_monotonic.patch	\
+  %D%/packages/patches/glibc-2.37-hurd-clock_t_centiseconds.patch	\
+  %D%/packages/patches/glibc-2.37-hurd-local-clock_gettime_MONOTONIC.patch	\
+  %D%/packages/patches/glibc-2.37-ldd-x86_64.patch	\
+  %D%/packages/patches/glibc-2.37-versioned-locpath.patch	\
   %D%/packages/patches/glibc-hurd-clock_t_centiseconds.patch	\
   %D%/packages/patches/glibc-hurd-gettyent.patch		\
   %D%/packages/patches/glibc-hurd-mach-print.patch		\
diff --git a/gnu/packages/base.scm b/gnu/packages/base.scm
index f944ab5d47..59c9632551 100644
--- a/gnu/packages/base.scm
+++ b/gnu/packages/base.scm
@@ -75,6 +75,7 @@ (define-module (gnu packages base)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:export (glibc
+            libc-for-target
             make-ld-wrapper
             libiconv-if-needed))
 
@@ -1415,20 +1416,42 @@ (define-public which
 command.")
     (license gpl3+))) ; some files are under GPLv2+
 
+(define-public glibc/hurd
+  (package/inherit glibc
+    (name "glibc-hurd")
+    (version "2.37")
+    (source (origin
+            (method url-fetch)
+            (uri (string-append "mirror://gnu/glibc/glibc-" version ".tar.xz"))
+            (sha256
+             (base32
+              "0hqsp4dzrjx0iga6jv0magjw26dh82pxlmk8yis5v0d127qyymr2"))
+            (patches (search-patches "glibc-ldd-powerpc.patch"
+                                     "glibc-2.37-ldd-x86_64.patch"
+                                     "glibc-dl-cache.patch"
+                                     "glibc-2.37-versioned-locpath.patch"
+                                     "glibc-reinstate-prlimit64-fallback.patch"
+                                     "glibc-supported-locales.patch"
+                                     "glibc-2.37-hurd-clock_t_centiseconds.patch"
+                                     "glibc-2.37-hurd-local-clock_gettime_MONOTONIC.patch"
+                                     "glibc-hurd-mach-print.patch"
+                                     "glibc-hurd-gettyent.patch"))))
+    (supported-systems %hurd-systems)))
+
 (define-public glibc/hurd-headers
-  (package (inherit glibc)
+  (package/inherit glibc/hurd
     (name "glibc-hurd-headers")
     (outputs '("out"))
     (propagated-inputs (list gnumach-headers hurd-headers))
     (native-inputs
-     (modify-inputs (package-native-inputs glibc)
+     (modify-inputs (package-native-inputs glibc/hurd)
        (prepend (if (%current-target-system)
                    (let* ((cross-base (resolve-interface '(gnu packages cross-base)))
                           (cross-mig (module-ref cross-base 'cross-mig)))
                      (cross-mig (%current-target-system)))
                    mig))))
     (arguments
-     (substitute-keyword-arguments (package-arguments glibc)
+     (substitute-keyword-arguments (package-arguments glibc/hurd)
        ;; We just pass the flags really needed to build the headers.
        ((#:configure-flags flags)
         `(list "--enable-add-ons"
@@ -1449,6 +1472,15 @@ (define-public glibc/hurd-headers
            (delete 'build)))))                  ; nothing to build
     (supported-systems %hurd-systems)))
 
+(define* (libc-for-target #:optional
+                          (target (or (%current-target-system)
+                                      (%current-system))))
+  (match target
+    ((? target-hurd?)
+     glibc/hurd)
+    (_
+     glibc)))
+
 (define-public tzdata
   (package
     (name "tzdata")
diff --git a/gnu/packages/commencement.scm b/gnu/packages/commencement.scm
index ab07fb37ab..a24c60ebf8 100644
--- a/gnu/packages/commencement.scm
+++ b/gnu/packages/commencement.scm
@@ -2816,13 +2816,13 @@ (define (%boot1-inputs)
     ("binutils-cross" ,binutils-boot0)
     ,@(alist-delete "binutils" (%boot0-inputs))))
 
-(define glibc-final-with-bootstrap-bash
+(define/system-dependent glibc-final-with-bootstrap-bash
   ;; The final libc, "cross-built".  If everything went well, the resulting
   ;; store path has no dependencies.  Actually, the really-final libc is
   ;; built just below; the only difference is that this one uses the
   ;; bootstrap Bash.
   (package
-    (inherit glibc)
+    (inherit (libc-for-target))
     (name "glibc-intermediate")
     (outputs (delete "debug" (package-outputs glibc)))
     (source (bootstrap-origin (package-source glibc)))
@@ -2991,39 +2991,39 @@ (define gettext-boot0
                         (("^PROGRAMS =.*$")
                          "PROGRAMS =\n")))))))))
 
-(define glibc-final
+(define/system-dependent glibc-final
   ;; The final glibc, which embeds the statically-linked Bash built above.
   ;; Use 'package/inherit' so we get the 'replacement' of 'glibc', if any.
-  (package/inherit
-   glibc
-   (name "glibc")
-   (source (bootstrap-origin (package-source glibc)))
-   (inputs `(("static-bash" ,static-bash-for-glibc)
-             ,@(alist-delete
-                "static-bash"
-                (package-inputs glibc-final-with-bootstrap-bash))))
-
-   ;; This time we need 'msgfmt' to install all the libc.mo files.
-   (native-inputs `(,@(package-native-inputs glibc-final-with-bootstrap-bash)
-                    ("gettext" ,gettext-boot0)))
-
-   (propagated-inputs
-    (package-propagated-inputs glibc-final-with-bootstrap-bash))
-
-   ;; The final libc only refers to itself, but the 'debug' output contains
-   ;; references to GCC-BOOT0 and to the Linux headers.  XXX: Would be great
-   ;; if 'allowed-references' were per-output.
-   (arguments
-    `(#:allowed-references
-      (,(gexp-input gcc-boot0 "lib")
-       ,(kernel-headers-boot0)
-       ,static-bash-for-glibc
-       ,@(if (hurd-system?)
-             `(,gnumach-headers-boot0
-               ,hurd-headers-boot0)
-             '())
-       ,@(package-outputs glibc-final-with-bootstrap-bash))
-      ,@(package-arguments glibc-final-with-bootstrap-bash)))))
+  (let ((libc (libc-for-target)))
+    (package/inherit libc
+      (name "glibc")
+      (source (bootstrap-origin (package-source glibc)))
+      (inputs `(("static-bash" ,static-bash-for-glibc)
+                ,@(alist-delete
+                   "static-bash"
+                   (package-inputs glibc-final-with-bootstrap-bash))))
+
+      ;; This time we need 'msgfmt' to install all the libc.mo files.
+      (native-inputs `(,@(package-native-inputs glibc-final-with-bootstrap-bash)
+                       ("gettext" ,gettext-boot0)))
+
+      (propagated-inputs
+       (package-propagated-inputs glibc-final-with-bootstrap-bash))
+
+      ;; The final libc only refers to itself, but the 'debug' output contains
+      ;; references to GCC-BOOT0 and to the Linux headers.  XXX: Would be great
+      ;; if 'allowed-references' were per-output.
+      (arguments
+       `(#:allowed-references
+         (,(gexp-input gcc-boot0 "lib")
+          ,(kernel-headers-boot0)
+          ,static-bash-for-glibc
+          ,@(if (hurd-system?)
+                `(,gnumach-headers-boot0
+                  ,hurd-headers-boot0)
+                '())
+          ,@(package-outputs glibc-final-with-bootstrap-bash))
+         ,@(package-arguments glibc-final-with-bootstrap-bash))))))
 
 (define/system-dependent gcc-boot0-wrapped
   ;; Make the cross-tools GCC-BOOT0 and BINUTILS-BOOT0 available under the
diff --git a/gnu/packages/cross-base.scm b/gnu/packages/cross-base.scm
index 01c2295853..b2823c73c5 100644
--- a/gnu/packages/cross-base.scm
+++ b/gnu/packages/cross-base.scm
@@ -577,7 +577,7 @@ (define* (cross-libc . args)
 
 (define* (cross-libc/deprecated target
                                 #:optional
-                                (libc glibc)
+                                (libc (libc-for-target target))
                                 (xgcc (cross-gcc target))
                                 (xbinutils (cross-binutils target))
                                 (xheaders (cross-kernel-headers target)))
@@ -590,7 +590,7 @@ (define* (cross-libc/deprecated target
 
 (define* (cross-libc* target
                       #:key
-                      (libc glibc)
+                      (libc (libc-for-target target))
                       (xgcc (cross-gcc target))
                       (xbinutils (cross-binutils target))
                       (xheaders (cross-kernel-headers target)))
diff --git a/gnu/packages/patches/glibc-2.37-hurd-clock_t_centiseconds.patch b/gnu/packages/patches/glibc-2.37-hurd-clock_t_centiseconds.patch
new file mode 100644
index 0000000000..27db59ec40
--- /dev/null
+++ b/gnu/packages/patches/glibc-2.37-hurd-clock_t_centiseconds.patch
@@ -0,0 +1,59 @@
+Some applications assume centisecond precision, or at most millisecond precision
+(e.g. guile).  This is a work-around for them.
+
+---
+ getclktck.c |    5 ++---
+ setitimer.c |    2 +-
+ times.c     |    2 +-
+ 4 files changed, 8 insertions(+), 9 deletions(-)
+commit d57f2f9b4bd007846af2fb4217486ea572579010
+Author: Richard Braun <[email-masked]>
+Date:   Tue Aug 27 11:35:31 2013 +0200
+
+    Express CPU time (clock_t of times(2)) in centiseconds
+
+diff --git a/sysdeps/mach/hurd/getclktck.c b/sysdeps/mach/hurd/getclktck.c
+index 69be2cc..5f7d946 100644
+--- a/sysdeps/mach/hurd/getclktck.c
++++ b/sysdeps/mach/hurd/getclktck.c
+@@ -18,12 +18,11 @@
+ 
+ #include <time.h>
+ 
+-/* Return frequency of `times'.
+-   Since Mach reports CPU times in microseconds, we always use 1 million.  */
++/* Return frequency of `times'.  */
+ int
+ __getclktck (void)
+ {
+-  return 1000000;
++  return 100;
+ }
+ 
+ /* Before glibc 2.2, the Hurd actually did this differently, so we
+diff --git a/sysdeps/mach/hurd/setitimer.c b/sysdeps/mach/hurd/setitimer.c
+index 39b6b16..4992c89 100644
+--- a/sysdeps/mach/hurd/setitimer.c
++++ b/sysdeps/mach/hurd/setitimer.c
+@@ -42,7 +42,7 @@ quantize_timeval (struct timeval *tv)
+   static time_t quantum = -1;
+ 
+   if (quantum == -1)
+-    quantum = 1000000 / __getclktck ();
++    quantum = 100 / __getclktck ();
+ 
+   tv->tv_usec = ((tv->tv_usec + (quantum - 1)) / quantum) * quantum;
+   if (tv->tv_usec >= 1000000)
+diff --git a/sysdeps/mach/hurd/times.c b/sysdeps/mach/hurd/times.c
+index 9e13a75..593c33a 100644
+--- a/sysdeps/mach/hurd/times.c
++++ b/sysdeps/mach/hurd/times.c
+@@ -29,7 +29,7 @@
+ static inline clock_t
+ clock_from_time_value (const time_value_t *t)
+ {
+-  return t->seconds * 1000000 + t->microseconds;
++  return t->seconds * 100 + t->microseconds / 10000;
+ }
+ 
+ /* Store the CPU time used by this process and all its
diff --git a/gnu/packages/patches/glibc-2.37-hurd-local-clock_gettime_MONOTONIC.patch b/gnu/packages/patches/glibc-2.37-hurd-local-clock_gettime_MONOTONIC.patch
new file mode 100644
index 0000000000..3207a94550
--- /dev/null
+++ b/gnu/packages/patches/glibc-2.37-hurd-local-clock_gettime_MONOTONIC.patch
@@ -0,0 +1,133 @@
+Use the realtime clock for the monotonic clock. This is of course not a proper
+implementation (which is being done in Mach), but will permit to fix at least
+the iceweasel stack.
+
+vlc however doesn't build when _POSIX_CLOCK_SELECTION is enabled but
+_POSIX_TIMERS is not, and they refuse to fix that (see #765578), so disable the
+former.
+
+---
+ sysdeps/mach/hurd/bits/posix_opt.h |    2 +-
+ sysdeps/unix/clock_gettime.c       |    1 +
+ 2 files changed, 2 insertions(+), 1 deletion(-)
+Index: glibc-2.27/sysdeps/mach/clock_gettime.c
+===================================================================
+--- glibc-2.27.orig/sysdeps/mach/clock_gettime.c
++++ glibc-2.27/sysdeps/mach/clock_gettime.c
+@@ -31,6 +31,10 @@ __clock_gettime (clockid_t clock_id, str
+   switch (clock_id) {
+ 
+     case CLOCK_REALTIME:
++    case CLOCK_MONOTONIC:
++    case CLOCK_MONOTONIC_RAW:
++    case CLOCK_REALTIME_COARSE:
++    case CLOCK_MONOTONIC_COARSE:
+       {
+ 	/* __host_get_time can only fail if passed an invalid host_t.
+ 	   __mach_host_self could theoretically fail (producing an
+Index: glibc-2.27/rt/timer_create.c
+===================================================================
+--- glibc-2.27.orig/rt/timer_create.c
++++ glibc-2.27/rt/timer_create.c
+@@ -48,7 +48,7 @@ timer_create (clockid_t clock_id, struct
+       return -1;
+     }
+ 
+-  if (clock_id != CLOCK_REALTIME)
++  if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC && clock_id != CLOCK_MONOTONIC_RAW && clock_id != CLOCK_REALTIME_COARSE && clock_id != CLOCK_MONOTONIC_COARSE)
+     {
+       __set_errno (EINVAL);
+       return -1;
+Index: glibc-2.27/sysdeps/mach/hurd/bits/posix_opt.h
+===================================================================
+--- glibc-2.27.orig/sysdeps/mach/hurd/bits/posix_opt.h
++++ glibc-2.27/sysdeps/mach/hurd/bits/posix_opt.h
+@@ -163,10 +163,10 @@
+ #define _POSIX_THREAD_PROCESS_SHARED	-1
+ 
+ /* The monotonic clock might be available.  */
+-#define _POSIX_MONOTONIC_CLOCK	0
++#define _POSIX_MONOTONIC_CLOCK	200809L
+ 
+-/* The clock selection interfaces are available.  */
+-#define _POSIX_CLOCK_SELECTION	200809L
++/* The clock selection interfaces are not really available yet.  */
++#define _POSIX_CLOCK_SELECTION	-1
+ 
+ /* Advisory information interfaces could be available in future.  */
+ #define _POSIX_ADVISORY_INFO	0
+Index: glibc-upstream/sysdeps/posix/clock_getres.c
+===================================================================
+--- glibc-upstream.orig/sysdeps/posix/clock_getres.c
++++ glibc-upstream/sysdeps/posix/clock_getres.c
+@@ -52,6 +52,10 @@ __clock_getres (clockid_t clock_id, stru
+   switch (clock_id)
+     {
+     case CLOCK_REALTIME:
++    case CLOCK_MONOTONIC:
++    case CLOCK_MONOTONIC_RAW:
++    case CLOCK_REALTIME_COARSE:
++    case CLOCK_MONOTONIC_COARSE:
+       retval = realtime_getres (res);
+       break;
+ 
+--- ./sysdeps/mach/clock_nanosleep.c.original	2020-07-21 00:31:35.226113142 +0200
++++ ./sysdeps/mach/clock_nanosleep.c	2020-07-21 00:31:49.026185761 +0200
+@@ -62,7 +62,7 @@
+ __clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
+ 		   struct timespec *rem)
+ {
+-  if (clock_id != CLOCK_REALTIME
++  if ((clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC && clock_id != CLOCK_MONOTONIC_RAW && clock_id != CLOCK_REALTIME_COARSE && clock_id != CLOCK_MONOTONIC_COARSE)
+       || !valid_nanoseconds (req->tv_nsec)
+       || (flags != 0 && flags != TIMER_ABSTIME))
+     return EINVAL;
+Index: glibc-2.32/hurd/hurdlock.c
+===================================================================
+--- glibc-2.32.orig/hurd/hurdlock.c
++++ glibc-2.32/hurd/hurdlock.c
+@@ -47,7 +47,7 @@ int
+ __lll_abstimed_wait (void *ptr, int val,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   int mlsec = compute_reltime (tsp, clk);
+@@ -59,7 +59,7 @@ int
+ __lll_abstimed_wait_intr (void *ptr, int val,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   int mlsec = compute_reltime (tsp, clk);
+@@ -79,7 +79,7 @@ int
+ __lll_abstimed_xwait (void *ptr, int lo, int hi,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   int mlsec = compute_reltime (tsp, clk);
+@@ -91,7 +91,7 @@ int
+ __lll_abstimed_lock (void *ptr,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   if (__lll_trylock (ptr) == 0)
+@@ -177,7 +177,7 @@ __lll_robust_abstimed_lock (void *ptr,
+   int wait_time = 25;
+   unsigned int val;
+ 
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   while (1)
diff --git a/gnu/packages/patches/glibc-2.37-ldd-x86_64.patch b/gnu/packages/patches/glibc-2.37-ldd-x86_64.patch
new file mode 100644
index 0000000000..078cb38bd0
--- /dev/null
+++ b/gnu/packages/patches/glibc-2.37-ldd-x86_64.patch
@@ -0,0 +1,8 @@
+By default, 'RTDLLIST' in 'ldd' refers to 'lib64/ld-linux-x86-64.so', whereas
+it's in 'lib/' for us.  This patch fixes that.
+
+--- glibc-2.17/sysdeps/unix/sysv/linux/x86_64/ldd-rewrite.sed	2012-12-25 04:02:13.000000000 +0100
++++ glibc-2.17/sysdeps/unix/sysv/linux/x86_64/ldd-rewrite.sed	2013-09-15 23:08:03.000000000 +0200
+@@ -1,1 +1,1 @@
+-s_^\(RTLDLIST=\)\(.*lib\)\(\|64\|x32\)\(/[^/]*\)\(-x86-64\|-x32\)\(\.so\.[0-9.]*\)[ 	]*$_\1"\2\4\6 \264\4-x86-64\6 \2x32\4-x32\6"_
++s_^\(RTLDLIST=\)\(.*lib\)\(\|64\|x32\)\(/[^/]*\)\(-x86-64\|-x32\)\(\.so\.[0-9.]*\)[ 	]*$_\1"\2\4\6 \2\4-x86-64\6 \2x32\4-x32\6"_
diff --git a/gnu/packages/patches/glibc-2.37-versioned-locpath.patch b/gnu/packages/patches/glibc-2.37-versioned-locpath.patch
new file mode 100644
index 0000000000..1964be4b4b
--- /dev/null
+++ b/gnu/packages/patches/glibc-2.37-versioned-locpath.patch
@@ -0,0 +1,247 @@
+From d73ba2caa10b8e9f51ff4239cc32eeb4e0de4279 Mon Sep 17 00:00:00 2001
+Message-Id: <[email-masked]>
+From: Josselin Poiret <[email-masked]>
+Date: Sat, 13 May 2023 14:10:43 +0200
+Subject: [PATCH] Add versioned locpath
+
+From: Josselin Poiret <[email-masked]>
+
+---
+ locale/newlocale.c   | 15 ++--------
+ locale/setlocale.c   | 68 +++++++++++++++++++++++++++++++++++++-------
+ string/Makefile      |  1 +
+ string/argz-suffix.c | 56 ++++++++++++++++++++++++++++++++++++
+ string/argz.h        | 10 +++++++
+ 5 files changed, 127 insertions(+), 23 deletions(-)
+ create mode 100644 string/argz-suffix.c
+
+diff --git a/locale/newlocale.c b/locale/newlocale.c
+index 108d2428bf..6218e0fa77 100644
+--- a/locale/newlocale.c
++++ b/locale/newlocale.c
+@@ -29,6 +29,7 @@
+ /* Lock for protecting global data.  */
+ __libc_rwlock_define (extern , __libc_setlocale_lock attribute_hidden)
+ 
++extern error_t compute_locale_search_path (char **, size_t *);
+ 
+ /* Use this when we come along an error.  */
+ #define ERROR_RETURN							      \
+@@ -47,7 +48,6 @@ __newlocale (int category_mask, const char *locale, locale_t base)
+   locale_t result_ptr;
+   char *locale_path;
+   size_t locale_path_len;
+-  const char *locpath_var;
+   int cnt;
+   size_t names_len;
+ 
+@@ -101,17 +101,8 @@ __newlocale (int category_mask, const char *locale, locale_t base)
+   locale_path = NULL;
+   locale_path_len = 0;
+ 
+-  locpath_var = getenv ("LOCPATH");
+-  if (locpath_var != NULL && locpath_var[0] != '\0')
+-    {
+-      if (__argz_create_sep (locpath_var, ':',
+-			     &locale_path, &locale_path_len) != 0)
+-	return NULL;
+-
+-      if (__argz_add_sep (&locale_path, &locale_path_len,
+-			  _nl_default_locale_path, ':') != 0)
+-	return NULL;
+-    }
++  if (compute_locale_search_path (&locale_path, &locale_path_len) != 0)
++    return NULL;
+ 
+   /* Get the names for the locales we are interested in.  We either
+      allow a composite name or a single name.  */
+diff --git a/locale/setlocale.c b/locale/setlocale.c
+index dd73fa4248..d8eb799384 100644
+--- a/locale/setlocale.c
++++ b/locale/setlocale.c
+@@ -213,12 +213,65 @@ setdata (int category, struct __locale_data *data)
+     }
+ }
+ 
++/* Return in *LOCALE_PATH and *LOCALE_PATH_LEN the locale data search path as
++   a colon-separated list.  Return ENOMEN on error, zero otherwise.  */
++error_t
++compute_locale_search_path (char **locale_path, size_t *locale_path_len)
++{
++  char* guix_locpath_var = getenv ("GUIX_LOCPATH");
++  char *locpath_var = getenv ("LOCPATH");
++
++  if (guix_locpath_var != NULL && guix_locpath_var[0] != '\0')
++    {
++      /* Entries in 'GUIX_LOCPATH' take precedence over 'LOCPATH'.  These
++	 entries are systematically prefixed with "/X.Y" where "X.Y" is the
++	 libc version.  */
++      if (__argz_create_sep (guix_locpath_var, ':',
++			     locale_path, locale_path_len) != 0
++	  || __argz_suffix_entries (locale_path, locale_path_len,
++				    "/" VERSION) != 0)
++	goto bail_out;
++    }
++
++  if (locpath_var != NULL && locpath_var[0] != '\0')
++    {
++      char *reg_locale_path = NULL;
++      size_t reg_locale_path_len = 0;
++
++      if (__argz_create_sep (locpath_var, ':',
++			     &reg_locale_path, &reg_locale_path_len) != 0)
++	goto bail_out;
++
++      if (__argz_append (locale_path, locale_path_len,
++			 reg_locale_path, reg_locale_path_len) != 0)
++	goto bail_out;
++
++      free (reg_locale_path);
++    }
++
++  if (*locale_path != NULL)
++    {
++      /* Append the system default locale directory.  */
++      if (__argz_add_sep (locale_path, locale_path_len,
++			  _nl_default_locale_path, ':') != 0)
++	goto bail_out;
++    }
++
++  return 0;
++
++ bail_out:
++  free (*locale_path);
++  *locale_path = NULL;
++  *locale_path_len = 0;
++
++  return ENOMEM;
++}
++
+ char *
+ setlocale (int category, const char *locale)
+ {
+   char *locale_path;
+   size_t locale_path_len;
+-  const char *locpath_var;
+   char *composite;
+ 
+   /* Sanity check for CATEGORY argument.  */
+@@ -249,17 +302,10 @@ setlocale (int category, const char *locale)
+   locale_path = NULL;
+   locale_path_len = 0;
+ 
+-  locpath_var = getenv ("LOCPATH");
+-  if (locpath_var != NULL && locpath_var[0] != '\0')
++  if (compute_locale_search_path (&locale_path, &locale_path_len) != 0)
+     {
+-      if (__argz_create_sep (locpath_var, ':',
+-			     &locale_path, &locale_path_len) != 0
+-	  || __argz_add_sep (&locale_path, &locale_path_len,
+-			     _nl_default_locale_path, ':') != 0)
+-	{
+-	  __libc_rwlock_unlock (__libc_setlocale_lock);
+-	  return NULL;
+-	}
++      __libc_rwlock_unlock (__libc_setlocale_lock);
++      return NULL;
+     }
+ 
+   if (category == LC_ALL)
+diff --git a/string/Makefile b/string/Makefile
+index 3eced0d027..a7e68729ad 100644
+--- a/string/Makefile
++++ b/string/Makefile
+@@ -51,6 +51,7 @@ routines := \
+   argz-next \
+   argz-replace \
+   argz-stringify \
++  argz-suffix \
+   basename \
+   bcopy \
+   bzero \
+diff --git a/string/argz-suffix.c b/string/argz-suffix.c
+new file mode 100644
+index 0000000000..505b0f248c
+--- /dev/null
++++ b/string/argz-suffix.c
+@@ -0,0 +1,56 @@
++/* Copyright (C) 2015 Free Software Foundation, Inc.
++   This file is part of the GNU C Library.
++   Contributed by Ludovic Courtès <[email-masked]>.
++
++   The GNU C Library is free software; you can redistribute it and/or
++   modify it under the terms of the GNU Lesser General Public
++   License as published by the Free Software Foundation; either
++   version 2.1 of the License, or (at your option) any later version.
++
++   The GNU C Library is distributed in the hope that it will be useful,
++   but WITHOUT ANY WARRANTY; without even the implied warranty of
++   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
++   Lesser General Public License for more details.
++
++   You should have received a copy of the GNU Lesser General Public
++   License along with the GNU C Library; if not, see
++   <http://www.gnu.org/licenses/>.  */
++
++#include <argz.h>
++#include <errno.h>
++#include <stdlib.h>
++#include <string.h>
++
++
++error_t
++__argz_suffix_entries (char **argz, size_t *argz_len, const char *suffix)
++
++{
++  size_t suffix_len = strlen (suffix);
++  size_t count = __argz_count (*argz, *argz_len);
++  size_t new_argz_len = *argz_len + count * suffix_len;
++  char *new_argz = malloc (new_argz_len);
++
++  if (new_argz)
++    {
++      char *p = new_argz, *entry;
++
++      for (entry = *argz;
++	   entry != NULL;
++	   entry = argz_next (*argz, *argz_len, entry))
++	{
++	  p = stpcpy (p, entry);
++	  p = stpcpy (p, suffix);
++	  p++;
++	}
++
++      free (*argz);
++      *argz = new_argz;
++      *argz_len = new_argz_len;
++
++      return 0;
++    }
++  else
++    return ENOMEM;
++}
++weak_alias (__argz_suffix_entries, argz_suffix_entries)
+diff --git a/string/argz.h b/string/argz.h
+index cbc588a8e6..bc6e484c9d 100644
+--- a/string/argz.h
++++ b/string/argz.h
+@@ -108,6 +108,16 @@ extern error_t argz_replace (char **__restrict __argz,
+ 			     const char *__restrict __str,
+ 			     const char *__restrict __with,
+ 			     unsigned int *__restrict __replace_count);
++
++/* Suffix each entry of ARGZ & ARGZ_LEN with SUFFIX.  Return 0 on success,
++   and ENOMEN if memory cannot be allocated.  */
++extern error_t __argz_suffix_entries (char **__restrict __argz,
++				      size_t *__restrict __argz_len,
++				      const char *__restrict __suffix);
++extern error_t argz_suffix_entries (char **__restrict __argz,
++				    size_t *__restrict __argz_len,
++				    const char *__restrict __suffix);
++
+ 
+ /* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
+    are no more.  If entry is NULL, then the first entry is returned.  This
+-- 
+2.40.1
+

base-commit: 849286ba66c96534bddc04df1a47d5692cbc977e
prerequisite-patch-id: bf9f6c2efa2216af0f1787dc1582e2cec821f19e
prerequisite-patch-id: 69acf79d4013ebdc3e7dfb4882bbe19498c820a5
prerequisite-patch-id: 900de3149fa074c39e463d859ccdf208ce120a76
prerequisite-patch-id: 196fd00997b26669da59cf6495fa390f42be05f4
-- 
2.40.1