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