guix_mirror_bot pushed a commit to branch add-compress-debug-symbols-phase
in repository guix.

commit 0978dac330add151e02e90eb05070c608dd9c4b8
Author: Maxim Cournoyer <[email protected]>
AuthorDate: Sun Oct 19 23:35:20 2025 +0900

    build/gnu-build-system: Add a compress-debug-info phase.
    
    This new phase reduces the debug output of large C++ packages such as
    qtdeclarative by about 20%.
    
    * guix/build/gnu-build-system.scm (compress-debug-info): New procedure.
    (%standard-phases): Register it.
    * gnu/packages/commencement.scm (gnu-make-mesboot0)
    (gnu-make-mesboot, gnu-make-boot0) [outputs]: New field.
    
    Relates-to: #3617
    Change-Id: I9463f1299d97c20eee5d6f685a2f1abbb05545f0
---
 gnu/packages/commencement.scm   |  3 +++
 guix/build/gnu-build-system.scm | 48 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/commencement.scm b/gnu/packages/commencement.scm
index 47ebe48f51..5869131a95 100644
--- a/gnu/packages/commencement.scm
+++ b/gnu/packages/commencement.scm
@@ -682,6 +682,7 @@ MesCC-Tools), and finally M2-Planet.")
     (propagated-inputs '())
     (native-inputs `(("tcc" ,tcc-boot0)
                      ,@(%boot-gash-inputs)))
+    (outputs '("out"))                  ;avoid debug output processing
     (arguments
      `(#:implicit-inputs? #f
        #:guile ,%bootstrap-guile
@@ -1259,6 +1260,7 @@ ac_cv_c_float_format='IEEE (little-endian)'
     (supported-systems '("i686-linux" "x86_64-linux"))
     (inputs '())
     (propagated-inputs '())
+    (outputs '("out"))                  ;avoid debug output processing
     (arguments
      `(#:implicit-inputs? #f
        #:parallel-build? #f
@@ -1985,6 +1987,7 @@ exec " gcc "/bin/" program
     (outputs (delete "debug" (package-outputs gnu-make)))
     (source (bootstrap-origin (package-source gnu-make)))
     (name "make-boot0")
+    (outputs '("out"))                  ;avoid debug output processing
     (arguments
      `(#:guile ,%bootstrap-guile
        #:implicit-inputs? #f
diff --git a/guix/build/gnu-build-system.scm b/guix/build/gnu-build-system.scm
index 63bbeae605..09b03fb6a2 100644
--- a/guix/build/gnu-build-system.scm
+++ b/guix/build/gnu-build-system.scm
@@ -567,6 +567,52 @@ makefiles."
                                 strip-directories)))
                  outputs))))
 
+(define* (compress-debug-info
+          #:key parallel-build? target outputs
+          (objcopy-command (if target
+                               (string-append target "-objcopy")
+                               "objcopy"))
+          (dwz-command (which "dwz"))
+          #:allow-other-keys)
+  (define debug-output (assoc-ref outputs "debug"))
+  (when debug-output
+    (let* ((common-file (string-append debug-output
+                                       "/lib/debug/" (assoc-ref outputs "out")
+                                       "/common.debug"))
+           (shared-object-file?
+            (lambda (file)
+              (and (elf-file? file)
+                   (member (call-with-input-file file
+                             (compose elf-type parse-elf
+                                      get-bytevector-all))
+                           (list ET_EXEC ET_DYN)))))
+           ;; DWZ only operates on ELF shared object files.
+           (debug-files (find-files debug-output
+                                    (lambda (f st)
+                                      ;; Ignore symlinks.
+                                      (and (eq? 'regular (stat:type st))
+                                           (shared-object-file? f)))
+                                    #:stat lstat))
+           (debug-files-count (length debug-files)))
+      (unless (zero? debug-files-count)
+        (when (> debug-files-count 1)
+          (mkdir-p (dirname common-file)))
+        ;; Deduplicate debug symbols with DWZ.
+        (apply invoke dwz-command
+               "-j" (number->string (if parallel-build?
+                                        (parallel-job-count)
+                                        1))
+               `(,@(if (> debug-files-count 1)
+                       `("--multifile" ,common-file)
+                       '())
+                 ,@debug-files))
+        ;; Compress the debug sections with Zstd.
+        (for-each (lambda (f)
+                    (make-file-writable f)
+                    (invoke objcopy-command
+                            "--compress-debug-sections=zstd" f))
+                  debug-files)))))
+
 (define* (validate-runpath #:key
                            (validate-runpath? #t)
                            (elf-directories '("lib" "lib64" "libexec"
@@ -941,7 +987,7 @@ that traversing all the RUNPATH entries entails."
             patch-usr-bin-file
             patch-source-shebangs configure patch-generated-file-shebangs
             build check install
-            patch-shebangs strip
+            patch-shebangs strip compress-debug-info
             validate-runpath
             validate-documentation-location
             delete-info-dir-file

Reply via email to