gnu: esbuild: Update to 0.11.14.
[jackhill/guix/guix.git] / guix / gexp.scm
index 8e80d4a..afb9357 100644 (file)
             file-like?
             lower-object
 
-            lower-inputs
-
             &gexp-error
             gexp-error?
             &gexp-input-error
@@ -759,19 +757,28 @@ attribute that is traversed."
        (append (let ((attribute (self-attribute gexp)))
                  (validate gexp attribute)
                  attribute)
-               (append-map (match-lambda
-                             (($ <gexp-input> (? gexp? exp))
-                              (gexp-attribute exp self-attribute
-                                              #:validate validate))
-                             (($ <gexp-input> (lst ...))
-                              (append-map (lambda (item)
-                                            (gexp-attribute item self-attribute
-                                                            #:validate
-                                                            validate))
-                                          lst))
-                             (_
-                              '()))
-                           (gexp-references gexp)))
+               (reverse
+                (fold (lambda (input result)
+                        (match input
+                          (($ <gexp-input> (? gexp? exp))
+                           (append (gexp-attribute exp self-attribute
+                                                   #:validate validate)
+                                   result))
+                          (($ <gexp-input> (lst ...))
+                           (fold/tree (lambda (obj result)
+                                        (match obj
+                                          ((? gexp? exp)
+                                           (append (gexp-attribute exp self-attribute
+                                                                   #:validate validate)
+                                                   result))
+                                          (_
+                                           result)))
+                                      result
+                                      lst))
+                          (_
+                           result)))
+                      '()
+                      (gexp-references gexp))))
        equal?)
       '()))                                       ;plain Scheme data type
 
@@ -828,8 +835,7 @@ list."
     (one-of symbol? string? keyword? pair? null? array?
             number? boolean? char?)))
 
-(define* (lower-inputs inputs
-                       #:key system target)
+(define (lower-inputs inputs system target)
   "Turn any object from INPUTS into a derivation input for SYSTEM or a store
 item (a \"source\"); return the corresponding input list as a monadic value.
 When TARGET is true, use it as the cross-compilation target triplet."
@@ -869,15 +875,14 @@ corresponding <derivation-input> or store item."
   (define tuple->gexp-input
     (match-lambda
       ((thing)
-       (%gexp-input thing "out" #t))
+       (%gexp-input thing "out" (not target)))
       ((thing output)
-       (%gexp-input thing output #t))))
+       (%gexp-input thing output (not target)))))
 
   (match graphs
     (((file-names . inputs) ...)
      (mlet %store-monad ((inputs (lower-inputs (map tuple->gexp-input inputs)
-                                               #:system system
-                                               #:target target)))
+                                               system target)))
        (return (map cons file-names inputs))))))
 
 (define* (lower-references lst #:key system target)
@@ -931,6 +936,7 @@ second element is the derivation to compile them."
   (mcached equal?
            (mlet %store-monad ((modules  (if (pair? modules)
                                              (imported-modules modules
+                                                               #:guile guile
                                                                #:system system
                                                                #:module-path module-path)
                                              (return #f)))
@@ -1006,16 +1012,9 @@ derivations--e.g., code evaluated for its side effects."
                        (guile     (if guile-for-build
                                       (return guile-for-build)
                                       (default-guile-derivation system)))
-                       (normals  (lower-inputs (gexp-inputs exp)
-                                               #:system system
-                                               #:target target))
-                       (natives  (lower-inputs (gexp-native-inputs exp)
-                                               #:system system
-                                               #:target #f))
-                       (inputs -> (append normals natives))
-                       (sexp     (gexp->sexp exp
-                                             #:system system
-                                             #:target target))
+                       (inputs   (lower-inputs (gexp-inputs exp)
+                                               system target))
+                       (sexp     (gexp->sexp exp system target))
                        (extensions -> (gexp-extensions exp))
                        (exts     (mapm %store-monad
                                        (lambda (obj)
@@ -1218,41 +1217,60 @@ The other arguments are as for 'derivation'."
                       #:substitutable? substitutable?
                       #:properties properties))))
 
-(define* (gexp-inputs exp #:key native?)
-  "Return the list of <gexp-input> for EXP.  When NATIVE? is true, return only
-native references; otherwise, return only non-native references."
+(define (fold/tree proc seed lst)
+  "Like 'fold', but recurse into sub-lists of LST and accept improper lists."
+  (let loop ((obj lst)
+             (result seed))
+    (match obj
+      ((head . tail)
+       (loop tail (loop head result)))
+      (_
+       (proc obj result)))))
+
+(define (gexp-inputs exp)
+  "Return the list of <gexp-input> for EXP."
+  (define set-gexp-input-native?
+    (match-lambda
+      (($ <gexp-input> thing output)
+       (%gexp-input thing output #t))))
+
+  (define (interesting? obj)
+    (or (file-like? obj)
+        (and (string? obj) (direct-store-path? obj))))
+
   (define (add-reference-inputs ref result)
     (match ref
       (($ <gexp-input> (? gexp? exp) _ #t)
-       (if native?
-           (append (gexp-inputs exp)
-                   (gexp-inputs exp #:native? #t)
-                   result)
-           result))
-      (($ <gexp-input> (? gexp? exp) _ #f)
-       (append (gexp-inputs exp #:native? native?)
+       (append (map set-gexp-input-native? (gexp-inputs exp))
                result))
+      (($ <gexp-input> (? gexp? exp) _ #f)
+       (append (gexp-inputs exp) result))
       (($ <gexp-input> (? string? str))
        (if (direct-store-path? str)
            (cons ref result)
            result))
       (($ <gexp-input> (? struct? thing) output n?)
-       (if (and (eqv? n? native?) (lookup-compiler thing))
+       (if (lookup-compiler thing)
            ;; THING is a derivation, or a package, or an origin, etc.
            (cons ref result)
            result))
-      (($ <gexp-input> (lst ...) output n?)
-       (fold-right add-reference-inputs result
-                   ;; XXX: For now, automatically convert LST to a list of
-                   ;; gexp-inputs.  Inherit N?.
-                   (map (match-lambda
-                          ((? gexp-input? x)
-                           (%gexp-input (gexp-input-thing x)
-                                        (gexp-input-output x)
-                                        n?))
-                          (x
-                           (%gexp-input x "out" n?)))
-                        lst)))
+      (($ <gexp-input> (? pair? lst) output n?)
+       ;; XXX: Scan LST for inputs.  Inherit N?.
+       (fold/tree (lambda (obj result)
+                    (match obj
+                      ((? gexp-input? x)
+                       (cons (%gexp-input (gexp-input-thing x)
+                                          (gexp-input-output x)
+                                          n?)
+                             result))
+                      ((? interesting? x)
+                       (cons (%gexp-input x "out" n?) result))
+                      ((? gexp? x)
+                       (append (gexp-inputs x) result))
+                      (_
+                       result)))
+                  result
+                  lst))
       (_
        ;; Ignore references to other kinds of objects.
        result)))
@@ -1261,9 +1279,6 @@ native references; otherwise, return only non-native references."
               '()
               (gexp-references exp)))
 
-(define gexp-native-inputs
-  (cut gexp-inputs <> #:native? #t))
-
 (define (gexp-outputs exp)
   "Return the outputs referred to by EXP as a list of strings."
   (define (add-reference-output ref result)
@@ -1272,24 +1287,22 @@ native references; otherwise, return only non-native references."
        (cons name result))
       (($ <gexp-input> (? gexp? exp))
        (append (gexp-outputs exp) result))
-      (($ <gexp-input> (lst ...) output native?)
-       ;; XXX: Automatically convert LST.
-       (add-reference-output (map (match-lambda
-                                   ((? gexp-input? x) x)
-                                   (x (%gexp-input x "out" native?)))
-                                  lst)
-                             result))
-      ((lst ...)
-       (fold-right add-reference-output result lst))
+      (($ <gexp-input> (? pair? lst))
+       ;; XXX: Scan LST for outputs.
+       (fold/tree (lambda (obj result)
+                    (match obj
+                      (($ <gexp-output> name) (cons name result))
+                      ((? gexp? x) (append (gexp-outputs x) result))
+                      (_ result)))
+                  result
+                  lst))
       (_
        result)))
 
   (delete-duplicates
-   (add-reference-output (gexp-references exp) '())))
+   (fold add-reference-output '() (gexp-references exp))))
 
-(define* (gexp->sexp exp #:key
-                     (system (%current-system))
-                     (target (%current-target-system)))
+(define (gexp->sexp exp system target)
   "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
 and in the current monad setting (system type, etc.)"
   (define* (reference->sexp ref #:optional native?)
@@ -1302,17 +1315,19 @@ and in the current monad setting (system type, etc.)"
          (return `((@ (guile) getenv) ,output)))
         (($ <gexp-input> (? gexp? exp) output n?)
          (gexp->sexp exp
-                     #:system system
-                     #:target (if (or n? native?) #f target)))
+                     system (if (or n? native?) #f target)))
         (($ <gexp-input> (refs ...) output n?)
          (mapm %store-monad
                (lambda (ref)
                  ;; XXX: Automatically convert REF to an gexp-input.
-                 (reference->sexp
-                  (if (gexp-input? ref)
-                      ref
-                      (%gexp-input ref "out" n?))
-                  (or n? native?)))
+                 (if (or (symbol? ref) (number? ref)
+                         (boolean? ref) (null? ref) (array? ref))
+                     (return ref)
+                     (reference->sexp
+                      (if (gexp-input? ref)
+                          ref
+                          (%gexp-input ref "out" n?))
+                      (or n? native?))))
                refs))
         (($ <gexp-input> (? struct? thing) output n?)
          (let ((target (if (or n? native?) #f target)))
@@ -1694,6 +1709,7 @@ TARGET, a GNU triplet."
     ;; TODO: Pass MODULES as an environment variable.
     (gexp->derivation name build
                       #:system system
+                      #:target target
                       #:guile-for-build guile
                       #:local-build? #t
                       #:env-vars
@@ -1719,21 +1735,26 @@ TARGET, a GNU triplet."
               'guile-3.0))
 
 (define* (load-path-expression modules #:optional (path %load-path)
-                               #:key (extensions '()) system target)
+                               #:key (extensions '()) system target
+                               (guile (default-guile)))
   "Return as a monadic value a gexp that sets '%load-path' and
 '%load-compiled-path' to point to MODULES, a list of module names.  MODULES
-are searched for in PATH.  Return #f when MODULES and EXTENSIONS are empty."
+are searched for in PATH.  Return #f when MODULES and EXTENSIONS are empty.
+Assume MODULES are compiled with GUILE."
   (if (and (null? modules) (null? extensions))
       (with-monad %store-monad
         (return #f))
-      (mlet %store-monad ((modules  (imported-modules modules
-                                                      #:module-path path
-                                                      #:system system))
-                          (compiled (compiled-modules modules
-                                                      #:extensions extensions
-                                                      #:module-path path
-                                                      #:system system
-                                                      #:target target)))
+      (mlet* %store-monad ((guile    (lower-object guile system #:target #f))
+                           (compiled (compiled-modules modules
+                                                       #:guile guile
+                                                       #:extensions extensions
+                                                       #:module-path path
+                                                       #:system system
+                                                       #:target target))
+                           (modules  (imported-modules modules
+                                                       #:guile guile
+                                                       #:module-path path
+                                                       #:system system)))
         (return
          (gexp (eval-when (expand load eval)
                  ;; Augment the load paths and delete duplicates.  Do that
@@ -1779,10 +1800,13 @@ imported modules in its search path.  Look up EXP's modules in MODULE-PATH."
                        (set-load-path
                         (load-path-expression (gexp-modules exp)
                                               module-path
+                                              #:guile guile
                                               #:extensions
                                               (gexp-extensions exp)
                                               #:system system
-                                              #:target target)))
+                                              #:target target))
+                       (guile-for-build
+                        (lower-object guile system #:target #f)))
     (gexp->derivation name
                       (gexp
                        (call-with-output-file (ungexp output)
@@ -1805,6 +1829,7 @@ imported modules in its search path.  Look up EXP's modules in MODULE-PATH."
                       #:system system
                       #:target target
                       #:module-path module-path
+                      #:guile-for-build guile-for-build
 
                       ;; These derivations are not worth offloading or
                       ;; substituting.