Subject: [bug#54239] [PATCH v2 4/5] gnu: libcxxabi-6: Allow cross-compilation.

From: Julien Lepiller <[email-masked]>
Date: Fri, 4 Mar 2022 10:59:52 +0100

* gnu/packages/llvm.scm (libcxxabi-6)[native-inputs]: Use
clang-for-target for cross-compilation.  Move llvm and libcxx to...
[inputs]: ...here.
[arguments]<adjust-CPLUS_INCLUDE_PATH>: Also adjust
CROSS_CPLUS_INCLUDE_PATH when necessary.
---
 gnu/packages/llvm.scm | 40 ++++++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/gnu/packages/llvm.scm b/gnu/packages/llvm.scm
index 137bc99f35..e00e92aa8c 100644
--- a/gnu/packages/llvm.scm
+++ b/gnu/packages/llvm.scm
@@ -1433,19 +1433,43 @@ (define-public libcxxabi-6
        (modify-phases (@ (guix build cmake-build-system) %standard-phases)
          (add-after 'unpack 'chdir
            (lambda _ (chdir "libcxxabi")))
-         (add-after 'set-paths 'adjust-CPLUS_INCLUDE_PATH
-           (lambda* (#:key inputs #:allow-other-keys)
-             (let ((gcc (assoc-ref inputs  "gcc")))
+         (add-after 'unpack 'adjust-CPLUS_INCLUDE_PATH
+           (lambda* (#:key inputs native-inputs #:allow-other-keys)
+             (define (delete* what lst)
+               (if (null? what)
+                   lst
+                   (delete* (cdr what) (delete (car what) lst))))
+
+             (let ((gcc (or (assoc-ref inputs  "gcc") (assoc-ref native-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"
                        (string-join
                         (cons (string-append
                                (assoc-ref inputs "libcxx") "/include/c++/v1")
-                              (delete (string-append gcc "/include/c++")
-                                      (string-split (getenv "CPLUS_INCLUDE_PATH")
-                                                    #\:)))
+                              (delete*
+                                `(,(string-append gcc "/include/c++")
+                                  ,@(if cross-gcc
+                                        `(,(string-append cross-gcc "/include/c++"))
+                                        '()))
+                                (string-split (getenv "CPLUS_INCLUDE_PATH")
+                                              #\:)))
                         ":"))
+               (when cross-gcc
+                 (setenv "CROSS_CPLUS_INCLUDE_PATH"
+                         (string-join
+                          (cons (string-append
+                                  (assoc-ref inputs "libcxx") "/include/c++/v1")
+                                (delete*
+                                  (list (string-append cross-gcc "/include/c++")
+                                        (string-append gcc "/include/c++"))
+                                  (string-split (getenv "CROSS_CPLUS_INCLUDE_PATH")
+                                                #\:)))
+                          ":"))
+                 (format #true
+                         "environment variable `CROSS_CPLUS_INCLUDE_PATH' changed to ~a~%"
+                         (getenv "CROSS_CPLUS_INCLUDE_PATH")))
                (format #true
                        "environment variable `CPLUS_INCLUDE_PATH' changed to ~a~%"
                        (getenv "CPLUS_INCLUDE_PATH")))))
@@ -1455,8 +1479,8 @@ (define-public libcxxabi-6
                                  (assoc-ref outputs "out") "/include")))
                (install-file "../libcxxabi/include/__cxxabi_config.h" include-dir)
                (install-file "../libcxxabi/include/cxxabi.h" include-dir)))))))
-    (native-inputs
-     (list clang-6 llvm-6 libcxx-6))
+    (inputs (list llvm-6 libcxx-6))
+    (native-inputs (list (clang-for-target clang-6)))
     (home-page "https://libcxxabi.llvm.org")
     (synopsis "C++ standard library support")
     (description
-- 
2.34.0