Subject: bug#62609: 29.0.60; [PATCH] src/comp.c: New variable `comp-el-to-eln-strip-prefix` for `comp-el-to-eln-rel-filename`

From: lin Sun <[email-masked]>
Date: Sun, 2 Apr 2023 05:13:28 +0000

Hi,

The emacs with native compile enabled will always rebuild all the
~/.emacs.d/eln-cache/VER/*.eln files if I moved/copied eln-cache
folder from
/home/userA/.emacs.d/eln-cache/ to
/home/userB/.emacs.d/eln-cache/.

It is caused by the function `comp-el-to-eln-rel-filename` calculating
the hash value from the absolute *.el filename to be the middle of the
eln file name.

The new variable `comp-el-to-eln-strip-prefix` in this patch will
allow moving the eln-cache/ folder without rebuilding *.eln in it.

Please help review this change, Thanks

Best Regards
Lin
From ae6a941236044d64e2e8f88f66739149f5260394 Mon Sep 17 00:00:00 2001
From: Lin Sun <[email-masked]>
Date: Sun, 2 Apr 2023 00:00:09 +0000
Subject: [PATCH] New variable `comp-el-to-eln-strip-prefix` for
 `comp-el-to-eln-rel-filename`

* src/comp.c: define the variable `comp-el-to-eln-strip-prefix`
* emacs-lisp/comp.el: forward the variable to native compile workers
---
 lisp/emacs-lisp/comp.el |  1 +
 src/comp.c              | 17 +++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el
index e97832455b9..6c9ccb247ca 100644
--- a/lisp/emacs-lisp/comp.el
+++ b/lisp/emacs-lisp/comp.el
@@ -3996,6 +3996,7 @@ comp-run-async-workers
                                              native-comp-driver-options
                                              load-path
                                              backtrace-line-length
+                                             comp-el-to-eln-strip-prefix
                                              ;; package-load-list
                                              ;; package-user-dir
                                              ;; package-directory-list
diff --git a/src/comp.c b/src/comp.c
index 3f72d088a66..ead1171b001 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -4400,7 +4400,11 @@ DEFUN ("comp-el-to-eln-rel-filename", Fcomp_el_to_eln_rel_filename,
 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.  */)
+one for the file name and another for its contents, followed by .eln.
+
+The file part hash value is generated from the absolute file path, however,
+the absolute path can be stripped with `comp-el-to-eln-strip-prefix' to left
+the significant part for hashing.  */)
   (Lisp_Object filename)
 {
   CHECK_STRING (filename);
@@ -4462,7 +4466,7 @@ DEFUN ("comp-el-to-eln-rel-filename", Fcomp_el_to_eln_rel_filename,
       loadsearch_re_list = list2 (sys_re, Fregexp_quote (dump_load_search));
     }
 
-  Lisp_Object lds_re_tail = loadsearch_re_list;
+  Lisp_Object lds_re_tail = CALLN (Fappend, Vcomp_el_to_eln_strip_prefix, loadsearch_re_list);
   FOR_EACH_TAIL (lds_re_tail)
     {
       Lisp_Object match_idx =
@@ -5864,6 +5868,15 @@ syms_of_comp (void)
      dump reload.  */
   Vnative_comp_eln_load_path = Fcons (build_string ("../native-lisp/"), Qnil);
 
+  DEFVAR_LISP ("comp-el-to-eln-strip-prefix", Vcomp_el_to_eln_strip_prefix,
+    doc: /* List of regex to strip the path prefix in the function
+`comp-el-to-eln-rel-filename'.
+
+When the `comp-el-to-eln-rel-filename' try to convert an <absolute-path>.el
+to eln file name, it will remove the matched prefix on this list and hash
+the rest part to be middle of eln file name, this will allow to move the
+eln-cache/ directory without rebuilding the *.eln files in it.  */);
+
   DEFVAR_LISP ("native-comp-enable-subr-trampolines",
 	       Vnative_comp_enable_subr_trampolines,
     doc: /* If non-nil, enable generation of trampolines for calling primitives.
-- 
2.20.5