Subject: [bug#54239] [PATCH v3 05/10] gnu: Add cross-clang.

From: Julien Lepiller <[email-masked]>
Date: Sat, 16 Jul 2022 21:48:02 +0200

* gnu/packages/llvm.scm (cross-clang): New variable.
---
 gnu/packages/llvm.scm | 169 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 168 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/llvm.scm b/gnu/packages/llvm.scm
index caf8264bef..560a64840a 100644
--- a/gnu/packages/llvm.scm
+++ b/gnu/packages/llvm.scm
@@ -48,6 +48,7 @@ (define-module (gnu packages llvm)
   #:use-module (guix git-download)
   #:use-module (guix memoization)
   #:use-module (guix utils)
+  #:use-module (guix build-system)
   #:use-module (guix build-system gnu)
   #:use-module (guix build-system cmake)
   #:use-module (guix build-system emacs)
@@ -75,9 +76,12 @@ (define-module (gnu packages llvm)
   #:use-module (gnu packages xml)
   #:use-module (srfi srfi-1)
   #:use-module (ice-9 match)
+  #:autoload (gnu packages cross-base) (cross-libc cross-gcc cross-binutils)
   #:export (make-lld-wrapper
             system->llvm-target
-            cross-llvm))
+            cross-llvm
+            cross-clang
+            clang-for-target))
 
 (define* (system->llvm-target #:optional
                               (system (or (and=> (%current-target-system)
@@ -1263,6 +1267,169 @@ (define-public clang-runtime clang-runtime-13)
 (define-public clang clang-13)
 (define-public clang-toolchain clang-toolchain-13)
 
+(define* (cross-clang target
+                      #:key
+                      (libc (cross-libc target))
+                      (xgcc (cross-gcc target
+                              #:xbinutils (cross-binutils target)
+                              #:libc (cross-libc target)))
+                      (clang clang))
+  "Return a cross-clang compiler for target."
+  (define cross-clang-aux
+    (mlambda (target libc xgcc clang)
+      (package
+        (inherit clang)
+        (name (string-append "clang-cross-" target))
+        (version (package-version clang))
+        ;; Support the same variables as clang, even in cross-compilation context.
+        ;; Clang does not make a difference between native and cross-compilation.
+        (search-paths
+          (list
+            (search-path-specification
+              (variable "CROSS_LIBRARY_PATH")
+              (files '("lib")))
+            (search-path-specification
+              (variable "CROSS_C_INCLUDE_PATH")
+              (files '("include")))
+            (search-path-specification
+              (variable "CROSS_CPLUS_INCLUDE_PATH")
+              (files '("include/c++" "include")))))
+        (native-search-paths '())
+        (arguments
+         (substitute-keyword-arguments (package-arguments clang)
+           ((#:configure-flags _)
+            #~(list "-DCLANG_INCLUDE_TESTS=True"
+                    (string-append "-DGCC_INSTALL_PREFIX="
+                                   (assoc-ref %build-inputs "cross-gcc-lib"))
+                    (string-append "-DC_INCLUDE_DIRS="
+                                   (assoc-ref %build-inputs "target-libc")
+                                   "/include")))
+           ((#:phases phases)
+            `(modify-phases ,phases
+               (add-after 'unpack 'add-missing-libdir
+                 (lambda _
+                   ;; cross-gcc installs its libraries in <target>/lib instead of
+                   ;; lib.
+                   (substitute* "lib/Driver/ToolChain.cpp"
+                     (("\"-L\"\\) \\+ LibPath\\)\\);")
+                      ,(string-append "\"-L\") + LibPath));
+  CmdArgs.push_back(Args.MakeArgString(StringRef(\"-L\") + "
+                                     "StringRef(GCC_INSTALL_PREFIX) + StringRef(\"/"
+                                     target "/lib\")));
+  CmdArgs.push_back(Args.MakeArgString(StringRef(\"-rpath=\") + "
+                                     "StringRef(GCC_INSTALL_PREFIX) + StringRef(\"/"
+                                     target "/lib\")));")))))
+               (add-after 'unpack 'support-cross-include-path
+                 (lambda _
+                   (substitute* ,(if (version>=? version "6.0")
+                                     "lib/Driver/ToolChains/Clang.cpp"
+                                     "lib/Driver/Tools.cpp")
+                     (("C_INCLUDE_PATH") "CROSS_C_INCLUDE_PATH")
+                     (("CPLUS_INCLUDE_PATH") "CROSS_CPLUS_INCLUDE_PATH"))))
+               (add-after 'unpack 'support-cross-library-path
+                 (lambda _
+                   ;; LIBRARY_PATH is only supported for native builds, but we still
+                   ;; need it (or CROSS_LIBRARY_PATH to be precise) when
+                   ;; cross-compiling
+                   ,(cond
+                      ((version>=? version "10")
+                       `(substitute* "lib/Driver/ToolChains/CommonArgs.cpp"
+                          (("LIBRARY_PATH\"")
+                           "LIBRARY_PATH\");
+  else
+    addDirectoryList(Args, CmdArgs, \"-L\", \"CROSS_LIBRARY_PATH\"")))
+                      ((version>=? version "6.0")
+                       `(substitute* "lib/Driver/ToolChains/CommonArgs.cpp"
+                          (("LIBRARY_PATH\"")
+                           "LIBRARY_PATH\");
+  } else {
+    addDirectoryList(Args, CmdArgs, \"-L\", \"CROSS_LIBRARY_PATH\"")))
+                      (else
+                        `(substitute* "lib/Driver/Tools.cpp"
+                          (("LIBRARY_PATH\"")
+                           "LIBRARY_PATH\");
+  else
+    addDirectoryList(Args, CmdArgs, \"-L\", \"CROSS_LIBRARY_PATH\""))))))
+               (replace 'set-glibc-file-names
+                 (lambda* (#:key inputs #:allow-other-keys)
+                   (let ((libc (assoc-ref inputs "target-libc"))
+                         (compiler-rt (assoc-ref inputs "clang-runtime"))
+                         (gcc (assoc-ref inputs "cross-gcc")))
+                     (setenv "LIBRARY_PATH"
+                             (string-append
+                               (assoc-ref inputs "libc") "/lib:" (getenv "LIBRARY_PATH")))
+                     ,@(cond
+                        ((version>=? version "6.0")
+                         `(;; Link to libclang_rt files from clang-runtime.
+                           (substitute* "lib/Driver/ToolChain.cpp"
+                             (("getDriver\\(\\)\\.ResourceDir")
+                              (string-append "\"" compiler-rt "\"")))
+
+                           ;; Make "LibDir" refer to <glibc>/lib so that it
+                           ;; uses the right dynamic linker file name.
+                           (substitute* "lib/Driver/ToolChains/Linux.cpp"
+                             (("(^[[:blank:]]+LibDir = ).*" _ declaration)
+                              (string-append declaration "\"" libc "/lib\";\n"))
+
+                             ;; Make clang look for libstdc++ in the right
+                             ;; location.
+                             (("LibStdCXXIncludePathCandidates\\[\\] = \\{")
+                              (string-append
+                               "LibStdCXXIncludePathCandidates[] = { \"" gcc
+                               "/include/c++\","))
+
+                             ;; Make sure libc's libdir is on the search path, to
+                             ;; allow crt1.o & co. to be found.
+                             (("@GLIBC_LIBDIR@")
+                              (string-append libc "/lib")))))
+                        (else
+                         `((substitute* "lib/Driver/Tools.cpp"
+                             ;; Patch the 'getLinuxDynamicLinker' function so that
+                             ;; it uses the right dynamic linker file name.
+                             (("/lib64/ld-linux-x86-64.so.2")
+                              (string-append libc ,(glibc-dynamic-linker))))
+
+                           ;; Link to libclang_rt files from clang-runtime.
+                           ;; This substitution needed slight adjustment in 3.8.
+                           ,@(if (version>=? version "3.8")
+                                 '((substitute* "lib/Driver/Tools.cpp"
+                                     (("TC\\.getDriver\\(\\)\\.ResourceDir")
+                                      (string-append "\"" compiler-rt "\""))))
+                                 '((substitute* "lib/Driver/ToolChain.cpp"
+                                     (("getDriver\\(\\)\\.ResourceDir")
+                                      (string-append "\"" compiler-rt "\"")))))
+
+                           ;; Make sure libc's libdir is on the search path, to
+                           ;; allow crt1.o & co. to be found.
+                           (substitute* "lib/Driver/ToolChains.cpp"
+                             (("@GLIBC_LIBDIR@")
+                              (string-append libc "/lib")))))))))))))
+        (inputs
+         `(("target-libc" ,libc)
+           ("cross-gcc-lib" ,xgcc "lib")
+           ("cross-gcc" ,xgcc)
+           ,@(package-inputs clang)))
+        (propagated-inputs
+          (modify-inputs (package-propagated-inputs clang)
+            (replace "clang-runtime"
+              (let ((base (car (assoc-ref (package-propagated-inputs clang)
+                                          "clang-runtime"))))
+                (package
+                  (inherit base)
+                  (build-system (build-system-with-target
+                                  (package-build-system base)
+                                  target)))))
+            (replace "llvm"
+              (cross-llvm
+                (car (assoc-ref (package-propagated-inputs clang) "llvm"))
+                target)))))))
+  (cross-clang-aux target libc xgcc clang))
+
+(define* (clang-for-target #:optional (clang clang))
+  (if (%current-target-system)
+      (cross-clang (%current-target-system) #:clang clang)
+      clang))
+
 (define-public llvm-for-rocm
   (package
     ;; Based on LLVM 14 as of v5.0.0
-- 
2.37.0