Subject: [PATCH 2/4] gnu: emacs-minimal: Enable native compilations

From: Morgan Smith <[email-masked]>
Date: Sun, 2 Mar 2025 16:04:57 -0500

* gnu/packages/emacs.scm(emacs-no-x): Move native compilation settings from
here to ...
(emacs-minimal): ... here.

Change-Id: Ib549f0e7435cab0c891bb8d6cec9b1af041ab7c4
---
 gnu/packages/emacs.scm | 130 ++++++++++++++++++++---------------------
 1 file changed, 64 insertions(+), 66 deletions(-)

diff --git a/gnu/packages/emacs.scm b/gnu/packages/emacs.scm
index deebb7282d..1e0c046989 100644
--- a/gnu/packages/emacs.scm
+++ b/gnu/packages/emacs.scm
@@ -154,7 +154,8 @@ (define-public emacs-minimal
      (list
       #:tests? #f                       ; no check target
       #:modules (%emacs-modules build-system)
-      #:configure-flags #~(list "--with-gnutls=no" "--disable-build-details")
+      #:configure-flags #~(list "--with-native-compilation=aot"
+                                "--with-gnutls=no" "--disable-build-details")
       #:phases
       #~(modify-phases %standard-phases
           (add-after 'unpack 'enable-elogind
@@ -239,6 +240,57 @@ (define-public emacs-minimal
               (substitute* (find-files "." "^Makefile\\.in$")
                 (("/bin/pwd")
                  "pwd"))))
+          (add-after 'set-paths 'set-libgccjit-path
+            (lambda* (#:key inputs #:allow-other-keys)
+              (define (first-subdirectory/absolute directory)
+                (let ((files (scandir
+                              directory
+                              (lambda (file)
+                                (and (not (member file '("." "..")))
+                                     (file-is-directory? (string-append
+                                                          directory "/"
+                                                          file)))))))
+                  (and (not (null? files))
+                       (string-append directory "/" (car files)))))
+              (let* ((libgccjit-libdir
+                      (first-subdirectory/absolute ;; version
+                       (first-subdirectory/absolute ;; host type
+                        (search-input-directory inputs "lib/gcc")))))
+                (setenv "LIBRARY_PATH"
+                        (string-append (getenv "LIBRARY_PATH")
+                                       ":" libgccjit-libdir)))))
+          (add-after 'unpack 'patch-compilation-driver
+            (lambda _
+              (substitute* "lisp/emacs-lisp/comp.el"
+                (("\\(defcustom native-comp-driver-options nil")
+                 (format
+                  #f "(defcustom native-comp-driver-options '(~@{~s~^ ~})"
+                  (string-append
+                   "-B" #$(this-package-input "binutils") "/bin/")
+                  (string-append
+                   "-B" #$(this-package-input "glibc") "/lib/")
+                  (string-append
+                   "-B" #$(this-package-input "libgccjit") "/lib/")
+                  (string-append
+                   "-B" #$(this-package-input "libgccjit") "/lib/gcc/"))))))
+          (add-after 'build 'build-trampolines
+            (lambda* (#:key make-flags #:allow-other-keys)
+              (apply invoke "make" "trampolines" make-flags)))
+          (add-after 'validate-runpath 'validate-comp-integrity
+            (lambda* (#:key outputs #:allow-other-keys)
+              #$(cond
+                 ((%current-target-system)
+                  #~(display "Cannot validate native-comp on cross builds.\n"))
+                 ((member (%current-system) '("armhf-linux" "i686-linux"))
+                  #~(display "Integrity test is broken on armhf.\n"))
+                 (else
+                  #~(invoke
+                     (string-append (assoc-ref outputs "out") "/bin/emacs")
+                     "--batch"
+                     "--load"
+                     #$(local-file
+                        (search-auxiliary-file "emacs/comp-integrity.el"))
+                     "-f" "ert-run-tests-batch-and-exit")))))
           (add-after 'install 'install-site-start
             ;; Use 'guix-emacs' in "site-start.el", which is used autoload the
             ;; Elisp packages found in EMACSLOADPATH.
@@ -308,10 +360,17 @@ (define-public emacs-minimal
                 (copy-file
                  (car (find-files "bin" "^emacs-([0-9]+\\.)+[0-9]+$"))
                  "bin/emacs")))))))
-    (inputs (list bash-minimal coreutils findutils gawk gzip ncurses sed))
+    (inputs (list bash-minimal coreutils findutils gawk gzip ncurses sed
+                  ;; To "unshadow" ld-wrapper in native builds
+                  (make-ld-wrapper "ld-wrapper" #:binutils binutils)
+                  ;; For native compilation
+                  binutils
+                  (libc-for-target)
+                  libgccjit
+                  zlib))
     (native-inputs (list autoconf pkg-config texinfo))
     (home-page "https://www.gnu.org/software/emacs/")
-    (synopsis "The extensible text editor (minimal build for byte-compilation)")
+    (synopsis "The extensible text editor (minimal build for elisp compilation)")
     (description
      "GNU Emacs is an extensible and highly customizable text editor.  It is
 based on an Emacs Lisp interpreter with extensions for text editing.  Emacs
@@ -346,70 +405,10 @@ (define-public emacs-no-x
     (arguments
      (substitute-keyword-arguments (package-arguments emacs-minimal)
        ((#:configure-flags flags #~'())
-        #~(cons* "--with-modules" "--with-native-compilation=aot"
-                 (delete "--with-gnutls=no" #$flags)))
-       ((#:phases phases)
-        #~(modify-phases #$phases
-            (add-after 'set-paths 'set-libgccjit-path
-              (lambda* (#:key inputs #:allow-other-keys)
-                (define (first-subdirectory/absolute directory)
-                  (let ((files (scandir
-                                directory
-                                (lambda (file)
-                                  (and (not (member file '("." "..")))
-                                       (file-is-directory? (string-append
-                                                            directory "/"
-                                                            file)))))))
-                    (and (not (null? files))
-                         (string-append directory "/" (car files)))))
-                (let* ((libgccjit-libdir
-                        (first-subdirectory/absolute ;; version
-                         (first-subdirectory/absolute ;; host type
-                          (search-input-directory inputs "lib/gcc")))))
-                  (setenv "LIBRARY_PATH"
-                          (string-append (getenv "LIBRARY_PATH")
-                                         ":" libgccjit-libdir)))))
-            (add-after 'unpack 'patch-compilation-driver
-              (lambda _
-                (substitute* "lisp/emacs-lisp/comp.el"
-                  (("\\(defcustom native-comp-driver-options nil")
-                   (format
-                    #f "(defcustom native-comp-driver-options '(~@{~s~^ ~})"
-                    (string-append
-                     "-B" #$(this-package-input "binutils") "/bin/")
-                    (string-append
-                     "-B" #$(this-package-input "glibc") "/lib/")
-                    (string-append
-                     "-B" #$(this-package-input "libgccjit") "/lib/")
-                    (string-append
-                     "-B" #$(this-package-input "libgccjit") "/lib/gcc/"))))))
-            (add-after 'build 'build-trampolines
-              (lambda* (#:key make-flags #:allow-other-keys)
-                (apply invoke "make" "trampolines" make-flags)))
-            (add-after 'validate-runpath 'validate-comp-integrity
-              (lambda* (#:key outputs #:allow-other-keys)
-                #$(cond
-                   ((%current-target-system)
-                    #~(display "Cannot validate native-comp on cross builds.\n"))
-                   ((member (%current-system) '("armhf-linux" "i686-linux"))
-                    #~(display "Integrity test is broken on armhf.\n"))
-                   (else
-                    #~(invoke
-                       (string-append (assoc-ref outputs "out") "/bin/emacs")
-                       "--batch"
-                       "--load"
-                       #$(local-file
-                          (search-auxiliary-file "emacs/comp-integrity.el"))
-                       "-f" "ert-run-tests-batch-and-exit")))))))))
+        #~(cons* "--with-modules" (delete "--with-gnutls=no" #$flags)))))
     (inputs
      (modify-inputs (package-inputs emacs-minimal)
        (prepend gnutls
-                ;; To "unshadow" ld-wrapper in native builds
-                (make-ld-wrapper "ld-wrapper" #:binutils binutils)
-                ;; For native compilation
-                binutils
-                (libc-for-target)
-                libgccjit
 
                 ;; Avoid Emacs's limited movemail substitute that retrieves POP3
                 ;; email only via insecure channels.
@@ -429,8 +428,7 @@ (define-public emacs-no-x
                 libxml2
                 m17n-lib
                 sqlite
-                tree-sitter
-                zlib)))))
+                tree-sitter)))))
 
 (define-public emacs
   (package/inherit emacs-no-x
-- 
2.48.1