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