Subject: [bug#54239] [PATCH 3/5] gnu: libcxx: Allow cross-compilation.

From: Julien Lepiller <[email-masked]>
Date: Thu, 3 Mar 2022 17:07:45 +0100

* gnu/packages/llvm.scm (libcxx)[native-inputs]: Use cross-clang for
cross-compilation.  Move llvm to...
[inputs]: ...here.
[arguments]: adjust CROSS_INCLUDE_PATH when cross-compiling.
(libcxx-6)[native-inputs]: Use cross-clang for cross-compilation.  Move
llvm to...
[inputs]: ...here.
---
 gnu/packages/llvm.scm | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/gnu/packages/llvm.scm b/gnu/packages/llvm.scm
index c02f8e5c7a..9b5e495d08 100644
--- a/gnu/packages/llvm.scm
+++ b/gnu/packages/llvm.scm
@@ -1026,7 +1026,7 @@ (define-public clang clang-13)
 (define-public clang-toolchain clang-toolchain-13)
 
 (define* (cross-clang target
-                      #:optional
+                      #:key
                       (libc (cross-libc target))
                       (xgcc (cross-gcc target
                               #:xbinutils (cross-binutils target)
@@ -1343,8 +1343,9 @@ (define-public libcxx
      `(#:phases
        (modify-phases (@ (guix build cmake-build-system) %standard-phases)
          (add-after 'set-paths 'adjust-CPLUS_INCLUDE_PATH
-           (lambda* (#:key inputs #:allow-other-keys)
-             (let ((gcc (assoc-ref inputs  "gcc")))
+           (lambda* (#:key inputs native-inputs #:allow-other-keys)
+             (let ((gcc (or (assoc-ref (or native-inputs inputs) "gcc")))
+                   (cross-gcc (assoc-ref native-inputs "cross-gcc")))
                ;; Hide GCC's C++ headers so that they do not interfere with
                ;; the ones we are attempting to build.
                (setenv "CPLUS_INCLUDE_PATH"
@@ -1352,12 +1353,23 @@ (define-public libcxx
                                             (string-split (getenv "CPLUS_INCLUDE_PATH")
                                                           #\:))
                                     ":"))
+               (when cross-gcc
+                 (setenv "CROSS_CPLUS_INCLUDE_PATH"
+                         (string-join (delete (string-append cross-gcc "/include/c++")
+                                              (string-split (getenv "CPLUS_INCLUDE_PATH")
+                                                            #\:))
+                                      ":"))
+                 (format #t
+                         "environment variable `CROSS_CPLUS_INCLUDE_PATH' changed to ~a~%"
+                         (getenv "CROSS_CPLUS_INCLUDE_PATH")))
                (format #t
                        "environment variable `CPLUS_INCLUDE_PATH' changed to ~a~%"
-                       (getenv "CPLUS_INCLUDE_PATH"))
-               #t))))))
+                       (getenv "CPLUS_INCLUDE_PATH"))))))))
+    (inputs (list llvm-9))
     (native-inputs
-     (list clang llvm))
+     (list (if (%current-target-system)
+               (cross-clang (%current-target-system) #:clang clang-9)
+               clang-9)))
     (home-page "https://libcxx.llvm.org")
     (synopsis "C++ standard library")
     (description
@@ -1378,8 +1390,11 @@ (define-public libcxx-6
        (sha256
         (base32
          "0rzw4qvxp6qx4l4h9amrq02gp7hbg8lw4m0sy3k60f50234gnm3n"))))
+    (inputs (list llvm-6))
     (native-inputs
-     (list clang-6 llvm-6))))
+     (list (if (%current-target-system)
+               (cross-clang (%current-target-system) #:clang clang-6)
+               clang-6)))))
 
 (define-public libcxxabi-6
   (package
-- 
2.34.0