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