Subject: [bug#67260] [PATCH emacs-team 2/2] gnu: emacs: Don't hash file names in native compilation.

From: Liliana Marie Prikler <[email-masked]>
Date: Sat, 18 Nov 2023 14:38:46 +0100

* gnu/packages/patches/emacs-native-comp-fix-filenames.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register it here.
* gnu/packages/emacs.scm (emacs-minimal)[source]: Use it here.

Change-Id: I2b7f6b45742a985760f0097bb53910f068e3d8e5
---
 gnu/local.mk                                  |  1 +
 gnu/packages/emacs.scm                        |  1 +
 .../emacs-native-comp-fix-filenames.patch     | 93 +++++++++++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 gnu/packages/patches/emacs-native-comp-fix-filenames.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 8d817379a7..e878551b37 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1111,6 +1111,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/emacs-highlight-stages-add-gexp.patch	\
   %D%/packages/patches/emacs-lispy-fix-thread-last-test.patch   \
   %D%/packages/patches/emacs-native-comp-driver-options.patch   \
+  %D%/packages/patches/emacs-native-comp-fix-filenames.patch   \
   %D%/packages/patches/emacs-next-exec-path.patch   \
   %D%/packages/patches/emacs-next-native-comp-driver-options.patch   \
   %D%/packages/patches/emacs-pasp-mode-quote-file-names.patch  \
diff --git a/gnu/packages/emacs.scm b/gnu/packages/emacs.scm
index 55eab48c25..822d15cc94 100644
--- a/gnu/packages/emacs.scm
+++ b/gnu/packages/emacs.scm
@@ -110,6 +110,7 @@ (define-public emacs-minimal
               (patches (search-patches "emacs-exec-path.patch"
                                        "emacs-fix-scheme-indent-function.patch"
                                        "emacs-native-comp-driver-options.patch"
+                                       "emacs-native-comp-fix-filenames.patch"
                                        "emacs-pgtk-super-key-fix.patch"))
               (modules '((guix build utils)))
               (snippet
diff --git a/gnu/packages/patches/emacs-native-comp-fix-filenames.patch b/gnu/packages/patches/emacs-native-comp-fix-filenames.patch
new file mode 100644
index 0000000000..f461bc8a78
--- /dev/null
+++ b/gnu/packages/patches/emacs-native-comp-fix-filenames.patch
@@ -0,0 +1,93 @@
+Upstream hashes both the absolute file name and the content of a file
+to derive the name for the natively compiled files.  This breaks the
+staged install used in guix, as any $GUIX_PROFILE is distinct from
+the build directory.
+
+Index: emacs-29.1/src/comp.c
+===================================================================
+--- emacs-29.1.orig/src/comp.c
++++ emacs-29.1/src/comp.c
+@@ -4399,8 +4399,8 @@ FILENAME must exist, and if it's a symli
+ If FILENAME is compressed, it must have the \".gz\" extension,
+ and Emacs must have been compiled with zlib; the file will be
+ uncompressed on the fly to hash its contents.
+-Value includes the original base name, followed by 2 hash values,
+-one for the file name and another for its contents, followed by .eln.  */)
++Value includes the original base name, followed by a hash of its
++content, followed by .eln.  */)
+   (Lisp_Object filename)
+ {
+   CHECK_STRING (filename);
+@@ -4423,64 +4423,26 @@ one for the file name and another for it
+   filename = Fw32_long_file_name (filename);
+ #endif
+ 
+-  Lisp_Object content_hash = comp_hash_source_file (filename);
+-
+-  if (suffix_p (filename, ".gz"))
+-    filename = Fsubstring (filename, Qnil, make_fixnum (-3));
+-
+-  /* We create eln filenames with an hash in order to look-up these
++  /* We create eln filenames with a hash in order to look-up these
+      starting from the source filename, IOW have a relation
+ 
+-     /absolute/path/filename.el + content ->
+-     eln-cache/filename-path_hash-content_hash.eln.
++     filename.el + content -> eln-cache/filename-content_hash.eln.
+ 
+      'dlopen' can return the same handle if two shared with the same
+      filename are loaded in two different times (even if the first was
+      deleted!).  To prevent this scenario the source file content is
+-     included in the hashing algorithm.
++     included in the hashing algorithm.  */
+ 
+-     As at any point in time no more then one file can exist with the
+-     same filename, should be possible to clean up all
+-     filename-path_hash-* except the most recent one (or the new one
+-     being recompiled).
+-
+-     As installing .eln files compiled during the build changes their
+-     absolute path we need an hashing mechanism that is not sensitive
+-     to that.  For this we replace if match PATH_DUMPLOADSEARCH or
+-     *PATH_REL_LOADSEARCH with '//' before computing the hash.  */
++  Lisp_Object content_hash = comp_hash_source_file (filename);
++  Lisp_Object separator = build_string ("-");
+ 
+-  if (NILP (loadsearch_re_list))
+-    {
+-      Lisp_Object sys_re =
+-	concat2 (build_string ("\\`[[:ascii:]]+"),
+-		 Fregexp_quote (build_string ("/" PATH_REL_LOADSEARCH "/")));
+-      Lisp_Object dump_load_search =
+-	Fexpand_file_name (build_string (PATH_DUMPLOADSEARCH "/"), Qnil);
+-#ifdef WINDOWSNT
+-      dump_load_search = Fw32_long_file_name (dump_load_search);
+-#endif
+-      loadsearch_re_list = list2 (sys_re, Fregexp_quote (dump_load_search));
+-    }
++  if (suffix_p (filename, ".gz"))
++    filename = Fsubstring (filename, Qnil, make_fixnum (-3));
+ 
+-  Lisp_Object lds_re_tail = loadsearch_re_list;
+-  FOR_EACH_TAIL (lds_re_tail)
+-    {
+-      Lisp_Object match_idx =
+-	Fstring_match (XCAR (lds_re_tail), filename, Qnil, Qnil);
+-      if (BASE_EQ (match_idx, make_fixnum (0)))
+-	{
+-	  filename =
+-	    Freplace_match (build_string ("//"), Qt, Qt, filename, Qnil);
+-	  break;
+-	}
+-    }
+-  Lisp_Object separator = build_string ("-");
+-  Lisp_Object path_hash = comp_hash_string (filename);
+   filename = concat2 (Ffile_name_nondirectory (Fsubstring (filename, Qnil,
+ 							   make_fixnum (-3))),
+ 		      separator);
+-  Lisp_Object hash = concat3 (path_hash, separator, content_hash);
+-  return concat3 (filename, hash, build_string (NATIVE_ELISP_SUFFIX));
++  return concat3 (filename, content_hash, build_string (NATIVE_ELISP_SUFFIX));
+ }
+ 
+ DEFUN ("comp-el-to-eln-filename", Fcomp_el_to_eln_filename,
-- 
2.41.0