file-systems: Add /var/run/nscd to '%network-file-mappings'.
[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
2e8cabb8
LC
430 #:guile (or guile (default-guile))
431 #:system system
432 #:target target))))
15a01c72 433
e1c153e0 434(define-record-type <scheme-file>
4fbd1a2b 435 (%scheme-file name gexp splice?)
e1c153e0
LC
436 scheme-file?
437 (name scheme-file-name) ;string
4fbd1a2b
LC
438 (gexp scheme-file-gexp) ;gexp
439 (splice? scheme-file-splice?)) ;Boolean
e1c153e0 440
4fbd1a2b 441(define* (scheme-file name gexp #:key splice?)
e1c153e0
LC
442 "Return an object representing the Scheme file NAME that contains GEXP.
443
444This is the declarative counterpart of 'gexp->file'."
4fbd1a2b 445 (%scheme-file name gexp splice?))
e1c153e0 446
1cdecf24 447(define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
e1c153e0
LC
448 system target)
449 ;; Compile FILE by returning a derivation that builds the file.
450 (match file
4fbd1a2b
LC
451 (($ <scheme-file> name gexp splice?)
452 (gexp->file name gexp #:splice? splice?))))
e1c153e0 453
a9e5e92f
LC
454;; Appending SUFFIX to BASE's output file name.
455(define-record-type <file-append>
456 (%file-append base suffix)
457 file-append?
458 (base file-append-base) ;<package> | <derivation> | ...
459 (suffix file-append-suffix)) ;list of strings
460
39d7fdce
LC
461(define (write-file-append file port)
462 (match file
463 (($ <file-append> base suffix)
464 (format port "#<file-append ~s ~s>" base
465 (string-join suffix)))))
466
467(set-record-type-printer! <file-append> write-file-append)
468
a9e5e92f
LC
469(define (file-append base . suffix)
470 "Return a <file-append> object that expands to the concatenation of BASE and
471SUFFIX."
472 (%file-append base suffix))
473
1cdecf24 474(define-gexp-compiler file-append-compiler <file-append>
a9e5e92f
LC
475 compiler => (lambda (obj system target)
476 (match obj
477 (($ <file-append> base _)
478 (lower-object base system #:target target))))
479 expander => (lambda (obj lowered output)
480 (match obj
481 (($ <file-append> base suffix)
482 (let* ((expand (lookup-expander base))
483 (base (expand base lowered output)))
484 (string-append base (string-concatenate suffix)))))))
485
d9ae938f 486\f
bcb13287
LC
487;;;
488;;; Inputs & outputs.
489;;;
490
e39d1461
LC
491;; The input of a gexp.
492(define-record-type <gexp-input>
0dbea56b 493 (%gexp-input thing output native?)
e39d1461
LC
494 gexp-input?
495 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
496 (output gexp-input-output) ;string
497 (native? gexp-input-native?)) ;Boolean
498
f7328634
LC
499(define (write-gexp-input input port)
500 (match input
501 (($ <gexp-input> thing output #f)
502 (format port "#<gexp-input ~s:~a>" thing output))
503 (($ <gexp-input> thing output #t)
504 (format port "#<gexp-input native ~s:~a>" thing output))))
505
506(set-record-type-printer! <gexp-input> write-gexp-input)
507
0dbea56b
LC
508(define* (gexp-input thing ;convenience procedure
509 #:optional (output "out")
510 #:key native?)
511 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
512whether this should be considered a \"native\" input or not."
513 (%gexp-input thing output native?))
514
21b679f6
LC
515;; Reference to one of the derivation's outputs, for gexps used in
516;; derivations.
1e87da58
LC
517(define-record-type <gexp-output>
518 (gexp-output name)
519 gexp-output?
520 (name gexp-output-name))
21b679f6 521
f7328634
LC
522(define (write-gexp-output output port)
523 (match output
524 (($ <gexp-output> name)
525 (format port "#<gexp-output ~a>" name))))
526
527(set-record-type-printer! <gexp-output> write-gexp-output)
528
932d1600 529(define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
838e17d8 530 "Recurse on GEXP and the expressions it refers to, summing the items
932d1600
LC
531returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
532second argument to 'delete-duplicates'."
2363bdd7
LC
533 (if (gexp? gexp)
534 (delete-duplicates
838e17d8 535 (append (self-attribute gexp)
2363bdd7
LC
536 (append-map (match-lambda
537 (($ <gexp-input> (? gexp? exp))
838e17d8 538 (gexp-attribute exp self-attribute))
2363bdd7
LC
539 (($ <gexp-input> (lst ...))
540 (append-map (lambda (item)
541 (if (gexp? item)
838e17d8
LC
542 (gexp-attribute item
543 self-attribute)
2363bdd7
LC
544 '()))
545 lst))
546 (_
547 '()))
932d1600
LC
548 (gexp-references gexp)))
549 equal?)
2363bdd7 550 '())) ;plain Scheme data type
0bb9929e 551
838e17d8
LC
552(define (gexp-modules gexp)
553 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
554false, meaning that GEXP is a plain Scheme object, return the empty list."
932d1600
LC
555 (define (module=? m1 m2)
556 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
557 ;; right-hand side may not be comparable with 'equal?': it's typically a
558 ;; file-like object that embeds a gexp, which in turn embeds closure;
559 ;; those closures may be 'eq?' when running compiled code but are unlikely
560 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
561 ;; avoid this discrepancy.
562 (match m1
563 (((name1 ...) '=> _)
564 (match m2
565 (((name2 ...) '=> _) (equal? name1 name2))
566 (_ #f)))
567 (_
568 (equal? m1 m2))))
569
570 (gexp-attribute gexp gexp-self-modules module=?))
838e17d8
LC
571
572(define (gexp-extensions gexp)
573 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
574GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
575list."
576 (gexp-attribute gexp gexp-self-extensions))
577
68a61e9f
LC
578(define* (lower-inputs inputs
579 #:key system target)
38685774
LC
580 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
581item (a \"source\"); return the corresponding input list as a monadic value.
582When TARGET is true, use it as the cross-compilation target triplet."
2ca41030
LC
583 (define (store-item? obj)
584 (and (string? obj) (store-path? obj)))
585
21b679f6 586 (with-monad %store-monad
b334674f
LC
587 (mapm %store-monad
588 (match-lambda
589 (((? struct? thing) sub-drv ...)
38685774 590 (mlet %store-monad ((obj (lower-object
b334674f 591 thing system #:target target)))
38685774
LC
592 (return (match obj
593 ((? derivation? drv)
594 (let ((outputs (if (null? sub-drv)
595 '("out")
596 sub-drv)))
597 (derivation-input drv outputs)))
598 ((? store-item? item)
599 item)))))
2ca41030 600 (((? store-item? item))
38685774 601 (return item)))
b334674f 602 inputs)))
21b679f6 603
b53833b2
LC
604(define* (lower-reference-graphs graphs #:key system target)
605 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
606#:reference-graphs argument, lower it such that each INPUT is replaced by the
38685774 607corresponding <derivation-input> or store item."
b53833b2
LC
608 (match graphs
609 (((file-names . inputs) ...)
610 (mlet %store-monad ((inputs (lower-inputs inputs
611 #:system system
612 #:target target)))
38685774 613 (return (map cons file-names inputs))))))
b53833b2 614
c8351d9a
LC
615(define* (lower-references lst #:key system target)
616 "Based on LST, a list of output names and packages, return a list of output
617names and file names suitable for the #:allowed-references argument to
618'derivation'."
c8351d9a
LC
619 (with-monad %store-monad
620 (define lower
621 (match-lambda
622 ((? string? output)
623 (return output))
accb682c 624 (($ <gexp-input> thing output native?)
c2b84676
LC
625 (mlet %store-monad ((drv (lower-object thing system
626 #:target (if native?
627 #f target))))
accb682c 628 (return (derivation->output-path drv output))))
bcb13287 629 (thing
c2b84676
LC
630 (mlet %store-monad ((drv (lower-object thing system
631 #:target target)))
c8351d9a
LC
632 (return (derivation->output-path drv))))))
633
b334674f 634 (mapm %store-monad lower lst)))
c8351d9a 635
ff40e9b7
LC
636(define default-guile-derivation
637 ;; Here we break the abstraction by talking to the higher-level layer.
638 ;; Thus, do the resolution lazily to hide the circular dependency.
639 (let ((proc (delay
640 (let ((iface (resolve-interface '(guix packages))))
641 (module-ref iface 'default-guile-derivation)))))
642 (lambda (system)
643 ((force proc) system))))
644
2ca41030 645;; Representation of a gexp instantiated for a given target and system.
38685774 646;; It's an intermediate representation between <gexp> and <derivation>.
2ca41030 647(define-record-type <lowered-gexp>
38685774 648 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
2ca41030
LC
649 lowered-gexp?
650 (sexp lowered-gexp-sexp) ;sexp
38685774
LC
651 (inputs lowered-gexp-inputs) ;list of <derivation-input>
652 (sources lowered-gexp-sources) ;list of store items
b9373e26 653 (guile lowered-gexp-guile) ;<derivation-input> | #f
2ca41030
LC
654 (load-path lowered-gexp-load-path) ;list of store items
655 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
656
657(define* (lower-gexp exp
658 #:key
659 (module-path %load-path)
660 (system (%current-system))
661 (target 'current)
662 (graft? (%graft?))
663 (guile-for-build (%guile-for-build))
664 (effective-version "2.2")
665
666 deprecation-warnings
667 (pre-load-modules? #t)) ;transitional
668 "*Note: This API is subject to change; use at your own risk!*
669
670Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
671<lowered-gexp> ready to be used.
672
673Lowered gexps are an intermediate representation that's useful for
674applications that deal with gexps outside in a way that is disconnected from
675derivations--e.g., code evaluated for its side effects."
676 (define %modules
677 (delete-duplicates (gexp-modules exp)))
678
679 (define (search-path modules extensions suffix)
680 (append (match modules
681 ((? derivation? drv)
682 (list (derivation->output-path drv)))
683 (#f
684 '())
685 ((? store-path? item)
686 (list item)))
687 (map (lambda (extension)
688 (string-append (match extension
689 ((? derivation? drv)
690 (derivation->output-path drv))
691 ((? store-path? item)
692 item))
693 suffix))
694 extensions)))
695
696 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
697 ;; '%current-target-system' to be looked up at >>=
698 ;; time.
699 (graft? (set-grafting graft?))
700
701 (system -> (or system (%current-system)))
702 (target -> (if (eq? target 'current)
703 (%current-target-system)
704 target))
705 (guile (if guile-for-build
706 (return guile-for-build)
707 (default-guile-derivation system)))
708 (normals (lower-inputs (gexp-inputs exp)
709 #:system system
710 #:target target))
711 (natives (lower-inputs (gexp-native-inputs exp)
712 #:system system
713 #:target #f))
714 (inputs -> (append normals natives))
715 (sexp (gexp->sexp exp
716 #:system system
717 #:target target))
718 (extensions -> (gexp-extensions exp))
719 (exts (mapm %store-monad
720 (lambda (obj)
721 (lower-object obj system))
722 extensions))
723 (modules (if (pair? %modules)
724 (imported-modules %modules
725 #:system system
726 #:module-path module-path)
727 (return #f)))
728 (compiled (if (pair? %modules)
729 (compiled-modules %modules
730 #:system system
731 #:module-path module-path
732 #:extensions extensions
733 #:guile guile
734 #:pre-load-modules?
735 pre-load-modules?
736 #:deprecation-warnings
737 deprecation-warnings)
738 (return #f))))
739 (define load-path
740 (search-path modules exts
741 (string-append "/share/guile/site/" effective-version)))
742
743 (define load-compiled-path
744 (search-path compiled exts
745 (string-append "/lib/guile/" effective-version
746 "/site-ccache")))
747
748 (mbegin %store-monad
749 (set-grafting graft?) ;restore the initial setting
750 (return (lowered-gexp sexp
38685774
LC
751 `(,@(if (derivation? modules)
752 (list (derivation-input modules))
2ca41030
LC
753 '())
754 ,@(if compiled
38685774 755 (list (derivation-input compiled))
2ca41030 756 '())
38685774
LC
757 ,@(map derivation-input exts)
758 ,@(filter derivation-input? inputs))
759 (filter string? (cons modules inputs))
b9373e26 760 (derivation-input guile '("out"))
2ca41030
LC
761 load-path
762 load-compiled-path)))))
763
21b679f6
LC
764(define* (gexp->derivation name exp
765 #:key
68a61e9f 766 system (target 'current)
21b679f6
LC
767 hash hash-algo recursive?
768 (env-vars '())
769 (modules '())
4684f301 770 (module-path %load-path)
21b679f6 771 (guile-for-build (%guile-for-build))
838e17d8 772 (effective-version "2.2")
ce45eb4c 773 (graft? (%graft?))
21b679f6 774 references-graphs
3f4ecf32 775 allowed-references disallowed-references
c0468155 776 leaked-env-vars
0309e1b0 777 local-build? (substitutable? #t)
8856f409 778 (properties '())
8df2eca6 779
a31174e8
LC
780 ;; TODO: This parameter is transitional; it's here
781 ;; to avoid a full rebuild. Remove it on the next
782 ;; rebuild cycle.
783 (pre-load-modules? #t)
784
a912c723 785 deprecation-warnings
0309e1b0 786 (script-name (string-append name "-builder")))
21b679f6 787 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
0309e1b0
LC
788derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
789TARGET is true, it is used as the cross-compilation target triplet for
790packages referred to by EXP.
21b679f6 791
0bb9929e
LC
792MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
793make MODULES available in the evaluation context of EXP; MODULES is a list of
4684f301 794names of Guile modules searched in MODULE-PATH to be copied in the store,
21b679f6
LC
795compiled, and made available in the load path during the execution of
796EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
797
838e17d8
LC
798EFFECTIVE-VERSION determines the string to use when adding extensions of
799EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
800
ce45eb4c
LC
801GRAFT? determines whether packages referred to by EXP should be grafted when
802applicable.
803
b53833b2
LC
804When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
805following forms:
806
807 (FILE-NAME PACKAGE)
808 (FILE-NAME PACKAGE OUTPUT)
809 (FILE-NAME DERIVATION)
810 (FILE-NAME DERIVATION OUTPUT)
811 (FILE-NAME STORE-ITEM)
812
813The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
814an input of the build process of EXP. In the build environment, each
815FILE-NAME contains the reference graph of the corresponding item, in a simple
816text format.
817
c8351d9a
LC
818ALLOWED-REFERENCES must be either #f or a list of output names and packages.
819In the latter case, the list denotes store items that the result is allowed to
820refer to. Any reference to another store item will lead to a build error.
3f4ecf32
LC
821Similarly for DISALLOWED-REFERENCES, which can list items that must not be
822referenced by the outputs.
b53833b2 823
a912c723
LC
824DEPRECATION-WARNINGS determines whether to show deprecation warnings while
825compiling modules. It can be #f, #t, or 'detailed.
826
21b679f6 827The other arguments are as for 'derivation'."
21b679f6 828 (define outputs (gexp-outputs exp))
2ca41030 829 (define requested-graft? graft?)
21b679f6 830
b53833b2
LC
831 (define (graphs-file-names graphs)
832 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
833 (map (match-lambda
38685774
LC
834 ((file-name . (? derivation-input? input))
835 (cons file-name (first (derivation-input-output-paths input))))
836 ((file-name . (? string? item))
837 (cons file-name item)))
b53833b2
LC
838 graphs))
839
2ca41030
LC
840 (define (add-modules exp modules)
841 (if (null? modules)
842 exp
843 (make-gexp (gexp-references exp)
844 (append modules (gexp-self-modules exp))
845 (gexp-self-extensions exp)
846 (gexp-proc exp))))
838e17d8
LC
847
848 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
ce45eb4c
LC
849 ;; '%current-target-system' to be looked up at >>=
850 ;; time.
851 (graft? (set-grafting graft?))
68a61e9f 852
5d098459 853 (system -> (or system (%current-system)))
68a61e9f
LC
854 (target -> (if (eq? target 'current)
855 (%current-target-system)
856 target))
2ca41030
LC
857 (exp -> (add-modules exp modules))
858 (lowered (lower-gexp exp
859 #:module-path module-path
860 #:system system
861 #:target target
862 #:graft? requested-graft?
863 #:guile-for-build
864 guile-for-build
865 #:effective-version
866 effective-version
867 #:deprecation-warnings
868 deprecation-warnings
869 #:pre-load-modules?
870 pre-load-modules?))
871
b53833b2
LC
872 (graphs (if references-graphs
873 (lower-reference-graphs references-graphs
874 #:system system
875 #:target target)
876 (return #f)))
c8351d9a
LC
877 (allowed (if allowed-references
878 (lower-references allowed-references
879 #:system system
880 #:target target)
881 (return #f)))
3f4ecf32
LC
882 (disallowed (if disallowed-references
883 (lower-references disallowed-references
884 #:system system
885 #:target target)
886 (return #f)))
2ca41030
LC
887 (guile -> (lowered-gexp-guile lowered))
888 (builder (text-file script-name
889 (object->string
890 (lowered-gexp-sexp lowered)))))
ce45eb4c
LC
891 (mbegin %store-monad
892 (set-grafting graft?) ;restore the initial setting
893 (raw-derivation name
b9373e26 894 (string-append (derivation-input-output-path guile)
ce45eb4c
LC
895 "/bin/guile")
896 `("--no-auto-compile"
2ca41030
LC
897 ,@(append-map (lambda (directory)
898 `("-L" ,directory))
899 (lowered-gexp-load-path lowered))
900 ,@(append-map (lambda (directory)
901 `("-C" ,directory))
902 (lowered-gexp-load-compiled-path lowered))
ce45eb4c
LC
903 ,builder)
904 #:outputs outputs
905 #:env-vars env-vars
906 #:system system
b9373e26 907 #:inputs `(,guile
38685774 908 ,@(lowered-gexp-inputs lowered)
ce45eb4c 909 ,@(match graphs
38685774
LC
910 (((_ . inputs) ...)
911 (filter derivation-input? inputs))
912 (#f '())))
913 #:sources `(,builder
914 ,@(if (and (string? modules)
915 (store-path? modules))
916 (list modules)
917 '())
918 ,@(lowered-gexp-sources lowered)
919 ,@(match graphs
920 (((_ . inputs) ...)
921 (filter string? inputs))
922 (#f '())))
923
ce45eb4c
LC
924 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
925 #:references-graphs (and=> graphs graphs-file-names)
926 #:allowed-references allowed
3f4ecf32 927 #:disallowed-references disallowed
c0468155 928 #:leaked-env-vars leaked-env-vars
4a6aeb67 929 #:local-build? local-build?
8856f409
LC
930 #:substitutable? substitutable?
931 #:properties properties))))
21b679f6 932
1123759b
LC
933(define* (gexp-inputs exp #:key native?)
934 "Return the input list for EXP. When NATIVE? is true, return only native
935references; otherwise, return only non-native references."
2ca41030 936 ;; TODO: Return <gexp-input> records instead of tuples.
21b679f6
LC
937 (define (add-reference-inputs ref result)
938 (match ref
1123759b
LC
939 (($ <gexp-input> (? gexp? exp) _ #t)
940 (if native?
941 (append (gexp-inputs exp)
942 (gexp-inputs exp #:native? #t)
943 result)
944 result))
945 (($ <gexp-input> (? gexp? exp) _ #f)
d343a60f
LC
946 (append (gexp-inputs exp #:native? native?)
947 result))
e39d1461
LC
948 (($ <gexp-input> (? string? str))
949 (if (direct-store-path? str)
950 (cons `(,str) result)
21b679f6 951 result))
5b14a790
LC
952 (($ <gexp-input> (? struct? thing) output n?)
953 (if (and (eqv? n? native?) (lookup-compiler thing))
bcb13287
LC
954 ;; THING is a derivation, or a package, or an origin, etc.
955 (cons `(,thing ,output) result)
956 result))
1123759b 957 (($ <gexp-input> (lst ...) output n?)
578dfbe0
LC
958 (fold-right add-reference-inputs result
959 ;; XXX: For now, automatically convert LST to a list of
960 ;; gexp-inputs. Inherit N?.
961 (map (match-lambda
962 ((? gexp-input? x)
963 (%gexp-input (gexp-input-thing x)
964 (gexp-input-output x)
965 n?))
966 (x
967 (%gexp-input x "out" n?)))
968 lst)))
21b679f6
LC
969 (_
970 ;; Ignore references to other kinds of objects.
971 result)))
972
973 (fold-right add-reference-inputs
974 '()
5b14a790 975 (gexp-references exp)))
667b2508
LC
976
977(define gexp-native-inputs
1123759b 978 (cut gexp-inputs <> #:native? #t))
21b679f6
LC
979
980(define (gexp-outputs exp)
981 "Return the outputs referred to by EXP as a list of strings."
982 (define (add-reference-output ref result)
983 (match ref
1e87da58 984 (($ <gexp-output> name)
21b679f6 985 (cons name result))
e39d1461 986 (($ <gexp-input> (? gexp? exp))
21b679f6 987 (append (gexp-outputs exp) result))
e39d1461
LC
988 (($ <gexp-input> (lst ...) output native?)
989 ;; XXX: Automatically convert LST.
0dbea56b
LC
990 (add-reference-output (map (match-lambda
991 ((? gexp-input? x) x)
992 (x (%gexp-input x "out" native?)))
993 lst)
e39d1461 994 result))
f9efe568
LC
995 ((lst ...)
996 (fold-right add-reference-output result lst))
21b679f6
LC
997 (_
998 result)))
999
7e75a673
LC
1000 (delete-duplicates
1001 (add-reference-output (gexp-references exp) '())))
21b679f6 1002
68a61e9f
LC
1003(define* (gexp->sexp exp #:key
1004 (system (%current-system))
1005 (target (%current-target-system)))
21b679f6
LC
1006 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
1007and in the current monad setting (system type, etc.)"
667b2508 1008 (define* (reference->sexp ref #:optional native?)
21b679f6
LC
1009 (with-monad %store-monad
1010 (match ref
1e87da58 1011 (($ <gexp-output> output)
bfd9eed9
LC
1012 ;; Output file names are not known in advance but the daemon defines
1013 ;; an environment variable for each of them at build time, so use
1014 ;; that trick.
1015 (return `((@ (guile) getenv) ,output)))
e39d1461 1016 (($ <gexp-input> (? gexp? exp) output n?)
667b2508
LC
1017 (gexp->sexp exp
1018 #:system system
e39d1461
LC
1019 #:target (if (or n? native?) #f target)))
1020 (($ <gexp-input> (refs ...) output n?)
b334674f
LC
1021 (mapm %store-monad
1022 (lambda (ref)
1023 ;; XXX: Automatically convert REF to an gexp-input.
1024 (reference->sexp
1025 (if (gexp-input? ref)
1026 ref
1027 (%gexp-input ref "out" n?))
1028 (or n? native?)))
1029 refs))
bcb13287 1030 (($ <gexp-input> (? struct? thing) output n?)
ebdfd776
LC
1031 (let ((target (if (or n? native?) #f target))
1032 (expand (lookup-expander thing)))
c2b84676
LC
1033 (mlet %store-monad ((obj (lower-object thing system
1034 #:target target)))
d9ae938f 1035 ;; OBJ must be either a derivation or a store file name.
ebdfd776 1036 (return (expand thing obj output)))))
e39d1461
LC
1037 (($ <gexp-input> x)
1038 (return x))
21b679f6
LC
1039 (x
1040 (return x)))))
1041
1042 (mlet %store-monad
b334674f
LC
1043 ((args (mapm %store-monad
1044 reference->sexp (gexp-references exp))))
21b679f6
LC
1045 (return (apply (gexp-proc exp) args))))
1046
21b679f6
LC
1047(define (syntax-location-string s)
1048 "Return a string representing the source code location of S."
1049 (let ((props (syntax-source s)))
1050 (if props
1051 (let ((file (assoc-ref props 'filename))
1052 (line (and=> (assoc-ref props 'line) 1+))
1053 (column (assoc-ref props 'column)))
1054 (if file
1055 (simple-format #f "~a:~a:~a"
1056 file line column)
1057 (simple-format #f "~a:~a" line column)))
1058 "<unknown location>")))
1059
8245bb74
LC
1060(define-syntax-rule (define-syntax-parameter-once name proc)
1061 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1062 ;; does not get redefined. This works around a race condition in a
1063 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1064 (eval-when (load eval expand compile)
1065 (define name
1066 (if (module-locally-bound? (current-module) 'name)
1067 (module-ref (current-module) 'name)
1068 (make-syntax-transformer 'name 'syntax-parameter
1069 (list proc))))))
1070
1071(define-syntax-parameter-once current-imported-modules
0bb9929e
LC
1072 ;; Current list of imported modules.
1073 (identifier-syntax '()))
1074
1075(define-syntax-rule (with-imported-modules modules body ...)
1076 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1077environment."
1078 (syntax-parameterize ((current-imported-modules
1079 (identifier-syntax modules)))
1080 body ...))
1081
8245bb74 1082(define-syntax-parameter-once current-imported-extensions
838e17d8
LC
1083 ;; Current list of extensions.
1084 (identifier-syntax '()))
1085
1086(define-syntax-rule (with-extensions extensions body ...)
1087 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1088execution environment."
1089 (syntax-parameterize ((current-imported-extensions
1090 (identifier-syntax extensions)))
1091 body ...))
1092
21b679f6
LC
1093(define-syntax gexp
1094 (lambda (s)
1095 (define (collect-escapes exp)
1096 ;; Return all the 'ungexp' present in EXP.
1097 (let loop ((exp exp)
1098 (result '()))
607e1b51
LC
1099 (syntax-case exp (ungexp
1100 ungexp-splicing
1101 ungexp-native
1102 ungexp-native-splicing)
21b679f6
LC
1103 ((ungexp _)
1104 (cons exp result))
1105 ((ungexp _ _)
1106 (cons exp result))
1107 ((ungexp-splicing _ ...)
1108 (cons exp result))
607e1b51 1109 ((ungexp-native _ ...)
667b2508
LC
1110 (cons exp result))
1111 ((ungexp-native-splicing _ ...)
1112 (cons exp result))
5e2e4a51 1113 ((exp0 . exp)
667b2508 1114 (let ((result (loop #'exp0 result)))
5e2e4a51 1115 (loop #'exp result)))
667b2508
LC
1116 (_
1117 result))))
1118
21b679f6
LC
1119 (define (escape->ref exp)
1120 ;; Turn 'ungexp' form EXP into a "reference".
667b2508
LC
1121 (syntax-case exp (ungexp ungexp-splicing
1122 ungexp-native ungexp-native-splicing
1123 output)
21b679f6 1124 ((ungexp output)
1e87da58 1125 #'(gexp-output "out"))
21b679f6 1126 ((ungexp output name)
1e87da58 1127 #'(gexp-output name))
21b679f6 1128 ((ungexp thing)
0dbea56b 1129 #'(%gexp-input thing "out" #f))
21b679f6 1130 ((ungexp drv-or-pkg out)
0dbea56b 1131 #'(%gexp-input drv-or-pkg out #f))
21b679f6 1132 ((ungexp-splicing lst)
0dbea56b 1133 #'(%gexp-input lst "out" #f))
667b2508 1134 ((ungexp-native thing)
0dbea56b 1135 #'(%gexp-input thing "out" #t))
667b2508 1136 ((ungexp-native drv-or-pkg out)
0dbea56b 1137 #'(%gexp-input drv-or-pkg out #t))
667b2508 1138 ((ungexp-native-splicing lst)
0dbea56b 1139 #'(%gexp-input lst "out" #t))))
21b679f6 1140
667b2508
LC
1141 (define (substitute-ungexp exp substs)
1142 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1143 ;; the corresponding form in SUBSTS.
1144 (match (assoc exp substs)
1145 ((_ id)
1146 id)
4a6e889f
LC
1147 (_ ;internal error
1148 (with-syntax ((exp exp))
1149 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
667b2508
LC
1150
1151 (define (substitute-ungexp-splicing exp substs)
1152 (syntax-case exp ()
1153 ((exp rest ...)
1154 (match (assoc #'exp substs)
1155 ((_ id)
1156 (with-syntax ((id id))
1157 #`(append id
1158 #,(substitute-references #'(rest ...) substs))))
1159 (_
1160 #'(syntax-error "error: no 'ungexp-splicing' substitution"
4a6e889f 1161 exp))))))
667b2508 1162
21b679f6
LC
1163 (define (substitute-references exp substs)
1164 ;; Return a variant of EXP where all the cars of SUBSTS have been
1165 ;; replaced by the corresponding cdr.
667b2508
LC
1166 (syntax-case exp (ungexp ungexp-native
1167 ungexp-splicing ungexp-native-splicing)
21b679f6 1168 ((ungexp _ ...)
667b2508
LC
1169 (substitute-ungexp exp substs))
1170 ((ungexp-native _ ...)
1171 (substitute-ungexp exp substs))
21b679f6 1172 (((ungexp-splicing _ ...) rest ...)
667b2508
LC
1173 (substitute-ungexp-splicing exp substs))
1174 (((ungexp-native-splicing _ ...) rest ...)
1175 (substitute-ungexp-splicing exp substs))
5e2e4a51 1176 ((exp0 . exp)
21b679f6 1177 #`(cons #,(substitute-references #'exp0 substs)
5e2e4a51 1178 #,(substitute-references #'exp substs)))
21b679f6
LC
1179 (x #''x)))
1180
1181 (syntax-case s (ungexp output)
1182 ((_ exp)
affd7761 1183 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
21b679f6
LC
1184 (formals (generate-temporaries escapes))
1185 (sexp (substitute-references #'exp (zip escapes formals)))
affd7761
LC
1186 (refs (map escape->ref escapes)))
1187 #`(make-gexp (list #,@refs)
0bb9929e 1188 current-imported-modules
838e17d8 1189 current-imported-extensions
21b679f6
LC
1190 (lambda #,formals
1191 #,sexp)))))))
1192
1193\f
aa72d9af
LC
1194;;;
1195;;; Module handling.
1196;;;
1197
8df2eca6
LC
1198(define %not-slash
1199 (char-set-complement (char-set #\/)))
1200
1201(define (file-mapping->tree mapping)
1202 "Convert MAPPING, an alist like:
1203
1204 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1205
1206to a tree suitable for 'interned-file-tree'."
1207 (let ((mapping (map (match-lambda
1208 ((destination . source)
1209 (cons (string-tokenize destination
1210 %not-slash)
1211 source)))
1212 mapping)))
1213 (fold (lambda (pair result)
1214 (match pair
1215 ((destination . source)
1216 (let loop ((destination destination)
1217 (result result))
1218 (match destination
1219 ((file)
1220 (let* ((mode (stat:mode (stat source)))
1221 (type (if (zero? (logand mode #o100))
1222 'regular
1223 'executable)))
1224 (alist-cons file
1225 `(,type (file ,source))
1226 result)))
1227 ((file rest ...)
1228 (let ((directory (assoc-ref result file)))
1229 (alist-cons file
1230 `(directory
1231 ,@(loop rest
1232 (match directory
1233 (('directory . entries) entries)
1234 (#f '()))))
1235 (if directory
1236 (alist-delete file result)
1237 result)))))))))
1238 '()
1239 mapping)))
1240
df2d51f0
LC
1241(define %utils-module
1242 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
a9601e23
LC
1243 ;; other primitives below. Note: We give the file name relative to this
1244 ;; file you are currently reading; 'search-path' could return a file name
1245 ;; relative to the current working directory.
1246 (local-file "build/utils.scm"
df2d51f0 1247 "build-utils.scm"))
aa72d9af 1248
8df2eca6
LC
1249(define* (imported-files/derivation files
1250 #:key (name "file-import")
e529d468 1251 (symlink? #f)
8df2eca6 1252 (system (%current-system))
8afa18d6 1253 (guile (%guile-for-build)))
aa72d9af 1254 "Return a derivation that imports FILES into STORE. FILES must be a list
d938a58b
LC
1255of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1256resulting store path. FILE can be either a file name, or a file-like object,
e529d468
LC
1257as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1258to the source files instead of copying them."
aa72d9af
LC
1259 (define file-pair
1260 (match-lambda
d938a58b 1261 ((final-path . (? string? file-name))
aa72d9af
LC
1262 (mlet %store-monad ((file (interned-file file-name
1263 (basename final-path))))
d938a58b
LC
1264 (return (list final-path file))))
1265 ((final-path . file-like)
1266 (mlet %store-monad ((file (lower-object file-like system)))
aa72d9af
LC
1267 (return (list final-path file))))))
1268
b334674f 1269 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
aa72d9af
LC
1270 (define build
1271 (gexp
1272 (begin
df2d51f0 1273 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
aa72d9af
LC
1274 (use-modules (ice-9 match))
1275
aa72d9af
LC
1276 (mkdir (ungexp output)) (chdir (ungexp output))
1277 (for-each (match-lambda
1278 ((final-path store-path)
1279 (mkdir-p (dirname final-path))
e529d468
LC
1280 ((ungexp (if symlink? 'symlink 'copy-file))
1281 store-path final-path)))
aa72d9af
LC
1282 '(ungexp files)))))
1283
1284 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1285 ;; exactly the same regardless of FILES: less disk space, and fewer
1286 ;; 'add-to-store' RPCs.
1287 (gexp->derivation name build
1288 #:system system
1289 #:guile-for-build guile
30d722c3
LC
1290 #:local-build? #t
1291
8afa18d6
LC
1292 ;; Avoid deprecation warnings about the use of the _IO*
1293 ;; constants in (guix build utils).
30d722c3 1294 #:env-vars
8afa18d6 1295 '(("GUILE_WARN_DEPRECATED" . "no")))))
aa72d9af 1296
8df2eca6
LC
1297(define* (imported-files files
1298 #:key (name "file-import")
8df2eca6
LC
1299 ;; The following parameters make sense when creating
1300 ;; an actual derivation.
1301 (system (%current-system))
8afa18d6 1302 (guile (%guile-for-build)))
8df2eca6
LC
1303 "Import FILES into the store and return the resulting derivation or store
1304file name (a derivation is created if and only if some elements of FILES are
1305file-like objects and not local file names.) FILES must be a list
1306of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1307resulting store path. FILE can be either a file name, or a file-like object,
1308as returned by 'local-file' for example."
8c7bebd6
LC
1309 (if (any (match-lambda
1310 ((_ . (? struct? source)) #t)
1311 (_ #f))
1312 files)
8df2eca6 1313 (imported-files/derivation files #:name name
e529d468 1314 #:symlink? derivation?
8afa18d6 1315 #:system system #:guile guile)
8df2eca6
LC
1316 (interned-file-tree `(,name directory
1317 ,@(file-mapping->tree files)))))
1318
aa72d9af
LC
1319(define* (imported-modules modules
1320 #:key (name "module-import")
1321 (system (%current-system))
1322 (guile (%guile-for-build))
8afa18d6 1323 (module-path %load-path))
aa72d9af 1324 "Return a derivation that contains the source files of MODULES, a list of
d938a58b
LC
1325module names such as `(ice-9 q)'. All of MODULES must be either names of
1326modules to be found in the MODULE-PATH search path, or a module name followed
1327by an arrow followed by a file-like object. For example:
1328
1329 (imported-modules `((guix build utils)
1330 (guix gcrypt)
1331 ((guix config) => ,(scheme-file …))))
1332
1333In this example, the first two modules are taken from MODULE-PATH, and the
1334last one is created from the given <scheme-file> object."
4d20d87b
LC
1335 (let ((files (map (match-lambda
1336 (((module ...) '=> file)
1337 (cons (module->source-file-name module)
1338 file))
1339 ((module ...)
1340 (let ((f (module->source-file-name module)))
1341 (cons f (search-path* module-path f)))))
1342 modules)))
8df2eca6 1343 (imported-files files #:name name
8df2eca6 1344 #:system system
8afa18d6 1345 #:guile guile)))
aa72d9af
LC
1346
1347(define* (compiled-modules modules
1348 #:key (name "module-import-compiled")
1349 (system (%current-system))
2cc5ec7f 1350 target
aa72d9af 1351 (guile (%guile-for-build))
a912c723 1352 (module-path %load-path)
838e17d8 1353 (extensions '())
a31174e8
LC
1354 (deprecation-warnings #f)
1355
1356 ;; TODO: This flag is here to prevent a full
1357 ;; rebuild. Remove it on the next rebuild cycle.
1358 (pre-load-modules? #t))
aa72d9af
LC
1359 "Return a derivation that builds a tree containing the `.go' files
1360corresponding to MODULES. All the MODULES are built in a context where
2cc5ec7f
LC
1361they can refer to each other. When TARGET is true, cross-compile MODULES for
1362TARGET, a GNU triplet."
d3292275
LC
1363 (define total (length modules))
1364
aa72d9af
LC
1365 (mlet %store-monad ((modules (imported-modules modules
1366 #:system system
1367 #:guile guile
1368 #:module-path
8afa18d6 1369 module-path)))
aa72d9af
LC
1370 (define build
1371 (gexp
1372 (begin
df2d51f0
LC
1373 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1374
aa72d9af 1375 (use-modules (ice-9 ftw)
d3292275
LC
1376 (ice-9 format)
1377 (srfi srfi-1)
aa72d9af
LC
1378 (srfi srfi-26)
1379 (system base compile))
1380
2cc5ec7f
LC
1381 ;; TODO: Inline this on the next rebuild cycle.
1382 (ungexp-splicing
1383 (if target
1384 (gexp ((use-modules (system base target))))
1385 (gexp ())))
1386
aa72d9af
LC
1387 (define (regular? file)
1388 (not (member file '("." ".."))))
1389
d3292275 1390 (define (process-entry entry output processed)
e640c9e6
LC
1391 (if (file-is-directory? entry)
1392 (let ((output (string-append output "/" (basename entry))))
1393 (mkdir-p output)
d3292275 1394 (process-directory entry output processed))
e640c9e6
LC
1395 (let* ((base (basename entry ".scm"))
1396 (output (string-append output "/" base ".go")))
d3292275 1397 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
a31174e8
LC
1398 (+ 1 processed
1399 (ungexp-splicing (if pre-load-modules?
1400 (gexp ((ungexp total)))
1401 (gexp ()))))
1402 (ungexp (* total (if pre-load-modules? 2 1)))
1403 entry)
2cc5ec7f
LC
1404
1405 (ungexp-splicing
1406 (if target
1407 (gexp ((with-target (ungexp target)
1408 (lambda ()
1409 (compile-file entry
1410 #:output-file output
1411 #:opts
1412 %auto-compilation-options)))))
1413 (gexp ((compile-file entry
1414 #:output-file output
1415 #:opts %auto-compilation-options)))))
1416
d3292275 1417 (+ 1 processed))))
e640c9e6 1418
d3292275 1419 (define (process-directory directory output processed)
aa72d9af
LC
1420 (let ((entries (map (cut string-append directory "/" <>)
1421 (scandir directory regular?))))
d3292275
LC
1422 (fold (cut process-entry <> output <>)
1423 processed
1424 entries)))
1425
1426 (setvbuf (current-output-port)
1427 (cond-expand (guile-2.2 'line) (else _IOLBF)))
aa72d9af 1428
4a42abc5
LC
1429 (define mkdir-p
1430 ;; Capture 'mkdir-p'.
1431 (@ (guix build utils) mkdir-p))
5d669883 1432
838e17d8 1433 ;; Add EXTENSIONS to the search path.
4a42abc5
LC
1434 (set! %load-path
1435 (append (map (lambda (extension)
1436 (string-append extension
1437 "/share/guile/site/"
1438 (effective-version)))
1439 '((ungexp-native-splicing extensions)))
1440 %load-path))
1441 (set! %load-compiled-path
1442 (append (map (lambda (extension)
1443 (string-append extension "/lib/guile/"
1444 (effective-version)
1445 "/site-ccache"))
1446 '((ungexp-native-splicing extensions)))
1447 %load-compiled-path))
838e17d8 1448
aa72d9af 1449 (set! %load-path (cons (ungexp modules) %load-path))
5d669883 1450
4a42abc5
LC
1451 ;; Above we loaded our own (guix build utils) but now we may need to
1452 ;; load a compile a different one. Thus, force a reload.
1453 (let ((utils (string-append (ungexp modules)
1454 "/guix/build/utils.scm")))
1455 (when (file-exists? utils)
1456 (load utils)))
5d669883 1457
aa72d9af
LC
1458 (mkdir (ungexp output))
1459 (chdir (ungexp modules))
a31174e8
LC
1460
1461 (ungexp-splicing
1462 (if pre-load-modules?
1463 (gexp ((define* (load-from-directory directory
1464 #:optional (loaded 0))
1465 "Load all the source files found in DIRECTORY."
1466 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1467 (let ((entries (map (cut string-append directory "/" <>)
1468 (scandir directory regular?))))
1469 (fold (lambda (file loaded)
1470 (if (file-is-directory? file)
1471 (load-from-directory file loaded)
1472 (begin
1473 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1474 (+ 1 loaded)
1475 (ungexp (* 2 total))
1476 file)
1477 (save-module-excursion
1478 (lambda ()
1479 (primitive-load file)))
1480 (+ 1 loaded))))
1481 loaded
1482 entries)))
1483
1484 (load-from-directory ".")))
1485 (gexp ())))
1486
d3292275 1487 (process-directory "." (ungexp output) 0))))
aa72d9af
LC
1488
1489 ;; TODO: Pass MODULES as an environment variable.
1490 (gexp->derivation name build
1491 #:system system
1492 #:guile-for-build guile
a912c723
LC
1493 #:local-build? #t
1494 #:env-vars
1495 (case deprecation-warnings
1496 ((#f)
1497 '(("GUILE_WARN_DEPRECATED" . "no")))
1498 ((detailed)
1499 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1500 (else
1501 '())))))
aa72d9af
LC
1502
1503\f
21b679f6
LC
1504;;;
1505;;; Convenience procedures.
1506;;;
1507
53e89b17 1508(define (default-guile)
6ee797f3
LC
1509 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1510 ;; programs returned by 'program-file' and we don't want to keep references
1511 ;; to several Guile packages). This module must not refer to (gnu …)
53e89b17 1512 ;; modules directly, to avoid circular dependencies, hence this hack.
6ee797f3
LC
1513 (module-ref (resolve-interface '(gnu packages guile))
1514 'guile-2.2))
53e89b17 1515
838e17d8 1516(define* (load-path-expression modules #:optional (path %load-path)
2e8cabb8 1517 #:key (extensions '()) system target)
dd8d1a30 1518 "Return as a monadic value a gexp that sets '%load-path' and
1ae16033 1519'%load-compiled-path' to point to MODULES, a list of module names. MODULES
efff3245
LC
1520are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1521 (if (and (null? modules) (null? extensions))
1522 (with-monad %store-monad
1523 (return #f))
1524 (mlet %store-monad ((modules (imported-modules modules
2e8cabb8
LC
1525 #:module-path path
1526 #:system system))
efff3245
LC
1527 (compiled (compiled-modules modules
1528 #:extensions extensions
2e8cabb8
LC
1529 #:module-path path
1530 #:system system
1531 #:target target)))
efff3245
LC
1532 (return (gexp (eval-when (expand load eval)
1533 (set! %load-path
1534 (cons (ungexp modules)
1535 (append (map (lambda (extension)
1536 (string-append extension
1537 "/share/guile/site/"
1538 (effective-version)))
1539 '((ungexp-native-splicing extensions)))
1540 %load-path)))
1541 (set! %load-compiled-path
1542 (cons (ungexp compiled)
1543 (append (map (lambda (extension)
1544 (string-append extension
1545 "/lib/guile/"
1546 (effective-version)
1547 "/site-ccache"))
1548 '((ungexp-native-splicing extensions)))
1549 %load-compiled-path)))))))))
dd8d1a30 1550
21b679f6 1551(define* (gexp->script name exp
1ae16033 1552 #:key (guile (default-guile))
2e8cabb8
LC
1553 (module-path %load-path)
1554 (system (%current-system))
1555 target)
9c14a487 1556 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1ae16033 1557imported modules in its search path. Look up EXP's modules in MODULE-PATH."
9c14a487 1558 (mlet %store-monad ((set-load-path
1ae16033 1559 (load-path-expression (gexp-modules exp)
838e17d8
LC
1560 module-path
1561 #:extensions
2e8cabb8
LC
1562 (gexp-extensions exp)
1563 #:system system
1564 #:target target)))
21b679f6
LC
1565 (gexp->derivation name
1566 (gexp
1567 (call-with-output-file (ungexp output)
1568 (lambda (port)
c17b5ab4
LC
1569 ;; Note: that makes a long shebang. When the store
1570 ;; is /gnu/store, that fits within the 128-byte
1571 ;; limit imposed by Linux, but that may go beyond
1572 ;; when running tests.
21b679f6
LC
1573 (format port
1574 "#!~a/bin/guile --no-auto-compile~%!#~%"
1575 (ungexp guile))
4a4cbd0b 1576
efff3245
LC
1577 (ungexp-splicing
1578 (if set-load-path
1579 (gexp ((write '(ungexp set-load-path) port)))
1580 (gexp ())))
1581
21b679f6 1582 (write '(ungexp exp) port)
1ae16033 1583 (chmod port #o555))))
2e8cabb8
LC
1584 #:system system
1585 #:target target
1ae16033 1586 #:module-path module-path)))
21b679f6 1587
1ae16033
LC
1588(define* (gexp->file name exp #:key
1589 (set-load-path? #t)
4fbd1a2b
LC
1590 (module-path %load-path)
1591 (splice? #f))
1592 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1593is true, EXP is considered to be a list of expressions that will be spliced in
1594the resulting file.
1595
1596When SET-LOAD-PATH? is true, emit code in the resulting file to set
1597'%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1598Lookup EXP's modules in MODULE-PATH."
838e17d8
LC
1599 (define modules (gexp-modules exp))
1600 (define extensions (gexp-extensions exp))
1601
1602 (if (or (not set-load-path?)
1603 (and (null? modules) (null? extensions)))
1604 (gexp->derivation name
1605 (gexp
1606 (call-with-output-file (ungexp output)
1607 (lambda (port)
1608 (for-each (lambda (exp)
1609 (write exp port))
1610 '(ungexp (if splice?
1611 exp
1612 (gexp ((ungexp exp)))))))))
1613 #:local-build? #t
1614 #:substitutable? #f)
1615 (mlet %store-monad ((set-load-path
1616 (load-path-expression modules module-path
1617 #:extensions extensions)))
1618 (gexp->derivation name
1619 (gexp
1620 (call-with-output-file (ungexp output)
1621 (lambda (port)
1622 (write '(ungexp set-load-path) port)
1623 (for-each (lambda (exp)
1624 (write exp port))
1625 '(ungexp (if splice?
1626 exp
1627 (gexp ((ungexp exp)))))))))
1628 #:module-path module-path
1629 #:local-build? #t
1630 #:substitutable? #f))))
21b679f6 1631
462a3fa3
LC
1632(define* (text-file* name #:rest text)
1633 "Return as a monadic value a derivation that builds a text file containing
d9ae938f
LC
1634all of TEXT. TEXT may list, in addition to strings, objects of any type that
1635can be used in a gexp: packages, derivations, local file objects, etc. The
1636resulting store file holds references to all these."
462a3fa3
LC
1637 (define builder
1638 (gexp (call-with-output-file (ungexp output "out")
1639 (lambda (port)
1640 (display (string-append (ungexp-splicing text)) port)))))
1641
851b6f62
LC
1642 (gexp->derivation name builder
1643 #:local-build? #t
1644 #:substitutable? #f))
462a3fa3 1645
b751cde3
LC
1646(define* (mixed-text-file name #:rest text)
1647 "Return an object representing store file NAME containing TEXT. TEXT is a
1648sequence of strings and file-like objects, as in:
1649
1650 (mixed-text-file \"profile\"
1651 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1652
1653This is the declarative counterpart of 'text-file*'."
1654 (define build
1655 (gexp (call-with-output-file (ungexp output "out")
1656 (lambda (port)
1657 (display (string-append (ungexp-splicing text)) port)))))
1658
1659 (computed-file name build))
1660
dedb512f
LC
1661(define (file-union name files)
1662 "Return a <computed-file> that builds a directory containing all of FILES.
1663Each item in FILES must be a two-element list where the first element is the
1664file name to use in the new directory, and the second element is a gexp
1665denoting the target file. Here's an example:
1666
1667 (file-union \"etc\"
1668 `((\"hosts\" ,(plain-file \"hosts\"
1669 \"127.0.0.1 localhost\"))
1670 (\"bashrc\" ,(plain-file \"bashrc\"
5dec93bb
LC
1671 \"alias ls='ls --color'\"))
1672 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
dedb512f
LC
1673
1674This yields an 'etc' directory containing these two files."
1675 (computed-file name
5dec93bb
LC
1676 (with-imported-modules '((guix build utils))
1677 (gexp
1678 (begin
1679 (use-modules (guix build utils))
1680
1681 (mkdir (ungexp output))
1682 (chdir (ungexp output))
1683 (ungexp-splicing
1684 (map (match-lambda
1685 ((target source)
1686 (gexp
1687 (begin
1688 ;; Stat the source to abort early if it does
1689 ;; not exist.
1690 (stat (ungexp source))
1691
1692 (mkdir-p (dirname (ungexp target)))
1693 (symlink (ungexp source)
1694 (ungexp target))))))
1695 files)))))))
dedb512f 1696
59523429 1697(define* (directory-union name things
b244ae25
LC
1698 #:key (copy? #f) (quiet? #f)
1699 (resolve-collision 'warn-about-collision))
d298c815
LC
1700 "Return a directory that is the union of THINGS, where THINGS is a list of
1701file-like objects denoting directories. For example:
1702
1703 (directory-union \"guile+emacs\" (list guile emacs))
1704
59523429
LC
1705yields a directory that is the union of the 'guile' and 'emacs' packages.
1706
b244ae25
LC
1707Call RESOLVE-COLLISION when several files collide, passing it the list of
1708colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1709which case the colliding entry is skipped altogether.
1710
de98b302
LC
1711When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1712is true, the derivation will not print anything."
59523429
LC
1713 (define symlink
1714 (if copy?
1715 (gexp (lambda (old new)
1716 (if (file-is-directory? old)
1717 (symlink old new)
1718 (copy-file old new))))
1719 (gexp symlink)))
1720
de98b302
LC
1721 (define log-port
1722 (if quiet?
1723 (gexp (%make-void-port "w"))
1724 (gexp (current-error-port))))
1725
d298c815
LC
1726 (match things
1727 ((one)
1728 ;; Only one thing; return it.
1729 one)
1730 (_
1731 (computed-file name
1732 (with-imported-modules '((guix build union))
1733 (gexp (begin
b244ae25
LC
1734 (use-modules (guix build union)
1735 (srfi srfi-1)) ;for 'first' and 'last'
1736
d298c815 1737 (union-build (ungexp output)
59523429
LC
1738 '(ungexp things)
1739
de98b302 1740 #:log-port (ungexp log-port)
b244ae25
LC
1741 #:symlink (ungexp symlink)
1742 #:resolve-collision
1743 (ungexp resolve-collision)))))))))
d298c815 1744
21b679f6
LC
1745\f
1746;;;
1747;;; Syntactic sugar.
1748;;;
1749
1750(eval-when (expand load eval)
667b2508
LC
1751 (define* (read-ungexp chr port #:optional native?)
1752 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1753true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
21b679f6
LC
1754 (define unquote-symbol
1755 (match (peek-char port)
1756 (#\@
1757 (read-char port)
667b2508
LC
1758 (if native?
1759 'ungexp-native-splicing
1760 'ungexp-splicing))
21b679f6 1761 (_
667b2508
LC
1762 (if native?
1763 'ungexp-native
1764 'ungexp))))
21b679f6
LC
1765
1766 (match (read port)
1767 ((? symbol? symbol)
1768 (let ((str (symbol->string symbol)))
1769 (match (string-index-right str #\:)
1770 (#f
1771 `(,unquote-symbol ,symbol))
1772 (colon
1773 (let ((name (string->symbol (substring str 0 colon)))
1774 (output (substring str (+ colon 1))))
1775 `(,unquote-symbol ,name ,output))))))
1776 (x
1777 `(,unquote-symbol ,x))))
1778
1779 (define (read-gexp chr port)
1780 "Read a 'gexp' form from PORT."
1781 `(gexp ,(read port)))
1782
1783 ;; Extend the reader
1784 (read-hash-extend #\~ read-gexp)
667b2508
LC
1785 (read-hash-extend #\$ read-ungexp)
1786 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
21b679f6
LC
1787
1788;;; gexp.scm ends here