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