gnu: jsoncpp: Update to 1.9.0.
[guix.git] / guix / gexp.scm
blobce48d8d001ebc451f74c6ba74457dc675701e2e8
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix gexp)
22   #:use-module (guix store)
23   #:use-module (guix monads)
24   #:use-module (guix derivations)
25   #:use-module (guix grafts)
26   #:use-module (guix utils)
27   #:use-module (rnrs bytevectors)
28   #:use-module (srfi srfi-1)
29   #:use-module (srfi srfi-9)
30   #:use-module (srfi srfi-9 gnu)
31   #:use-module (srfi srfi-26)
32   #:use-module (srfi srfi-34)
33   #:use-module (srfi srfi-35)
34   #:use-module (ice-9 match)
35   #:export (gexp
36             gexp?
37             with-imported-modules
38             with-extensions
40             gexp-input
41             gexp-input?
42             gexp-input-thing
43             gexp-input-output
44             gexp-input-native?
46             local-file
47             local-file?
48             local-file-file
49             local-file-absolute-file-name
50             local-file-name
51             local-file-recursive?
53             plain-file
54             plain-file?
55             plain-file-name
56             plain-file-content
58             computed-file
59             computed-file?
60             computed-file-name
61             computed-file-gexp
62             computed-file-options
64             program-file
65             program-file?
66             program-file-name
67             program-file-gexp
68             program-file-guile
69             program-file-module-path
71             scheme-file
72             scheme-file?
73             scheme-file-name
74             scheme-file-gexp
76             file-append
77             file-append?
78             file-append-base
79             file-append-suffix
81             load-path-expression
82             gexp-modules
84             lower-gexp
85             lowered-gexp?
86             lowered-gexp-sexp
87             lowered-gexp-inputs
88             lowered-gexp-guile
89             lowered-gexp-load-path
90             lowered-gexp-load-compiled-path
92             gexp->derivation
93             gexp->file
94             gexp->script
95             text-file*
96             mixed-text-file
97             file-union
98             directory-union
99             imported-files
100             imported-modules
101             compiled-modules
103             define-gexp-compiler
104             gexp-compiler?
105             file-like?
106             lower-object
108             lower-inputs
110             &gexp-error
111             gexp-error?
112             &gexp-input-error
113             gexp-input-error?
114             gexp-error-invalid-input))
116 ;;; Commentary:
118 ;;; This module implements "G-expressions", or "gexps".  Gexps are like
119 ;;; S-expressions (sexps), with two differences:
121 ;;;   1. References (un-quotations) to derivations or packages in a gexp are
122 ;;;      replaced by the corresponding output file name; in addition, the
123 ;;;      'ungexp-native' unquote-like form allows code to explicitly refer to
124 ;;;      the native code of a given package, in case of cross-compilation;
126 ;;;   2. Gexps embed information about the derivations they refer to.
128 ;;; Gexps make it easy to write to files Scheme code that refers to store
129 ;;; items, or to write Scheme code to build derivations.
131 ;;; Code:
133 ;; "G expressions".
134 (define-record-type <gexp>
135   (make-gexp references modules extensions proc)
136   gexp?
137   (references gexp-references)                    ;list of <gexp-input>
138   (modules    gexp-self-modules)                  ;list of module names
139   (extensions gexp-self-extensions)               ;list of lowerable things
140   (proc       gexp-proc))                         ;procedure
142 (define (write-gexp gexp port)
143   "Write GEXP on PORT."
144   (display "#<gexp " port)
146   ;; Try to write the underlying sexp.  Now, this trick doesn't work when
147   ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
148   ;; tries to use 'append' on that, which fails with wrong-type-arg.
149   (false-if-exception
150    (write (apply (gexp-proc gexp)
151                  (gexp-references gexp))
152           port))
153   (format port " ~a>"
154           (number->string (object-address gexp) 16)))
156 (set-record-type-printer! <gexp> write-gexp)
160 ;;; Methods.
163 ;; Compiler for a type of objects that may be introduced in a gexp.
164 (define-record-type <gexp-compiler>
165   (gexp-compiler type lower expand)
166   gexp-compiler?
167   (type       gexp-compiler-type)                 ;record type descriptor
168   (lower      gexp-compiler-lower)
169   (expand     gexp-compiler-expand))              ;#f | DRV -> sexp
171 (define-condition-type &gexp-error &error
172   gexp-error?)
174 (define-condition-type &gexp-input-error &gexp-error
175   gexp-input-error?
176   (input gexp-error-invalid-input))
179 (define %gexp-compilers
180   ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
181   (make-hash-table 20))
183 (define (default-expander thing obj output)
184   "This is the default expander for \"things\" that appear in gexps.  It
185 returns its output file name of OBJ's OUTPUT."
186   (match obj
187     ((? derivation? drv)
188      (derivation->output-path drv output))
189     ((? string? file)
190      file)))
192 (define (register-compiler! compiler)
193   "Register COMPILER as a gexp compiler."
194   (hashq-set! %gexp-compilers
195               (gexp-compiler-type compiler) compiler))
197 (define (lookup-compiler object)
198   "Search for a compiler for OBJECT.  Upon success, return the three argument
199 procedure to lower it; otherwise return #f."
200   (and=> (hashq-ref %gexp-compilers (struct-vtable object))
201          gexp-compiler-lower))
203 (define (file-like? object)
204   "Return #t if OBJECT leads to a file in the store once unquoted in a
205 G-expression; otherwise return #f."
206   (and (struct? object) (->bool (lookup-compiler object))))
208 (define (lookup-expander object)
209   "Search for an expander for OBJECT.  Upon success, return the three argument
210 procedure to expand it; otherwise return #f."
211   (and=> (hashq-ref %gexp-compilers (struct-vtable object))
212          gexp-compiler-expand))
214 (define* (lower-object obj
215                        #:optional (system (%current-system))
216                        #:key target)
217   "Return as a value in %STORE-MONAD the derivation or store item
218 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
219 OBJ must be an object that has an associated gexp compiler, such as a
220 <package>."
221   (match (lookup-compiler obj)
222     (#f
223      (raise (condition (&gexp-input-error (input obj)))))
224     (lower
225      ;; Cache in STORE the result of lowering OBJ.
226      (mlet %store-monad ((graft? (grafting?)))
227        (mcached (let ((lower (lookup-compiler obj)))
228                   (lower obj system target))
229                 obj
230                 system target graft?)))))
232 (define-syntax define-gexp-compiler
233   (syntax-rules (=> compiler expander)
234     "Define NAME as a compiler for objects matching PREDICATE encountered in
235 gexps.
237 In the simplest form of the macro, BODY must return a derivation for PARAM, an
238 object that matches PREDICATE, for SYSTEM and TARGET (the latter of which is
239 #f except when cross-compiling.)
241 The more elaborate form allows you to specify an expander:
243   (define-gexp-compiler something something?
244     compiler => (lambda (param system target) ...)
245     expander => (lambda (param drv output) ...))
247 The expander specifies how an object is converted to its sexp representation."
248     ((_ (name (param record-type) system target) body ...)
249      (define-gexp-compiler name record-type
250        compiler => (lambda (param system target) body ...)
251        expander => default-expander))
252     ((_ name record-type
253         compiler => compile
254         expander => expand)
255      (begin
256        (define name
257          (gexp-compiler record-type compile expand))
258        (register-compiler! name)))))
260 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
261   ;; Derivations are the lowest-level representation, so this is the identity
262   ;; compiler.
263   (with-monad %store-monad
264     (return drv)))
268 ;;; File declarations.
271 ;; A local file name.  FILE is the file name the user entered, which can be a
272 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
273 ;; absolute file name.  We keep it in a promise to compute it lazily and avoid
274 ;; repeated 'stat' calls.
275 (define-record-type <local-file>
276   (%%local-file file absolute name recursive? select?)
277   local-file?
278   (file       local-file-file)                    ;string
279   (absolute   %local-file-absolute-file-name)     ;promise string
280   (name       local-file-name)                    ;string
281   (recursive? local-file-recursive?)              ;Boolean
282   (select?    local-file-select?))                ;string stat -> Boolean
284 (define (true file stat) #t)
286 (define* (%local-file file promise #:optional (name (basename file))
287                       #:key recursive? (select? true))
288   ;; This intermediate procedure is part of our ABI, but the underlying
289   ;; %%LOCAL-FILE is not.
290   (%%local-file file promise name recursive? select?))
292 (define (absolute-file-name file directory)
293   "Return the canonical absolute file name for FILE, which lives in the
294 vicinity of DIRECTORY."
295   (canonicalize-path
296    (cond ((string-prefix? "/" file) file)
297          ((not directory) file)
298          ((string-prefix? "/" directory)
299           (string-append directory "/" file))
300          (else file))))
302 (define-syntax local-file
303   (lambda (s)
304     "Return an object representing local file FILE to add to the store; this
305 object can be used in a gexp.  If FILE is a relative file name, it is looked
306 up relative to the source file where this form appears.  FILE will be added to
307 the store under NAME--by default the base name of FILE.
309 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
310 designates a flat file and RECURSIVE? is true, its contents are added, and its
311 permission bits are kept.
313 When RECURSIVE? is true, call (SELECT?  FILE STAT) for each directory entry,
314 where FILE is the entry's absolute file name and STAT is the result of
315 'lstat'; exclude entries for which SELECT? does not return true.
317 This is the declarative counterpart of the 'interned-file' monadic procedure.
318 It is implemented as a macro to capture the current source directory where it
319 appears."
320     (syntax-case s ()
321       ((_ file rest ...)
322        #'(%local-file file
323                       (delay (absolute-file-name file (current-source-directory)))
324                       rest ...))
325       ((_)
326        #'(syntax-error "missing file name"))
327       (id
328        (identifier? #'id)
329        ;; XXX: We could return #'(lambda (file . rest) ...).  However,
330        ;; (syntax-source #'id) is #f so (current-source-directory) would not
331        ;; work.  Thus, simply forbid this form.
332        #'(syntax-error
333           "'local-file' is a macro and cannot be used like this")))))
335 (define (local-file-absolute-file-name file)
336   "Return the absolute file name for FILE, a <local-file> instance.  A
337 'system-error' exception is raised if FILE could not be found."
338   (force (%local-file-absolute-file-name file)))
340 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
341   ;; "Compile" FILE by adding it to the store.
342   (match file
343     (($ <local-file> file (= force absolute) name recursive? select?)
344      ;; Canonicalize FILE so that if it's a symlink, it is resolved.  Failing
345      ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
346      ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
347      ;; just throw an error, both of which are inconvenient.
348      (interned-file absolute name
349                     #:recursive? recursive? #:select? select?))))
351 (define-record-type <plain-file>
352   (%plain-file name content references)
353   plain-file?
354   (name        plain-file-name)                   ;string
355   (content     plain-file-content)                ;string or bytevector
356   (references  plain-file-references))            ;list (currently unused)
358 (define (plain-file name content)
359   "Return an object representing a text file called NAME with the given
360 CONTENT (a string) to be added to the store.
362 This is the declarative counterpart of 'text-file'."
363   ;; XXX: For now just ignore 'references' because it's not clear how to use
364   ;; them in a declarative context.
365   (%plain-file name content '()))
367 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
368   ;; "Compile" FILE by adding it to the store.
369   (match file
370     (($ <plain-file> name (and (? string?) content) references)
371      (text-file name content references))
372     (($ <plain-file> name (and (? bytevector?) content) references)
373      (binary-file name content references))))
375 (define-record-type <computed-file>
376   (%computed-file name gexp guile options)
377   computed-file?
378   (name       computed-file-name)                 ;string
379   (gexp       computed-file-gexp)                 ;gexp
380   (guile      computed-file-guile)                ;<package>
381   (options    computed-file-options))             ;list of arguments
383 (define* (computed-file name gexp
384                         #:key guile (options '(#:local-build? #t)))
385   "Return an object representing the store item NAME, a file or directory
386 computed by GEXP.  OPTIONS is a list of additional arguments to pass
387 to 'gexp->derivation'.
389 This is the declarative counterpart of 'gexp->derivation'."
390   (%computed-file name gexp guile options))
392 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
393                                               system target)
394   ;; Compile FILE by returning a derivation whose build expression is its
395   ;; gexp.
396   (match file
397     (($ <computed-file> name gexp guile options)
398      (if guile
399          (mlet %store-monad ((guile (lower-object guile system
400                                                   #:target target)))
401            (apply gexp->derivation name gexp #:guile-for-build guile
402                   #:system system #:target target options))
403          (apply gexp->derivation name gexp
404                 #:system system #:target target options)))))
406 (define-record-type <program-file>
407   (%program-file name gexp guile path)
408   program-file?
409   (name       program-file-name)                  ;string
410   (gexp       program-file-gexp)                  ;gexp
411   (guile      program-file-guile)                 ;package
412   (path       program-file-module-path))          ;list of strings
414 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
415   "Return an object representing the executable store item NAME that runs
416 GEXP.  GUILE is the Guile package used to execute that script.  Imported
417 modules of GEXP are looked up in MODULE-PATH.
419 This is the declarative counterpart of 'gexp->script'."
420   (%program-file name gexp guile module-path))
422 (define-gexp-compiler (program-file-compiler (file <program-file>)
423                                              system target)
424   ;; Compile FILE by returning a derivation that builds the script.
425   (match file
426     (($ <program-file> name gexp guile module-path)
427      (gexp->script name gexp
428                    #:module-path module-path
429                    #:guile (or guile (default-guile))))))
431 (define-record-type <scheme-file>
432   (%scheme-file name gexp splice?)
433   scheme-file?
434   (name       scheme-file-name)                  ;string
435   (gexp       scheme-file-gexp)                  ;gexp
436   (splice?    scheme-file-splice?))              ;Boolean
438 (define* (scheme-file name gexp #:key splice?)
439   "Return an object representing the Scheme file NAME that contains GEXP.
441 This is the declarative counterpart of 'gexp->file'."
442   (%scheme-file name gexp splice?))
444 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
445                                             system target)
446   ;; Compile FILE by returning a derivation that builds the file.
447   (match file
448     (($ <scheme-file> name gexp splice?)
449      (gexp->file name gexp #:splice? splice?))))
451 ;; Appending SUFFIX to BASE's output file name.
452 (define-record-type <file-append>
453   (%file-append base suffix)
454   file-append?
455   (base   file-append-base)                    ;<package> | <derivation> | ...
456   (suffix file-append-suffix))                 ;list of strings
458 (define (write-file-append file port)
459   (match file
460     (($ <file-append> base suffix)
461      (format port "#<file-append ~s ~s>" base
462              (string-join suffix)))))
464 (set-record-type-printer! <file-append> write-file-append)
466 (define (file-append base . suffix)
467   "Return a <file-append> object that expands to the concatenation of BASE and
468 SUFFIX."
469   (%file-append base suffix))
471 (define-gexp-compiler file-append-compiler <file-append>
472   compiler => (lambda (obj system target)
473                 (match obj
474                   (($ <file-append> base _)
475                    (lower-object base system #:target target))))
476   expander => (lambda (obj lowered output)
477                 (match obj
478                   (($ <file-append> base suffix)
479                    (let* ((expand (lookup-expander base))
480                           (base   (expand base lowered output)))
481                      (string-append base (string-concatenate suffix)))))))
485 ;;; Inputs & outputs.
488 ;; The input of a gexp.
489 (define-record-type <gexp-input>
490   (%gexp-input thing output native?)
491   gexp-input?
492   (thing     gexp-input-thing)       ;<package> | <origin> | <derivation> | ...
493   (output    gexp-input-output)      ;string
494   (native?   gexp-input-native?))    ;Boolean
496 (define (write-gexp-input input port)
497   (match input
498     (($ <gexp-input> thing output #f)
499      (format port "#<gexp-input ~s:~a>" thing output))
500     (($ <gexp-input> thing output #t)
501      (format port "#<gexp-input native ~s:~a>" thing output))))
503 (set-record-type-printer! <gexp-input> write-gexp-input)
505 (define* (gexp-input thing                        ;convenience procedure
506                      #:optional (output "out")
507                      #:key native?)
508   "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
509 whether this should be considered a \"native\" input or not."
510   (%gexp-input thing output native?))
512 ;; Reference to one of the derivation's outputs, for gexps used in
513 ;; derivations.
514 (define-record-type <gexp-output>
515   (gexp-output name)
516   gexp-output?
517   (name gexp-output-name))
519 (define (write-gexp-output output port)
520   (match output
521     (($ <gexp-output> name)
522      (format port "#<gexp-output ~a>" name))))
524 (set-record-type-printer! <gexp-output> write-gexp-output)
526 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
527   "Recurse on GEXP and the expressions it refers to, summing the items
528 returned by SELF-ATTRIBUTE, a procedure that takes a gexp.  Use EQUAL? as the
529 second argument to 'delete-duplicates'."
530   (if (gexp? gexp)
531       (delete-duplicates
532        (append (self-attribute gexp)
533                (append-map (match-lambda
534                              (($ <gexp-input> (? gexp? exp))
535                               (gexp-attribute exp self-attribute))
536                              (($ <gexp-input> (lst ...))
537                               (append-map (lambda (item)
538                                             (if (gexp? item)
539                                                 (gexp-attribute item
540                                                                 self-attribute)
541                                                 '()))
542                                           lst))
543                              (_
544                               '()))
545                            (gexp-references gexp)))
546        equal?)
547       '()))                                       ;plain Scheme data type
549 (define (gexp-modules gexp)
550   "Return the list of Guile module names GEXP relies on.  If (gexp? GEXP) is
551 false, meaning that GEXP is a plain Scheme object, return the empty list."
552   (define (module=? m1 m2)
553     ;; Return #t when M1 equals M2.  Special-case '=>' specs because their
554     ;; right-hand side may not be comparable with 'equal?': it's typically a
555     ;; file-like object that embeds a gexp, which in turn embeds closure;
556     ;; those closures may be 'eq?' when running compiled code but are unlikely
557     ;; to be 'eq?' when running on 'eval'.  Ignore the right-hand side to
558     ;; avoid this discrepancy.
559     (match m1
560       (((name1 ...) '=> _)
561        (match m2
562          (((name2 ...) '=> _) (equal? name1 name2))
563          (_ #f)))
564       (_
565        (equal? m1 m2))))
567   (gexp-attribute gexp gexp-self-modules module=?))
569 (define (gexp-extensions gexp)
570   "Return the list of Guile extensions (packages) GEXP relies on.  If (gexp?
571 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
572 list."
573   (gexp-attribute gexp gexp-self-extensions))
575 (define* (lower-inputs inputs
576                        #:key system target)
577   "Turn any package from INPUTS into a derivation for SYSTEM; return the
578 corresponding input list as a monadic value.  When TARGET is true, use it as
579 the cross-compilation target triplet."
580   (define (store-item? obj)
581     (and (string? obj) (store-path? obj)))
583   (with-monad %store-monad
584     (mapm %store-monad
585           (match-lambda
586             (((? struct? thing) sub-drv ...)
587              (mlet %store-monad ((drv (lower-object
588                                        thing system #:target target)))
589                (return (apply gexp-input drv sub-drv))))
590             (((? store-item? item))
591              (return (gexp-input item)))
592             (input
593              (return (gexp-input input))))
594           inputs)))
596 (define* (lower-reference-graphs graphs #:key system target)
597   "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
598 #:reference-graphs argument, lower it such that each INPUT is replaced by the
599 corresponding derivation."
600   (match graphs
601     (((file-names . inputs) ...)
602      (mlet %store-monad ((inputs (lower-inputs inputs
603                                                #:system system
604                                                #:target target)))
605        (return (map (lambda (file input)
606                       (cons file (gexp-input->tuple input)))
607                     file-names inputs))))))
609 (define* (lower-references lst #:key system target)
610   "Based on LST, a list of output names and packages, return a list of output
611 names and file names suitable for the #:allowed-references argument to
612 'derivation'."
613   (with-monad %store-monad
614     (define lower
615       (match-lambda
616        ((? string? output)
617         (return output))
618        (($ <gexp-input> thing output native?)
619         (mlet %store-monad ((drv (lower-object thing system
620                                                #:target (if native?
621                                                             #f target))))
622           (return (derivation->output-path drv output))))
623        (thing
624         (mlet %store-monad ((drv (lower-object thing system
625                                                #:target target)))
626           (return (derivation->output-path drv))))))
628     (mapm %store-monad lower lst)))
630 (define default-guile-derivation
631   ;; Here we break the abstraction by talking to the higher-level layer.
632   ;; Thus, do the resolution lazily to hide the circular dependency.
633   (let ((proc (delay
634                 (let ((iface (resolve-interface '(guix packages))))
635                   (module-ref iface 'default-guile-derivation)))))
636     (lambda (system)
637       ((force proc) system))))
639 ;; Representation of a gexp instantiated for a given target and system.
640 (define-record-type <lowered-gexp>
641   (lowered-gexp sexp inputs guile load-path load-compiled-path)
642   lowered-gexp?
643   (sexp                lowered-gexp-sexp)         ;sexp
644   (inputs              lowered-gexp-inputs)       ;list of <gexp-input>
645   (guile               lowered-gexp-guile)        ;<derivation> | #f
646   (load-path           lowered-gexp-load-path)    ;list of store items
647   (load-compiled-path  lowered-gexp-load-compiled-path)) ;list of store items
649 (define* (lower-gexp exp
650                      #:key
651                      (module-path %load-path)
652                      (system (%current-system))
653                      (target 'current)
654                      (graft? (%graft?))
655                      (guile-for-build (%guile-for-build))
656                      (effective-version "2.2")
658                      deprecation-warnings
659                      (pre-load-modules? #t))      ;transitional
660   "*Note: This API is subject to change; use at your own risk!*
662 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET.  Return a
663 <lowered-gexp> ready to be used.
665 Lowered gexps are an intermediate representation that's useful for
666 applications that deal with gexps outside in a way that is disconnected from
667 derivations--e.g., code evaluated for its side effects."
668   (define %modules
669     (delete-duplicates (gexp-modules exp)))
671   (define (search-path modules extensions suffix)
672     (append (match modules
673               ((? derivation? drv)
674                (list (derivation->output-path drv)))
675               (#f
676                '())
677               ((? store-path? item)
678                (list item)))
679             (map (lambda (extension)
680                    (string-append (match extension
681                                     ((? derivation? drv)
682                                      (derivation->output-path drv))
683                                     ((? store-path? item)
684                                      item))
685                                   suffix))
686                  extensions)))
688   (mlet* %store-monad ( ;; The following binding forces '%current-system' and
689                        ;; '%current-target-system' to be looked up at >>=
690                        ;; time.
691                        (graft?    (set-grafting graft?))
693                        (system -> (or system (%current-system)))
694                        (target -> (if (eq? target 'current)
695                                       (%current-target-system)
696                                       target))
697                        (guile     (if guile-for-build
698                                       (return guile-for-build)
699                                       (default-guile-derivation system)))
700                        (normals  (lower-inputs (gexp-inputs exp)
701                                                #:system system
702                                                #:target target))
703                        (natives  (lower-inputs (gexp-native-inputs exp)
704                                                #:system system
705                                                #:target #f))
706                        (inputs -> (append normals natives))
707                        (sexp     (gexp->sexp exp
708                                              #:system system
709                                              #:target target))
710                        (extensions -> (gexp-extensions exp))
711                        (exts     (mapm %store-monad
712                                        (lambda (obj)
713                                          (lower-object obj system))
714                                        extensions))
715                        (modules  (if (pair? %modules)
716                                      (imported-modules %modules
717                                                        #:system system
718                                                        #:module-path module-path)
719                                      (return #f)))
720                        (compiled (if (pair? %modules)
721                                      (compiled-modules %modules
722                                                        #:system system
723                                                        #:module-path module-path
724                                                        #:extensions extensions
725                                                        #:guile guile
726                                                        #:pre-load-modules?
727                                                        pre-load-modules?
728                                                        #:deprecation-warnings
729                                                        deprecation-warnings)
730                                      (return #f))))
731     (define load-path
732       (search-path modules exts
733                    (string-append "/share/guile/site/" effective-version)))
735     (define load-compiled-path
736       (search-path compiled exts
737                    (string-append "/lib/guile/" effective-version
738                                   "/site-ccache")))
740     (mbegin %store-monad
741       (set-grafting graft?)                       ;restore the initial setting
742       (return (lowered-gexp sexp
743                             `(,@(if modules
744                                     (list (gexp-input modules))
745                                     '())
746                               ,@(if compiled
747                                     (list (gexp-input compiled))
748                                     '())
749                               ,@(map gexp-input exts)
750                               ,@inputs)
751                             guile
752                             load-path
753                             load-compiled-path)))))
755 (define (gexp-input->tuple input)
756   "Given INPUT, a <gexp-input> record, return the corresponding input tuple
757 suitable for the 'derivation' procedure."
758   (match (gexp-input-output input)
759     ("out"  `(,(gexp-input-thing input)))
760     (output `(,(gexp-input-thing input)
761               ,(gexp-input-output input)))))
763 (define* (gexp->derivation name exp
764                            #:key
765                            system (target 'current)
766                            hash hash-algo recursive?
767                            (env-vars '())
768                            (modules '())
769                            (module-path %load-path)
770                            (guile-for-build (%guile-for-build))
771                            (effective-version "2.2")
772                            (graft? (%graft?))
773                            references-graphs
774                            allowed-references disallowed-references
775                            leaked-env-vars
776                            local-build? (substitutable? #t)
777                            (properties '())
779                            ;; TODO: This parameter is transitional; it's here
780                            ;; to avoid a full rebuild.  Remove it on the next
781                            ;; rebuild cycle.
782                            (pre-load-modules? #t)
784                            deprecation-warnings
785                            (script-name (string-append name "-builder")))
786   "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
787 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME.  When
788 TARGET is true, it is used as the cross-compilation target triplet for
789 packages referred to by EXP.
791 MODULES is deprecated in favor of 'with-imported-modules'.  Its meaning is to
792 make MODULES available in the evaluation context of EXP; MODULES is a list of
793 names of Guile modules searched in MODULE-PATH to be copied in the store,
794 compiled, and made available in the load path during the execution of
795 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
797 EFFECTIVE-VERSION determines the string to use when adding extensions of
798 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
800 GRAFT? determines whether packages referred to by EXP should be grafted when
801 applicable.
803 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
804 following forms:
806   (FILE-NAME PACKAGE)
807   (FILE-NAME PACKAGE OUTPUT)
808   (FILE-NAME DERIVATION)
809   (FILE-NAME DERIVATION OUTPUT)
810   (FILE-NAME STORE-ITEM)
812 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
813 an input of the build process of EXP.  In the build environment, each
814 FILE-NAME contains the reference graph of the corresponding item, in a simple
815 text format.
817 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
818 In the latter case, the list denotes store items that the result is allowed to
819 refer to.  Any reference to another store item will lead to a build error.
820 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
821 referenced by the outputs.
823 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
824 compiling modules.  It can be #f, #t, or 'detailed.
826 The other arguments are as for 'derivation'."
827   (define outputs (gexp-outputs exp))
828   (define requested-graft? graft?)
830   (define (graphs-file-names graphs)
831     ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
832     (map (match-lambda
833            ;; TODO: Remove 'derivation?' special cases.
834            ((file-name (? derivation? drv))
835             (cons file-name (derivation->output-path drv)))
836            ((file-name (? derivation? drv) sub-drv)
837             (cons file-name (derivation->output-path drv sub-drv)))
838            ((file-name thing)
839             (cons file-name thing)))
840          graphs))
842   (define (add-modules exp modules)
843     (if (null? modules)
844         exp
845         (make-gexp (gexp-references exp)
846                    (append modules (gexp-self-modules exp))
847                    (gexp-self-extensions exp)
848                    (gexp-proc exp))))
850   (mlet* %store-monad ( ;; The following binding forces '%current-system' and
851                        ;; '%current-target-system' to be looked up at >>=
852                        ;; time.
853                        (graft?    (set-grafting graft?))
855                        (system -> (or system (%current-system)))
856                        (target -> (if (eq? target 'current)
857                                       (%current-target-system)
858                                       target))
859                        (exp ->    (add-modules exp modules))
860                        (lowered   (lower-gexp exp
861                                               #:module-path module-path
862                                               #:system system
863                                               #:target target
864                                               #:graft? requested-graft?
865                                               #:guile-for-build
866                                               guile-for-build
867                                               #:effective-version
868                                               effective-version
869                                               #:deprecation-warnings
870                                               deprecation-warnings
871                                               #:pre-load-modules?
872                                               pre-load-modules?))
874                        (graphs   (if references-graphs
875                                      (lower-reference-graphs references-graphs
876                                                              #:system system
877                                                              #:target target)
878                                      (return #f)))
879                        (allowed  (if allowed-references
880                                      (lower-references allowed-references
881                                                        #:system system
882                                                        #:target target)
883                                      (return #f)))
884                        (disallowed (if disallowed-references
885                                        (lower-references disallowed-references
886                                                          #:system system
887                                                          #:target target)
888                                        (return #f)))
889                        (guile -> (lowered-gexp-guile lowered))
890                        (builder  (text-file script-name
891                                             (object->string
892                                              (lowered-gexp-sexp lowered)))))
893     (mbegin %store-monad
894       (set-grafting graft?)                       ;restore the initial setting
895       (raw-derivation name
896                       (string-append (derivation->output-path guile)
897                                      "/bin/guile")
898                       `("--no-auto-compile"
899                         ,@(append-map (lambda (directory)
900                                         `("-L" ,directory))
901                                       (lowered-gexp-load-path lowered))
902                         ,@(append-map (lambda (directory)
903                                         `("-C" ,directory))
904                                       (lowered-gexp-load-compiled-path lowered))
905                         ,builder)
906                       #:outputs outputs
907                       #:env-vars env-vars
908                       #:system system
909                       #:inputs `((,guile)
910                                  (,builder)
911                                  ,@(map gexp-input->tuple
912                                         (lowered-gexp-inputs lowered))
913                                  ,@(match graphs
914                                      (((_ . inputs) ...) inputs)
915                                      (_ '())))
916                       #:hash hash #:hash-algo hash-algo #:recursive? recursive?
917                       #:references-graphs (and=> graphs graphs-file-names)
918                       #:allowed-references allowed
919                       #:disallowed-references disallowed
920                       #:leaked-env-vars leaked-env-vars
921                       #:local-build? local-build?
922                       #:substitutable? substitutable?
923                       #:properties properties))))
925 (define* (gexp-inputs exp #:key native?)
926   "Return the input list for EXP.  When NATIVE? is true, return only native
927 references; otherwise, return only non-native references."
928   ;; TODO: Return <gexp-input> records instead of tuples.
929   (define (add-reference-inputs ref result)
930     (match ref
931       (($ <gexp-input> (? gexp? exp) _ #t)
932        (if native?
933            (append (gexp-inputs exp)
934                    (gexp-inputs exp #:native? #t)
935                    result)
936            result))
937       (($ <gexp-input> (? gexp? exp) _ #f)
938        (append (gexp-inputs exp #:native? native?)
939                result))
940       (($ <gexp-input> (? string? str))
941        (if (direct-store-path? str)
942            (cons `(,str) result)
943            result))
944       (($ <gexp-input> (? struct? thing) output n?)
945        (if (and (eqv? n? native?) (lookup-compiler thing))
946            ;; THING is a derivation, or a package, or an origin, etc.
947            (cons `(,thing ,output) result)
948            result))
949       (($ <gexp-input> (lst ...) output n?)
950        (fold-right add-reference-inputs result
951                    ;; XXX: For now, automatically convert LST to a list of
952                    ;; gexp-inputs.  Inherit N?.
953                    (map (match-lambda
954                           ((? gexp-input? x)
955                            (%gexp-input (gexp-input-thing x)
956                                         (gexp-input-output x)
957                                         n?))
958                           (x
959                            (%gexp-input x "out" n?)))
960                         lst)))
961       (_
962        ;; Ignore references to other kinds of objects.
963        result)))
965   (fold-right add-reference-inputs
966               '()
967               (gexp-references exp)))
969 (define gexp-native-inputs
970   (cut gexp-inputs <> #:native? #t))
972 (define (gexp-outputs exp)
973   "Return the outputs referred to by EXP as a list of strings."
974   (define (add-reference-output ref result)
975     (match ref
976       (($ <gexp-output> name)
977        (cons name result))
978       (($ <gexp-input> (? gexp? exp))
979        (append (gexp-outputs exp) result))
980       (($ <gexp-input> (lst ...) output native?)
981        ;; XXX: Automatically convert LST.
982        (add-reference-output (map (match-lambda
983                                    ((? gexp-input? x) x)
984                                    (x (%gexp-input x "out" native?)))
985                                   lst)
986                              result))
987       ((lst ...)
988        (fold-right add-reference-output result lst))
989       (_
990        result)))
992   (delete-duplicates
993    (add-reference-output (gexp-references exp) '())))
995 (define* (gexp->sexp exp #:key
996                      (system (%current-system))
997                      (target (%current-target-system)))
998   "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
999 and in the current monad setting (system type, etc.)"
1000   (define* (reference->sexp ref #:optional native?)
1001     (with-monad %store-monad
1002       (match ref
1003         (($ <gexp-output> output)
1004          ;; Output file names are not known in advance but the daemon defines
1005          ;; an environment variable for each of them at build time, so use
1006          ;; that trick.
1007          (return `((@ (guile) getenv) ,output)))
1008         (($ <gexp-input> (? gexp? exp) output n?)
1009          (gexp->sexp exp
1010                      #:system system
1011                      #:target (if (or n? native?) #f target)))
1012         (($ <gexp-input> (refs ...) output n?)
1013          (mapm %store-monad
1014                (lambda (ref)
1015                  ;; XXX: Automatically convert REF to an gexp-input.
1016                  (reference->sexp
1017                   (if (gexp-input? ref)
1018                       ref
1019                       (%gexp-input ref "out" n?))
1020                   (or n? native?)))
1021                refs))
1022         (($ <gexp-input> (? struct? thing) output n?)
1023          (let ((target (if (or n? native?) #f target))
1024                (expand (lookup-expander thing)))
1025            (mlet %store-monad ((obj (lower-object thing system
1026                                                   #:target target)))
1027              ;; OBJ must be either a derivation or a store file name.
1028              (return (expand thing obj output)))))
1029         (($ <gexp-input> x)
1030          (return x))
1031         (x
1032          (return x)))))
1034   (mlet %store-monad
1035       ((args (mapm %store-monad
1036                    reference->sexp (gexp-references exp))))
1037     (return (apply (gexp-proc exp) args))))
1039 (define (syntax-location-string s)
1040   "Return a string representing the source code location of S."
1041   (let ((props (syntax-source s)))
1042     (if props
1043         (let ((file   (assoc-ref props 'filename))
1044               (line   (and=> (assoc-ref props 'line) 1+))
1045               (column (assoc-ref props 'column)))
1046           (if file
1047               (simple-format #f "~a:~a:~a"
1048                              file line column)
1049               (simple-format #f "~a:~a" line column)))
1050         "<unknown location>")))
1052 (define-syntax-rule (define-syntax-parameter-once name proc)
1053   ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1054   ;; does not get redefined.  This works around a race condition in a
1055   ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1056   (eval-when (load eval expand compile)
1057     (define name
1058       (if (module-locally-bound? (current-module) 'name)
1059           (module-ref (current-module) 'name)
1060           (make-syntax-transformer 'name 'syntax-parameter
1061                                    (list proc))))))
1063 (define-syntax-parameter-once current-imported-modules
1064   ;; Current list of imported modules.
1065   (identifier-syntax '()))
1067 (define-syntax-rule (with-imported-modules modules body ...)
1068   "Mark the gexps defined in BODY... as requiring MODULES in their execution
1069 environment."
1070   (syntax-parameterize ((current-imported-modules
1071                          (identifier-syntax modules)))
1072     body ...))
1074 (define-syntax-parameter-once current-imported-extensions
1075   ;; Current list of extensions.
1076   (identifier-syntax '()))
1078 (define-syntax-rule (with-extensions extensions body ...)
1079   "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1080 execution environment."
1081   (syntax-parameterize ((current-imported-extensions
1082                          (identifier-syntax extensions)))
1083     body ...))
1085 (define-syntax gexp
1086   (lambda (s)
1087     (define (collect-escapes exp)
1088       ;; Return all the 'ungexp' present in EXP.
1089       (let loop ((exp    exp)
1090                  (result '()))
1091         (syntax-case exp (ungexp
1092                           ungexp-splicing
1093                           ungexp-native
1094                           ungexp-native-splicing)
1095           ((ungexp _)
1096            (cons exp result))
1097           ((ungexp _ _)
1098            (cons exp result))
1099           ((ungexp-splicing _ ...)
1100            (cons exp result))
1101           ((ungexp-native _ ...)
1102            (cons exp result))
1103           ((ungexp-native-splicing _ ...)
1104            (cons exp result))
1105           ((exp0 . exp)
1106            (let ((result (loop #'exp0 result)))
1107              (loop  #'exp result)))
1108           (_
1109            result))))
1111     (define (escape->ref exp)
1112       ;; Turn 'ungexp' form EXP into a "reference".
1113       (syntax-case exp (ungexp ungexp-splicing
1114                         ungexp-native ungexp-native-splicing
1115                         output)
1116         ((ungexp output)
1117          #'(gexp-output "out"))
1118         ((ungexp output name)
1119          #'(gexp-output name))
1120         ((ungexp thing)
1121          #'(%gexp-input thing "out" #f))
1122         ((ungexp drv-or-pkg out)
1123          #'(%gexp-input drv-or-pkg out #f))
1124         ((ungexp-splicing lst)
1125          #'(%gexp-input lst "out" #f))
1126         ((ungexp-native thing)
1127          #'(%gexp-input thing "out" #t))
1128         ((ungexp-native drv-or-pkg out)
1129          #'(%gexp-input drv-or-pkg out #t))
1130         ((ungexp-native-splicing lst)
1131          #'(%gexp-input lst "out" #t))))
1133     (define (substitute-ungexp exp substs)
1134       ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1135       ;; the corresponding form in SUBSTS.
1136       (match (assoc exp substs)
1137         ((_ id)
1138          id)
1139         (_                                        ;internal error
1140          (with-syntax ((exp exp))
1141            #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1143     (define (substitute-ungexp-splicing exp substs)
1144       (syntax-case exp ()
1145         ((exp rest ...)
1146          (match (assoc #'exp substs)
1147            ((_ id)
1148             (with-syntax ((id id))
1149               #`(append id
1150                         #,(substitute-references #'(rest ...) substs))))
1151            (_
1152             #'(syntax-error "error: no 'ungexp-splicing' substitution"
1153                             exp))))))
1155     (define (substitute-references exp substs)
1156       ;; Return a variant of EXP where all the cars of SUBSTS have been
1157       ;; replaced by the corresponding cdr.
1158       (syntax-case exp (ungexp ungexp-native
1159                         ungexp-splicing ungexp-native-splicing)
1160         ((ungexp _ ...)
1161          (substitute-ungexp exp substs))
1162         ((ungexp-native _ ...)
1163          (substitute-ungexp exp substs))
1164         (((ungexp-splicing _ ...) rest ...)
1165          (substitute-ungexp-splicing exp substs))
1166         (((ungexp-native-splicing _ ...) rest ...)
1167          (substitute-ungexp-splicing exp substs))
1168         ((exp0 . exp)
1169          #`(cons #,(substitute-references #'exp0 substs)
1170                  #,(substitute-references #'exp substs)))
1171         (x #''x)))
1173     (syntax-case s (ungexp output)
1174       ((_ exp)
1175        (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1176               (formals (generate-temporaries escapes))
1177               (sexp    (substitute-references #'exp (zip escapes formals)))
1178               (refs    (map escape->ref escapes)))
1179          #`(make-gexp (list #,@refs)
1180                       current-imported-modules
1181                       current-imported-extensions
1182                       (lambda #,formals
1183                         #,sexp)))))))
1187 ;;; Module handling.
1190 (define %not-slash
1191   (char-set-complement (char-set #\/)))
1193 (define (file-mapping->tree mapping)
1194   "Convert MAPPING, an alist like:
1196   ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1198 to a tree suitable for 'interned-file-tree'."
1199   (let ((mapping (map (match-lambda
1200                         ((destination . source)
1201                          (cons (string-tokenize destination
1202                                                 %not-slash)
1203                                source)))
1204                       mapping)))
1205     (fold (lambda (pair result)
1206             (match pair
1207               ((destination . source)
1208                (let loop ((destination destination)
1209                           (result result))
1210                  (match destination
1211                    ((file)
1212                     (let* ((mode (stat:mode (stat source)))
1213                            (type (if (zero? (logand mode #o100))
1214                                      'regular
1215                                      'executable)))
1216                       (alist-cons file
1217                                   `(,type (file ,source))
1218                                   result)))
1219                    ((file rest ...)
1220                     (let ((directory (assoc-ref result file)))
1221                       (alist-cons file
1222                                   `(directory
1223                                     ,@(loop rest
1224                                             (match directory
1225                                               (('directory . entries) entries)
1226                                               (#f '()))))
1227                                   (if directory
1228                                       (alist-delete file result)
1229                                       result)))))))))
1230           '()
1231           mapping)))
1233 (define %utils-module
1234   ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1235   ;; other primitives below.  Note: We give the file name relative to this
1236   ;; file you are currently reading; 'search-path' could return a file name
1237   ;; relative to the current working directory.
1238   (local-file "build/utils.scm"
1239               "build-utils.scm"))
1241 (define* (imported-files/derivation files
1242                                     #:key (name "file-import")
1243                                     (symlink? #f)
1244                                     (system (%current-system))
1245                                     (guile (%guile-for-build)))
1246   "Return a derivation that imports FILES into STORE.  FILES must be a list
1247 of (FINAL-PATH . FILE) pairs.  Each FILE is mapped to FINAL-PATH in the
1248 resulting store path.  FILE can be either a file name, or a file-like object,
1249 as returned by 'local-file' for example.  If SYMLINK? is true, create symlinks
1250 to the source files instead of copying them."
1251   (define file-pair
1252     (match-lambda
1253      ((final-path . (? string? file-name))
1254       (mlet %store-monad ((file (interned-file file-name
1255                                                (basename final-path))))
1256         (return (list final-path file))))
1257      ((final-path . file-like)
1258       (mlet %store-monad ((file (lower-object file-like system)))
1259         (return (list final-path file))))))
1261   (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1262     (define build
1263       (gexp
1264        (begin
1265          (primitive-load (ungexp %utils-module))  ;for 'mkdir-p'
1266          (use-modules (ice-9 match))
1268          (mkdir (ungexp output)) (chdir (ungexp output))
1269          (for-each (match-lambda
1270                     ((final-path store-path)
1271                      (mkdir-p (dirname final-path))
1272                      ((ungexp (if symlink? 'symlink 'copy-file))
1273                       store-path final-path)))
1274                    '(ungexp files)))))
1276     ;; TODO: Pass FILES as an environment variable so that BUILD remains
1277     ;; exactly the same regardless of FILES: less disk space, and fewer
1278     ;; 'add-to-store' RPCs.
1279     (gexp->derivation name build
1280                       #:system system
1281                       #:guile-for-build guile
1282                       #:local-build? #t
1284                       ;; Avoid deprecation warnings about the use of the _IO*
1285                       ;; constants in (guix build utils).
1286                       #:env-vars
1287                       '(("GUILE_WARN_DEPRECATED" . "no")))))
1289 (define* (imported-files files
1290                          #:key (name "file-import")
1291                          ;; The following parameters make sense when creating
1292                          ;; an actual derivation.
1293                          (system (%current-system))
1294                          (guile (%guile-for-build)))
1295   "Import FILES into the store and return the resulting derivation or store
1296 file name (a derivation is created if and only if some elements of FILES are
1297 file-like objects and not local file names.)  FILES must be a list
1298 of (FINAL-PATH . FILE) pairs.  Each FILE is mapped to FINAL-PATH in the
1299 resulting store path.  FILE can be either a file name, or a file-like object,
1300 as returned by 'local-file' for example."
1301   (if (any (match-lambda
1302              ((_ . (? struct? source)) #t)
1303              (_ #f))
1304            files)
1305       (imported-files/derivation files #:name name
1306                                  #:symlink? derivation?
1307                                  #:system system #:guile guile)
1308       (interned-file-tree `(,name directory
1309                                   ,@(file-mapping->tree files)))))
1311 (define* (imported-modules modules
1312                            #:key (name "module-import")
1313                            (system (%current-system))
1314                            (guile (%guile-for-build))
1315                            (module-path %load-path))
1316   "Return a derivation that contains the source files of MODULES, a list of
1317 module names such as `(ice-9 q)'.  All of MODULES must be either names of
1318 modules to be found in the MODULE-PATH search path, or a module name followed
1319 by an arrow followed by a file-like object.  For example:
1321   (imported-modules `((guix build utils)
1322                       (guix gcrypt)
1323                       ((guix config) => ,(scheme-file …))))
1325 In this example, the first two modules are taken from MODULE-PATH, and the
1326 last one is created from the given <scheme-file> object."
1327   (let ((files (map (match-lambda
1328                       (((module ...) '=> file)
1329                        (cons (module->source-file-name module)
1330                              file))
1331                       ((module ...)
1332                        (let ((f (module->source-file-name module)))
1333                          (cons f (search-path* module-path f)))))
1334                     modules)))
1335     (imported-files files #:name name
1336                     #:system system
1337                     #:guile guile)))
1339 (define* (compiled-modules modules
1340                            #:key (name "module-import-compiled")
1341                            (system (%current-system))
1342                            (guile (%guile-for-build))
1343                            (module-path %load-path)
1344                            (extensions '())
1345                            (deprecation-warnings #f)
1347                            ;; TODO: This flag is here to prevent a full
1348                            ;; rebuild.  Remove it on the next rebuild cycle.
1349                            (pre-load-modules? #t))
1350   "Return a derivation that builds a tree containing the `.go' files
1351 corresponding to MODULES.  All the MODULES are built in a context where
1352 they can refer to each other."
1353   (define total (length modules))
1355   (mlet %store-monad ((modules (imported-modules modules
1356                                                  #:system system
1357                                                  #:guile guile
1358                                                  #:module-path
1359                                                  module-path)))
1360     (define build
1361       (gexp
1362        (begin
1363          (primitive-load (ungexp %utils-module))  ;for 'mkdir-p'
1365          (use-modules (ice-9 ftw)
1366                       (ice-9 format)
1367                       (srfi srfi-1)
1368                       (srfi srfi-26)
1369                       (system base compile))
1371          (define (regular? file)
1372            (not (member file '("." ".."))))
1374          (define (process-entry entry output processed)
1375            (if (file-is-directory? entry)
1376                (let ((output (string-append output "/" (basename entry))))
1377                  (mkdir-p output)
1378                  (process-directory entry output processed))
1379                (let* ((base   (basename entry ".scm"))
1380                       (output (string-append output "/" base ".go")))
1381                  (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1382                          (+ 1 processed
1383                               (ungexp-splicing (if pre-load-modules?
1384                                                    (gexp ((ungexp total)))
1385                                                    (gexp ()))))
1386                          (ungexp (* total (if pre-load-modules? 2 1)))
1387                          entry)
1388                  (compile-file entry
1389                                #:output-file output
1390                                #:opts %auto-compilation-options)
1391                  (+ 1 processed))))
1393          (define (process-directory directory output processed)
1394            (let ((entries (map (cut string-append directory "/" <>)
1395                                (scandir directory regular?))))
1396              (fold (cut process-entry <> output <>)
1397                    processed
1398                    entries)))
1400          (setvbuf (current-output-port)
1401                   (cond-expand (guile-2.2 'line) (else _IOLBF)))
1403          (define mkdir-p
1404            ;; Capture 'mkdir-p'.
1405            (@ (guix build utils) mkdir-p))
1407          ;; Add EXTENSIONS to the search path.
1408          (set! %load-path
1409            (append (map (lambda (extension)
1410                           (string-append extension
1411                                          "/share/guile/site/"
1412                                          (effective-version)))
1413                         '((ungexp-native-splicing extensions)))
1414                    %load-path))
1415          (set! %load-compiled-path
1416            (append (map (lambda (extension)
1417                           (string-append extension "/lib/guile/"
1418                                          (effective-version)
1419                                          "/site-ccache"))
1420                         '((ungexp-native-splicing extensions)))
1421                    %load-compiled-path))
1423          (set! %load-path (cons (ungexp modules) %load-path))
1425          ;; Above we loaded our own (guix build utils) but now we may need to
1426          ;; load a compile a different one.  Thus, force a reload.
1427          (let ((utils (string-append (ungexp modules)
1428                                      "/guix/build/utils.scm")))
1429            (when (file-exists? utils)
1430              (load utils)))
1432          (mkdir (ungexp output))
1433          (chdir (ungexp modules))
1435          (ungexp-splicing
1436           (if pre-load-modules?
1437               (gexp ((define* (load-from-directory directory
1438                                                    #:optional (loaded 0))
1439                        "Load all the source files found in DIRECTORY."
1440                        ;; XXX: This works around <https://bugs.gnu.org/15602>.
1441                        (let ((entries (map (cut string-append directory "/" <>)
1442                                            (scandir directory regular?))))
1443                          (fold (lambda (file loaded)
1444                                  (if (file-is-directory? file)
1445                                      (load-from-directory file loaded)
1446                                      (begin
1447                                        (format #t "[~2@a/~2@a] Loading '~a'...~%"
1448                                                (+ 1 loaded)
1449                                                (ungexp (* 2 total))
1450                                                file)
1451                                        (save-module-excursion
1452                                         (lambda ()
1453                                           (primitive-load file)))
1454                                        (+ 1 loaded))))
1455                                loaded
1456                                entries)))
1458                      (load-from-directory ".")))
1459               (gexp ())))
1461          (process-directory "." (ungexp output) 0))))
1463     ;; TODO: Pass MODULES as an environment variable.
1464     (gexp->derivation name build
1465                       #:system system
1466                       #:guile-for-build guile
1467                       #:local-build? #t
1468                       #:env-vars
1469                       (case deprecation-warnings
1470                         ((#f)
1471                          '(("GUILE_WARN_DEPRECATED" . "no")))
1472                         ((detailed)
1473                          '(("GUILE_WARN_DEPRECATED" . "detailed")))
1474                         (else
1475                          '())))))
1479 ;;; Convenience procedures.
1482 (define (default-guile)
1483   ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1484   ;; programs returned by 'program-file' and we don't want to keep references
1485   ;; to several Guile packages).  This module must not refer to (gnu …)
1486   ;; modules directly, to avoid circular dependencies, hence this hack.
1487   (module-ref (resolve-interface '(gnu packages guile))
1488               'guile-2.2))
1490 (define* (load-path-expression modules #:optional (path %load-path)
1491                                #:key (extensions '()))
1492   "Return as a monadic value a gexp that sets '%load-path' and
1493 '%load-compiled-path' to point to MODULES, a list of module names.  MODULES
1494 are searched for in PATH.  Return #f when MODULES and EXTENSIONS are empty."
1495   (if (and (null? modules) (null? extensions))
1496       (with-monad %store-monad
1497         (return #f))
1498       (mlet %store-monad ((modules  (imported-modules modules
1499                                                       #:module-path path))
1500                           (compiled (compiled-modules modules
1501                                                       #:extensions extensions
1502                                                       #:module-path path)))
1503         (return (gexp (eval-when (expand load eval)
1504                         (set! %load-path
1505                           (cons (ungexp modules)
1506                                 (append (map (lambda (extension)
1507                                                (string-append extension
1508                                                               "/share/guile/site/"
1509                                                               (effective-version)))
1510                                              '((ungexp-native-splicing extensions)))
1511                                         %load-path)))
1512                         (set! %load-compiled-path
1513                           (cons (ungexp compiled)
1514                                 (append (map (lambda (extension)
1515                                                (string-append extension
1516                                                               "/lib/guile/"
1517                                                               (effective-version)
1518                                                               "/site-ccache"))
1519                                              '((ungexp-native-splicing extensions)))
1520                                         %load-compiled-path)))))))))
1522 (define* (gexp->script name exp
1523                        #:key (guile (default-guile))
1524                        (module-path %load-path))
1525   "Return an executable script NAME that runs EXP using GUILE, with EXP's
1526 imported modules in its search path.  Look up EXP's modules in MODULE-PATH."
1527   (mlet %store-monad ((set-load-path
1528                        (load-path-expression (gexp-modules exp)
1529                                              module-path
1530                                              #:extensions
1531                                              (gexp-extensions exp))))
1532     (gexp->derivation name
1533                       (gexp
1534                        (call-with-output-file (ungexp output)
1535                          (lambda (port)
1536                            ;; Note: that makes a long shebang.  When the store
1537                            ;; is /gnu/store, that fits within the 128-byte
1538                            ;; limit imposed by Linux, but that may go beyond
1539                            ;; when running tests.
1540                            (format port
1541                                    "#!~a/bin/guile --no-auto-compile~%!#~%"
1542                                    (ungexp guile))
1544                            (ungexp-splicing
1545                             (if set-load-path
1546                                 (gexp ((write '(ungexp set-load-path) port)))
1547                                 (gexp ())))
1549                            (write '(ungexp exp) port)
1550                            (chmod port #o555))))
1551                       #:module-path module-path)))
1553 (define* (gexp->file name exp #:key
1554                      (set-load-path? #t)
1555                      (module-path %load-path)
1556                      (splice? #f))
1557   "Return a derivation that builds a file NAME containing EXP.  When SPLICE?
1558 is true, EXP is considered to be a list of expressions that will be spliced in
1559 the resulting file.
1561 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1562 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1563 Lookup EXP's modules in MODULE-PATH."
1564   (define modules (gexp-modules exp))
1565   (define extensions (gexp-extensions exp))
1567   (if (or (not set-load-path?)
1568           (and (null? modules) (null? extensions)))
1569       (gexp->derivation name
1570                         (gexp
1571                          (call-with-output-file (ungexp output)
1572                            (lambda (port)
1573                              (for-each (lambda (exp)
1574                                          (write exp port))
1575                                        '(ungexp (if splice?
1576                                                     exp
1577                                                     (gexp ((ungexp exp)))))))))
1578                         #:local-build? #t
1579                         #:substitutable? #f)
1580       (mlet %store-monad ((set-load-path
1581                            (load-path-expression modules module-path
1582                                                  #:extensions extensions)))
1583         (gexp->derivation name
1584                           (gexp
1585                            (call-with-output-file (ungexp output)
1586                              (lambda (port)
1587                                (write '(ungexp set-load-path) port)
1588                                (for-each (lambda (exp)
1589                                            (write exp port))
1590                                          '(ungexp (if splice?
1591                                                       exp
1592                                                       (gexp ((ungexp exp)))))))))
1593                           #:module-path module-path
1594                           #:local-build? #t
1595                           #:substitutable? #f))))
1597 (define* (text-file* name #:rest text)
1598   "Return as a monadic value a derivation that builds a text file containing
1599 all of TEXT.  TEXT may list, in addition to strings, objects of any type that
1600 can be used in a gexp: packages, derivations, local file objects, etc.  The
1601 resulting store file holds references to all these."
1602   (define builder
1603     (gexp (call-with-output-file (ungexp output "out")
1604             (lambda (port)
1605               (display (string-append (ungexp-splicing text)) port)))))
1607   (gexp->derivation name builder
1608                     #:local-build? #t
1609                     #:substitutable? #f))
1611 (define* (mixed-text-file name #:rest text)
1612   "Return an object representing store file NAME containing TEXT.  TEXT is a
1613 sequence of strings and file-like objects, as in:
1615   (mixed-text-file \"profile\"
1616                    \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1618 This is the declarative counterpart of 'text-file*'."
1619   (define build
1620     (gexp (call-with-output-file (ungexp output "out")
1621             (lambda (port)
1622               (display (string-append (ungexp-splicing text)) port)))))
1624   (computed-file name build))
1626 (define (file-union name files)
1627   "Return a <computed-file> that builds a directory containing all of FILES.
1628 Each item in FILES must be a two-element list where the first element is the
1629 file name to use in the new directory, and the second element is a gexp
1630 denoting the target file.  Here's an example:
1632   (file-union \"etc\"
1633               `((\"hosts\" ,(plain-file \"hosts\"
1634                                         \"127.0.0.1 localhost\"))
1635                 (\"bashrc\" ,(plain-file \"bashrc\"
1636                                          \"alias ls='ls --color'\"))
1637                 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1639 This yields an 'etc' directory containing these two files."
1640   (computed-file name
1641                  (with-imported-modules '((guix build utils))
1642                    (gexp
1643                     (begin
1644                       (use-modules (guix build utils))
1646                       (mkdir (ungexp output))
1647                       (chdir (ungexp output))
1648                       (ungexp-splicing
1649                        (map (match-lambda
1650                               ((target source)
1651                                (gexp
1652                                 (begin
1653                                   ;; Stat the source to abort early if it does
1654                                   ;; not exist.
1655                                   (stat (ungexp source))
1657                                   (mkdir-p (dirname (ungexp target)))
1658                                   (symlink (ungexp source)
1659                                            (ungexp target))))))
1660                             files)))))))
1662 (define* (directory-union name things
1663                           #:key (copy? #f) (quiet? #f)
1664                           (resolve-collision 'warn-about-collision))
1665   "Return a directory that is the union of THINGS, where THINGS is a list of
1666 file-like objects denoting directories.  For example:
1668   (directory-union \"guile+emacs\" (list guile emacs))
1670 yields a directory that is the union of the 'guile' and 'emacs' packages.
1672 Call RESOLVE-COLLISION when several files collide, passing it the list of
1673 colliding files.  RESOLVE-COLLISION must return the chosen file or #f, in
1674 which case the colliding entry is skipped altogether.
1676 When HARD-LINKS? is true, create hard links instead of symlinks.  When QUIET?
1677 is true, the derivation will not print anything."
1678   (define symlink
1679     (if copy?
1680         (gexp (lambda (old new)
1681                 (if (file-is-directory? old)
1682                     (symlink old new)
1683                     (copy-file old new))))
1684         (gexp symlink)))
1686   (define log-port
1687     (if quiet?
1688         (gexp (%make-void-port "w"))
1689         (gexp (current-error-port))))
1691   (match things
1692     ((one)
1693      ;; Only one thing; return it.
1694      one)
1695     (_
1696      (computed-file name
1697                     (with-imported-modules '((guix build union))
1698                       (gexp (begin
1699                               (use-modules (guix build union)
1700                                            (srfi srfi-1)) ;for 'first' and 'last'
1702                               (union-build (ungexp output)
1703                                            '(ungexp things)
1705                                            #:log-port (ungexp log-port)
1706                                            #:symlink (ungexp symlink)
1707                                            #:resolve-collision
1708                                            (ungexp resolve-collision)))))))))
1712 ;;; Syntactic sugar.
1715 (eval-when (expand load eval)
1716   (define* (read-ungexp chr port #:optional native?)
1717     "Read an 'ungexp' or 'ungexp-splicing' form from PORT.  When NATIVE? is
1718 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1719     (define unquote-symbol
1720       (match (peek-char port)
1721         (#\@
1722          (read-char port)
1723          (if native?
1724              'ungexp-native-splicing
1725              'ungexp-splicing))
1726         (_
1727          (if native?
1728              'ungexp-native
1729              'ungexp))))
1731     (match (read port)
1732       ((? symbol? symbol)
1733        (let ((str (symbol->string symbol)))
1734          (match (string-index-right str #\:)
1735            (#f
1736             `(,unquote-symbol ,symbol))
1737            (colon
1738             (let ((name   (string->symbol (substring str 0 colon)))
1739                   (output (substring str (+ colon 1))))
1740               `(,unquote-symbol ,name ,output))))))
1741       (x
1742        `(,unquote-symbol ,x))))
1744   (define (read-gexp chr port)
1745     "Read a 'gexp' form from PORT."
1746     `(gexp ,(read port)))
1748   ;; Extend the reader
1749   (read-hash-extend #\~ read-gexp)
1750   (read-hash-extend #\$ read-ungexp)
1751   (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1753 ;;; gexp.scm ends here