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