guix_mirror_bot pushed a commit to branch core-packages-team
in repository guix.

commit 6dd839a14822612ffcc6b6aeae7d8a6a4cdd698c
Author: Sergio Pastor Pérez <[email protected]>
AuthorDate: Tue Mar 3 17:40:59 2026 +0100

    gnu: gcc: Add patch to remove references to store items.
    
    * doc/guix-cookbook.texi: Update examples.
    * gnu/packages/gcc.scm: Apply store mangling patch to GCC newer than 
version 13.
    * gnu/packages/patches/gcc-mangle-guix-store.patch: New file.
    * gnu/local.mk (dist_patch_DATA): Add it.
    * guix/build/gnu-build-system.scm (%standard-phases): Add
    `set-GUIX_GCC_MANGLE_PREFIX_MAP' phase to enable store mangling behavior in
    patched GCCs.
    
    Fixes: #2394, #2846, #6644
    Change-Id: Ifcc2907aaa4535dbc06491495e4c236e32afe555
    Signed-off-by: Ludovic Courtès <[email protected]>
    Merges: #7101
---
 doc/guix-cookbook.texi                           |  6 +-
 gnu/local.mk                                     |  1 +
 gnu/packages/gcc.scm                             |  9 ++-
 gnu/packages/patches/gcc-mangle-guix-store.patch | 70 ++++++++++++++++++++++++
 guix/build/gnu-build-system.scm                  | 11 +++-
 5 files changed, 91 insertions(+), 6 deletions(-)

diff --git a/doc/guix-cookbook.texi b/doc/guix-cookbook.texi
index 90d6c9ff1d5..ad7ff1f9789 100644
--- a/doc/guix-cookbook.texi
+++ b/doc/guix-cookbook.texi
@@ -1293,7 +1293,9 @@ definition in 
@samp{$GUIX_CHECKOUT/guix/build/gnu-build-system.scm}:
   ;; Standard build phases, as a list of symbol/procedure pairs.
   (let-syntax ((phases (syntax-rules ()
                          ((_ p ...) `((p . ,p) ...)))))
