Subject: [PATCH 3/4] build-system/cargo: Add a propagated-phases argument.

From: Jakub Kądziołka <[email-masked]>
Date: Thu, 25 Jun 2020 23:26:37 +0200

* guix/build-system/cargo.scm (package-propagated-phases, add-phases):
  New procedures.
  (lower): Collect phases from the crate closure.
---
 guix/build-system/cargo.scm | 39 +++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/guix/build-system/cargo.scm b/guix/build-system/cargo.scm
index 3f518343ec..d3ec97f7fd 100644
--- a/guix/build-system/cargo.scm
+++ b/guix/build-system/cargo.scm
@@ -149,6 +149,12 @@ to NAME and VERSION."
       propagated-native-inputs)
     (package-arguments p)))
 
+(define (package-propagated-phases p)
+  (apply
+    (lambda* (#:key (propagated-phases '()) #:allow-other-keys)
+      propagated-phases)
+    (package-arguments p)))
+
 (define (crate-closure inputs)
   "Return the closure of INPUTS when considering the 'cargo-inputs' and
 'cargod-dev-deps' edges.  Omit duplicate inputs, except for those
@@ -243,23 +249,43 @@ any dependent crates. This can be a benefits:
        (list label input)))
     crate-closure))
 
+(define (add-phases propagated-phases base-phases)
+  ;; TODO(rebuild-rust): This could be simpler if avoiding rebuilds wasn't a goal.
+  (if (null? propagated-phases)
+    base-phases
+    (let ((phase-list `(modify-phases %standard-phases ,@propagated-phases)))
+      (if base-phases
+        `(let ((%standard-phases ,phase-list)) ,base-phases)
+        phase-list))))
+
 (define* (lower name
-                #:key source inputs native-inputs outputs system target
+                #:key source inputs native-inputs outputs system target phases
                 (rust (default-rust))
                 (cargo-inputs '())
                 (cargo-development-inputs '())
                 (propagated-native-inputs '())
+                (propagated-phases '())
                 #:allow-other-keys
                 #:rest arguments)
   "Return a bag for NAME."
 
   (define private-keywords
     '(#:source #:target #:rust #:inputs #:native-inputs #:outputs
+      #:phases #:propagated-phases
       #:cargo-inputs #:cargo-development-inputs #:propagated-native-inputs))
 
   (and (not target) ;; TODO: support cross-compilation
-       (let ((closure (crate-closure
-                        (append cargo-inputs cargo-development-inputs))))
+       (let* ((closure (crate-closure
+                         (append cargo-inputs cargo-development-inputs)))
+              (closure-phases
+                (append-map
+                  (compose package-propagated-phases second)
+                  ;; Make sure the leaves of the dependency graph come first,
+                  ;; such that their dependents may refer to the names of
+                  ;; the phases
+                  (reverse closure)))
+              (closure-phases (append closure-phases propagated-phases))
+              (phases (add-phases closure-phases phases)))
          (bag
            (name name)
            (system system)
@@ -284,7 +310,12 @@ any dependent crates. This can be a benefits:
                            ,@native-inputs))
            (outputs outputs)
            (build cargo-build)
-           (arguments (strip-keyword-arguments private-keywords arguments))))))
+           (arguments
+             (let ((provided-arguments
+                     (strip-keyword-arguments private-keywords arguments)))
+               (if phases
+                 (cons* #:phases phases provided-arguments)
+                 provided-arguments)))))))
 
 (define cargo-build-system
   (build-system
-- 
2.26.2