Subject: [PATCH 1/2] gnu: llvm: Move dynamic libraries to a separate "lib" output.

From: Pierre Neidhardt <[email-masked]>
Date: Tue, 28 Jul 2020 11:58:22 +0200

* gnu/packages/llvm.scm (llvm)[arguments]: Set configure-flags to build
a dynamic library bundle in the "lib" output.
Add phases to move the /bin and /include directories to the "out" output.

The goal of this change is to reduce the closure size of LLVM dependents.

- The dynamic library bundles saves a few dozen MiB over the separate dynamic
  libraries.

- Removing the /bin and the /include directories from the dependent input
  saves about 35 MiB for LLVM 10.
---
 gnu/packages/llvm.scm | 65 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 53 insertions(+), 12 deletions(-)

diff --git a/gnu/packages/llvm.scm b/gnu/packages/llvm.scm
index b7bc21ea6e..3e9d428b9f 100644
--- a/gnu/packages/llvm.scm
+++ b/gnu/packages/llvm.scm
@@ -99,7 +99,7 @@ as \"x86_64-linux\"."
        (base32
         "1pwgm6cr0xr5a0hrbqs1zvsvvjvy0yq1y47c96804wcs795s90yz"))))
     (build-system cmake-build-system)
-    (outputs '("out" "opt-viewer"))
+    (outputs '("out" "opt-viewer" "lib"))
     (native-inputs
      `(("python" ,python-2) ;bytes->str conversion in clang>=3.7 needs python-2
        ("perl"   ,perl)))
@@ -108,12 +108,18 @@ as \"x86_64-linux\"."
     (propagated-inputs
      `(("zlib" ,zlib)))                 ;to use output from llvm-config
     (arguments
-     `(#:configure-flags '("-DCMAKE_SKIP_BUILD_RPATH=FALSE"
-                           "-DCMAKE_BUILD_WITH_INSTALL_RPATH=FALSE"
-                           "-DBUILD_SHARED_LIBS:BOOL=TRUE"
-                           "-DLLVM_ENABLE_FFI:BOOL=TRUE"
-                           "-DLLVM_REQUIRES_RTTI=1" ; For some third-party utilities
-                           "-DLLVM_INSTALL_UTILS=ON") ; Needed for rustc.
+     `(#:configure-flags (list "-DCMAKE_SKIP_BUILD_RPATH=FALSE"
+                               "-DCMAKE_BUILD_WITH_INSTALL_RPATH=FALSE"
+                               ;; LLVM cannot enable BUILD_SHARED_LIBS with LLVM_LINK_LLVM_DYLIB.
+                               ;; "-DBUILD_SHARED_LIBS:BOOL=TRUE"
+                               "-DLLVM_BUILD_LLVM_DYLIB=ON"
+                               "-DLLVM_LINK_LLVM_DYLIB=ON"
+                               (string-append "-DCMAKE_INSTALL_PREFIX=" (assoc-ref %outputs "lib"))
+                               (string-append "-DCMAKE_INSTALL_RPATH=" (assoc-ref %outputs "lib")
+                                              "/lib")
+                               "-DLLVM_ENABLE_FFI:BOOL=TRUE"
+                               "-DLLVM_REQUIRES_RTTI=1" ; For some third-party utilities
+                               "-DLLVM_INSTALL_UTILS=ON") ; Needed for rustc.
 
        ;; Don't use '-g' during the build, to save space.
        #:build-type "Release"
@@ -128,14 +134,49 @@ as \"x86_64-linux\"."
              (setenv "LD_LIBRARY_PATH"
                      (string-append (getcwd) "/lib"))
              #t))
-         (add-after 'install 'install-opt-viewer
+         (add-after 'install 'install-bin
            (lambda* (#:key outputs #:allow-other-keys)
              (let* ((out (assoc-ref outputs "out"))
+                    (out-lib (string-append out "/lib"))
+                    (lib-output (assoc-ref outputs "lib"))
+                    (lib-bin (string-append lib-output "/bin")))
+               (mkdir-p out)
+               (rename-file (string-append lib-output "/bin")
+                            (string-append out "/bin"))
+               ;; llvm-config is required by most lib dependents.  It's only a
+               ;; few KiB, so it does not warrant a separate output.
+               (mkdir-p lib-bin)
+               (rename-file (string-append out "/bin/llvm-config")
+                            (string-append lib-bin "/llvm-config"))
+               (rename-file (string-append lib-output "/include")
+                            (string-append out "/include"))
+               (mkdir-p out-lib)
+               (if (file-exists? (string-append lib-output "/lib/cmake"))
+                   (rename-file (string-append lib-output "/lib/cmake")
+                                (string-append out-lib "/cmake"))
+                   ;; The cmake files change location in llvm 3.9.
+                   (begin
+                     (mkdir-p (string-append out "/share/llvm"))
+                     (rename-file (string-append lib-output "/share/llvm/cmake")
+                                  (string-append out "/share/llvm/cmake"))))
+               (for-each
+                (lambda (file)
+                  (rename-file file
+                               (string-append out-lib "/" (basename file))))
+                (find-files (string-append lib-output "/lib") "\\.a$"))
+               (for-each
+                (lambda (file)
+                  (symlink file
+                           (string-append out-lib "/" (basename file))))
+                (find-files (string-append lib-output "/lib") "\\.so")))
+             #t))
+         (add-after 'install 'install-opt-viewer
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let* ((lib-output (assoc-ref outputs "lib"))
                     (opt-viewer-out (assoc-ref outputs "opt-viewer"))
-                    (opt-viewer-share-dir (string-append opt-viewer-out "/share"))
-                    (opt-viewer-dir (string-append opt-viewer-share-dir "/opt-viewer")))
-               (mkdir-p opt-viewer-share-dir)
-               (rename-file (string-append out "/share/opt-viewer")
+                    (opt-viewer-dir (string-append opt-viewer-out "/share/opt-viewer")))
+               (mkdir-p (dirname opt-viewer-dir))
+               (rename-file (string-append lib-output "/share/opt-viewer")
                             opt-viewer-dir))
              #t)))))
     (home-page "https://www.llvm.org")

base-commit: 0e1428ac5dc3a7f1aa68988dd88885009e9706a6
-- 
2.27.0