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