gexp: 'load-path-expression' produces an expression that deletes duplicates.
[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.)"
24ab804c
LC
1008 (define (self-quoting? x)
1009 (letrec-syntax ((one-of (syntax-rules ()
1010 ((_) #f)
1011 ((_ pred rest ...)
1012 (or (pred x)
1013 (one-of rest ...))))))
1014 (one-of symbol? string? keyword? pair? null? array?
1015 number? boolean?)))
1016
667b2508 1017 (define* (reference->sexp ref #:optional native?)
21b679f6
LC
1018 (with-monad %store-monad
1019 (match ref
1e87da58 1020 (($ <gexp-output> output)
bfd9eed9
LC
1021 ;; Output file names are not known in advance but the daemon defines
1022 ;; an environment variable for each of them at build time, so use
1023 ;; that trick.
1024 (return `((@ (guile) getenv) ,output)))
e39d1461 1025 (($ <gexp-input> (? gexp? exp) output n?)
667b2508
LC
1026 (gexp->sexp exp
1027 #:system system
e39d1461
LC
1028 #:target (if (or n? native?) #f target)))
1029 (($ <gexp-input> (refs ...) output n?)
b334674f
LC
1030 (mapm %store-monad
1031 (lambda (ref)
1032 ;; XXX: Automatically convert REF to an gexp-input.
1033 (reference->sexp
1034 (if (gexp-input? ref)
1035 ref
1036 (%gexp-input ref "out" n?))
1037 (or n? native?)))
1038 refs))
bcb13287 1039 (($ <gexp-input> (? struct? thing) output n?)
ebdfd776
LC
1040 (let ((target (if (or n? native?) #f target))
1041 (expand (lookup-expander thing)))
c2b84676
LC
1042 (mlet %store-monad ((obj (lower-object thing system
1043 #:target target)))
d9ae938f 1044 ;; OBJ must be either a derivation or a store file name.
ebdfd776 1045 (return (expand thing obj output)))))
24ab804c 1046 (($ <gexp-input> (? self-quoting? x))
e39d1461 1047 (return x))
24ab804c
LC
1048 (($ <gexp-input> x)
1049 (raise (condition (&gexp-input-error (input x)))))
21b679f6
LC
1050 (x
1051 (return x)))))
1052
1053 (mlet %store-monad
b334674f
LC
1054 ((args (mapm %store-monad
1055 reference->sexp (gexp-references exp))))
21b679f6
LC
1056 (return (apply (gexp-proc exp) args))))
1057
8245bb74
LC
1058(define-syntax-rule (define-syntax-parameter-once name proc)
1059 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1060 ;; does not get redefined. This works around a race condition in a
1061 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1062 (eval-when (load eval expand compile)
1063 (define name
1064 (if (module-locally-bound? (current-module) 'name)
1065 (module-ref (current-module) 'name)
1066 (make-syntax-transformer 'name 'syntax-parameter
1067 (list proc))))))
1068
1069(define-syntax-parameter-once current-imported-modules
0bb9929e
LC
1070 ;; Current list of imported modules.
1071 (identifier-syntax '()))
1072
1073(define-syntax-rule (with-imported-modules modules body ...)
1074 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1075environment."
1076 (syntax-parameterize ((current-imported-modules
1077 (identifier-syntax modules)))
1078 body ...))
1079
8245bb74 1080(define-syntax-parameter-once current-imported-extensions
838e17d8
LC
1081 ;; Current list of extensions.
1082 (identifier-syntax '()))
1083
1084(define-syntax-rule (with-extensions extensions body ...)
1085 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1086execution environment."
1087 (syntax-parameterize ((current-imported-extensions
1088 (identifier-syntax extensions)))
1089 body ...))
1090
21b679f6
LC
1091(define-syntax gexp
1092 (lambda (s)
1093 (define (collect-escapes exp)
1094 ;; Return all the 'ungexp' present in EXP.
1095 (let loop ((exp exp)
1096 (result '()))
607e1b51
LC
1097 (syntax-case exp (ungexp
1098 ungexp-splicing
1099 ungexp-native
1100 ungexp-native-splicing)
21b679f6
LC
1101 ((ungexp _)
1102 (cons exp result))
1103 ((ungexp _ _)
1104 (cons exp result))
1105 ((ungexp-splicing _ ...)
1106 (cons exp result))
607e1b51 1107 ((ungexp-native _ ...)
667b2508
LC
1108 (cons exp result))
1109 ((ungexp-native-splicing _ ...)
1110 (cons exp result))
5e2e4a51 1111 ((exp0 . exp)
667b2508 1112 (let ((result (loop #'exp0 result)))
5e2e4a51 1113 (loop #'exp result)))
667b2508
LC
1114 (_
1115 result))))
1116
21b679f6
LC
1117 (define (escape->ref exp)
1118 ;; Turn 'ungexp' form EXP into a "reference".
667b2508
LC
1119 (syntax-case exp (ungexp ungexp-splicing
1120 ungexp-native ungexp-native-splicing
1121 output)
21b679f6 1122 ((ungexp output)
1e87da58 1123 #'(gexp-output "out"))
21b679f6 1124 ((ungexp output name)
1e87da58 1125 #'(gexp-output name))
21b679f6 1126 ((ungexp thing)
0dbea56b 1127 #'(%gexp-input thing "out" #f))
21b679f6 1128 ((ungexp drv-or-pkg out)
0dbea56b 1129 #'(%gexp-input drv-or-pkg out #f))
21b679f6 1130 ((ungexp-splicing lst)
0dbea56b 1131 #'(%gexp-input lst "out" #f))
667b2508 1132 ((ungexp-native thing)
0dbea56b 1133 #'(%gexp-input thing "out" #t))
667b2508 1134 ((ungexp-native drv-or-pkg out)
0dbea56b 1135 #'(%gexp-input drv-or-pkg out #t))
667b2508 1136 ((ungexp-native-splicing lst)
0dbea56b 1137 #'(%gexp-input lst "out" #t))))
21b679f6 1138
667b2508
LC
1139 (define (substitute-ungexp exp substs)
1140 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1141 ;; the corresponding form in SUBSTS.
1142 (match (assoc exp substs)
1143 ((_ id)
1144 id)
4a6e889f
LC
1145 (_ ;internal error
1146 (with-syntax ((exp exp))
1147 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
667b2508
LC
1148
1149 (define (substitute-ungexp-splicing exp substs)
1150 (syntax-case exp ()
1151 ((exp rest ...)
1152 (match (assoc #'exp substs)
1153 ((_ id)
1154 (with-syntax ((id id))
1155 #`(append id
1156 #,(substitute-references #'(rest ...) substs))))
1157 (_
1158 #'(syntax-error "error: no 'ungexp-splicing' substitution"
4a6e889f 1159 exp))))))
667b2508 1160
21b679f6
LC
1161 (define (substitute-references exp substs)
1162 ;; Return a variant of EXP where all the cars of SUBSTS have been
1163 ;; replaced by the corresponding cdr.
667b2508
LC
1164 (syntax-case exp (ungexp ungexp-native
1165 ungexp-splicing ungexp-native-splicing)
21b679f6 1166 ((ungexp _ ...)
667b2508
LC
1167 (substitute-ungexp exp substs))
1168 ((ungexp-native _ ...)
1169 (substitute-ungexp exp substs))
21b679f6 1170 (((ungexp-splicing _ ...) rest ...)
667b2508
LC
1171 (substitute-ungexp-splicing exp substs))
1172 (((ungexp-native-splicing _ ...) rest ...)
1173 (substitute-ungexp-splicing exp substs))
5e2e4a51 1174 ((exp0 . exp)
21b679f6 1175 #`(cons #,(substitute-references #'exp0 substs)
5e2e4a51 1176 #,(substitute-references #'exp substs)))
21b679f6
LC
1177 (x #''x)))
1178
1179 (syntax-case s (ungexp output)
1180 ((_ exp)
affd7761 1181 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
21b679f6
LC
1182 (formals (generate-temporaries escapes))
1183 (sexp (substitute-references #'exp (zip escapes formals)))
affd7761
LC
1184 (refs (map escape->ref escapes)))
1185 #`(make-gexp (list #,@refs)
0bb9929e 1186 current-imported-modules
838e17d8 1187 current-imported-extensions
21b679f6
LC
1188 (lambda #,formals
1189 #,sexp)))))))
1190
1191\f
aa72d9af
LC
1192;;;
1193;;; Module handling.
1194;;;
1195
8df2eca6
LC
1196(define %not-slash
1197 (char-set-complement (char-set #\/)))
1198
1199(define (file-mapping->tree mapping)
1200 "Convert MAPPING, an alist like:
1201
1202 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1203
1204to a tree suitable for 'interned-file-tree'."
1205 (let ((mapping (map (match-lambda
1206 ((destination . source)
1207 (cons (string-tokenize destination
1208 %not-slash)
1209 source)))
1210 mapping)))
1211 (fold (lambda (pair result)
1212 (match pair
1213 ((destination . source)
1214 (let loop ((destination destination)
1215 (result result))
1216 (match destination
1217 ((file)
1218 (let* ((mode (stat:mode (stat source)))
1219 (type (if (zero? (logand mode #o100))
1220 'regular
1221 'executable)))
1222 (alist-cons file
1223 `(,type (file ,source))
1224 result)))
1225 ((file rest ...)
1226 (let ((directory (assoc-ref result file)))
1227 (alist-cons file
1228 `(directory
1229 ,@(loop rest
1230 (match directory
1231 (('directory . entries) entries)
1232 (#f '()))))
1233 (if directory
1234 (alist-delete file result)
1235 result)))))))))
1236 '()
1237 mapping)))
1238
df2d51f0
LC
1239(define %utils-module
1240 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
a9601e23
LC
1241 ;; other primitives below. Note: We give the file name relative to this
1242 ;; file you are currently reading; 'search-path' could return a file name
1243 ;; relative to the current working directory.
1244 (local-file "build/utils.scm"
df2d51f0 1245 "build-utils.scm"))
aa72d9af 1246
8df2eca6
LC
1247(define* (imported-files/derivation files
1248 #:key (name "file-import")
e529d468 1249 (symlink? #f)
8df2eca6 1250 (system (%current-system))
8afa18d6 1251 (guile (%guile-for-build)))
aa72d9af 1252 "Return a derivation that imports FILES into STORE. FILES must be a list
d938a58b
LC
1253of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1254resulting store path. FILE can be either a file name, or a file-like object,
e529d468
LC
1255as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1256to the source files instead of copying them."
aa72d9af
LC
1257 (define file-pair
1258 (match-lambda
d938a58b 1259 ((final-path . (? string? file-name))
aa72d9af
LC
1260 (mlet %store-monad ((file (interned-file file-name
1261 (basename final-path))))
d938a58b
LC
1262 (return (list final-path file))))
1263 ((final-path . file-like)
1264 (mlet %store-monad ((file (lower-object file-like system)))
aa72d9af
LC
1265 (return (list final-path file))))))
1266
b334674f 1267 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
aa72d9af
LC
1268 (define build
1269 (gexp
1270 (begin
df2d51f0 1271 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
aa72d9af
LC
1272 (use-modules (ice-9 match))
1273
aa72d9af
LC
1274 (mkdir (ungexp output)) (chdir (ungexp output))
1275 (for-each (match-lambda
1276 ((final-path store-path)
1277 (mkdir-p (dirname final-path))
e529d468
LC
1278 ((ungexp (if symlink? 'symlink 'copy-file))
1279 store-path final-path)))
aa72d9af
LC
1280 '(ungexp files)))))
1281
1282 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1283 ;; exactly the same regardless of FILES: less disk space, and fewer
1284 ;; 'add-to-store' RPCs.
1285 (gexp->derivation name build
1286 #:system system
1287 #:guile-for-build guile
30d722c3
LC
1288 #:local-build? #t
1289
8afa18d6
LC
1290 ;; Avoid deprecation warnings about the use of the _IO*
1291 ;; constants in (guix build utils).
30d722c3 1292 #:env-vars
8afa18d6 1293 '(("GUILE_WARN_DEPRECATED" . "no")))))
aa72d9af 1294
8df2eca6
LC
1295(define* (imported-files files
1296 #:key (name "file-import")
8df2eca6
LC
1297 ;; The following parameters make sense when creating
1298 ;; an actual derivation.
1299 (system (%current-system))
8afa18d6 1300 (guile (%guile-for-build)))
8df2eca6
LC
1301 "Import FILES into the store and return the resulting derivation or store
1302file name (a derivation is created if and only if some elements of FILES are
1303file-like objects and not local file names.) FILES must be a list
1304of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1305resulting store path. FILE can be either a file name, or a file-like object,
1306as returned by 'local-file' for example."
8c7bebd6
LC
1307 (if (any (match-lambda
1308 ((_ . (? struct? source)) #t)
1309 (_ #f))
1310 files)
8df2eca6 1311 (imported-files/derivation files #:name name
e529d468 1312 #:symlink? derivation?
8afa18d6 1313 #:system system #:guile guile)
8df2eca6
LC
1314 (interned-file-tree `(,name directory
1315 ,@(file-mapping->tree files)))))
1316
aa72d9af
LC
1317(define* (imported-modules modules
1318 #:key (name "module-import")
1319 (system (%current-system))
1320 (guile (%guile-for-build))
8afa18d6 1321 (module-path %load-path))
aa72d9af 1322 "Return a derivation that contains the source files of MODULES, a list of
d938a58b
LC
1323module names such as `(ice-9 q)'. All of MODULES must be either names of
1324modules to be found in the MODULE-PATH search path, or a module name followed
1325by an arrow followed by a file-like object. For example:
1326
1327 (imported-modules `((guix build utils)
1328 (guix gcrypt)
1329 ((guix config) => ,(scheme-file …))))
1330
1331In this example, the first two modules are taken from MODULE-PATH, and the
1332last one is created from the given <scheme-file> object."
4d20d87b
LC
1333 (let ((files (map (match-lambda
1334 (((module ...) '=> file)
1335 (cons (module->source-file-name module)
1336 file))
1337 ((module ...)
1338 (let ((f (module->source-file-name module)))
1339 (cons f (search-path* module-path f)))))
1340 modules)))
8df2eca6 1341 (imported-files files #:name name
8df2eca6 1342 #:system system
8afa18d6 1343 #:guile guile)))
aa72d9af
LC
1344
1345(define* (compiled-modules modules
1346 #:key (name "module-import-compiled")
1347 (system (%current-system))
2cc5ec7f 1348 target
aa72d9af 1349 (guile (%guile-for-build))
a912c723 1350 (module-path %load-path)
838e17d8 1351 (extensions '())
a31174e8
LC
1352 (deprecation-warnings #f)
1353
1354 ;; TODO: This flag is here to prevent a full
1355 ;; rebuild. Remove it on the next rebuild cycle.
1356 (pre-load-modules? #t))
aa72d9af
LC
1357 "Return a derivation that builds a tree containing the `.go' files
1358corresponding to MODULES. All the MODULES are built in a context where
2cc5ec7f
LC
1359they can refer to each other. When TARGET is true, cross-compile MODULES for
1360TARGET, a GNU triplet."
d3292275
LC
1361 (define total (length modules))
1362
aa72d9af
LC
1363 (mlet %store-monad ((modules (imported-modules modules
1364 #:system system
1365 #:guile guile
1366 #:module-path
8afa18d6 1367 module-path)))
aa72d9af
LC
1368 (define build
1369 (gexp
1370 (begin
df2d51f0
LC
1371 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1372
aa72d9af 1373 (use-modules (ice-9 ftw)
d3292275
LC
1374 (ice-9 format)
1375 (srfi srfi-1)
aa72d9af
LC
1376 (srfi srfi-26)
1377 (system base compile))
1378
2cc5ec7f
LC
1379 ;; TODO: Inline this on the next rebuild cycle.
1380 (ungexp-splicing
1381 (if target
1382 (gexp ((use-modules (system base target))))
1383 (gexp ())))
1384
aa72d9af
LC
1385 (define (regular? file)
1386 (not (member file '("." ".."))))
1387
d3292275 1388 (define (process-entry entry output processed)
e640c9e6
LC
1389 (if (file-is-directory? entry)
1390 (let ((output (string-append output "/" (basename entry))))
1391 (mkdir-p output)
d3292275 1392 (process-directory entry output processed))
e640c9e6
LC
1393 (let* ((base (basename entry ".scm"))
1394 (output (string-append output "/" base ".go")))
d3292275 1395 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
a31174e8
LC
1396 (+ 1 processed
1397 (ungexp-splicing (if pre-load-modules?
1398 (gexp ((ungexp total)))
1399 (gexp ()))))
1400 (ungexp (* total (if pre-load-modules? 2 1)))
1401 entry)
2cc5ec7f
LC
1402
1403 (ungexp-splicing
1404 (if target
1405 (gexp ((with-target (ungexp target)
1406 (lambda ()
1407 (compile-file entry
1408 #:output-file output
1409 #:opts
1410 %auto-compilation-options)))))
1411 (gexp ((compile-file entry
1412 #:output-file output
1413 #:opts %auto-compilation-options)))))
1414
d3292275 1415 (+ 1 processed))))
e640c9e6 1416
d3292275 1417 (define (process-directory directory output processed)
aa72d9af
LC
1418 (let ((entries (map (cut string-append directory "/" <>)
1419 (scandir directory regular?))))
d3292275
LC
1420 (fold (cut process-entry <> output <>)
1421 processed
1422 entries)))
1423
1424 (setvbuf (current-output-port)
1425 (cond-expand (guile-2.2 'line) (else _IOLBF)))
aa72d9af 1426
4a42abc5
LC
1427 (define mkdir-p
1428 ;; Capture 'mkdir-p'.
1429 (@ (guix build utils) mkdir-p))
5d669883 1430
838e17d8 1431 ;; Add EXTENSIONS to the search path.
4a42abc5
LC
1432 (set! %load-path
1433 (append (map (lambda (extension)
1434 (string-append extension
1435 "/share/guile/site/"
1436 (effective-version)))
1437 '((ungexp-native-splicing extensions)))
1438 %load-path))
1439 (set! %load-compiled-path
1440 (append (map (lambda (extension)
1441 (string-append extension "/lib/guile/"
1442 (effective-version)
1443 "/site-ccache"))
1444 '((ungexp-native-splicing extensions)))
1445 %load-compiled-path))
838e17d8 1446
aa72d9af 1447 (set! %load-path (cons (ungexp modules) %load-path))
5d669883 1448
4a42abc5
LC
1449 ;; Above we loaded our own (guix build utils) but now we may need to
1450 ;; load a compile a different one. Thus, force a reload.
1451 (let ((utils (string-append (ungexp modules)
1452 "/guix/build/utils.scm")))
1453 (when (file-exists? utils)
1454 (load utils)))
5d669883 1455
aa72d9af
LC
1456 (mkdir (ungexp output))
1457 (chdir (ungexp modules))
a31174e8
LC
1458
1459 (ungexp-splicing
1460 (if pre-load-modules?
1461 (gexp ((define* (load-from-directory directory
1462 #:optional (loaded 0))
1463 "Load all the source files found in DIRECTORY."
1464 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1465 (let ((entries (map (cut string-append directory "/" <>)
1466 (scandir directory regular?))))
1467 (fold (lambda (file loaded)
1468 (if (file-is-directory? file)
1469 (load-from-directory file loaded)
1470 (begin
1471 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1472 (+ 1 loaded)
1473 (ungexp (* 2 total))
1474 file)
1475 (save-module-excursion
1476 (lambda ()
1477 (primitive-load file)))
1478 (+ 1 loaded))))
1479 loaded
1480 entries)))
1481
1482 (load-from-directory ".")))
1483 (gexp ())))
1484
d3292275 1485 (process-directory "." (ungexp output) 0))))
aa72d9af
LC
1486
1487 ;; TODO: Pass MODULES as an environment variable.
1488 (gexp->derivation name build
1489 #:system system
1490 #:guile-for-build guile
a912c723
LC
1491 #:local-build? #t
1492 #:env-vars
1493 (case deprecation-warnings
1494 ((#f)
1495 '(("GUILE_WARN_DEPRECATED" . "no")))
1496 ((detailed)
1497 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1498 (else
1499 '())))))
aa72d9af
LC
1500
1501\f
21b679f6
LC
1502;;;
1503;;; Convenience procedures.
1504;;;
1505
53e89b17 1506(define (default-guile)
6ee797f3
LC
1507 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1508 ;; programs returned by 'program-file' and we don't want to keep references
1509 ;; to several Guile packages). This module must not refer to (gnu …)
53e89b17 1510 ;; modules directly, to avoid circular dependencies, hence this hack.
6ee797f3
LC
1511 (module-ref (resolve-interface '(gnu packages guile))
1512 'guile-2.2))
53e89b17 1513
838e17d8 1514(define* (load-path-expression modules #:optional (path %load-path)
2e8cabb8 1515 #:key (extensions '()) system target)
dd8d1a30 1516 "Return as a monadic value a gexp that sets '%load-path' and
1ae16033 1517'%load-compiled-path' to point to MODULES, a list of module names. MODULES
efff3245
LC
1518are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1519 (if (and (null? modules) (null? extensions))
1520 (with-monad %store-monad
1521 (return #f))
1522 (mlet %store-monad ((modules (imported-modules modules
2e8cabb8
LC
1523 #:module-path path
1524 #:system system))
efff3245
LC
1525 (compiled (compiled-modules modules
1526 #:extensions extensions
2e8cabb8
LC
1527 #:module-path path
1528 #:system system
1529 #:target target)))
cdf9811d
LC
1530 (return
1531 (gexp (eval-when (expand load eval)
1532 ;; Augment the load paths and delete duplicates. Do that
1533 ;; without loading (srfi srfi-1) or anything.
1534 (let ((extensions '((ungexp-native-splicing extensions)))
1535 (prepend (lambda (items lst)
1536 ;; This is O(N²) but N is typically small.
1537 (let loop ((items items)
1538 (lst lst))
1539 (if (null? items)
1540 lst
1541 (loop (cdr items)
1542 (cons (car items)
1543 (delete (car items) lst))))))))
1544 (set! %load-path
1545 (prepend (cons (ungexp modules)
1546 (map (lambda (extension)
1547 (string-append extension
1548 "/share/guile/site/"
1549 (effective-version)))
1550 extensions))
1551 %load-path))
1552 (set! %load-compiled-path
1553 (prepend (cons (ungexp compiled)
1554 (map (lambda (extension)
1555 (string-append extension
1556 "/lib/guile/"
1557 (effective-version)
1558 "/site-ccache"))
1559 extensions))
1560 %load-compiled-path)))))))))
dd8d1a30 1561
21b679f6 1562(define* (gexp->script name exp
1ae16033 1563 #:key (guile (default-guile))
2e8cabb8
LC
1564 (module-path %load-path)
1565 (system (%current-system))
1566 target)
9c14a487 1567 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1ae16033 1568imported modules in its search path. Look up EXP's modules in MODULE-PATH."
9c14a487 1569 (mlet %store-monad ((set-load-path
1ae16033 1570 (load-path-expression (gexp-modules exp)
838e17d8
LC
1571 module-path
1572 #:extensions
2e8cabb8
LC
1573 (gexp-extensions exp)
1574 #:system system
1575 #:target target)))
21b679f6
LC
1576 (gexp->derivation name
1577 (gexp
1578 (call-with-output-file (ungexp output)
1579 (lambda (port)
c17b5ab4
LC
1580 ;; Note: that makes a long shebang. When the store
1581 ;; is /gnu/store, that fits within the 128-byte
1582 ;; limit imposed by Linux, but that may go beyond
1583 ;; when running tests.
21b679f6
LC
1584 (format port
1585 "#!~a/bin/guile --no-auto-compile~%!#~%"
1586 (ungexp guile))
4a4cbd0b 1587
efff3245
LC
1588 (ungexp-splicing
1589 (if set-load-path
1590 (gexp ((write '(ungexp set-load-path) port)))
1591 (gexp ())))
1592
21b679f6 1593 (write '(ungexp exp) port)
1ae16033 1594 (chmod port #o555))))
2e8cabb8
LC
1595 #:system system
1596 #:target target
1ae16033 1597 #:module-path module-path)))
21b679f6 1598
1ae16033
LC
1599(define* (gexp->file name exp #:key
1600 (set-load-path? #t)
4fbd1a2b
LC
1601 (module-path %load-path)
1602 (splice? #f))
1603 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1604is true, EXP is considered to be a list of expressions that will be spliced in
1605the resulting file.
1606
1607When SET-LOAD-PATH? is true, emit code in the resulting file to set
1608'%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1609Lookup EXP's modules in MODULE-PATH."
838e17d8
LC
1610 (define modules (gexp-modules exp))
1611 (define extensions (gexp-extensions exp))
1612
1613 (if (or (not set-load-path?)
1614 (and (null? modules) (null? extensions)))
1615 (gexp->derivation name
1616 (gexp
1617 (call-with-output-file (ungexp output)
1618 (lambda (port)
1619 (for-each (lambda (exp)
1620 (write exp port))
1621 '(ungexp (if splice?
1622 exp
1623 (gexp ((ungexp exp)))))))))
1624 #:local-build? #t
1625 #:substitutable? #f)
1626 (mlet %store-monad ((set-load-path
1627 (load-path-expression modules module-path
1628 #:extensions extensions)))
1629 (gexp->derivation name
1630 (gexp
1631 (call-with-output-file (ungexp output)
1632 (lambda (port)
1633 (write '(ungexp set-load-path) port)
1634 (for-each (lambda (exp)
1635 (write exp port))
1636 '(ungexp (if splice?
1637 exp
1638 (gexp ((ungexp exp)))))))))
1639 #:module-path module-path
1640 #:local-build? #t
1641 #:substitutable? #f))))
21b679f6 1642
462a3fa3
LC
1643(define* (text-file* name #:rest text)
1644 "Return as a monadic value a derivation that builds a text file containing
d9ae938f
LC
1645all of TEXT. TEXT may list, in addition to strings, objects of any type that
1646can be used in a gexp: packages, derivations, local file objects, etc. The
1647resulting store file holds references to all these."
462a3fa3
LC
1648 (define builder
1649 (gexp (call-with-output-file (ungexp output "out")
1650 (lambda (port)
1651 (display (string-append (ungexp-splicing text)) port)))))
1652
851b6f62
LC
1653 (gexp->derivation name builder
1654 #:local-build? #t
1655 #:substitutable? #f))
462a3fa3 1656
b751cde3
LC
1657(define* (mixed-text-file name #:rest text)
1658 "Return an object representing store file NAME containing TEXT. TEXT is a
1659sequence of strings and file-like objects, as in:
1660
1661 (mixed-text-file \"profile\"
1662 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1663
1664This is the declarative counterpart of 'text-file*'."
1665 (define build
1666 (gexp (call-with-output-file (ungexp output "out")
1667 (lambda (port)
1668 (display (string-append (ungexp-splicing text)) port)))))
1669
1670 (computed-file name build))
1671
dedb512f
LC
1672(define (file-union name files)
1673 "Return a <computed-file> that builds a directory containing all of FILES.
1674Each item in FILES must be a two-element list where the first element is the
1675file name to use in the new directory, and the second element is a gexp
1676denoting the target file. Here's an example:
1677
1678 (file-union \"etc\"
1679 `((\"hosts\" ,(plain-file \"hosts\"
1680 \"127.0.0.1 localhost\"))
1681 (\"bashrc\" ,(plain-file \"bashrc\"
5dec93bb
LC
1682 \"alias ls='ls --color'\"))
1683 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
dedb512f
LC
1684
1685This yields an 'etc' directory containing these two files."
1686 (computed-file name
5dec93bb
LC
1687 (with-imported-modules '((guix build utils))
1688 (gexp
1689 (begin
1690 (use-modules (guix build utils))
1691
1692 (mkdir (ungexp output))
1693 (chdir (ungexp output))
1694 (ungexp-splicing
1695 (map (match-lambda
1696 ((target source)
1697 (gexp
1698 (begin
1699 ;; Stat the source to abort early if it does
1700 ;; not exist.
1701 (stat (ungexp source))
1702
1703 (mkdir-p (dirname (ungexp target)))
1704 (symlink (ungexp source)
1705 (ungexp target))))))
1706 files)))))))
dedb512f 1707
59523429 1708(define* (directory-union name things
b244ae25
LC
1709 #:key (copy? #f) (quiet? #f)
1710 (resolve-collision 'warn-about-collision))
d298c815
LC
1711 "Return a directory that is the union of THINGS, where THINGS is a list of
1712file-like objects denoting directories. For example:
1713
1714 (directory-union \"guile+emacs\" (list guile emacs))
1715
59523429
LC
1716yields a directory that is the union of the 'guile' and 'emacs' packages.
1717
b244ae25
LC
1718Call RESOLVE-COLLISION when several files collide, passing it the list of
1719colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1720which case the colliding entry is skipped altogether.
1721
de98b302
LC
1722When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1723is true, the derivation will not print anything."
59523429
LC
1724 (define symlink
1725 (if copy?
1726 (gexp (lambda (old new)
1727 (if (file-is-directory? old)
1728 (symlink old new)
1729 (copy-file old new))))
1730 (gexp symlink)))
1731
de98b302
LC
1732 (define log-port
1733 (if quiet?
1734 (gexp (%make-void-port "w"))
1735 (gexp (current-error-port))))
1736
d298c815
LC
1737 (match things
1738 ((one)
1739 ;; Only one thing; return it.
1740 one)
1741 (_
1742 (computed-file name
1743 (with-imported-modules '((guix build union))
1744 (gexp (begin
b244ae25
LC
1745 (use-modules (guix build union)
1746 (srfi srfi-1)) ;for 'first' and 'last'
1747
d298c815 1748 (union-build (ungexp output)
59523429
LC
1749 '(ungexp things)
1750
de98b302 1751 #:log-port (ungexp log-port)
b244ae25
LC
1752 #:symlink (ungexp symlink)
1753 #:resolve-collision
1754 (ungexp resolve-collision)))))))))
d298c815 1755
21b679f6
LC
1756\f
1757;;;
1758;;; Syntactic sugar.
1759;;;
1760
1761(eval-when (expand load eval)
667b2508
LC
1762 (define* (read-ungexp chr port #:optional native?)
1763 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1764true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
21b679f6
LC
1765 (define unquote-symbol
1766 (match (peek-char port)
1767 (#\@
1768 (read-char port)
667b2508
LC
1769 (if native?
1770 'ungexp-native-splicing
1771 'ungexp-splicing))
21b679f6 1772 (_
667b2508
LC
1773 (if native?
1774 'ungexp-native
1775 'ungexp))))
21b679f6
LC
1776
1777 (match (read port)
1778 ((? symbol? symbol)
1779 (let ((str (symbol->string symbol)))
1780 (match (string-index-right str #\:)
1781 (#f
1782 `(,unquote-symbol ,symbol))
1783 (colon
1784 (let ((name (string->symbol (substring str 0 colon)))
1785 (output (substring str (+ colon 1))))
1786 `(,unquote-symbol ,name ,output))))))
1787 (x
1788 `(,unquote-symbol ,x))))
1789
1790 (define (read-gexp chr port)
1791 "Read a 'gexp' form from PORT."
1792 `(gexp ,(read port)))
1793
1794 ;; Extend the reader
1795 (read-hash-extend #\~ read-gexp)
667b2508
LC
1796 (read-hash-extend #\$ read-ungexp)
1797 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
21b679f6
LC
1798
1799;;; gexp.scm ends here