-    (phases set-SOURCE-DATE-EPOCH set-paths install-locale unpack
+    (phases separate-from-pid1
+            set-GUIX_GCC_MANGLE_PREFIX_MAP set-SOURCE-DATE-EPOCH
+            set-paths install-locale unpack
             bootstrap
             patch-usr-bin-file
             patch-source-shebangs configure patch-generated-file-shebangs
@@ -1315,7 +1317,7 @@ Or from the REPL:
 (add-to-load-path "/path/to/guix/checkout")
 ,use (guix build gnu-build-system)
 (map car %standard-phases)
-@result{} (set-SOURCE-DATE-EPOCH set-paths install-locale unpack bootstrap 
patch-usr-bin-file patch-source-shebangs configure 
patch-generated-file-shebangs build check install patch-shebangs strip 
validate-runpath validate-documentation-location delete-info-dir-file 
patch-dot-desktop-files make-dynamic-linker-cache install-license-files 
reset-gzip-timestamps compress-documentation)
+@result{} (separate-from-pid1 set-GUIX_GCC_MANGLE_PREFIX_MAP 
set-SOURCE-DATE-EPOCH set-paths install-locale unpack bootstrap 
patch-usr-bin-file patch-source-shebangs configure 
patch-generated-file-shebangs build check install patch-shebangs strip 
validate-runpath validate-documentation-location delete-info-dir-file 
patch-dot-desktop-files make-dynamic-linker-cache install-license-files 
reset-gzip-timestamps compress-documentation)
 @end lisp
 
 If you want to know more about what happens during those phases, consult the
diff --git a/gnu/local.mk b/gnu/local.mk
index 795f323f1dd..4b9f2ee9d88 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1395,6 +1395,7 @@ dist_patch_DATA =                                         
\
   %D%/packages/patches/gcc-fix-texi2pod.patch                  \
   %D%/packages/patches/gcc-hurd-split-stack.patch              \
   %D%/packages/patches/gcc-libstdc++-newer-gcc.patch           \
+  %D%/packages/patches/gcc-mangle-guix-store.patch             \
   %D%/packages/patches/gcc-4.8-libsanitizer-fix.patch          \
   %D%/packages/patches/gcc-4.9-inline.patch            \
   %D%/packages/patches/gcc-4.9-libsanitizer-fix.patch          \
diff --git a/gnu/packages/gcc.scm b/gnu/packages/gcc.scm
index d09ed52891f..eefc49cddc7 100644
--- a/gnu/packages/gcc.scm
+++ b/gnu/packages/gcc.scm
@@ -1007,7 +1007,8 @@ It also includes runtime support libraries for these 
languages.")
               (patches (search-patches "gcc-12-strmov-store-file-names.patch"
                                        "gcc-5.0-libvtv-runpath.patch"
                                        "gcc-13-libsanitizer-no-crypt.patch"
-                                       "gcc-libstdc++-newer-gcc.patch"))
+                                       "gcc-libstdc++-newer-gcc.patch"
+                                       "gcc-mangle-guix-store.patch"))
               (modules '((guix build utils)))
               (snippet gcc-canadian-cross-objdump-snippet)))
     (arguments
@@ -1038,7 +1039,8 @@ It also includes runtime support libraries for these 
languages.")
                (base32
                 "0fna78ly417g69fdm4i5f3ms96g8xzzjza8gwp41lqr5fqlpgp70"))
               (patches (search-patches "gcc-12-strmov-store-file-names.patch"
-                                       "gcc-5.0-libvtv-runpath.patch"))
+                                       "gcc-5.0-libvtv-runpath.patch"
+                                       "gcc-mangle-guix-store.patch"))
               (modules '((guix build utils)))
               (snippet gcc-canadian-cross-objdump-snippet)))
     (arguments (substitute-keyword-arguments arguments
@@ -1071,7 +1073,8 @@ It also includes runtime support libraries for these 
languages.")
                 "0knj4ph6y7r7yhnp1v4339af7mki5nkh7ni9b948433bhabdk3s3"))
               (patches (search-patches "gcc-12-strmov-store-file-names.patch"
                                        "gcc-5.0-libvtv-runpath.patch"
-                                       "gcc-hurd-split-stack.patch"))
+                                       "gcc-hurd-split-stack.patch"
+                                       "gcc-mangle-guix-store.patch"))
               (modules '((guix build utils)))
               (snippet gcc-canadian-cross-objdump-snippet)))
     (arguments
diff --git a/gnu/packages/patches/gcc-mangle-guix-store.patch 
b/gnu/packages/patches/gcc-mangle-guix-store.patch
new file mode 100644
index 00000000000..17591dc0249
--- /dev/null
+++ b/gnu/packages/patches/gcc-mangle-guix-store.patch
@@ -0,0 +1,70 @@
+Patch for GCC to mangle store item references to inputs introduced by __FILE__
+macros from derivation outputs.  This helps reduce the closure size of certain
+packages.
+
+This patch is based on:
+https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/gcc/patches/13/mangle-NIX_STORE-in-__FILE__.patch
+
+At the time of writting, this patch applies successfuly for GCC versions from
+13 to 16.  The patch is not meant to be upstreamed, it provides a solution to
+a specific problem of Guix.
+---
+ gcc/file-prefix-map.cc | 28 ++++++++++++++++++++++++++--
+ 1 file changed, 26 insertions(+), 2 deletions(-)
+
+diff --git a/gcc/file-prefix-map.cc b/gcc/file-prefix-map.cc
+index 6b432e2a15a..6cc20bacd38 100644
+--- a/gcc/file-prefix-map.cc
++++ b/gcc/file-prefix-map.cc
+@@ -74,7 +74,7 @@ add_prefix_map (file_prefix_map *&maps, const char *arg, 
const char *opt)
+    remapping was performed.  */
+ 
+ static const char *
+-remap_filename (file_prefix_map *maps, const char *filename)
++remap_filename (file_prefix_map *maps, const char *filename, bool 
mangle_guix_store = false)
+ {
+   file_prefix_map *map;
+   char *s;
+@@ -102,6 +102,30 @@ remap_filename (file_prefix_map *maps, const char 
*filename)
+       break;
+   if (!map)
+     {
++      if (mangle_guix_store && getenv("GUIX_GCC_MANGLE_PREFIX_MAP"))
++      {
++        /* Remap all fo $NIX_STORE/.{32} paths to uppercase
++         *
++         * That way we avoid argument parameters explosion
++         * and still avoid embedding headers into runtime closure:
++         *   https://gcc.gnu.org/PR111527
++         */
++         const char * guix_store = getenv("NIX_STORE") ? getenv("NIX_STORE") 
: "/gnu/store";
++         size_t guix_store_len = guix_store ? strlen(guix_store) : 0;
++         const char * name = realname ? realname : filename;
++         size_t name_len = strlen(name);
++         if (guix_store && name_len >= guix_store_len + 1 + 32 && 
memcmp(name, guix_store, guix_store_len) == 0)
++           {
++              s = (char *) ggc_alloc_atomic (name_len + 1);
++              memcpy(s, name, name_len + 1);
++              for (size_t i = guix_store_len + 1; i < guix_store_len + 1 + 
32; i++) {
++                s[i] = TOUPPER(s[i]);
++              }
++              if (realname != filename)
++                free (const_cast <char *> (realname));
++              return s;
++           }
++      }
+       if (realname != filename)
+       free (const_cast <char *> (realname));
+       return filename;
+@@ -163,7 +187,7 @@ add_profile_prefix_map (const char *arg)
+ const char *
+ remap_macro_filename (const char *filename)
+ {
+-  return remap_filename (macro_prefix_maps, filename);
++  return remap_filename (macro_prefix_maps, filename, true);
+ }
+ 
+ /* Remap using -fdebug-prefix-map.  Return the GC-allocated new name
+-- 
+2.52.0
+
diff --git a/guix/build/gnu-build-system.scm b/guix/build/gnu-build-system.scm
index 63bbeae6053..cf5fa53630f 100644
--- a/guix/build/gnu-build-system.scm
+++ b/guix/build/gnu-build-system.scm
@@ -62,6 +62,14 @@ that incorporate timestamps as a way to tell them to use a 
fixed timestamp.
 See https://reproducible-builds.org/specs/source-date-epoch/.";
   (setenv "SOURCE_DATE_EPOCH" "1"))
 
+(define* (set-GUIX_GCC_MANGLE_PREFIX_MAP #:rest _)
+  "Set the 'GUIX_GCC_MANGLE_PREFIX_MAP' environment variable.
+Guix applies 'gcc-mangle-guix-store.patch' to ensure that there are no
+hardcoded references to inputs from the store in derivation outputs due to
+macro expansions of __FILE__.  Prefix mangling is enabled when this variable
+is set.  See https://codeberg.org/guix/guix/issues/2394.";
+  (setenv "GUIX_GCC_MANGLE_PREFIX_MAP" "1"))
+
 (define (first-subdirectory directory)
   "Return the file name of the first sub-directory of DIRECTORY or false, when
 there are none."
@@ -936,7 +944,8 @@ that traversing all the RUNPATH entries entails."
   (let-syntax ((phases (syntax-rules ()
                          ((_ p ...) `((p . ,p) ...)))))
     (phases separate-from-pid1
-            set-SOURCE-DATE-EPOCH set-paths install-locale unpack
+            set-GUIX_GCC_MANGLE_PREFIX_MAP set-SOURCE-DATE-EPOCH
+            set-paths install-locale unpack
             bootstrap
             patch-usr-bin-file
             patch-source-shebangs configure patch-generated-file-shebangs

Reply via email to